Reputation: 31
I am using tf Dataset API to read images and its labels. I like to do multiple image augmentations on images and increase my training data size. What i have done now is like below.
def flip(self, img, lbl):
image = tf.image.flip_left_right(img)
return image, lbl
def transpose(self, img, lbl):
image = tf.image.transpose_image(img)
return image, lbl
# just read and resize the image.
process_fn = lambda img, lbl: self.read_convert_image(img, lbl, self.args)
flip_fn = lambda img, lbl: self.flip(img,lbl)
transpose_fn = lambda img, lbl: self.transpose(img,lbl)
train_set = self.train_set.repeat()
train_set = train_set.shuffle(args.batch_size)
train_set = train_set.map(process_fn)
fliped_data = train_set.map(flip_fn)
transpose_data = train_set.map(transpose_fn)
train_set = train_set.concatenate(fliped_data)
train_set = train_set.concatenate(transpose_data)
train_set = train_set.batch(args.batch_size)
iterator = train_set.make_one_shot_iterator()
images, labels = iterator.get_next()
Is there a better way to do multiple augmentations. The problem with above approach is if i add more augmentation function , that many map and concatenate is required.
Thank You
Upvotes: 1
Views: 2921
Reputation: 7103
If you want to do augmentations yourself, without relying on Keras's ImageDataGenerator
you can create a function like img_aug
and then use it in your model or in the Dataset API pipeline. The code below is just a pseudocode, but it shows the idea. You define all your transformations, then you have some generic threshold above which you apply a transformation and try to apply them up to X times (in the code below it is 4)
def img_aug(image):
image = distorted_image
def h_flip():
return tf.image.flip_left_right(distorted_image)
def v_flip():
return tf.image.flip_up_down(distorted_image)
threshold = tf.constant(0.9, dtype=tf.float32)
def body(i, distorted_image):
p_order = tf.random_uniform(shape=[2], minval=0., maxval=1., dtype=tf.float32)
distorted_image = tf.case({
tf.greater(p_order[0], threshold): h_flip,
tf.greater(p_order[1], threshold): v_flip,
}
,default=identity, exclusive=False)
return (i+1, distorted_image)
def cond(i, *args):
return i < 4 # max number of transformations
parallel_iterations = 1
tf.while_loop(cond, body, [0,distorted_image],
parallel_iterations=parallel_iterations)
return distorted_image
Upvotes: 3
Reputation: 3205
A simple alternative for Image Augmentation is using Tensorflow implemented Keras which contains easy to use api
It's looks like this
ImageDataGenerator(rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range = 0.2,
horizontal_flip = True)
And you are ready to use the augmented image as much as you want.
Here is an working github code example Conv_net_with_augmentation
Upvotes: 0