Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras - How to use ImageDataGenerator without deforming aspect ratio

I have a folder with images of different sizes. I tried to use ImageDataGenerator with flow_from_directory for batch loading / data augmentation.

Is there a way to keep the aspect ratios of my images ? It seems like the images are being stretched to target_size : I would like to "pad" my images without deforming them (filling the gaps with a constant value)

Here's my code :

datagen = ImageDataGenerator(
    rescale = 1./255,
    fill_mode='constant')

generator = datagen.flow_from_directory(
    'data/images',
    target_size=(256,256),
    color_mode = 'grayscale',
    batch_size=99,
    class_mode=None,
    shuffle=False)

Images are being stretched to (256,256).

like image 707
LeWiZ Avatar asked Feb 26 '17 11:02

LeWiZ


1 Answers

I found an answer to my question.

There's currently no way to keep aspect ratio when using ImageDataGenerator / flow_from_directory, but a pull request is opened on Github to add this feature.

like image 53
LeWiZ Avatar answered Sep 21 '22 01:09

LeWiZ