-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
Conversation
keras/preprocessing/image.py
Outdated
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`. |
There was a problem hiding this comment.
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?
You are right, of course. Fixed now. |
For the consistency with the broader API, it should be specified as a lowercase string, rather than as a constant from |
@fchollet I addressed your comment. |
keras/preprocessing/image.py
Outdated
@@ -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"): |
There was a problem hiding this comment.
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
keras/preprocessing/image.py
Outdated
"""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 |
There was a problem hiding this comment.
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"
keras/preprocessing/image.py
Outdated
@@ -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') |
There was a problem hiding this comment.
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.
@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". |
keras/preprocessing/image.py
Outdated
'bicubic': pil_image.BICUBIC, | ||
} | ||
# These methods were only introduced in version 3.4.0 (2016). | ||
if hasattr(PIL.Image, "HAMMING"): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
keras/preprocessing/image.py
Outdated
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
Re-running tests. |
Re-running tests, again. |
@fchollet Added unit tests to test the correct behavior of the interpolation and the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
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)
.