我想用张量"图像"来提供CNN.当占位符is_training为True时,我希望此张量包含来自训练集的图像(具有FIXED大小),否则我希望它包含来自测试集的图像(其未固定大小).
这是必要的,因为在训练中我从训练图像中采取随机固定裁剪,而在测试中我想进行密集评估并将整个图像馈送到网络内部(它完全卷积,因此它将接受它们)
当前的NOT WORKING方法是创建两个不同的迭代器,并尝试在session.run(images,{is_training:True/False})中使用tf.cond选择训练/测试输入.
问题是两个迭代器都被评估了.训练和测试数据集也有不同的大小,所以我不能迭代它们直到最后.有没有办法让这项工作?或者以更聪明的方式重写它?
我已经看到了一些有关这方面的问题/答案,但他们总是使用tf.assign,它采用numpy数组并将其分配给张量.在这种情况下,我不能使用tf.assign,因为我已经有一个来自迭代器的张量.
我拥有的当前代码就是这个代码.它只是检查张量"图像"的形状:
train_filenames, train_labels = list_images(args.train_dir)
val_filenames, val_labels = list_images(args.val_dir)
graph = tf.Graph()
with graph.as_default():
# Preprocessing (for both training and validation):
def _parse_function(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string, channels=3)
image = tf.cast(image_decoded, tf.float32)
return image, label
# Preprocessing (for training)
def training_preprocess(image, label):
# Random flip and crop
image = tf.image.random_flip_left_right(image)
image = tf.random_crop(image, [args.crop,args.crop, 3])
return image, label
# Preprocessing (for validation)
def val_preprocess(image, label):
flipped_image = tf.image.flip_left_right(image)
batch = tf.stack([image,flipped_image],axis=0) …Run Code Online (Sandbox Code Playgroud) tensorflow ×1