Convert ResNet152 to Fully convolutional Network (FCN) for classification of variable size images

username: rashidch
cooked: <p>I am trying to convert ResNet152 to fully convolutional network (FCN). To do this, I removed last two layers of ResNet152 and add conv2d layer at bottom. But When I tried to train conv2d, I got the following error,  <strong>RuntimeError: 1only batches of spatial targets supported (non-empty 3D tensors) but got targets of size: : [16]</strong><br>
Can you tell me why I got this error? I tried to search on internet but could not find any appropriate answer.</p>
<pre><code class="lang-auto">class ResNet152Bottom(nn.Module):
    def __init__(self, original_model):
        super(ResNet50Bottom, self).__init__()
        self.features = nn.Sequential(*list(original_model.children())[:-2])
        self.conv = nn.Conv2d(2048, 10, kernel_size=(7, 7), stride=(1, 1), bias=False)
        
    def forward(self, x):
        x = self.features(x)
        x = self.conv(x)
        return x

res50_model = torchvision.models.resnet152(pretrained=True)
for param in res50_model.parameters():
    param.requires_grad = False
res50_conv2 = ResNet50Bottom(res50_model)
inputs, classes = next(iter(dataloaders['train']))
outputs = res50_conv2(inputs)
outputs.data.shape  
out = torch.squeeze(outputs, dim=2)
out2 = torch.squeeze(out, dim=2)
out2.data.shape
</code></pre>
<p>Appreciate a lot if someone can do me a favor! Thanks in advance!!</p>

username: n0obcoder
cooked: <aside class="quote no-group" data-username="rashidch" data-post="1" data-topic="51583">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/2acd7d/48.png" class="avatar"> rashidch:</div>
<blockquote>
<p>original_model.children()</p>
</blockquote>
</aside>
<p>can you pls show what</p>
<pre><code class="lang-auto">original_model.children()
</code></pre>
<p>prints</p>

username: rashidch
cooked: <p>I have solved the issue. Thanks for reply.<br>
This is a complete code for Resnet FCN for classification except train_model function. It works fine on imagewoof dataset and give 90% accuracy. My training is still working.</p>
<pre><code class="lang-auto">class ResNet50Bottom(nn.Module):
    def __init__(self, original_model):
        super(ResNet50Bottom, self).__init__()
        self.features = nn.Sequential(*list(original_model.children())[:-2])
        self.conv = nn.Conv2d(512, 10, kernel_size=(7, 7), stride=(1, 1), bias=False)
        
    def forward(self, x):
        x = self.features(x)
        x = self.conv(x)
        return x

res50_model = torchvision.models.resnet34(pretrained=True)
for param in res50_model.parameters():
    param.requires_grad = False
res50_conv2 = ResNet50Bottom(res50_model)
inputs, classes = next(iter(dataloaders['train']))
outputs = res50_conv2(inputs)
outputs.data.shape 
res50_conv2 =res50_conv2.to(device)
criterion = nn.CrossEntropyLoss()
optimizer_conv = optim.SGD(res50_conv2.conv.parameters(), lr=0.001, momentum=0.9)
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=20, gamma=0.1)

res50_conv2= train_model(res50_conv2, criterion, optimizer_conv, exp_lr_scheduler, num_epochs=30, flag_Load=False)
</code></pre>

username: n0obcoder
cooked: <p>it seems that you have changed self.conv layer<br>
i think that layer was causing some mismatch of shapes?</p>