Keras: Is there image random cropping function?

Created on 7 Oct 2016  路  5Comments  路  Source: keras-team/keras

In images preprocessing before CNN training, we often randomly crop the images. For example, in VGGNet or GoogLeNet, the 256脳256 image is randomly cropped to 224脳224.
So, is there image random cropping function in Keras?

stale

Most helpful comment

I shared my solution in this blog post:
"Extending Keras' ImageDataGenerator to Support Random Cropping"
https://jkjung-avt.github.io/keras-image-cropping/

All 5 comments

Random cropping is not possible using the standard preprocessing tools as far as I can tell. I also need random cropping, and noticed that work has been done towards this by others (see #3338). Hopefully something like this will get merged into the main repo soon.

I shared my solution in this blog post:
"Extending Keras' ImageDataGenerator to Support Random Cropping"
https://jkjung-avt.github.io/keras-image-cropping/

@jkjung-avt I used your random cropping code; however, it gave me an error because of the size of the cropped images needs to be returned back to the original image size, in your case (256,256). I tried to modified your function however no success. Any idea on how to resize the cropped images back to the original size so that the last line of the code to fit_generator does not throw an error for the size of the cropped batches (224)?

The modified code below, which didnt work for me:

def crop_generator(batches, crop_length):

'''

Take as input a Keras ImageGen (Iterator) and generate random

crops from the image batches generated by the original iterator

'''

while True:

    batch_x, batch_y = next(batches)

    batch_crops = np.zeros((batch_x.shape[0], crop_length, crop_length, 3))

    for i in range(batch_x.shape[0]):

        batch_crops[i] = random_crop(batch_x[i], (crop_length, crop_length))

    yield (tf.image.resize_images(batch_crops[i], (img_height, img_width)), batch_y)

Please note that you should call fit_generator() with batches produced by the crop_generator(). Since the ResNet50 based classifier is designed to take (224, 224, 3) images as inputs, the code should work OK.

Check out the source code: train_cropped.py


    net_final.fit_generator(train_crops, ......)

Otherwise, please provide your modified code in full. I can take a look and try to locate the problem.

It is more precise to make the function of random_crop as the argument of ImageDataGenerator. It is the simple script to stress the argument of preprocessing_function.

_preprocessing_function=random_crop_

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import numpy
import numpy.random
from skimage.transform import resize 

# Please create the two folders and then put lena.jpg into the source directory. 
src_path = '~/source/lena.jpg'
dst_path = '~/destination'

def random_crop(image):
    height, width = image.shape[:2]
    random_array = numpy.random.random(size=4);
    w = int((width*0.5) * (1+random_array[0]*0.5))
    h = int((height*0.5) * (1+random_array[1]*0.5))
    x = int(random_array[2] * (width-w))
    y = int(random_array[3] * (height-h))

    image_crop = image[y:h+y, x:w+x, 0:3]
    image_crop = resize(image_crop, image.shape)
    return image_crop

# Data generator 
datagen = ImageDataGenerator(rotation_range=0.2,
                             width_shift_range=0.2,
                             height_shift_range=0.2,
                             shear_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=True,
                             fill_mode='nearest',
                             preprocessing_function=random_crop)

img = load_img(src_path)
x = img_to_array(img) 
x = x.reshape((1,) + x.shape) 

# The flow() command generates batches of randomly transformed images
# and saves the results to the 'destination' directory. 
i = 0
for batch in datagen.flow(x, batch_size=1, save_to_dir=dst_path, 
                          save_prefix='lena', save_format='jpg'):
    i += 1
    if i > 60:
        break  # otherwise the generator would loop indefinitely

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nryant picture nryant  路  3Comments

harishkrishnav picture harishkrishnav  路  3Comments

KeironO picture KeironO  路  3Comments

anjishnu picture anjishnu  路  3Comments

farizrahman4u picture farizrahman4u  路  3Comments