Converting tensors to images

Varun_Tirupathi: <p>Hi I want to convert my output of tensor values those I’m getting from UNet to images . Is there any way to do this? Below is my code chunk where i want to do</p>
<pre><code class="lang-auto">    def test_step(self, batch, batch_nb):
        x, y = batch
        y_hat = self.forward(x)
        loss = torch.nn.MSELoss()
        op_loss = loss(y_hat, y)
        #saving tensors to images code goes here
        print(op_loss)
        return {'test_loss': op_loss}
</code></pre>
<p>I want to save the tensors to images to some local file path after calculating op_loss</p>

a_d: <p>you can convert the tensors to numpy and save them using opencv</p>
<pre><code class="lang-auto">tensor  = tensor.cpu().numpy() # make sure tensor is on cpu
cv2.imwrite(tensor, "image.png")
</code></pre>

Varun_Tirupathi: <p>Thanks for the response! Can’t I do if my tensor is on GPU because I’m training my model on GPU. If No, Is there any other way to do it?</p>

a_d: <p>No,  you have to make sure your data is on the CPU.<br>
Also, even if you train your model on the GPU, all you have to do is shift the output tensor to the cpu and store the images. Here is a minimal Example</p>
<pre><code class="lang-auto">model.cuda()
output = model(input)
output = output.cpu().numpy()
cv2.imwrite(output, 'pic.png')
</code></pre>

Varun_Tirupathi: <p>Hi,</p>
<p>while doing the conversion i’m getting length exception I posted the issue there:</p>
<aside class="quote quote-modified" data-post="1" data-topic="99782">
  <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/varun_tirupathi/48/29583_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/getting-the-error-while-converting-the-tensors-to-images-using-cv2/99782">Getting the error while converting the tensors to Images using cv2</a> 
  </div>
  <blockquote>
    Hi, 
I have converted the tensors which I’m getting as an output(test set) from my UNet to images. When I’m testing the model it is throwing out of index exception which I’m not getting when I didn’t convert the tensors to images. I have 74 images in my test set but after getting tested with one image it is throwing out of index exception in my custom dataset class. any idea will there will be any problem if I do a conversion like that? 
# this is where im converting
 def test_step(self, batch, …
  </blockquote>
</aside>

<p>Please give any suggestion about this</p>

dash: <p>Once you have your tensor in CPU, another possibility is to apply Sigmoid to your output and estimate a threshold (the mid point for example) in order to save it as an binary image.</p>
<pre><code class="lang-auto">from torchvision.utils import save_image
</code></pre>
<pre><code class="lang-auto">img1 = torch.sigmoid(output) # output is the output tensor of your UNet, the sigmoid will center the range around 0.
# Binarize the image
threshold = (img1.min() + img1.max()) * 0.5
ima = torch.where(img1 &gt; threshold, 0.9, 0.1)
save_image(ima, 'BIN_ima.png')
</code></pre>
<p>Or  you could try to “greyscale” the image…</p>
<pre><code class="lang-auto">img1 = torch.sigmoid(output) 
min = img1.min()
max = img1.max()
img2 = 1./(max-min) * img1 + 1.*min / (min-max)
save_image(img2, 'GREY_img.png')
</code></pre>