我只想在训练时做一些随机增强。
我将扩充作为图形的一部分进行了组合-由于同一图形也用于测试,因此我认为这是一种错误-而且我不希望扩充测试图像。
x = tf.placeholder(tf.float32, shape=[None, _IMAGE_SIZE * _IMAGE_SIZE * _IMAGE_CHANNELS], name='Input')
y = tf.placeholder(tf.float32, shape=[None, _NUM_CLASSES], name='Output')
#reshape the input so we can apply conv2d########
x_image = tf.reshape(x, [-1,32,32,3])
x_image = tf.map_fn(lambda frame: tf.random_crop(frame, [_IMAGE_SIZE, _IMAGE_SIZE, _IMAGE_CHANNELS]), x_image)
x_image = tf.map_fn(lambda frame: tf.image.random_flip_left_right(frame), x_image)
x_image = tf.map_fn(lambda frame: tf.image.random_brightness(frame, max_delta=63), x_image)
x_image = tf.map_fn(lambda frame: tf.image.random_contrast(frame, lower=0.2, upper=1.8), x_image)
x_image = tf.map_fn(lambda frame: tf.image.per_image_standardization(frame), x_image)
我希望上述增强功能只能在测试时应用-如何完成?
答案 0 :(得分:0)
解决方案很简单
def pre_process_image(image, training):
if training:
Do things
else:
Do some other things
return image
def pre_process(images, training):
images = tf.map_fn(lambda image: pre_process_image(image, training), images)
return images
然后根据需要在模型内部调用pre_process
if is_training == True:
with tf.variable_scope('augment', reuse=False):
with tf.device('/cpu:0'):
x_image = tf.reshape(x, [-1, _IMAGE_SIZE, _IMAGE_SIZE, _IMAGE_CHANNELS], name='images')
x_image = pre_process(x_image, is_training)
else:
with tf.variable_scope('augment', reuse=True):
with tf.device('/cpu:0'):
x_image = tf.reshape(x, [-1, _IMAGE_SIZE, _IMAGE_SIZE, _IMAGE_CHANNELS], name='images')
x_image = pre_process(x_image, is_training)