Simple way to inverse transform ? Normalization

(Tristan Stérin) #1

Hi all!
I’m using torchvision.transforms to normalize my images before sending them to a pre trained vgg19.
Therefore I have the following:

normalize = transforms.Normalize(mean = [ 0.485, 0.456, 0.406 ],
                      std = [ 0.229, 0.224, 0.225 ])

My process is generative and I get an image back from it but, in order to visualize, I’d like to “un-normalize” it.
Is there a simple way, in the API, to inverse the normalize transform ?
Or should it be coded by hand ?

Also I’m a bit surprise that the process works really fine without any normalization step.
The whole thing is about style transfer, from this paper:, and there’s a nice pytorch implementation outhere (not mine) here:

That implementation doesn’t normalize anything before feeding images to vgg19 and the results are OK.
Basically vgg19 is used to extract features from feeded images.
Your thoughts on why it stills works ?

(Maximilian Hötger) #2

did you find any solution for your problem yet? I also normalize before training, to get better loss values, but the generated images look very dark and in terms of colors strange.

(Joel Simon) #3

A way to reverse the normalization does not seem to exist. However it is pretty straightforward to create a simple class that does so.

class UnNormalize(object):
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std

    def __call__(self, tensor):
            tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
            Tensor: Normalized image.
        for t, m, s in zip(tensor, self.mean, self.std):
            # The normalize code -> t.sub_(m).div_(s)
        return tensor

You instantiate it with the same arguments used for the normalize. and then use it the same way

unorm = UnNormalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))

(Saurabh) #4

Most easiest way would be:

invTrans = transforms.Compose([ transforms.Normalize(mean = [ 0., 0., 0. ],
                                                     std = [ 1/0.229, 1/0.224, 1/0.225 ]),
                                transforms.Normalize(mean = [ -0.485, -0.456, -0.406 ],
                                                     std = [ 1., 1., 1. ]),

inv_tensor = invTrans(inp_tensor)

(Kaican Li) #5

A more concise approach based on Saurabh’s answer:

inv_normalize = transforms.Normalize(
    mean=[-0.485/0.229, -0.456/0.224, -0.406/0.255],
    std=[1/0.229, 1/0.224, 1/0.255]
inv_tensor = inv_normalize(tensor)