Incorrect dtype conversion from PIL


I am experiencing a surprising behavior when converting a binary PIL image to BoolTensor through numpy.

import numpy as np
import torch
from PIL import Image, ImageDraw

mask ='1', size=(6, 6), color=False)
draw = ImageDraw.Draw(mask)
draw.polygon([(2, 2), (4, 2), (4, 4), (2, 4)], fill=True)

np_mask = np.array(mask)
print(np_mask.dtype, np.unique(np_mask))

tensor_mask = torch.from_numpy(np_mask)
print(tensor_mask.dtype, torch.unique(tensor_mask))

float_mask = tensor_mask.float()
print(float_mask.dtype, torch.unique(float_mask))

I would expect this code to print 0 and 1 as unique values of the float_mask. However, the unique values found are 0 and 255.

Where does this behavior come from? It does not happen for numpy as the code shows.

This seems to be related to this issue as it seems that the BoolTensor is transformed to a ByteTensor where True represents 255. Feel free to add your use case to this issue.

Indeed this seems to be the same issue. For some reason I did not find it before posting. Thanks!