How to customize shortcuts with ImageDataGenerator in Keras?

I want to increase my dataset using Keras ImageDataGenerator for use with model.fit_generator () . I see that I can randomly flip images. For inverted images, I need to change the corresponding label. How can i do this?

EDIT: I'm doing a regression, not a classification, so if the image is upside down, I need to set up a shortcut. Actual images are taken from a self-starting car simulator, and the stickers are rotation angles. If I horizontally flipped the image, I need to cancel the rotation angle.

+5
source share
1 answer

You can do something like:

import numpy

def fliping_gen(image_generator, flip_p=0.5):
    for x, y in image_generator:
        flip_selector = numpy.random.binomial(1, flip_p, size=x.shape[0]) == 1
        x[flip_selector,:,:,:] = x[flip_selector,:,::-1,:]
        y[flip_selector] = (-1) * y[flip_selector]
        yield x, y
+3
source

Source: https://habr.com/ru/post/1668558/


All Articles