royco
royco

Reputation: 5529

How to adjust labels with ImageDataGenerator in Keras?

I want to augment my dataset with Keras's ImageDataGenerator for use with model.fit_generator(). I see I can randomly flip images. For flipped images, I need to modify the corresponding label. How can I do that?

EDIT: I'm doing regression, not classification, so if an image is flipped I need to adjust the label. The actual images are from a self-driving car simulator, and the labels are the steering angles. If I horizontally flip an image, I need to negate the steering angle.

Upvotes: 6

Views: 1822

Answers (1)

Marcin Możejko
Marcin Możejko

Reputation: 40516

You might 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

Upvotes: 5

Related Questions