Expected 4-dimensional input for 4-dimensional weight 16 3 3 3, but got 2-dimensional input of size [136, 408] instead

MUHAMMAD_JAHANZAIB_K: <p>if i pass the image to the my model i got this error</p>
<p>my model is</p>
<pre><code class="lang-auto">class mandal(nn.Module):
    def __init__(self):
        super(mandal,self).__init__()
        
        #layer1 inpit (136,136 *3) output (136,136)
        self.layer1=nn.Conv2d(3,16,3,stride=1,padding=1)
        
        #layer2 input (136,136*16) output (136,136*32)
        self.layer2=nn.Conv2d(16,32,3,stride=1,padding=1)
        
        #layer3 input(136,136*32) output(640,480*64)
        self.layer3=nn.Conv2d(32,64,3,stride=1,padding=1)
        
        #layer4 input(136,136*64) output(66,66*64)
        self.layer4=nn.MaxPool2d(2,2)
        
        #layer5 input(66,66*64) output (33,33*128)
        self.layer5=nn.Conv2d(64,128,3,stride=2,padding=2)
        
        #layeer6 input(33,33*128) output(17,17*128)
        self.layer6=nn.MaxPool2d(2,2)
        
        #layer7 input(17,17*128) output(8,8*192)
        self.layer7=nn.Conv2d(128,192,3,stride=2,padding=2)
        
        #layer8 input(8,8*192) output(8,8*120)
        self.layer8=nn.Conv2d(192,120,1,stride=1,padding=0)
        
        #layer9 input(8,8*94) output(8,8*64)
        self.layer9=nn.Conv2d(120,64,1,stride=1,padding=0)
        
        #layer10 input(8,8*64) output(8,8*32)
        self.layer10=nn.Conv2d(64,32,1,stride=1,padding=0)
        
        #layer11 input(8,8*32) output(8,8*16)
        self.layer11=nn.Conv2d(32,10,1,stride=1,padding=0)
        self.falten=nn.Flatten()
        #layer12 input(8,8*16) output(1024)
        self.layer12=nn.Linear(32*9*9,500)
        
        #layer13 input(1024) 0utput(500)
        self.layer13=nn.Linear(500,80) 
        
        #layer14 input(80) output(10)
        self.layer14=nn.Linear(80,10)
        
        #layer15 input(10) output(3)
        self.layer15=nn.Linear(10,2)
        
        #layer12 input(8,8*16) output(1024)
        self.layer16=nn.Linear(32*9*9,500)
        
        #layer13 input(1024) 0utput(500)
        self.layer17=nn.Linear(500,80)
        
        #layer14 input(80) output(10)
        self.layer18=nn.Linear(80,10)
        
        #layer15 input(10)  output(3)
        self.layer19=nn.Linear(10,4)
        #dropout p=0.20
        
        self.dropout=nn.Dropout(0.20)
        
        
    def forward(self,x):
        x = x.view(x.size(0), -1)
      # x = self.encoder(x)
      # x = x.unsqueeze(1)
        x = F.relu(self.layer1(x))
        x = F.relu(self.layer2(x))
        x = F.relu(self.layer3(x))
        x = self.layer4(x)
        x = F.relu(self.layer5(x))
        
        x = F.relu(self.layer7(x))
        x = F.relu(self.layer8(x))
        x = F.relu(self.layer9(x))
        x = F.relu(self.layer10(x))
        x = self.dropout(x)
        x = self.layer6(x)
        x = self.falten(x)
        y = x
        x = F.relu(self.layer12(x))
        x = self.dropout(x)
        x = F.relu(self.layer13(x))
        x = self.dropout(x)
        x = F.relu(self.layer14(x))
        x = self.dropout(x)
        x = F.relu(self.layer15(x))
        y = F.relu(self.layer16(y))
        y = self.dropout(x)
        y = F.relu(self.layer17(y))
        y = self.dropout(x)
        y = F.relu(self.layer18(y))
        y = self.dropout(x)
        y = F.relu(self.layer19(y))
        
        return x,y
</code></pre>

