0

I have 7 labeled classes all with varying quantities of images in them (ranging from 2000-20000). I know in keras when using the model.fit I can change how many times each folder of labeled images is read in. Instead, I would like to compare the results if I augmented the images in the folders with fewer images. I only know how to do this image by image, how would I augment all the images in the folder instead of 1 at a time?

gen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1,
                    height_shift_range=0.1, shear_range=0.15, zoom_range=0.1,
                    channel_shift_range=10, horizontal_flip=True,
                     vertical_flip=True)
Folder_path = 'Folder_path_with_images'
image = np.expand_dims(ndimage.imread(Folder_path),0) # I get no permission from Folder_path_with_images

# Generate batches of augmented images from this image
aug_iter = gen.flow(image, save_to_dir = 'NEW_SAVE_PATH')
aug_images = [next(aug_iter)[0].astype(np.uint8) for i in range(10)]
realr
  • 3,652
  • 6
  • 23
  • 34
cdr
  • 21
  • 1
  • 7

1 Answers1

0

model.fit_generator will do the job.

Here is one of my example code:

model.fit_generator(datagen.flow(x_train, to_categorical(y_train), \
        batch_size=mini_batch_size),
        # steps_per_epoch= ceil(len(x_train)/mini_batch_size), \
        epochs=60, \
        validation_data=(test_images, to_categorical(test_labels)), \
        )
Will
  • 166
  • 1
  • 9
  • So instead of generating augmented images and then saving them to memory I will be generating them while running the model? How would this balance out the uneven # of data in training classes? – cdr Dec 31 '19 at 14:56
  • I am not sure what you mean by "balance the uneven data" but if you can manage it by `model.fit` then you should probably do it in `model.fit_generator` since them functions in the same way. `Model.fit_generator` merely augments the data that feed into the model and it is recommended by Keras documentation. – Will Jan 02 '20 at 02:53