transform.ToTensor data type concerns?

I find that if my data type is uint8, after the transform.ToTensor(), the range is changed to [0,1]
while if my data type is int32, the range doesn’t change.
someone could explain me why? thank you.

a = torch.randint(0,255,(500,500), dtype=torch.uint8)
print(a.size())
print(torch.max(a))
a = torch.unsqueeze(a, dim =0)
print(a.size())
compose = transforms.Compose([transforms.ToPILImage(),transforms.ToTensor()])
a_trans = compose(a)
print(a_trans.size())
print(torch.max(a_trans))

Result:

torch.Size([500, 500])
tensor(254, dtype=torch.uint8)
torch.Size([1, 500, 500])
torch.Size([1, 500, 500])
tensor(0.9961)

IF:

a = torch.randint(0,255,(500,500), dtype=torch.int32)
print(a.size())
print(torch.max(a))
a = torch.unsqueeze(a, dim =0)
print(a.size())
compose = transforms.Compose([transforms.ToPILImage(),transforms.ToTensor()])
a_trans = compose(a)
print(a_trans.size())
print(torch.max(a_trans))

Result:

torch.Size([500, 500])
tensor(254, dtype=torch.int32)
torch.Size([1, 500, 500])
torch.Size([1, 500, 500])
tensor(254, dtype=torch.int32)

Hi,
I think it depends on the ToPILImage instead of ToTensor:
https://pytorch.org/docs/stable/torchvision/transforms.html#torchvision.transforms.ToPILImage

if the input has 1 channel, the mode is determined by the data type (i,e, int , float , short )

Maybe you can assign the mode you want:

The transforms.ToPILImage is defined as follows:
Converts a torch.*Tensor of shape C x H x W or a numpy ndarray of shape H x W x C to a PIL Image while preserving the value range.
So I don’t think it will change the value range.

The  `mode`  of an image defines the type and depth of a pixel in the image

In my case, the data value range change. I don’t know why and I think it’s correlated to

transforms.ToTensor()

But I’m not sure

I faced the same issue. You may consider changing your data-type before using.totensor(). Dont know why this issue occurs.