Nikronic: <p>Hi,</p>
<aside class="quote no-group" data-username="MUHAMMAD_JAHANZAIB_K" data-post="1" data-topic="80817">
<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/muhammad_jahanzaib_k/48/19211_2.png" class="avatar"> MUHAMMAD_JAHANZAIB_K:</div>
<blockquote>
<p><code>x = x.view(x.size(0), -1)</code></p>
</blockquote>
</aside>
<p><code>Conv2d</code> accepts inputs in this manner <code>[batch_size, channel_size, height, width]</code>. So, for an rabitrary input <code>x = torch.randn(1, 3, 136, 136)</code> you are resizing it into 2d tensor which is not proper for a <code>Conv2d</code> operator which takes 3d input. In the error also you can see that it says “got 2d tensor with size of (136, 408)” where 408=136*3. if you remove the aforementioned line, your model will work properly. Although, <code>layer11</code> has never been used which leads to another mismatch error in layer 17.</p>
<p>bests</p>

MUHAMMAD_JAHANZAIB_K: <p>Hi Nikronic I can’t understand can you explain more clearly . You describe well but I am not able to understand it it’s my bad.</p>

Nikronic: <p>No problem.,<br>
The issue is you have defined <code>Conv2d</code>. It expects for <code>[channel, height, width]</code> input at least. But when you passing your input to <code>forward</code> method, you are reshaping it into a <code>[sth, sth]</code> which is not proper.</p>
<p>for instance, just run this code:</p>
<pre><code class="lang-auto">x = torch.randn(1, 3, 136, 136)
x = x.view(x.size(0), -1)
print(x.shape)  # (xxx, xxx)
</code></pre>
<p>So, that line is changing the shape of tensor to something that <code>Conv2d</code> layers cannot accept.</p>
<p>Now run this code:</p>
<pre><code class="lang-auto">x = torch.randn(1, 3, 136, 136)
# x = x.view(x.size(0), -1)
x = nn.Conv2d(3,16,3,stride=1,padding=1)
print(x.shape)  # (1, 16, 136, 136)
</code></pre>
<p>which works just fine. But if you uncomment the second line (currently commented), you get your mentioned error.<br>
The whole idea is if you are not going to use Fully Connected layer or you are just using <code>Conv2d</code>, your input has to be in the form I previously explained.</p>

MUHAMMAD_JAHANZAIB_K: <p>My image size is (136,136,3)<br>
After applying x.view(x.size(0),-1) torch.size(136,408)<br>
How to convert my image to (1,3,136,136)<br>
If I use torch.rand(1,3,136,136) I got an other error  size mismatch , m1: [1×2] , m2: [580×80]<br>
Error at line y=F.relu(self.layer17(y))</p>

MUHAMMAD_JAHANZAIB_K: <p>How to remove error of line 17  how layer 11 is not use</p>

Nikronic: <p>About image size, you can load images channel-first. PyTorch uses channel-first by default or you can convert your code to accept channel-last tensors.</p>
<p>The reason I considered <code>(1, 3, 136, 136)</code> is that probably you are using batch operations. So you can aggregate let’s say 1000 images in one tensor <code>(1000, 3, 136, 136)</code>.</p>
<p>about your error, I’ve mentioned it in first post. You network is not properly constructed.</p>
<aside class="quote no-group" data-username="Nikronic" data-post="2" data-topic="80817">
<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/nikronic/48/71873_2.png" class="avatar"> Nikronic:</div>
<blockquote>
<p>Although, <code>layer11</code> has never been used which leads to another mismatch error in layer 17.</p>
</blockquote>
</aside>
<p>And you have not explained why you are using this line.</p>
<aside class="quote no-group" data-username="MUHAMMAD_JAHANZAIB_K" data-post="5" data-topic="80817">
<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/muhammad_jahanzaib_k/48/19211_2.png" class="avatar"> MUHAMMAD_JAHANZAIB_K:</div>
<blockquote>
<p>x.view(x.size(0),-1)</p>
</blockquote>
</aside>

MUHAMMAD_JAHANZAIB_K: <p>x.view(x.size(0),-1)<br>
This line is not a part of code I just tray to over come error find this line to resolve the error<br>
My basic code start from layer1<br>
I again calculate the network and tray again if found any error tell you about that<br>
Thank you for your help it clears my concepts</p>