Trying to get the loss of a CNN, but getting three (contradicting?) Runtime Errors

username: BelindaMK
cooked: <p>Hello!</p>
<p>I’ve managed to get a small CNN model running, and am now trying to extract its outputs (the loss, mainly). My code for running the data through the CNN looks as follows:</p>
<pre><code class="lang-auto"># Get the model information.
model, opt = get_model()
# Define the loss function.
loss_func = F.cross_entropy

# Run the data through the neural network:
for epoch in range(epochs):
    for train_x, train_y in train_dl:
        # Create a prediction.
        pred = model(train_x.permute(0, 3, 1, 2))
        # Reshaping the prediction so cross entropy can be applied.
        pred = pred.permute(2, 3, 0, 1)[0][0]
        # Calculate the loss.
        loss = loss_func(pred, train_y)

        # Backwards propagation.
        loss.backward()
        opt.step()
        opt.zero_grad()
</code></pre>
<p>As you can already see I had to do a lot of permuting to make the data fit. It worked in this case, but for the loss:</p>
<pre><code class="lang-auto">print(loss_func(model(train_x), train_y))
</code></pre>
<p>I get the error: <em>RuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[10, 256, 256, 3] to have 3 channels, but got 256 channels instead</em>. When I change it to the following:</p>
<pre><code class="lang-auto">print(loss_func(model(train_x.permute(0, 3, 1, 2)), train_y))
</code></pre>
<p>I get: <em>RuntimeError: only batches of spatial targets supported (3D tensors) but got targets of dimension: 1</em>. And then when I try:</p>
<pre><code class="lang-auto">print(loss_func(model(train_x.permute(0, 3, 1, 2)[0][0]), train_y))
</code></pre>
<p>It says: <em>RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 3, 3, 3], but got 2-dimensional input of size [256, 256] instead</em>. Which indicates that it wants me to remove the [0]s, but when I do, it complains about the above issue, which would need them added again.</p>
<p>Can anyone help me figure out what it wants me to do here?<br>
(I hope I’ve provided all relevant code, but if anything else is needed, let me know and I’ll add it!)</p>

username: rad
cooked: <p>Could you also post the code of the model? Also, what is the task that you are using this CNN for? If it’s image classification, the output of the CNN should not have 4 dimensions.</p>

username: BelindaMK
cooked: <p>The model code is as follows:</p>
<pre><code class="lang-auto">def get_model():
    model = nn.Sequential(
            nn.Conv2d(3, 32, kernel_size=3, stride=2, padding=1),
            nn.ReLU(),
            nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1),
            nn.ReLU(),
            nn.Conv2d(64, 21, kernel_size=3, stride=2, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(1)
    )
    opt = optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
    return model, opt
</code></pre>
<p>It’s just a placeholder model for now, so it probably won’t yield very good results. The task is indeed image classification, for trying to identify 21 different types of land use.</p>

username: rad
cooked: <p>Okay, that sounds good! You are, however, missing a part of your model.</p>
<p>CNNs usually consist of the feature extraction part (convolutions, poolings, etc.), and the classification part (few fully connected layers to learn how extracted features contribute to the classification, in your case, and in the end a softmax layer, which gives you the probability distribution of your classes).</p>
<p>In Pytorch, it is recommended that you extend the nn.Module class when writing a model.</p>
<pre><code class="lang-auto">class ConvNet(nn.Module):

def __init__(self, n_classes):
   super.__init__()
   self.featurizer = nn.Sequential(...) # what you  have in your model variable 
   self.classifier = nn.Sequential(...) # 2-3 linear layers, first should in_features = have n_kernels * h_kernel * w_kernel from last conv layer, last should have out_feautres=n_classes )

def forward(self, x)
   # x (b, c, h, w)

   x = self.featurizer(x) # x (b, n_kernels, h_kernel, w_kernel)

   x = torch.flatten(x, start_dim=1) # x (b, n_kernels * h_kernel, w_kernel)

   x = self.classifier(x) # x (b, n_classes)
   x = torch.nn.f.softmax(x) 
</code></pre>
<p>On this, you should be able to apply cross-entropy loss. The optimizer, you could also instantiate separately. Let me know if you get stuck <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

