keras-team / keras-preprocessing

Utilities for working with image data, text data, and sequence data.
Other
1.02k stars 444 forks source link

ImageDataGenerator resizing and transforming images before preprocessing #95

Open ldm314 opened 5 years ago

ldm314 commented 5 years ago

I am trying to use the preprocessing function to take a network sized crop out of inconsistently sized input images instead of resizing to the network size. I have tried to do this using the preprocessing function but found that it is not easily possible. Using Keras 2.2.2

TypeError: 'NoneType' object is not iterable

While looking into this I saw that the preprocessing function runs at the start of standardize, which is after the random transforms are applied. To me this sounds like preprocssing is a bad name since it isn't actually happening first.

TurnipEntropy commented 5 years ago

Agreed; Having the same problem. I'm resizing the images in the preprocessing function, but then batch_x[i] complains about the image being the wrong size. No, it's just using self.x.shape (older version, admittedly) or self.image_shape instead of determining the image shape from the images flowing into it. I do understand the dilemma here though. You don't want to run the batch process before setting self.image_shape, and you don't want to allow different batches to have different image sizes. It's tricky. Maybe if target_size were in flow, this problem could be avoided though.

rragundez commented 5 years ago

I would like to help you guys out. If I understand correctly you need to be able to crop images instead of resizing/stretching/etc? I just want to get the point clear.

ldm314 commented 5 years ago

@rragundez, The specific need is the ability to resize the images in the preprocessing function. In the end, I wrote a generator specific to the application.

pratiklodha95 commented 5 years ago

@rragundez I jumped into the issue which you described where I want to crop the image and before other transformation in the images are done

Dref360 commented 5 years ago

I think this will be solved in the new design? See https://github.com/keras-team/governance/pull/6/files

I know that this proposition is not yet accepted, but once it is, it should be pretty fast.

zaccharieramzi commented 4 years ago

I also find myself in the position where I would like to flow directly from a directory where images have different sizes because I can't hold all the images in memory and also would like to crop/rotate/pad on-the-fly.

Currently I would need to implement my own Sequence object, but if there was a resizing function that could be called before the pre-processing function (that is supposed to return images of the same size as the input) and before the augmentation, I could just use that.

The new design @Dref360 pointed out was closed I think sine die. I also think in this new design the pre-processing function (even if called before the resizing and augmentation) was supposed to keep the same size for input and output.

Wouldn't it be possible to just add the possibility to "override" (i.e. provide our own) the resizing function when flowing from a directory? This wouldn't change the preprocessing function's behaviour and I guess (didn't look at the code for this) would mean just replacing the current resizing function if overridden. I can try and do a PR for this.

zaccharieramzi commented 4 years ago

For those interested by this problem, I have a fork where I implemented this idea, I have been using it and it's working great so far. I opened a PR, but so far no one has taken the time to review it: https://github.com/keras-team/keras-preprocessing/pull/248.