Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 225
royco Avatar asked Oct 18 '22 17:10

royco


1 Answers

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
like image 53
Marcin Możejko Avatar answered Oct 20 '22 10:10

Marcin Możejko