Remove last two layers (FC and average pooling) of ResNet152 and add Conv2d at the end

I want to remove last two layers of ResNet152 and add Conv2D at the end to handle variable size inputs. Can anyone help me?

Thanks!

res = model.resnet18(pretrained=True)
res = list(res.children())[:-2]
_res = nn.Sequential(*res)

does it work?

This works fine but how to add Conv2d at the end and then fine tune Conv2D only?

_res .add_module("con-end", nn.conv2d(x, x, x))
you can add the conv2d by above line
And if you want to finetune this layer only, you should set the params on resNet into autogrand == False

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

This code works fine. It removes last two layers of ResNet152 and add conv2d layer at bottom. But I want to train the conv2d layer. 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.