user121
user121

Reputation: 931

How can I apply rotation to image in Keras without using model.fit_generator?

I am working on an image pixel classification problem using convolution neural nets. The size of my training images is 128x128x3 and the size of the label mask is 128x128

I do training in Keras as follows:

Xtrain, Xvalid, ytrain, yvalid = train_test_split(images, masks,test_size=0.3, random_state=567)

model.fit(Xtrain, ytrain, batch_size=32, epochs=20, verbose=1, shuffle=True, validation_data=(Xvalid, yvalid))

However, I want to apply a Random 2D rotation to Xtrain and ytrain which is also of size 128x128x3 and 128x128 respectively. More specifically, I want to apply this rotation for every epoch iteration.

For the time being, I would like to continue using model.fit and not use model.fit_generator, as I know data augmentation is commonly done using .fit_generator.

So essentially, I want to loop model.fit so that Xtrain and ytrain is randomly rotated for every epoch. I am new to Python and Keras so any insights are welcome if this is even possible.

Upvotes: 2

Views: 6144

Answers (1)

jrjames83
jrjames83

Reputation: 901

Here's an exmaple of using ImageDataGenerator to save the output to a specified directory, thus getting around the requirement to use model.fit_generator.

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

datagen = ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest')

img = load_img('data/train/cats/cat.0.jpg')  # this is a PIL image
x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)

# the .flow() command below generates batches of randomly transformed images
# and saves the results to the `preview/` directory
i = 0
for batch in datagen.flow(x, batch_size=1,
                          save_to_dir='preview', save_prefix='cat', save_format='jpeg'):
    i += 1
    if i > 20:
        break  # otherwise the generator would loop indefinitely

Taken from here: https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html

You can change the args to suit your use case and then generate your X_train and X_valid or whatever datasets, then load into memory and use plain old model.fit.

Upvotes: 1

Related Questions