I want to use VGG16 network for transfer learning. Following the transfer learning tutorial, which is based on the Resnet network, I want to replace the lines:
where, as far as I understand, the two lines in the middle are required in order to replace the classification process (from 10 classes, to 2). The problem is that the VGG16 class does not contain a â.fcâ attribute, so running these lines results in an error.
What is the best way by which I can replace the corresponding lines in the Resnet transfer learning?
Since I am new in Pytorch (and Machine learning in general), any further (relevant) details regarding the structure of the VGG16 class (even details that are not necessarily required for the specific implementation I requested) will be gratefully appreciated.
For VGG16 you would have to use model_ft.classifier. You can find the corresponding code here.
Here is a small example how to reset the last layer. Of course you could also replace the whole classifier, if thatâs what you wish.
model = models.vgg16(pretrained=False)
model.classifier[-1] = nn.Linear(in_features=4096, out_features=num_classes)
I have a similar question, but for the fcn resnet 101 segmentation model. In my case I am following this tutorial and I am trying to adapt this part of the code to fcn resnet 101.
Since this is a segmentation model, the output layer would be a conv layer instead of a linear one.
If you want to train your model from scratch, you could just use the num_classes argument:
modelA = models.segmentation.fcn_resnet101(
pretrained=False, num_classes=2)
On the other hand, if you just want to use the pretrained model and create a new classification layer, you could use: