This may be a lack of understanding on my part, however, to me there seems to be an issue using the target_size parameter in flow_from_directory.
I am using Keras 2.04 pulled from master on 2017-05-17. Tensorflow 1.x backend.
When using flow_from_directory the images appear to be resized incorrectly. I call flow_from_directory with input images of width 150, height 100 pixels. The output images are stored with width 100, height 150, and represent the original image squashed in the width direction from 150 to 100 pixels and stretched in the height direction from 100 to 150 pixels.
flow_from_directory('dataset',
target_size=(150, 100),
batch_size = batch_size,
class_mode='binary',
shuffle=True,
save_to_dir='/out'
)
The problem appears to be in image.py in the load_img function.
Here img.size is in (width, height) format but hw_tuple is in (height,width) format resulting in an erroneous call to img.resize.
hw_tuple = (target_size[1], target_size[0])
if img.size != hw_tuple:
img = img.resize(hw_tuple)
The suggested fix is to change to
wh_tuple = (target_size[0], target_size[1])
if img.size != wh_tuple:
img = img.resize(wh_tuple)
In addition the image may need to be transposed to be output in (height, width) format or img_to_array and array_to_img altered to use (width, height) format. I'm not sure which format was the intended.
[X ] Check that you are up-to-date with the master branch of Keras. You can update with:
pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps
[X ] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.
[ ] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps
[ X] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).
More like a Documentation issue, since it's clearly state here that the size is in a (H,W) format.
It's not clear to me from the keras documentation whether I should be supplying target_size as (width, height) or (height, width). In the docs here it is not specified. So perhaps the documentation could be improved here too.
I agree that the documentation for load_img states (height, width) format but I think the problem is also in the code.
In the code we do
hw_tuple = (target_size[1], target_size[0])
if img.size != hw_tuple:
img = img.resize(hw_tuple
however, img.size is (width, height) so at the very least the variable is named incorrectly and should be wh_tuple.
I think it is more than this though, I believe there is a mismatch between the interpretation of the target_size parameter and other parts of the codebase. I can't be more specific as I'm new to Keras. I know though that if I were to set target_size to (height, width) then I would have "shape" issues when it comes to training my model.
Is it possible that flow_from_directory has only been tested with square images? Many of the public datasets have square training data.
Perhaps someone more familiar with Keras could take a look at this.
Yeah, lack of documentation and wrong variable naming. Maybe you could do a PR? Otherwise nobody probably will.
Alright, I can take a look at this in June. I'm leaving for holidays shortly...
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
Most helpful comment
Alright, I can take a look at this in June. I'm leaving for holidays shortly...