I am trying to add in a pretrained resnet50 model and then tack on 2 extra Linear (FC) layers to that. I defined a Class with a few inputs like the below but it gives me an error.
RuntimeError: size mismatch, m1: [32 x 1000], m2: [2048 x 2048] at /opt/conda/conda-bld/pytorch_1579027003190/work/aten/src/THC/generic/THCTensorMathBlas.cu:290
I have an idea of what this error is saying that the resnet is producing the classification (1000) output and not the 2048 from an early FC layer. I just dont know how to get the FC layer out from the resnet model. So here is what I am doing.
class FinetuneResnet(nn.Module):
def __init__(self, num_classes):
super(FinetuneResnet, self).__init__()
self.model = models.resnet50(pretrained=True)
self.fc1 = nn.Linear(2048, 2048)
self.fc2 = nn.Linear(2048, num_classes)
self.dropout = nn.Dropout(0.3)
def forward(self, x):
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
x = self.model.layer1(x)
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
x = self.model.avgpool(x)
x = x.view(x.size(0), -1)
x = nn.functional.relu(self.fc1(x))
x = self.dropout(x)
x = nn.functional.softmax(self.fc2(x), dim=1)
return x
This is what I would want, But I really dont get the forward method for it. Could you explain it a little more detail. From what I gathered you are pushing x through the resnet50 model. But what is the layer1-4?
Thank you
Thank you @ptrblck and @vgsprasad
I got it to work and I did remove the softmax (thank you ptrbick for the explanation for that)
This is how my forward feed is now
def forward(self, x):
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
x = self.model.layer1(x)
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
x = self.model.avgpool(x)
x = x.view(x.size(0), -1)
x = nn.functional.relu(self.fc1(x))
x = self.dropout(x)
#x = nn.functional.softmax(self.fc2(x), dim=1)
x = self.fc2(x)
return x
And it even works better than the one I did in keras.
I really appreciate your advice.