Data Augmentation Implementation using TensorFlow
<p>My data augmentation code in TensorFlow seems to be running, but not properly serving the intended purposes. I have a tensor with  an 'in_batch' number of images in a shape of <code>[in_batch, height, width, 3]</code> and apply the <code>data_augment</code> function below.</p>

<p>The run time increases when I apply data augmentation compared to the model without it, so the function is definitely being run. However, the test accuracy drops significantly, i.e. about 80% without data augmentation to about 10% with. What is happening?</p>

<pre><code>def prep_data_augment(input_tensor, i):
    image = tf.slice(input_tensor, [i, 0, 0, 0], [1, 32, 32, 3])[0]
    image = tf.image.random_flip_left_right(image)
    image = tf.image.random_brightness(image, max_delta=63)
    image = tf.image.random_contrast(image, lower=0.2, upper=1.8)
    image = tf.expand_dims(image, 0)
    return image

def data_augment(input_tensor, in_batch):
    fake_i = tf.Variable(0, dtype=tf.int32, trainable=False)
    i = 0
    output_tensor = prep_data_augment(input_tensor, i)
    i += 1

    def while_body(output_tensor, i):
        output_tensor = tf.concat(0, [output_tensor, prep_data_augment(input_tensor, i)])
        i += 1
        return (output_tensor, i)

    (output_tensor, i) = tf.while_loop(lambda output_tensor, i: tf.less(i, in_batch), 
                                       lambda output_tensor, i: while_body(output_tensor, i), 
                                       (output_tensor, i), 
                                       shape_invariants = (tf.TensorShape([None, 32, 32, 3]), fake_i.get_shape()))
    return output_tensor
</code></pre>

<p>[ Update ]
Instead of using the while loop, the above function could be simplified by using the map function as below.</p>

<pre><code>def prep_data_augment(image):
    image = tf.image.random_flip_left_right(image)
    image = tf.image.random_brightness(image, max_delta=63/255.0)
    image = tf.image.random_contrast(image, lower=0.2, upper=1.8)
    return image

def data_augment(input_tensor):
    output_tensor = tf.map_fn(prep_data_augment, input_tensor)
    return output_tensor
</code></pre>