Changing the number of output classes of fc layer of vgg16

Ayush_rai: <p>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:</p>
<p>for param in model_vgg.parameters():<br>
(Indented by one tab space) param.requires_grad = False   <span class="hashtag">#freeze</span> all the layers</p>
<p>model_vgg.classifier[6].out_features = 52  <span class="hashtag">#set</span> number of output classes to 52</p>
<p>for param in model_vgg.classifier[6].parameters():<br>
(Indented by one tab space)param.requires_grad = True      <span class="hashtag">#unfreeze</span> only the last layer of classifier block</p>
<p>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 <strong>print( model_vgg.classifier[6].out_features )</strong> which prints 52, still the problem persists.<br>
What’s the problem with my code? Any suggestions will be helpful.</p>

Happy_NewYear: <p>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.</p>

Ayush_rai: <p>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?</p>
<p>Edit: I have just figured out how to add linear layer. I wrote something like this:</p>
<p>for param in model_vgg.parameters():<br>
(Indented by one tab space) param.requires_grad = False <span class="hashtag">#freeze</span> all the layers</p>
<p>model_vgg.classifier[6].out_features = 52 <span class="hashtag">#set</span> number of output classes to 52</p>
<p>for param in model_vgg.classifier[6].parameters():<br>
(Indented by one tab space)param.requires_grad = True <span class="hashtag">#unfreeze</span> only the last layer of classifier block</p>
<p>model_vgg.classifier=nn.Linear(25088,52)  <span class="hashtag">#I</span> knew that number of inputs to my classifier block was 25088</p>
<p>However, now I want to change only the last layer of my classifier that is model_vgg.classifier[6] and write something like:<br>
model_vgg.classifer[6]=nn.Linear(num_inputs,52)<br>
But here I don’t know the number of inputs to my model_vgg.classifer[6]. How to find that?</p>

Happy_NewYear: <pre><code class="lang-auto">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()
</code></pre>
<p>The above model will have 52 outputs. I think you can use similiar thing for your network.</p>

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