-
Notifications
You must be signed in to change notification settings - Fork 442
Add functionality to read 32-bit images #242
Conversation
CI should pass once #243 is merged and this branch is rebased. |
in general, i think that type cast (e.g., from uint32 to uint8) should NOT be used when converting images. my suggestion is to leave the source integer data type as the closest destination data type possibile, that is, uint8, uint16, uint32.
i came from the C++ background and scale=True seems a bit ambiguous to me (change type? normalize to what?), so i won't comment on this. |
Thanks for thee feedback @dibenedetto. I think I agree with you about not casting will be the best but there are some things to take into account. First some clarifications by array we mean a numpy array and by image a PIL image. When discussing makes it confusing to call an array an image even if the intent of the array is to represent an image. The functionality in the Your point makes total sense though if the function
You made good points and I'll think a bit more about it, I also think this PR is not in the best shape to address all points yet. @Dref360 do you have any comments on this? |
I think we can use the |
I think that array_to_img is only used when saving the image and when we apply brightness augmentation. Could we infer the correct dtype from the original dtype of the array before we cast it to float32? |
It might be, but then I would argue why the need of changing the Why not determine the PIL image mode directly from the array dtype without having that intermediate casting to |
I investigated using Then about grayscale images, if the user has an array of type int16 or int32, it will still work, yes the image will occupy more space if it was originally in int16 but the image will not be distorted. And we cannot infer the mode from the array type when images are coming from So I think this PR addresses the 32-bit problem given the circumstances of the intended use of the functions. @Dref360 could you have a proper review? |
d32d51e
to
9a4260f
Compare
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, but I think we also need to test it in https://github.com/keras-team/keras-preprocessing/blob/master/tests/image/directory_iterator_test.py#L14
I added the mentioned tests plus added tests in |
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
I am working with Keras '2.4.3' and I can not load float32 grayscale images with color_mode='rgb' and also color_mode='grayscale' using Image Data Generator. Can you help how I can resolve it? |
@sam6485 could you create an issue with the error and some steps to reproduce? I'll take a look |
Summary
This PR adds the functionality to read 32-bit images (32-bit signed integer pixel values) without losing information. This is specifically important for medical images as a great deal of information is being lost on the conversion to 8-bit.
The
load_img
function only affects and leaves 32-bit signed integer format ("I" for PIL modes) untouched.The
array_to_img
function now returns a 32-bit signed integer ("I" mode in PIL) if the array has a single channel and the maximum value of any pixel exceeds 255.One thing to note is that if
array_to_img
is executed with the argumentscale=True
then even if the array is in 32-bit format, the array will be normalized to 8-bit and return an 8-bit PIL image (mode "L").This PR has tests for 32-bit but I also added for grayscale 8-bit specifically.
Related Issues
Closes #239
PR Overview