0

I want to do some random augmentation , only at train time.

I've combined the augmentation as part of the graph - which I think is kind of mistake since the same graph is used for testing also - and I don't want the test images to be augmented.

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)

I want the above augmentations to be applied only at test time - how can it be done ?

M.F
  • 345
  • 3
  • 15
  • you can use dataset api with two separate functions. It's not completely clear from your code – Sharky May 19 '19 at 21:02
  • @Sharky this is part of building the model itself - meaning it is called per batch (train and test). I want to use those augmentations only at test time . For example like when we use a placeholder for keeping probability of a dropout layer (Only that `map_fn` can't take a placeholder style `is_training`) – M.F May 19 '19 at 21:16

1 Answers1

0

The solution for this is quit simple

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

Then call pre_process inside the model as I wanted

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)
M.F
  • 345
  • 3
  • 15