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
Nilesh_Pandey:
transforms.normalize()
I guess this has to be normalize
.
You can refer to this post:
Normalize does the following for each channel:
image = (image - mean) / std
The parameters mean, std are passed as 0.5, 0.5 in your case. This will normalize the image in the range [-1,1]. For example, the minimum value 0 will be converted to (0-0.5)/0.5=-1, the maximum value of 1 will be converted to (1-0.5)/0.5=1.
if you would like to get your image back in [0,1] range, you could use,
image = ((image * std) + mean)
About whether it helps CNN to learn better, I’m not sure. But majority o…
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?
bruceyo
(Bruce YU)
October 17, 2019, 2:17am
7
# 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