Save_image not working: RuntimeError: result type Float can't be cast to the desired output type Byte

hanoody: <p>I have an output tensor called <code>output</code> of shape [12000, 12000, 3] and of type uint8. It is a tensor of RGB values ranging in [0, 255]. I am trying to save it using the following line</p>
<pre><code class="lang-auto">torchvision.utils.save_image(output, "stitched_output.jpg")
</code></pre>
<p>But I get the following error:</p>
<pre><code class="lang-auto">----&gt; 7 torchvision.utils.save_image(output, "stitched_output.jpg")

~/.local/lib/python3.6/site-packages/torchvision/utils.py in save_image(tensor, fp, nrow, padding, normalize, range, scale_each, pad_value, format)
    126                      normalize=normalize, range=range, scale_each=scale_each)
    127     # Add 0.5 after unnormalizing to [0, 255] to round to nearest integer
--&gt; 128     ndarr = grid.mul(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to('cpu', torch.uint8).numpy()
    129     im = Image.fromarray(ndarr)
    130     im.save(fp, format=format)

RuntimeError: result type Float can't be cast to the desired output type Byte
</code></pre>
<p>Any idea why this is happening? I never had issues before when it came to saving tensors as images. Thank you.</p>

hanoody: <p>Hi <a class="mention" href="/u/eqy">@eqy</a> <a class="mention" href="/u/ptrblck">@ptrblck</a>, any ideas? I just tagged you both because you both have been great help for me on this forum. I’ve been scratching my head at this for a bit but I can’t seem to find any solutions online. Thank you!</p>

eqy: <p>Does it work if you try to save an image in tensor format (e.g., channels first, in range (0, 1.0), dtype float)?</p>
<p>You can call the to tensor transformation to do this if you do not have it readily available in that format:<br>
<a href="https://pytorch.org/vision/stable/_modules/torchvision/transforms/functional.html#to_tensor" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/vision/stable/_modules/torchvision/transforms/functional.html#to_tensor</a></p>
<p>Alternatively, you might be able to use a PIL image saving utility as well if you already have a PIL image on-hand:<br>
<a href="https://pillow.readthedocs.io/en/stable/_modules/PIL/Image.html#Image.save" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pillow.readthedocs.io/en/stable/_modules/PIL/Image.html#Image.save</a></p>

hanoody: <p>Thank you very much! By permuting the dimensions into image tensor format and normalizing the tensor in float range [0.0, 1.0], we got it to work!</p>

Ankit_Maurya: <p>Hi <a class="mention" href="/u/hanoody">@hanoody</a> I also have similar problem. Can you please share your code ?</p>

Rishit_Ratan: <p>To convert your tensor into the image tensor format, it has to be in the format (batch_size, channels, height, width). To achieve this, you can permute the image using the <code>permute()</code> function.</p>
<p><code>permuted_tensor = tensor.permute(0, 1, 2, 3),</code></p>
<p>Again, keep in mind that your tensor might only have 3 dimensions, the height, width and channel, to add another dimension of batch size you can run:</p>
<p><code>permuted_tensor = tensor.unsqueeze(0).permute(0, 1, 2, 3)</code></p>
<p>And to normalize, you can run:</p>
<p><code>normalized_tensor = permuted_tensor / 255</code>, if the range of your channel is 255.</p>

Yanyu: <p>It seems like when you use</p>
<pre><code class="lang-auto">torchvision.utils.save_image(output.float(), "stitched_output.jpg")
</code></pre>
<p>It works. But I wonder why?</p>