Transfer learning using resnet18

username: bsha
cooked: <p>Hi,</p>
<p>I try to load the pretrained ResNet-18 network, create a new sequential model with the layers<br>
of the pretrained network without the top fully connected layer and then add another fully connected layer so it would match my data (of two classes only). I tried the go by the tutorials but I keep getting the next error:<br>
RuntimeError: size mismatch, m1: [16384 x 1], m2: [16384 x 2]</p>
<p>This is my cod:</p>
<pre><code class="lang-python">class CNN(nn.Module):
    def __init__(self, num_classes):
        super(CNN, self).__init__()
              self.model_resnet = models.resnet18(pretrained=True)
              self.seq = nn.Sequential(*list(self.model_resnet.children()
              fc_inputs = self.model_resnet.fc.in_features
              self.label_model = nn.Linear(fc_inputs * 32, num_classes)

    def forward(self, images):
             with torch.no_grad():
                  features = self.seq(images)
             labels = self.label_model(features)
             return labels
</code></pre>
<p>Thank you!</p>

username: ptrblck
cooked: <p>I think the easier way would be to set the last <code>fc</code> layer in your pretrained resnet to an <code>nn.Identity</code> layer and pass the output to the new <code>label_model</code> layer.<br>
Would this code work for you?</p>
<pre><code class="lang-python">class CNN(nn.Module):
    def __init__(self, num_classes):
        super(CNN, self).__init__()
        self.model_resnet = models.resnet18(pretrained=True)
        fc_inputs = self.model_resnet.fc.in_features
        self.model_resnet.fc = nn.Identity()
        self.label_model = nn.Linear(fc_inputs, num_classes)

    def forward(self, images):
        with torch.no_grad():
            features = self.model_resnet(images)
        labels = self.label_model(features)
        return labels
    

model = CNN(2)
x = torch.randn(2, 3, 224, 224)
output = model(x)
</code></pre>
<p>I’m not sure where the <code>fc_inputs * 32</code> came from.<br>
Also, I’ve formatted your code so that I could copy it foe debugging.<br>
If you would like to post some code, you can wrap it in three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: bsha
cooked: <p>Thank you very much for your help!<br>
Now I try to add localization. After looking for some information on the internet, this is the code:</p>
<pre><code class="lang-auto">class CNN(nn.Module):
    def __init__(self, num_classes):
        super(CNN, self).__init__()
        self.model_resnet = models.resnet18(pretrained=True)
        fc_inputs = self.model_resnet.fc.in_features
        self.model_resnet.fc = nn.Identity()

        self.labels_model = nn.Sequential(
            nn.Linear(fc_inputs, 256),
            nn.ReLU(),
            nn.Dropout(0.4),
            nn.Linear(256, num_classes), 
            nn.LogSoftmax(dim=1) # For using NLLLoss()
        )
  
        self.conv1 = nn.Conv2d(fc_inputs, 256, kernel_size=3, padding=1)
        self.act1 = nn.ReLU()
        self.conv2 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
        self.act2 = nn.ReLU()
        self.conv3 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
        self.act3 = nn.ReLU()
        self.conv4 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
        self.act4 = nn.ReLU()
        self.output = nn.Conv2d(256, 4, kernel_size=3, padding=1)


    def forward(self, images):
        with torch.no_grad():
            features = self.model_resnet(images)

        labels = self.labels_model(features)      
      
        box = self.conv1(features)
        box = self.act1(box)
        box = self.conv2(box)
        box = self.act2(box)
        box = self.conv3(box)
        box = self.act3(box)
        box = self.conv4(box)
        box = self.act4(box)
        box = self.output(box)
        box = out.permute(0, 2, 3, 1)
        box = out.contiguous().view(out.shape[0], -1, 4)

        return labels, box
</code></pre>
<p>But I get the next error:<br>
“RuntimeError: Expected 4-dimensional input for 4-dimensional weight 256 512, but got 2-dimensional input of size [32, 512] instead”</p>
<p>Thank you again!</p>

username: ptrblck
cooked: <p><code>features</code> will have the shape <code>[batch_size, 512]</code>, which will throw the error if you pass it to a conv layer.<br>
How would you like to reshape/treat this tensor?</p>

username: bsha
cooked: <p>I would like to get at the end a tensor of size [batch_size, 4].</p>

username: god_sp33d
cooked: <p>No, I think <a class="mention" href="/u/ptrblck">@ptrblck</a>’s question was how would you like the input to your conv1 be ? So, that features can be reshaped and passed in proper format.</p>