Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble with save_to_dir in Keras ImageDataGenerator flow method

Tags:

keras

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?

like image 794
TheSneak Avatar asked Nov 21 '16 08:11

TheSneak


People also ask

What is target size in ImageDataGenerator?

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.

How many images does ImageDataGenerator generate?

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.

What is zoom range in ImageDataGenerator?

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].

What is shear range in ImageDataGenerator?

'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.


2 Answers

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
like image 112
pyan Avatar answered Oct 07 '22 10:10

pyan


Its only a declaration, you must use that generator, for example, .next()

batches.next()

then you will see images in path+'augmented'

like image 2
DappWind Avatar answered Oct 07 '22 10:10

DappWind