

All you need to do is print out the model: > import torchvision.models as models Luckily, it is easy to figure out a model’s PyTorch structure if you don’t know it already. (That is, childen 0, 1, 2, 3, 4, 5, 6, and 7, because in Python indexing the last number you index is not included.) I say you need to know the “PyTorch structure” of the model because often, PyTorch groups together different layers into one “child” so knowing the number of layers in a model’s architecture (e.g., 18 in a ResNet-18) does not tell you the PyTorch structure that you need to know in order to select out the part of the model that you want. In this case, we already knew the PyTorch structure of the ResNet18 – we knew that the first 8 “children” composed the feature extractor part of the model that we wanted to keep.
#Inception v3 pytorch finetune code
If you already know the structure of the model, it’s literally one line of code to pick out the feature extractor: features = nn.Sequential(*(list(resnet18.children()))) Then, you can tack on your own fully connected layers that have the right number of outputs for whatever task you are solving. It is common to chop off the final fully connected layers (yellow) and keep only the convolutional feature extractor (orange).

Pre-training might not lead to better performance but it probably does accelerate convergence due to nice scaling of the pretrained parameters.)Ĭonceptually, CNN models often look like this: (Side note: pre-training on ImageNet is popular even though the exact advantages of pre-training are debated. What if you want to take advantage of pre-training, but you don’t want to make predictions on those exact ImageNet classes? In that case, you can chop off the parts of the pre-trained model that you don’t want to use, and keep only the parts you do want to use. Models pre-trained on ImageNet will output predictions for the 1,000 ImageNet classes. Notice that the only difference between loading a randomly-initialized model and a pretrained model is whether you set pretrained=True or not. Mobilenet = models.mobilenet_v2(pretrained=True) Shufflenet = models.shufflenet_v2_x1_0(pretrained=True) Googlenet = models.googlenet(pretrained=True) Inception = models.inception_v3(pretrained=True) Squeezenet = models.squeezenet1_0(pretrained=True)ĭensenet = nsenet161(pretrained=True) Resnet18 = models.resnet18(pretrained=True)Īlexnet = models.alexnet(pretrained=True) In torchvision.models, all pre-trained models are pre-trained on ImageNet, meaning that their parameters have been optimized to perform well on the ImageNet 1000-class natural image classification task. Any model with randomly-initialized parameters will need to be trained on a particular task in order to perform well. This use of the constructor produces a model that has the predefined architecture, but randomly-initialized parameters. You can construct a model with randomly-initialized parameters by calling its constructor: import torchvision.models as models It is easy to use existing models via torchvision.models. Image Sources: VGG-16 model architecture (CC license), ResNet – Deep Residual Learning for Image Recognition (arXiv), GoogLeNet – Going deeper with convolutions (arXiv)
