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

Nilesh_Pandey: <p>Hi, I use torchvision.transform to transform the image as</p>
<pre><code class="lang-auto">normalize = transforms.Normalize(mean=[0.5,0.5,0.5],std=[0.5,0.5,0.5])
transform = transforms.Compose(
        [transforms.ToTensor(),
        transforms.normalize()])
       
</code></pre>
<p>The images are in the range of [-1,1], whereas I need the range to be in [0,1].<br>
Any help or clue would be appreciated, thank you.</p>

InnovArul: <aside class="quote no-group" data-username="Nilesh_Pandey" data-post="1" data-topic="26792">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nilesh_pandey/48/5893_2.png" class="avatar"> Nilesh_Pandey:</div>
<blockquote>
<p>transforms.normalize()</p>
</blockquote>
</aside>
<p>I guess this has to be <code>normalize</code>.</p>
<p>You can refer to this post:</p>
<aside class="quote quote-modified" data-post="2" data-topic="21730">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/innovarul/48/5282_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/understanding-transform-normalize/21730/2">Understanding transform.Normalize( )</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    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…
  </blockquote>
</aside>

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

Nilesh_Pandey: <p>I have no idea, but saving Images normalize at mean=0,SD=1 are blurry.</p>

InnovArul: <p>can you paste the exact code that you are using?</p>

Nilesh_Pandey: <p>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</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>The saved images are good when saved using normalization of mean=05, sd=1 or any other, but using 0,1 makes it blurry.</p>

InnovArul: <aside class="quote no-group" data-username="Nilesh_Pandey" data-post="5" data-topic="26792">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nilesh_pandey/48/5893_2.png" class="avatar"> Nilesh_Pandey:</div>
<blockquote>
<p>but using 0,1 makes it blurry.</p>
</blockquote>
</aside>
<p>I’m not sure what you mean by this. can you please elaborate?</p>

bruceyo: <pre><code class="lang-auto"> # 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())
</code></pre>