I want to save the augmented images my ImageDataGenerator is creating so that I can use them later. When I execute the following code, it runs fine, but the images I was hoping to save do not show up in the directory I am trying to save them in.
gen = image.ImageDataGenerator(rotation_range=17, width_shift_range=0.12,
height_shift_range=0.12, zoom_range=0.12, horizontal_flip=True, dim_ordering='th')
batches = gen.flow_from_directory(path+'train', target_size=(224,224),
class_mode='categorical', shuffle=False, batch_size=batch_size, save_to_dir=path+'augmented', save_prefix='hi')
I feel like I must not be using this feature correctly. Any idea what I'm doing wrong?
target_size: Size of the input image. color_mode: Set to rgb for colored images otherwise grayscale if the images are black and white. batch_size: Size of the batches of data. class_mode: Set to binary is for 1-D binary labels whereas categorical is for 2-D one-hot encoded labels.
Then the "ImageDataGenerator" will produce 10 images in each iteration of the training. An iteration is defined as steps per epoch i.e. the total number of samples / batch_size. In above case, in each epoch of training there will be 100 iterations.
This method uses the zoom_range argument of the ImageDataGenerator class. We can specify the percentage value of the zooms either in a float, range in the form of an array, or python tuple. If we specify the value of the zoom-in using float value then it will be [1-floatValue, 1+floatValue].
'Shear' means that the image will be distorted along an axis, mostly to create or rectify the perception angles. It's usually used to augment images so that computers can see how humans see things from different angles.
gen.flow_from_directory
gives you a generator. The images are not really generated. In order to get the images, you can iterate through the generator. For example
i = 0
for batch in gen.flow_from_directory(path+'train', target_size=(224,224),
class_mode='categorical', shuffle=False, batch_size=batch_size,
save_to_dir=path+'augmented', save_prefix='hi'):
i += 1
if i > 20: # save 20 images
break # otherwise the generator would loop indefinitely
Its only a declaration, you must use that generator, for example, .next()
batches.next()
then you will see images in path+'augmented'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With