Vision: Reverse normalize on batches of images?(tensor type not PIL image)

Created on 3 Jul 2019  路  2Comments  路  Source: pytorch/vision

I have batches of images with size of (B,C,H,W) (torch.tensor) i want to unnormalized it with mean/std tensor (e.g.:[0.4,0.3,0.2]) with size of(C,H,W)

My question is is there any operations in Pytorch can do this likedataset.map or tf.expand_dims in Tensorflow ?

Be careful ,it not PIL image format

transforms question

Most helpful comment

You can achieve that by adding a batch dimension and let the broadcasting mechanics handle it for you:

mean, std = mean.unsqueeze(0), std.unsqueeze(0) # 1xCxHxW
y = x * std + mean

Why do your normalisation tensors (mean and std) have spatial dimensions (H and W)? This would indicate that the mean and std change depending on the position in the image, which is quite unusual. I'm guessing you want to do a per-channel normalisation, which makes mean and std 1D tensors of size C.

To follow up on you example:

device, dtype = x.device, x.dtype
mean = torch.tensor([0.4, 0.3, 0.2], device=device, dtype=dtype).view(1, -1, 1, 1)
std = torch.tensor([0.4, 0.3, 0.2], device=device, dtype=dtype).view(1, -1, 1, 1)
y = x * std + mean

If you want to have the denormalisation as a torchvision transform have a look at this comment from a similar issue.

All 2 comments

You can achieve that by adding a batch dimension and let the broadcasting mechanics handle it for you:

mean, std = mean.unsqueeze(0), std.unsqueeze(0) # 1xCxHxW
y = x * std + mean

Why do your normalisation tensors (mean and std) have spatial dimensions (H and W)? This would indicate that the mean and std change depending on the position in the image, which is quite unusual. I'm guessing you want to do a per-channel normalisation, which makes mean and std 1D tensors of size C.

To follow up on you example:

device, dtype = x.device, x.dtype
mean = torch.tensor([0.4, 0.3, 0.2], device=device, dtype=dtype).view(1, -1, 1, 1)
std = torch.tensor([0.4, 0.3, 0.2], device=device, dtype=dtype).view(1, -1, 1, 1)
y = x * std + mean

If you want to have the denormalisation as a torchvision transform have a look at this comment from a similar issue.

Thank you so much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

300LiterPropofol picture 300LiterPropofol  路  3Comments

iacolippo picture iacolippo  路  4Comments

zhang-zhenyu picture zhang-zhenyu  路  3Comments

alpha-gradient picture alpha-gradient  路  3Comments

a-maci picture a-maci  路  3Comments