Permute error number of dims don't match error

sparshgarg23: <p>While running inference on my models,I ran into the following error</p>
<pre><code class="lang-auto">Runtime error number of dims don't match in permute
</code></pre>
<p>This is what I was doing</p>
<pre><code class="lang-auto">    val_transforms = T.Compose([
        T.Resize((256, 256)),
        T.CenterCrop((224, 224)),
        T.ToTensor(),
        T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])
    img_name=cfg.TEST.IMAGE
    img=cv2.imread(img_name)
    img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    if (isinstance(img,np.ndarray)):
        img=Image.fromarray(img)
    
    plt.imshow(img)
    img=val_transforms(img)
</code></pre>
<p>The shape of img is [3,224,224]<br>
Next I passed it to the model as follows</p>
<pre><code class="lang-auto">model=Model()#Model for my purpose transformer VIT small_patch_16-224
with torch.no_grad():
img=img.to(device).squeeze()
probs=model(img)
"""
apply softmax on probs to get result
"""
   ps = ps.cpu().data.numpy().squeeze()
   #ps is the output after applying softmax on probs 
    image = image.permute(1,2,0)#Runtime error generated at this point
    mean = torch.FloatTensor([0.485, 0.456, 0.406])
    std = torch.FloatTensor([0.229, 0.224, 0.225])
    
    
    image = image*std + mean
    img = np.clip(image,0,1)
    
    fig, (ax1, ax2) = plt.subplots(figsize=(8,12), ncols=2)
    ax1.imshow(img)
</code></pre>
<p>At first,I thought that because img has changed shape to [1,3,224,224] ,so that’s the reason why this is happening<br>
So I did img.squeeze() before  applying permute but that didn’t change the outcome.<br>
So I changed my code as follows and that resolved the issue.<br>
The new code</p>
<pre><code class="lang-auto">img_name=cfg.TEST.IMAGE
    img=cv2.imread(img_name)
    img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    if (isinstance(img,np.ndarray)):
        img=Image.fromarray(img)
    
    plt.imshow(img)
    img_1=val_transforms(img)
   output_prob=[]
   with torch.no_grad():
      prob=model(img_1.to(device).unsqueeze(0))
     output_prob.append(prob)
     output_prob=torch.cat(output_prob,dim=0)
     ps=nn.Softmax(output_prob,dim=1)
</code></pre>
<p>Any ideas why the second code version doesn’t raise the permute dimension mismatch error.<br>
As far as I could understand both versions are the same</p>

AlphaBetaGamma96: <aside class="quote no-group" data-username="sparshgarg23" data-post="1" data-topic="154663">
<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/sparshgarg23/48/41060_2.png" class="avatar"> sparshgarg23:</div>
<blockquote>
<pre><code class="lang-auto">        T.Resize((256, 256)),
</code></pre>
</blockquote>
</aside>
<p>Can you print the shape of <code>image</code>? It’s most likely 2D, and hence permuting over 3 dimensions isn’t possible. If you’re using greyscale images, you’ll want to include a channel dimension when you reshape. So the shape goes from <code>[256,256] -&gt; [1,256,256]</code>.</p>

sparshgarg23: <p>Shape of the image is 224x224x3.<br>
As shown in</p>
<pre><code class="lang-auto">T.CenterCrop((224, 224)),
</code></pre>
<p>The first version of the code gave the permute dimension mismatch error but the second one didn’t.</p>

AlphaBetaGamma96: <aside class="quote no-group" data-username="sparshgarg23" data-post="1" data-topic="154663">
<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/sparshgarg23/48/41060_2.png" class="avatar"> sparshgarg23:</div>
<blockquote>
<pre><code class="lang-auto">    image = image.permute(1,2,0)#Runtime error generated at this point
</code></pre>
</blockquote>
</aside>
<p>So if you print <code>image.shape</code> before this line ^, what is the shape? Just to check it is 3D and not somehow 2D.</p>

sparshgarg23: <p>Before that line the shape is (1,3,224,224).After applying squeeze the shape is (3,224,224).<br>
I will check and revert back<br>
image shape before permute</p>
<pre><code class="lang-auto">torch.Size([3, 224, 224])
</code></pre>

sparshgarg23: <p>not sure what is the error here. Because I ran the same code right now,and now it’s working.</p>

AlphaBetaGamma96: <aside class="quote no-group" data-username="sparshgarg23" data-post="5" data-topic="154663">
<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/sparshgarg23/48/41060_2.png" class="avatar"> sparshgarg23:</div>
<blockquote>
<p>After applying squeeze the shape is (3,224,224).</p>
</blockquote>
</aside>
<p>Probably applying <code>torch.squeeze</code> resolved the shape issue which was causing your code to previously fail.</p>

Anshumaan_Dash: <aside class="quote no-group" data-username="sparshgarg23" data-post="1" data-topic="154663">
<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/sparshgarg23/48/41060_2.png" class="avatar"> sparshgarg23:</div>
<blockquote>
<p><code>img=img.to(device).squeeze()</code></p>
</blockquote>
</aside>
<p>The <code>squeeze</code> operation gets rid of any single dimensions. E.g. <code>[1,3,224,224].squeze()</code> → <code>[3,224,224]</code></p>
<p>So, you need to do <code>.unsqueeze(0)</code> before passing the <code>img</code> to your model and do <code>.squeeze()</code> before <code>permute</code></p>