I try to load the pretrained ResNet-18 network, create a new sequential model with the layers
of the pretrained network without the top fully connected layer and then add another fully connected layer so it would match my data (of two classes only). I tried the go by the tutorials but I keep getting the next error:
RuntimeError: size mismatch, m1: [16384 x 1], m2: [16384 x 2]
I think the easier way would be to set the last fc layer in your pretrained resnet to an nn.Identity layer and pass the output to the new label_model layer.
Would this code work for you?
class CNN(nn.Module):
def __init__(self, num_classes):
super(CNN, self).__init__()
self.model_resnet = models.resnet18(pretrained=True)
fc_inputs = self.model_resnet.fc.in_features
self.model_resnet.fc = nn.Identity()
self.label_model = nn.Linear(fc_inputs, num_classes)
def forward(self, images):
with torch.no_grad():
features = self.model_resnet(images)
labels = self.label_model(features)
return labels
model = CNN(2)
x = torch.randn(2, 3, 224, 224)
output = model(x)
I’m not sure where the fc_inputs * 32 came from.
Also, I’ve formatted your code so that I could copy it foe debugging.
If you would like to post some code, you can wrap it in three backticks ```
But I get the next error:
“RuntimeError: Expected 4-dimensional input for 4-dimensional weight 256 512, but got 2-dimensional input of size [32, 512] instead”
features will have the shape [batch_size, 512], which will throw the error if you pass it to a conv layer.
How would you like to reshape/treat this tensor?