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!!
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.