Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: how to disable resizing of images when using an ImageDataGenerator with flow_from_dataframe / flow_from_directory?

I am trying to apply some cropping operations on my images by using an ImageDataGenerator and passing a custom function to the preprocessing_function parameter . However, according to the Keras docs, this function will only run after the images are already resized:

preprocessing_function: function that will be implied on each input. The function will run after the image is resized and augmented.

Now I would like to disable resizing, however the target_size parameter defaults to (256,256) if no value is provided. Again from the docs:

target_size: Tuple of integers (height, width), default: (256, 256). The dimensions to which all images found will be resized.

I've been trying to set the target_size parameter to None, however this results in an error:

TypeError: 'NoneType' object cannot be interpreted as an integer

Cropping an already resized image yields wrong results in my case. Hence I am looking for a way, to prevent either the resizing to happen at all or for it to only happen after my custom preprocessing function has been applied. Is this possible without writing an entire custom data generator?

like image 630
AaronDT Avatar asked Feb 27 '19 21:02

AaronDT


1 Answers

With the current implementation of ImageDataGenerator it is impossible. Image resizing happens while loading image, while preprocessing function is called from standardize few steps later.

Not to resize during loading is also not an option, since iterator pre-allocates the array for a batch and needs to know its shape.

like image 181
Dmytro Prylipko Avatar answered Nov 05 '22 04:11

Dmytro Prylipko