username: BelindaMK
cooked: <p>Thank you very much for your quick reply! I’ve tried adding this code, filling in the blanks as follows:</p>
<pre><code class="lang-auto">class ConvNet(nn.Module):

  def __init__(self, n_classes):
    super.__init__()
    self.featurizer = nn.Sequential(
              nn.Conv2d(3, 32, kernel_size=3, stride=2, padding=1),
              nn.ReLU(),
              nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1),
              nn.ReLU(),
              nn.Conv2d(64, 21, kernel_size=3, stride=2, padding=1),
              nn.ReLU(),
              nn.MaxPool2d(1)) # what you  have in your model variable 
    self.classifier = nn.Sequential(
              nn.Linear(21*3*3, 120),
              nn.Sigmoid(),
              nn.Linear(120, 84),
              nn.Sigmoid(),
              nn.Linear(84, 21)
    ) # 2-3 linear layers, first should have in_features = n_kernels * h_kernel * w_kernel from last conv layer, last should have out_feautres=n_classes )

  def forward(self, x):
    # x (b, c, h, w)
    x = self.featurizer(x) # x (b, n_kernels, h_kernel, w_kernel)
    x = torch.flatten(x, start_dim=1) # x (b, n_kernels * h_kernel, w_kernel)
    x = self.classifier(x) # x (b, n_classes)
    x = torch.nn.f.softmax(x) 
</code></pre>
<p>I’m afraid to speak too soon but the permute seems to no longer be necessary now! I do, however, get the following error still:<br>
<em>RuntimeError: mat1 and mat2 shapes cannot be multiplied (10x21504 and 189x120)</em></p>
<p>The 189 x 120 are clearly the parameters I set in the first linear layer, and I assume this error refers to a mismatch between these parameters and the data it actually receives. Which does however make me quite confused as to where the 10 x 21504 comes from.<br>
Is something in my convolutional layers shaping the data in these dimensions?</p>
<p>Edit: Or the “flatten” layer, I just realized. But there doesn’t really seem to be a way to control its output dimensions other than start_dim and end_dim.</p>

username: rad
cooked: <p>Is your batch size 10?</p>
<p>21504 is 21 (the number of output kernels/channels from your last conv dimension) * 32 (h_kernel) * 32 (w_kernel). I think that should be the size of your linear layers. You should calculate the width and height of kernel using formulae you can find online. Also, that 21 from the last conv does not need to be the number of classes you have, you can put 128 for example.</p>
<p>Let me know how it works <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: BelindaMK
cooked: <p>Yes, my batch size was indeed 10!</p>
<p>I unfortunately couldn’t find any online formulas (to be quite honest, I’m not sure what to look for; when I search “cnn kernel size calculator” or similar, I instead find threads that calculate output image size or more in-depth discussions that go beyond my understanding).</p>
<p>I have also tried making an Excel sheet to figure out working combinations of numbers by myself, but the numbers don’t match with the actual results from the model, so I think I’m still missing something.</p>
<p>Could you offer me a hint as to which formula I need?</p>

