How the torchvision.transforms works?

Yes! Given that you’ve calculated them from your data.
Normalization of the data usually helps the model training.

To transform your data, you can just scale with the desired std and add the mean:

# Create random input images
mean = 2.
std = 4.
x = torch.randn(100, 3, 24, 24) * std + mean
print('mean: {}, std: {}'.format(x.mean(), x.std()))


# Scale so that mean = 0.5 and std = 2
x = x - x.mean()
x = x / x.std()
print('mean: {}, std: {}'.format(x.mean(), x.std()))

new_mean = 0.5
new_std = 2.
x = x * new_std
x = x + new_mean
print('mean: {}, std: {}'.format(x.mean(), x.std()))

If you want to apply it on images using transforms, have a look at this thread.

1 Like