Changing the number of output classes of fc layer of vgg16

I wanted to implement transfer learning on a data consisting of images which are divided into 52 classes. I wanted to do that using vgg16 and the concept of transfer learning. I have frozen the weights of feature block and from the ouptut of feature block, I am training my own classifier which takes input of 25088( 512 X 7 X 7 number of neurons ). Now, vgg16’s classifier layer outputs 1000 dimension vector indicating which class the input image belonged to. I want to change this 1000 to 52. For that I have written something like:

for param in model_vgg.parameters():
(Indented by one tab space) param.requires_grad = False #freeze all the layers

model_vgg.classifier[6].out_features = 52 #set number of output classes to 52

for param in model_vgg.classifier[6].parameters():
(Indented by one tab space)param.requires_grad = True #unfreeze only the last layer of classifier block

But this still doesn’t work, as the output of my classifier block while training and validating is still a 1000 dimension vector for each input image. I tried print( model_vgg.classifier[6].out_features ) which prints 52, still the problem persists.
What’s the problem with my code? Any suggestions will be helpful.

1 Like

You can add a linear layer with output dimension to be 52 at the end of this network and with that you will be able to use the pre-trained network weights in the better manner.

Hi, thanks for your quick reply. I have just started using pytorch and thus new to it. Can you show me how do one add a new linear layer to an already established model?

Edit: I have just figured out how to add linear layer. I wrote something like this:

for param in model_vgg.parameters():
(Indented by one tab space) param.requires_grad = False #freeze all the layers

model_vgg.classifier[6].out_features = 52 #set number of output classes to 52

for param in model_vgg.classifier[6].parameters():
(Indented by one tab space)param.requires_grad = True #unfreeze only the last layer of classifier block

model_vgg.classifier=nn.Linear(25088,52) #I knew that number of inputs to my classifier block was 25088

However, now I want to change only the last layer of my classifier that is model_vgg.classifier[6] and write something like:
model_vgg.classifer[6]=nn.Linear(num_inputs,52)
But here I don’t know the number of inputs to my model_vgg.classifer[6]. How to find that?

class ClassifierModule(nn.Module):
    def __init__(self):
        super(ClassifierModule,self).__init__()
        self.layer1 = nn.Linear(1000,52)
        self.net = model_vgg.classifier
        for p in self.net.parameters():
            p.requires_grad=False

    def forward(self,x):
        x1 = self.net(x)
        print 'Passed Thru VGG'
        y = self.layer1(x1)
        return y

model = ClassifierModule()

The above model will have 52 outputs. I think you can use similiar thing for your network.

2 Likes

print the model and you should see the last linear classifier taking in 4096 features
so, model_vgg.classifer[6]=nn.Linear(4096,52)