Transfer learning with ResNet: very low accuracy

username: lorenzo_fabbri
cooked: <p>I’m trying to use ResNet (18 and 34) for transfer learning. Although my loss (cross-entropy) is decreasing (slowly), the accuracy remains extremely low. My model is the following:</p>
<pre><code class="lang-auto">class ResNet(nn.Module):
    
    def __init__(self):
        super().__init__()
        
        # Download pre-trained ResNet model and remove FC layer
        resnet = torchvision.models.resnet18(pretrained=True)
        modules = list(resnet.children())[:-1]
        resnet = nn.Sequential(*modules)
        for param in resnet.parameters():
            param.requires_grad = False
        self.features = resnet
        
        # Add FC layer(s) for classification
        self.fc1 = nn.Linear(512, 1024)
        torch.nn.init.xavier_uniform_(self.fc1.weight)
        
        self.fc2 = nn.Linear(1024, 2048)
        torch.nn.init.xavier_uniform_(self.fc2.weight)
        
        self.fc3 = nn.Linear(2048, 2048)
        torch.nn.init.xavier_uniform_(self.fc3.weight)
        
        self.fc4 = nn.Linear(2048, NUM_CLASSES)
        torch.nn.init.xavier_uniform_(self.fc4.weight)
        
    def forward(self, x):
        out = F.relu(self.features(x))

        out = F.relu(self.fc1(out.view(-1, 512)))
        out = F.relu(self.fc2(out))
        out = F.relu(self.fc3(out))
        out = self.fc4(out)
        
        return out

net = ResNet().to(device)
</code></pre>
<p>I’m not using dropout at the moment since I’m trying to overfit the training data. It’s the first time I try to use a pre-trained model: can you spot anything wrong in my code?</p>

username: tree33
cooked: <p>What about the performance if you do not use a pretrained model?<br>
If that’s all right, then the problem is probably about imput normalization</p>
<p>Please check the following post for more details.</p><aside class="quote quote-modified" data-post="2" data-topic="1683">
  <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/smth/48/13_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/whats-the-range-of-the-input-value-desired-to-use-pretrained-resnet152-and-vgg19/1683/2">What's the range of the input value desired to use pretrained resnet152 and vgg19?</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    input image is first loaded to range [0, 1] and then this normalization is applied to RGB image as described here: 



All pre-trained models expect input images normalized in the same way, i.e. mini-batches of 3-channel RGB images of shape (3 x H x W), where H and W are expected to be atleast 224. 
The images have to be loaded in to a range of [0, 1] and then normalized using mean=[0.485, 0.456, 0.406] and std=[0.229, 0.224, 0.225] 
An example of such normalization can be found in the imagenet …
  </blockquote>
</aside>


username: harshitlakhani
cooked: <p>i’m also gettin the same issue using Densnet, ResNet152.</p>