How should I convert tensor image range [-1,1] to [0,1]

Hi, I use torchvision.transform to transform the image as

normalize = transforms.Normalize(mean=[0.5,0.5,0.5],std=[0.5,0.5,0.5])
transform = transforms.Compose(
        [transforms.ToTensor(),
        transforms.normalize()])
       

The images are in the range of [-1,1], whereas I need the range to be in [0,1].
Any help or clue would be appreciated, thank you.

1 Like

I guess this has to be normalize.

You can refer to this post:

To renormalize the image, you can use transforms.Normalize(mean=[-1,-1,-1],std=[2,2,2]).

1 Like

I have no idea, but saving Images normalize at mean=0,SD=1 are blurry.

can you paste the exact code that you are using?

Once I do normaization, I load the images using loader_image, and loader_2nsimage, after working on image, it is saved using following comand, where first image is original image from loader

pic = (torch.cat([original_image_A,original_image_B], dim=0).data + 1) / 2.0

save_dir = './img4_1/'
#             os.mkdir(save_dir)
torchvision.utils.save_image(pic, '%s/Epoch_(%d)_(%dof%d).jpg' % (save_dir, epoch, i + 1, min(len(loader_image), len(loader_2ndimage))), nrow=3)

The saved images are good when saved using normalization of mean=05, sd=1 or any other, but using 0,1 makes it blurry.

I’m not sure what you mean by this. can you please elaborate?

 # step 1: convert it to [0 ,2]
tensor_image = tensor_image +1

 # step 2: convert it to [0 ,1]
tensor_image = tensor_image - tensor_image.min()
tensor_image_0_1 = tensor_image / (tensor_image.max() - tensor_image.min())
2 Likes