Why the output size of a pretrained model is always 1000?

I have trained Resnet50 provided by torchvision.models on my own dataset. And because my dataset has 100 classes, I modified the fc layer, and everything goes fine. After finishing the training, I use torch.save(best_model, path) to save the best model.

Now I want to load the best model to predict on the test set, So I load the model structure as model = torch.load(path), and print the model as follows:

We can see that the out_features of fc layer is 100, but the size of output became 1000, is it weird? How to solve this problem?

Edit: Looking closer, I see that your model is correct. Are you sure that the model you are printing is the model m2 used for the input?

Hi there. Hmm, can you show us how you changed the model? Or print the model after you’ve changed it?

When I do resnet.fc = nn.Linear(self.basenet.fc.in_features, n_out_features) it works fine after saving/loading

My code ->

n_features = 100
resnet = models.resnet18()
resnet.fc = nn.Linear(resnet.fc.in_features, n_features)
torch.save(resnet, 'model.pth')
model = torch.load('model.pth')

inp = torch.randn(1, 3, 224, 224)
out = model(inp)
print(out.size()) -> torch.Size([1, 100])

Thanks for your reply. My code is as follows:


m2 = torchvision.models.resnet50(pretrained=True)
m2.fc.out_features = 100

# after training
...
torch.save(m2, 'best.pt')

Then I use ipython to load the model:

[1] import torch

[2] m2 = torch.load('best.pt')

[3] m2
the model architecture is shown as above figure.

[4] m2(torch.rand(1,3,224,224).cuda(0)).shape

but the result of [4] is torch.Size([1, 1000])

m2 = torchvision.models.resnet50(pretrained=True)
in_ftr  = m2.fc.in_features
out_ftr = 100
m2.fc = nn.Linear(in_ftr,out_ftr,bias=True)

Hope this helps!