Image data agumentation tequniques using keras.pre

2019-05-11 01:18发布

问题:

I would like to generate augmented data for images by Random rotation, shifts, shear and flips.

I have found this keras function.

The function keras.preprocessing.image.ImageDataGenerator But I've seen this being used to directly train networks.

Is there a way to input images and then save the transformed images on HDD instead of how if currently works in examples in this link

Or is there another simple plug and use python package I can use instead of implementing everything with numpy or opencv ?

回答1:

Basically - this is generator which is infinitely returning a batches of images. One could do the following:

def save_images_from_generator(maximal_nb_of_images, generator):
    nb_of_images_processed = 0
    for x, _ in generator:
        nb_of_images += x.shape[0]
        if nb_of_images <= maximal_nb_of_images:
            for image_nb in range(x.shape[0]):
                your_custom_save(x[image_nb]) # your custom function for saving images
        else:
            break

to save images from keras image generator.



回答2:

You can save the images outputted by ImageGenerator to HDD. One option is to use datagen.flow as follows:

for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size=9, save_to_dir='images', save_prefix='aug', save_format='png')

A second option is to manually loop over each image, load it, and apply a random transformation. Once you have instantiated your ImageGenerator, just call:

img_trans = datagen.random_transform(img)

Then, save the transformed image to HDD using PIL etc.

A third option is to manually loop over each image, load it, and apply a random transformation using a third party program. I recommend imgaug, found here.