username: rad
cooked: <p>Okay. Let’s see, you have an image of size <em>(h, w, c)</em>, which in your case should be a square RGB image, so h=w – in the examples below I will assume H_in = 32 --, c=3. Your input volume is of shape <em>(n, n, 3)</em>. First, you apply 32 filters of size (3, 3), with stride 2 and padding 1.</p>
<p>The dimension of the new output is given by the formula, from <a href="https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html" rel="noopener nofollow ugc">Pytorch Conv2d docs</a>: <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/b/3bce25bc145a256712f1fdbd7479a0d6d78fcd92.png" data-download-href="https://discuss.pytorch.org/uploads/default/3bce25bc145a256712f1fdbd7479a0d6d78fcd92" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3bce25bc145a256712f1fdbd7479a0d6d78fcd92_2_690x203.png" alt="image" data-base62-sha1="8x3Q38EBjx5wlvvrcSi1cdLqBsS" width="690" height="203" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3bce25bc145a256712f1fdbd7479a0d6d78fcd92_2_690x203.png, https://discuss.pytorch.org/uploads/default/original/3X/3/b/3bce25bc145a256712f1fdbd7479a0d6d78fcd92.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/b/3bce25bc145a256712f1fdbd7479a0d6d78fcd92.png 2x" data-dominant-color="F5F5F5"><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">image</span><span class="informations">717×211 21.8 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> - In your case, Hout = Wout and padding/kernel size are the same for H and W. Dilation is 1 by default.</p>
<p>So, n_out is floor((32+2<em>1-1</em>2-1)/2 + 1) =  16. Your output volume after first conv layer is <em>(16, 16, 32)</em> .</p>
<p>The second layer applies 64 filters of shape <em>(3, 3)</em>, with stride 2 and padding 1.</p>
<p>So, n_out is floor((16+2<em>1-1</em>2-1)/2 + 1)  = 8. Your output volume after second conv layer is <em>(8, 8, 64)</em>.</p>
<p>The last layer applies 21 (or actually 128, after my last edit) filters of shape (3, 3), with stride 2 and padding 1.</p>
<p>So, n_out is floor((8+2<em>1-1</em>2-1)/2 + 1)  = 4. Your output after last conv is <em>(4, 4, 128)</em></p>
<p>Now with MaxPool2d, if kernel size = 1, that won’t do anything at all. It takes the maximum in a (1, 1) square and puts it in the result. You should go for a kernel size of 2 perhaps, but don’t forget to set the stride to 1. From PyTorch [nn.MaxPool2d docs:] (<a href="https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html" class="inline-onebox" rel="noopener nofollow ugc">MaxPool2d — PyTorch 2.1 documentation</a>).</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/b/8b3d0e54d8e0c7c68c40fbf1756a7503a2847fd8.png" data-download-href="https://discuss.pytorch.org/uploads/default/8b3d0e54d8e0c7c68c40fbf1756a7503a2847fd8" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/b/8b3d0e54d8e0c7c68c40fbf1756a7503a2847fd8_2_690x182.png" alt="image" data-base62-sha1="jRLbaO4tDdlfMjMoRlvPZ0YgNW8" width="690" height="182" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/b/8b3d0e54d8e0c7c68c40fbf1756a7503a2847fd8_2_690x182.png, https://discuss.pytorch.org/uploads/default/original/3X/8/b/8b3d0e54d8e0c7c68c40fbf1756a7503a2847fd8.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/b/8b3d0e54d8e0c7c68c40fbf1756a7503a2847fd8.png 2x" data-dominant-color="F6F5F5"><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">image</span><span class="informations">760×201 21.9 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>Based on your Pooling kernel size, you have 4x4x128 or 3x3x128 total activations/values/neurons, which you flatten in a single 2048 or 1152-dimensional vector. That number needs to be the same as the first input to your linear layer.</p>
<p>I hope that this is the info you need.</p>

username: BelindaMK
cooked: <p>Thank you so very much for taking the time to explain this! I see indeed that my MaxPool didn’t do anything, so I upped it to 2 as you suggested. It took me a little while, but I was also able to use the dimension formulas to ensure the linear input size was correct. And lo and behold, it finally ran! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>
<p>The initial results were really poor, so I adjusted the model a litle bit, and this is what it looks like after some tweaking:</p>
<pre><code class="lang-auto">def __init__(self, n_classes):
    super.__init__()
    self.featurizer = nn.Sequential(
            # Convolutional part.
            nn.Conv2d(3, 25, kernel_size=4, stride=2, padding=1),
            nn.ReLU(),
            nn.Conv2d(25, 50, kernel_size=4, stride=2, padding=1),
            nn.ReLU(),
            nn.Conv2d(50, 100, kernel_size=4, stride=2, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(2),

    self.classifier = nn.Sequential(
            # Linear part.
            nn.Linear(100*16*16, 224),
            nn.Sigmoid(),
            nn.Linear(224, 112),
            nn.Sigmoid(),
            nn.Linear(112, 21)
    )
</code></pre>
<p>And of course, I made sure that the linear input was updated accordingly.</p>
<p>With this model, I was able to get a training loss of 2.681906. Still not ideal, probably, but I’m really happy with these results for my very first ever CNN. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> I can continue to tweak the parameters now however I need.</p>
<p>I genuinely wouldn’t have been able to get this off the ground without your help. Thanks a lot again!</p>

username: rad
cooked: <p>My pleasure! Make sure to also check out</p>
<pre><code class="lang-auto">torchvision.models
</code></pre>
<p>for some state-of-the-art CNN models, if you are looking for some solid results.</p>
<p>Cheers!</p>