pytorch / pytorch/vision

Setting the tensors and ndarrays of the 2, 3 and 4 channels of `int64` and `int32` to `ToPILImage()` with `None` for `mode` argument gets errors

Open
#9,043 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
17.9k
Forks
7.3k
Avg merge
1d 15h
Merged PRs (30d)
13

Description

🐛 Describe the bug

Setting the tensors and ndarrays of the 2, 3 and 4 channels of int64 and int32 to ToPILImage() with None for mode argument gets the errors as shown below:

from torchvision.transforms.v2 import ToPILImage
import torch
import numpy as np

tp = ToPILImage()
tp = ToPILImage(mode=None)

tp((torch.tensor([[[0]], [[1]]]), 0)) # int64
tp((torch.tensor([[[0]], [[1]], [[2]]]), 0)) #int64
tp((torch.tensor([[[0]], [[1]], [[2]], [[3]]]), 0)) #int64
tp((np.array([[[0, 1]]], dtype=np.int64), 0))
tp((np.array([[[0, 1, 2]]], dtype=np.int64), 0))
tp((np.array([[[0, 1, 2, 3]]], dtype=np.int64), 0))
# Error

tp((torch.tensor([[[0]], [[1]]], dtype=torch.int32), 0))
tp((torch.tensor([[[0]], [[1]], [[2]]], dtype=torch.int32), 0))
tp((torch.tensor([[[0]], [[1]], [[2]], [[3]]], dtype=torch.int32), 0))
tp((np.array([[[0, 1]]]), 0)) #int32
tp((np.array([[[0, 1, 2]]]), 0)) #int32
tp((np.array([[[0, 1, 2, 3]]]), 0)) #int32
# Error

TypeError: Input type int64 is not supported

TypeError: Input type int32 is not supported

But setting the tensors and ndarrays of the 2, 3 and 4 channels of int64 and int32 to ToPILImage() with 'LA', 'RGB', 'YCbCr', 'HSV', 'RGBA', 'CMYK' and 'RGBX' for mode argument works as shown below:

from torchvision.transforms.v2 import ToPILImage
import torch
import numpy as np

tp = ToPILImage(mode="LA")

tp((torch.tensor([[[0]], [[1]]]), 0)) # int64
tp((torch.tensor([[[0]], [[1]]], dtype=torch.int32), 0))
tp((np.array([[[0, 1]]], dtype=np.int64), 0))
tp((np.array([[[0, 1]]]), 0)) #int32
# (<PIL.Image.Image image mode=LA size=1x1>, 0)

tp = ToPILImage(mode="RGB")

tp((torch.tensor([[[0]], [[1]], [[2]]]), 0))
tp((torch.tensor([[[0]], [[1]], [[2]]], dtype=torch.int32), 0))
tp((np.array([[[0, 1, 2]]], dtype=np.int64), 0))
tp((np.array([[[0, 1, 2]]]), 0)) #int32
# (<PIL.Image.Image image mode=RGB size=1x1>, 0)

tp = ToPILImage(mode="YCbCr")

tp((torch.tensor([[[0]], [[1]], [[2]]]), 0))
tp((torch.tensor([[[0]], [[1]], [[2]]], dtype=torch.int32), 0))
tp((np.array([[[0, 1, 2]]], dtype=np.int64), 0))
tp((np.array([[[0, 1, 2]]]), 0)) #int32
# (<PIL.Image.Image image mode=YCbCr size=1x1>, 0)

tp = ToPILImage(mode="HSV")

tp((torch.tensor([[[0]], [[1]], [[2]]]), 0))
tp((torch.tensor([[[0]], [[1]], [[2]]], dtype=torch.int32), 0))
tp((np.array([[[0, 1, 2]]], dtype=np.int64), 0))
tp((np.array([[[0, 1, 2]]]), 0)) #int32
# (<PIL.Image.Image image mode=HSV size=1x1>, 0)

tp = ToPILImage(mode="RGBA")

tp((torch.tensor([[[0]], [[1]], [[2]], [[3]]]), 0))
tp((torch.tensor([[[0]], [[1]], [[2]], [[3]]], dtype=torch.int32), 0))
tp((np.array([[[0, 1, 2, 3]]], dtype=np.int64), 0))
tp((np.array([[[0, 1, 2, 3]]]), 0)) #int32
# (<PIL.Image.Image image mode=RGBA size=1x1>, 0)

tp = ToPILImage(mode="CMYK")

tp((torch.tensor([[[0]], [[1]], [[2]], [[3]]]), 0))
tp((torch.tensor([[[0]], [[1]], [[2]], [[3]]], dtype=torch.int32), 0))
tp((np.array([[[0, 1, 2, 3]]], dtype=np.int64), 0))
tp((np.array([[[0, 1, 2, 3]]]), 0)) #int32
# (<PIL.Image.Image image mode=CMYK size=1x1>, 0)

tp = ToPILImage(mode="RGBX")

tp((torch.tensor([[[0]], [[1]], [[2]], [[3]]]), 0))
tp((torch.tensor([[[0]], [[1]], [[2]], [[3]]], dtype=torch.int32), 0))
tp((np.array([[[0, 1, 2, 3]]], dtype=np.int64), 0))
tp((np.array([[[0, 1, 2, 3]]]), 0)) #int32
# (<PIL.Image.Image image mode=RGBX size=1x1>, 0)

In addition, setting the tensor and ndarray of the 1 channel of int64 to ToPILImage() with None and 'I' for mode argument gets the error as shown below:

from torchvision.transforms.v2 import ToPILImage
import torch
import numpy as np

tp = ToPILImage()
tp = ToPILImage(mode=None)
tp = ToPILImage(mode="I")

tp((torch.tensor([[[0]]]), 0)) # int64
tp((np.array([[[0]]], dtype=np.int64), 0))
# Error

ValueError: Incorrect mode (I) supplied for input type <class 'numpy.dtype'>. Should be None

But setting the tensor and ndarray of the 1 channel of int32 to ToPILImage() with None and 'I' for mode argument works as shown below:

from torchvision.transforms.v2 import ToPILImage
import torch
import numpy as np

tp = ToPILImage()
tp = ToPILImage(mode=None)
tp = ToPILImage(mode="I")

tp((torch.tensor([[[0]]], dtype=torch.int32), 0))
tp((np.array([[[0]]]), 0)) # int32
# (<PIL.Image.Image image mode=I size=1x1>, 0)
Versions
import torchvision

torchvision.__version__ # '0.20.1'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the torchvision.transforms.v2.ToPILImage entry point and reproduce the reported int64 and int32 tensor and ndarray cases with mode=None and the listed explicit modes. Compare the accepted and rejected channel and dtype combinations, then add coverage for the intended behavior and verify that the examples no longer raise the reported errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.