Skip to content

handle I;16 mode in pil_to_tensor#9457

Open
knQzx wants to merge 7 commits intopytorch:mainfrom
knQzx:fix/pil-to-tensor-i16-mode
Open

handle I;16 mode in pil_to_tensor#9457
knQzx wants to merge 7 commits intopytorch:mainfrom
knQzx:fix/pil-to-tensor-i16-mode

Conversation

@knQzx
Copy link
Copy Markdown

@knQzx knQzx commented Mar 29, 2026

fixes #8188

pil images with I;16 mode use uint16 under the hood which pytorch doesn't support, so this converts them to int32 instead. also fixed the same issue in to_tensor where it was incorrectly using signed int16.

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot bot commented Mar 29, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9457

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 New Failures

As of commit aefaae1 with merge base 4e58149 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Mar 29, 2026

Hi @knQzx!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla bot added the cla signed label Mar 29, 2026
cm = pytest.warns(UserWarning, match="deprecated") if f is F.to_tensor else contextlib.nullcontext()
with cm:
out = f(I16_pil_img)
assert out.dtype == torch.int32
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @knQzx , thanks for the PR, as mentioned in #8359 I think the correct output type here should be uint16.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

hey, thanks for the feedback! updated to use uint16 for both to_tensor and pil_to_tensor. also fixed the failing tests - they were using signed ShortTensor data which doesn't make sense for I;16 (unsigned), so now they generate values in valid range and expect uint16 output

Copy link
Copy Markdown
Contributor

@zy1git zy1git left a comment

Choose a reason for hiding this comment

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

@knQzx Thanks for this PR! I left a comment. And you can also complete the PR as suggested here in the "Proposed fix" section.

Comment on lines +210 to +211
if pic.mode == "I;16":
img = img.astype(np.uint16)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this part is redundant because np.array(pic, copy=True) on an I;16 PIL image already returns a uint16 array. We can just keep the original code.

@knQzx knQzx force-pushed the fix/pil-to-tensor-i16-mode branch from d3f956b to 3fbe21e Compare April 2, 2026 13:38
@knQzx
Copy link
Copy Markdown
Author

knQzx commented Apr 2, 2026

you're right, removed the redundant astype - np.array already handles it. also rebased on main and added a docs note about uint16/32/64 not being officially supported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pil_to_tensor() doesn't work for PIL Image with I;16 mode

3 participants