How to solve this error - Invalid dimensions for image data

username: Nisheeth_Jaiswal
cooked: <p>def img_convert(tensor):<br>
image = tensor.clone().detach().numpy()<br>
image = image.transpose(1, 2, 0)<br>
print(image.shape)<br>
image = image*np.array(0.5,) + np.array(0.5,)<br>
image = image.clip(0, 1)<br>
return image</p>
<p>dataiter = iter(training_loader)<br>
images, labels = dataiter.next()<br>
fig = plt.figure(figsize=(25, 4))</p>
<p>for idx in np.arange(20):<br>
ax = fig.add_subplot(2, 10, idx+1, xticks=[], yticks=[])<br>
plt.imshow(img_convert(images[idx]))<br>
ax.set_title([labels[idx].item()])</p>

username: Nisheeth_Jaiswal
cooked: <p>Can someone help me with this error can’t figure out how to resolve this, first I had gray scale problem then after fixing it there is a new issue.<br>
Invalid dimension for image data</p>

username: ptrblck
cooked: <p>Could you print the shape of <code>images[idx]</code> and the returned numpy array from <code>img_convert</code>?</p>
<p>PS: You can add code snippets using three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: Nisheeth_Jaiswal
cooked: <p>On printing the shape of images[idx] I find the shape is reversed<br>
Output:<br>
torch.Size([1, 28, 28])<br>
(28, 28, 1)</p>
<p>However if I put a transpose command<br>
images[idx].transpose(1, 2, 0)<br>
so which 2 positional arguments should I give</p>

username: ptrblck
cooked: <p>If you are dealing with grayscale images, you should remove the channel dimension for <code>matplotlib</code>:</p>
<pre><code class="lang-python">plt.imshow(np.random.randn(24, 24, 3)) # works
plt.imshow(np.random.randn(24, 24)) # works
plt.imshow(np.random.randn(24, 24, 1)) # fails
</code></pre>

username: Nisheeth_Jaiswal
cooked: <p>So what do you suggest I should do in my above mentioned code<br>
Where should I remove the channel dimensions in the img_conver method or in the below for loop</p>

username: ptrblck
cooked: <p>I would add something like:</p>
<pre><code class="lang-python">if image.shape[2] == 1:
    image = image[:, :, 0]
return image
</code></pre>
<p>into your <code>img_convert</code> method.</p>

username: Nisheeth_Jaiswal
cooked: <p>Thanks, it worked!!!</p>

username: Sushmanth_reddy_Mere
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  I was having a Tiff file dataset and the problem am having is ,couldn’t plot the tiff file and these are the following information about the tiff file.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/2/b225070dcfb9344dc50276033d5b1094fdfd65aa.png" data-download-href="https://discuss.pytorch.org/uploads/default/b225070dcfb9344dc50276033d5b1094fdfd65aa" title="Screenshot 2023-05-10 at 1.10.31 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/2/b225070dcfb9344dc50276033d5b1094fdfd65aa_2_690x283.png" alt="Screenshot 2023-05-10 at 1.10.31 AM" data-base62-sha1="ppWjTzJA8oIzExCSa02FsodnO1c" width="690" height="283" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/2/b225070dcfb9344dc50276033d5b1094fdfd65aa_2_690x283.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/2/b225070dcfb9344dc50276033d5b1094fdfd65aa_2_1035x424.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/2/b225070dcfb9344dc50276033d5b1094fdfd65aa_2_1380x566.png 2x" data-dominant-color="2A2A29"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2023-05-10 at 1.10.31 AM</span><span class="informations">2056×846 102 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
and this is the following error, could know where is the problem.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/d/0db1db8aca89d180a20bf504a9fd6a5bc2d39652.png" data-download-href="https://discuss.pytorch.org/uploads/default/0db1db8aca89d180a20bf504a9fd6a5bc2d39652" title="Screenshot 2023-05-10 at 1.12.00 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/d/0db1db8aca89d180a20bf504a9fd6a5bc2d39652_2_690x312.png" alt="Screenshot 2023-05-10 at 1.12.00 AM" data-base62-sha1="1X9grzxqiUD71gb2zPrtQUMiglc" width="690" height="312" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/d/0db1db8aca89d180a20bf504a9fd6a5bc2d39652_2_690x312.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/d/0db1db8aca89d180a20bf504a9fd6a5bc2d39652_2_1035x468.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/d/0db1db8aca89d180a20bf504a9fd6a5bc2d39652_2_1380x624.png 2x" data-dominant-color="474747"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2023-05-10 at 1.12.00 AM</span><span class="informations">2596×1174 256 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
and these are the following functions to convert from tiff to image array and display the tiff file<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/d/bd066ecf63c34c44939854e0b32d44bd0f1d9063.png" data-download-href="https://discuss.pytorch.org/uploads/default/bd066ecf63c34c44939854e0b32d44bd0f1d9063" title="Screenshot 2023-05-10 at 1.14.03 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/d/bd066ecf63c34c44939854e0b32d44bd0f1d9063_2_690x237.png" alt="Screenshot 2023-05-10 at 1.14.03 AM" data-base62-sha1="qYc1vainuiJBw92gCustC8NNCtZ" width="690" height="237" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/d/bd066ecf63c34c44939854e0b32d44bd0f1d9063_2_690x237.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/d/bd066ecf63c34c44939854e0b32d44bd0f1d9063_2_1035x355.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/d/bd066ecf63c34c44939854e0b32d44bd0f1d9063_2_1380x474.png 2x" data-dominant-color="252525"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2023-05-10 at 1.14.03 AM</span><span class="informations">2368×816 116 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>here is the following link to colab Notebook<br>
plss tell me , how to resolve this problem</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/drive/1YnKvzRN2K9YAwy46MN3QBJNf9UR0xzR9?usp=sharing">
  <header class="source">
      <img src="https://ssl.gstatic.com/colaboratory-static/common/ac0e6bae38aa2b5a52a4f6e10b43c43d/img/favicon.ico" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/drive/1YnKvzRN2K9YAwy46MN3QBJNf9UR0xzR9?usp=sharing" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img width="260" height="260" src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar">

<h3><a href="https://colab.research.google.com/drive/1YnKvzRN2K9YAwy46MN3QBJNf9UR0xzR9?usp=sharing" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: ptrblck
cooked: <p>I guess you are slicing the image in <code>diaplay_images</code> or are iterating each row later. Try to load the image once manually and display it directly without any additional logic to plot multiple images etc.</p>