How to create additional training images with Keras preprocessing layers?

415 views Asked by At

I am following the official Tensorflow/Keras docs on image classification, in particular the section on image augmentation. There it says:

Data augmentation takes the approach of generating additional training data from your existing examples by augmenting then using random transformations that yield believable-looking images. This helps expose the model to more aspects of the data and generalize better.

So my understanding of this is that - for example if I have not many training images - I want to generate additional training data by creating new, augmented images in addition to the existing training images.

Then in the Keras docs linked above it is shown how some preprocessing layers from the layers.experimental.preprocessing module are being added as first layers to the Sequential model of the example. So in theory that makes sense, those new preprocessing layers augment the input data (=images) before the "enter" the real TF model.

However, as quoted above and what I thought we want to do is to create additional images, i.e. create new, more images for the existing training images. But how would such a set of preprocessing layers in the model create additional images? Wouldn't they simple (randomly) augment the existing training images before the enter the model, but not create new, additional images?

1

There are 1 answers

3
Nicolas Gervais - Open to Work On BEST ANSWER

It is creating additional images, but that doesn't necessarily mean that it will create new jpg files.

If this is what you're trying to do, ImageDataGenerator can do that, with the save_to_dir argument.

Wouldn't they simple (randomly) augment the existing training images before the enter the model, but not create new, additional images?

Yes, it creates new images. But it doesn't create new files on your machine. You can use this:

ImageDataGenerator.flow_from_directory(directory, target_size=(256, 256), save_to_dir=None, save_prefix='', save_format='png' )