Skip to content

ToTensor cannot handle PIL Image with mode '1' #371

@IanChen83

Description

@IanChen83

img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))

If the image's mode is 1, image.tobytes is called to convert image to bytes. However, this will return a Tensor object with element size 8x of the original image, and result in RuntimeError.

Traceback (most recent call last):
  File "test.py", line 106, in <module>
    (0.2156, 0.2111, 0.2125)),
  File "test.py", line 80, in __init__
    img, pk = pk_loader(f'{fpath}/{path}')
  File "test.py", line 64, in pk_loader
    m = ToTensor(data[channel])
  File "/usr/lib/python3.6/site-packages/torchvision/transforms.py", line 58, in __call__
    img = img.view(pic.size[1], pic.size[0], nchannel)
RuntimeError: invalid argument 2: size '[1461 x 512 x 1]' is invalid for input of with 93504 elements at /pytorch/torch/lib/TH/THStorage.c:41

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions