transforms.ToTensor() Division by 255 doesn't work

Hello together,

At first: This is my first question at the forum and I am not a native speaker so please forgive me if I make some mistakes.

so here is my code:

data = np.random.rand(3,3,1)*255

[ 58.35949555]]
[[113.9660475 ]
[196.0943086 ]]
[152.0496204 ]
[ 20.19787269]]]


tensor([[[ 244.4174, 107.1074, 58.3595],
[ 113.9660, 219.2927, 196.0943],
[ 226.9142, 152.0496, 20.1979]]], dtype=torch.float64)

The transpose is normal but there is no division with 255?
What do i make wrong?

Thank you in front

1 Like

Currently the division by 255. is done, when the transformation assumes an image.
Using a np.array this is done by checking against the dtype.
If you pass a np.uint8 array into the transformation, it will be scaled.
You can find the line of code here.

data = np.array(np.random.rand(3, 3, 1) * 255., dtype=np.uint8)

Hello, i noticed this strange thing some days ago.

When i use images /= 255 to make them between 0 and 1 my code works just fine.

If instead of images /= 255 i use

    X_copy = copy.deepcopy(images)
    X_copy = Variable(torch.FloatTensor(X_copy))

    trans = transforms.Compose([transforms.ToTensor()])

    for i in range(X_copy.shape[0]):
        a = F.to_pil_image(X_copy[i])
        X_copy[i] = trans(a)

    return X_copy`

Then my program trains for 8-40 epochs in cifar100 dataset. Then at some point the train error goes higher and the learning stops. I discovered this when i was trying to find the correct way to have my images between 0 and 1. Any idea why does this happen? Is my way to make images between 0 and 1 using ToTensor wrong?

Could you check, if trans creates the same output as your manual normalization?
If that’s the case, how reproducible is this issue? E.g. if you run the manual approach with 10 different seeds vs. the ToTensor approach, is the second one always worse than the first use case?