Fine Tuning dimensionality error
I am trying to use Resnet50 for image classification problem. However it shows error and I could not fix it.

RuntimeError: inconsistent tensor size, expected tensor [120 x 2048] and src [1000 x 2048] to have the same number of elements, but got 245760 and 2048000 elements respectively at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensorCopy.c:86  


and error happens below.

self.resnet = models.resnet50(num_classes=num_breeds, pretrained='imagenet')


Model is below

class Resnet(nn.Module):
    def __init__(self):
        super(Resnet,self).__init__()
        self.resnet = models.resnet50(num_classes=num_breeds, pretrained='imagenet')
        #self.resnet = nn.Sequential(*list(resnet.children())[:-2])
        #self.fc = nn.Linear(2048,num_breeds)

    def forward(self,x):
        x = self.resnet(x)
        return x


When you create your models.resnet50 with num_classes=num_breeds, the last layer is a fully connected layer from 2048 to num_classes (which is 120 in your case).

Having pretrained='imagenet' asks pytorch to load all the corresponding weights into your network, but it has for its last layer 1000 classes, not 120. This is the source of the error since the 2048x120 tensor doesn't match the loaded weights 2048x1000.

You should either create your network with 1000 classes and load the weights, then "trim" to the classes you want to keep. Or you could create the network you wished for with 120 classes, but manually load the weights. In the last case, you only need to pay specific attention to the last layer.
