Skip to content

handle I;16 mode in pil_to_tensor#9457

Open
knQzx wants to merge 9 commits into
pytorch:mainfrom
knQzx:fix/pil-to-tensor-i16-mode
Open

handle I;16 mode in pil_to_tensor#9457
knQzx wants to merge 9 commits into
pytorch: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.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ You can merge normally! (2 Unrelated Failures)

As of commit 2813bc1 with merge base b9ee001 (image):

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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
Comment thread test/test_transforms_v2.py Outdated
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 thread torchvision/transforms/functional.py Outdated
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

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.

I left some comments on the note section.

Comment thread docs/source/transforms.rst Outdated
Comment on lines +107 to +109
inputs. If you're working with uint16 images (e.g. from 16-bit medical or
scientific imaging), consider converting to ``float32`` first using
:class:`~torchvision.transforms.v2.ToDtype`.
Copy link
Copy Markdown
Contributor

@zy1git zy1git Apr 13, 2026

Choose a reason for hiding this comment

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

Is there any reason we mention the uint16-to-float32 conversion specifically?

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.

no specific reason, dropped that bit in the latest commit. the note just lists the unsupported dtypes now.

Comment thread docs/source/transforms.rst Outdated
Comment on lines +102 to +110
.. note::

``torch.uint16``, ``torch.uint32``, and ``torch.uint64`` dtypes are not
officially supported by the torchvision transforms. While some operations
may work, most transforms expect ``torch.uint8`` or ``torch.float32``
inputs. If you're working with uint16 images (e.g. from 16-bit medical or
scientific imaging), consider converting to ``float32`` first using
:class:`~torchvision.transforms.v2.ToDtype`.

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.

We can put this note after "Use :class:~torchvision.transforms.v2.ToDtype to convert both the dtype and range of the inputs."

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.

moved it there in the latest commit.

@knQzx
Copy link
Copy Markdown
Author

knQzx commented Apr 14, 2026

@zy1git moved the note after the ToDtype line and dropped the float32 specifics, also merged main

@knQzx
Copy link
Copy Markdown
Author

knQzx commented Apr 15, 2026

the 5 macos failures in TestErase::test_transform_image_correctness[*-cpu-dtype1-value-random] are pre-existing and reproduce on main without any of my changes — see https://github.com/pytorch/vision/actions/runs/24390062916 (same 5 tests fail on main). not related to this PR.

my added test_I16_to_tensor[to_tensor] and [pil_to_tensor] both pass on all platforms.

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