Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify resampling method in load_img #7975

Merged
merged 10 commits into from
Oct 8, 2017

Conversation

ahojnnes
Copy link
Contributor

Otherwise, nearest neighbor interpolation is used, which produces awful results. I consider this as a bug. In any case, the old behavior can be recovered using load_img(..., resample=PIL.Image.NEAREST).

of `PIL.Image.NEAREST`, `PIL.Image.BOX`, `PIL.Image.BILINEAR`,
`PIL.Image.HAMMING`, `PIL.Image.BICUBIC` or `PIL.Image.LANCZOS`. If
omitted, or if the image has mode "1" or "P",
it is set `PIL.Image.NEAREST`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it'll be PIL.Image.BILINEAR if omitted?

@ahojnnes
Copy link
Contributor Author

You are right, of course. Fixed now.

@fchollet
Copy link
Member

For the consistency with the broader API, it should be specified as a lowercase string, rather than as a constant from PIL.Image. Also "resampling" would probably be a better name?

@ahojnnes
Copy link
Contributor Author

@fchollet I addressed your comment.

@@ -302,14 +314,19 @@ def img_to_array(img, data_format=None):
return x


def load_img(path, grayscale=False, target_size=None):
def load_img(path, grayscale=False, target_size=None,
interpolation="bilinear"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ' as quote char for consistency

"""Loads an image into PIL format.

# Arguments
path: Path to image file
grayscale: Boolean, whether to load the image as grayscale.
target_size: Either `None` (default to original size)
or tuple of ints `(img_height, img_width)`.
interpolation: Interpolation method used to resample the image if the
target size does not equal the loaded image. Supported methods are
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"is different from that of the loaded image"

@@ -330,7 +347,10 @@ def load_img(path, grayscale=False, target_size=None):
if target_size:
hw_tuple = (target_size[1], target_size[0])
if img.size != hw_tuple:
img = img.resize(hw_tuple)
if interpolation not in _PIL_INTERPOLATION_METHODS:
raise ValueError('Invalid interpolation method specified')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ValueError should be listed in the docstring. The error message should specify what was passed, and the list of values expected instead.

@ahojnnes
Copy link
Contributor Author

@fchollet I addressed your comments. In addition, I added a more fine-grain check for the available interpolation methods. Only newer pillow versions support "box" and "hamming".

'bicubic': pil_image.BICUBIC,
}
# These methods were only introduced in version 3.4.0 (2016).
if hasattr(PIL.Image, "HAMMING"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use consistent quote chars. If you know at which PIL version these attributes were introduce, prefer doing a version number check instead of repeated hasattr calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the various forks of PIL and hence different versioning schemes, I'd say it is safer to use this explicit test in this case.

img = img.resize(hw_tuple)
width_height_tuple = (target_size[1], target_size[0])
if img.size != width_height_tuple:
if interpolation not in _PIL_INTERPOLATION_METHODS:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a unit test for this feature, include a check for the ValueError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any test images shipped with keras that could be used for this test? Otherwise, it is not possible to get to this point without a failure before getting to this point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haves test code that creates test images and write them in a temporary folder. You can check out existing tests for preprocessing/image.

@fchollet
Copy link
Member

Re-running tests.

@fchollet fchollet closed this Sep 29, 2017
@fchollet fchollet reopened this Sep 29, 2017
@fchollet
Copy link
Member

fchollet commented Oct 2, 2017

Re-running tests, again.

@fchollet fchollet closed this Oct 2, 2017
@fchollet fchollet reopened this Oct 2, 2017
@ahojnnes
Copy link
Contributor Author

ahojnnes commented Oct 4, 2017

@fchollet Added unit tests to test the correct behavior of the interpolation and the load_img function itself, since I haven't found any other tests.

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@fchollet fchollet merged commit c034825 into keras-team:master Oct 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants