6

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?

TheSneak
  • 500
  • 6
  • 15

2 Answers2

15

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
pyan
  • 3,577
  • 4
  • 23
  • 36
  • I have categorical data - 26 classes. For each class, do I have to make a folder to save the augmented images of that class (so 26 folders altogether)? – Debbie Jul 04 '20 at 16:18
2

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

batches.next()

then you will see images in path+'augmented'

DappWind
  • 666
  • 6
  • 6
  • that puts all the augmented images in one folder. Is there any way to separate them out by class/label? – Syed Ali Dec 01 '20 at 06:54