hub icon indicating copy to clipboard operation
hub copied to clipboard

hub/pytorch_vision_deeplabv3_resnet101. plot error

Open soldier828 opened this issue 5 years ago • 3 comments

# create a color pallette, selecting a color for each class
palette = torch.tensor([2 ** 25 - 1, 2 ** 15 - 1, 2 ** 21 - 1])
colors = torch.as_tensor([i for i in range(21)])[:, None] * palette
colors = (colors % 255).numpy().astype("uint8")

# plot the semantic segmentation predictions of 21 classes in each color
r = Image.fromarray(output_predictions.byte().cpu().numpy()).resize(input_image.size)
r.putpalette(colors)

import matplotlib.pyplot as plt
plt.imshow(r)
# plt.show()

The above code will raise TypeError: only size-1 arrays can be converted to Python scalars. The code r.putpalette(colors) requires single color not colors. How to fix this code?

soldier828 avatar Jun 26 '19 09:06 soldier828

Hi @soldier828 could you paste your env here, like python version and how to repro? The script runs for me, and putpalette does take sequence. https://pillow.readthedocs.io/en/4.1.x/reference/Image.html#PIL.Image.Image.putpalette

ailzhang avatar Jun 26 '19 16:06 ailzhang

Hi @soldier828 could you paste your env here, like python version and how to repro? The script runs for me, and putpalette does take sequence. https://pillow.readthedocs.io/en/4.1.x/reference/Image.html#PIL.Image.Image.putpalette

I follow the code refer to https://pytorch.org/hub/pytorch_vision_deeplabv3_resnet101/. Except one change as below, namely import deeplabv3 model. Did this lead to the r.putpalette error?

model = torchvision.models.segmentation.deeplabv3_resnet101(pretrained=True)
model.eval()

When I use the raw code for import model, I got this Cannot find callable deeplabv3_resnet101 in hubconf, so I change the code to the torchvision.models.segmentation.deeplabv3_resnet101

My env: Python 2.7.11 torch 1.1.0 torchvision 0.3.0 Pillow 4.1.1

CentOS release 6.3

All bug info

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-083f663418f9> in <module>()
      6 # plot the semantic segmentation predictions of 21 classes in each color
      7 r = Image.fromarray(output_predictions.byte().cpu().numpy()).resize(input_image.size)
----> 8 r.putpalette(colors)
      9 
     10 # import matplotlib.pyplot as plt

/home/users/project/standalone_torch041_cuda90/mms-hpc-devel/lib/python2.7/site-packages/PIL/Image.pyc in putpalette(self, data, rawmode)
   1491             if not isinstance(data, bytes):
   1492                 if bytes is str:
-> 1493                     data = "".join(chr(x) for x in data)
   1494                 else:
   1495                     data = bytes(data)

/home/users/project/standalone_torch041_cuda90/mms-hpc-devel/lib/python2.7/site-packages/PIL/Image.pyc in <genexpr>((x,))
   1491             if not isinstance(data, bytes):
   1492                 if bytes is str:
-> 1493                     data = "".join(chr(x) for x in data)
   1494                 else:
   1495                     data = bytes(data)

TypeError: only size-1 arrays can be converted to Python scalars

soldier828 avatar Jun 27 '19 11:06 soldier828

@soldier828 please give this a try again with the latest pytorch. it works in the CI environment it looks like.

soumith avatar Oct 29 '19 15:10 soumith