Fix pil_to_tensor() to support I;16 mode PIL images - #9661
Conversation
Fixes pytorch#8188. pil_to_tensor() converted PIL images to numpy arrays without mapping mode-specific dtypes, so I;16 mode images (which PIL exposes as uint16) failed since PyTorch has no native uint16 tensor type. This applies the same mode_to_nptype mapping that to_tensor() already uses.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9661
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @Anuragcap! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Fixes #8188.
pil_to_tensor() converted PIL images with a raw np.array(pic, copy=True) call, which inherits whatever dtype PIL returns for the image's mode. For I;16 mode (16-bit grayscale), PIL returns uint16, which PyTorch has no native tensor type for, causing a TypeError.
to_tensor() already handles this correctly via a mode_to_nptype mapping. This PR applies the same mapping to pil_to_tensor().
Test
Added test_pil_to_tensor_i16 in test/test_transforms.py, verifying that an I;16 mode image converts to an int16 tensor with correct shape and values.