transforms.ToTensor() does not work as document

import numpy as np
from torchvision import transforms
a=np.arange(534).reshape(5,3,4).astype(np.float)
b=transforms.ToTensor()(a)
b
tensor([[[ 0., 4., 8.],
[12., 16., 20.],
[24., 28., 32.],
[36., 40., 44.],
[48., 52., 56.]],
[[ 1., 5., 9.],
[13., 17., 21.],
[25., 29., 33.],
[37., 41., 45.],
[49., 53., 57.]],

    [[ 2.,  6., 10.],
     [14., 18., 22.],
     [26., 30., 34.],
     [38., 42., 46.],
     [50., 54., 58.]],

    [[ 3.,  7., 11.],
     [15., 19., 23.],
     [27., 31., 35.],
     [39., 43., 47.],
     [51., 55., 59.]]], dtype=torch.float64)

the range of be is also in the [0,255], not in the range [0,1]. But its docs says as follows:

Convert a PIL Image or numpy.ndarray to tensor.

Converts a PIL Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].

__call__ ( pic )

Parameters: pic ( PIL Image or numpy.ndarray ) – Image to be converted to tensor.
Returns: Converted image.
Return type: Tensor

the pytoch version is 0.4!

ToTensor() checks, if the input is of type uint8 in this line of code.
If you create your array a as np.uint8, it will work.

2 Likes

Thank you, You are right! However, in pytorch 0.3, it seems that when the dtype is np.float, it also convert to the range [0,1]