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

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, RuntimeError: 1only batches of spatial targets supported (non-empty 3D tensors) but got targets of size: : [16]
Can you tell me why I got this error? I tried to search on internet but could not find any appropriate answer.

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

Appreciate a lot if someone can do me a favor! Thanks in advance!!

can you pls show what

original_model.children()

prints

I have solved the issue. Thanks for reply.
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.

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)
1 Like

it seems that you have changed self.conv layer
i think that layer was causing some mismatch of shapes?

1 Like