What is the best way to "UnNormalize" a Tensor?

I am using pre-trained inceptionv3 model in my script. I normalize tensor with following mean and std:

mean=[0.485, 0.456, 0.406]
std=[0.229, 0.224, 0.225]

I would like to know how can I perform reverse operation. Currently, I perform this op in the following way:

#first-remove batch dimension
img[0] = img[0] * 0.229
img[1] = img[1] * 0.224 
img[2] = img[2] * 0.225 
img[0] += 0.485 
img[1] += 0.456 
img[2] += 0.406

Is there a better way to do this?


you can vectorize it: mt = torch.FloatTensor(mean).view(3,1,1), st similar allows you to do img*mt. This is by virtue of the broadcasting implemented in PyTorch. As PyTorch automatically prepends dimensions, it even works if img is a batch of images.

Best regards


1 Like


Thanks for the reply. It works fine. In fact, I knew this and also tried it before posting my question here. I thought the problem is with unnormalization but it seems like the problem is something else.