do padding and flipping batch-wise
@@ -41,24 +41,18 @@ def random_crop_and_flip(x, pad_rows=4, pad_cols=4):
"""Augment a batch by randomly cropping and horizontally flipping it."""
rows = tf.shape(x)[1]
cols = tf.shape(x)[2]
-
- def _pad_img(img):
- """Pad an individual image"""
- return tf.image.resize_image_with_crop_or_pad(img, rows + pad_rows,
- cols + pad_cols)
+ channels = x.get_shape()[3]
def _rand_crop_img(img):
"""Randomly crop an individual image"""
- channels = img.get_shape()[2]
return tf.random_crop(img, [rows, cols, channels])
- def random_crop_and_flip_image(img):
- """Pad, randomly crop, and randomly flip an individual image"""
- return tf.image.random_flip_left_right(_rand_crop_img(_pad_img(img)))
-
# Some of these ops are only on CPU.
# This function will often be called with the device set to GPU.
# We need to set it to CPU temporarily to avoid an exception.
with tf.device('/CPU:0'):
- x = tf.map_fn(random_crop_and_flip_image, x)
+ x = tf.image.resize_image_with_crop_or_pad(x, rows + pad_rows,
+ cols + pad_cols)
+ x = tf.map_fn(_rand_crop_img, x)
+ x = tf.image.random_flip_left_right(x)
return x
