Fine Tuning Pretrained Model MobileNet_V2 in Pytorch
Asked Answered
K

4

9

I am new to pyTorch and I am trying to Create a Classifier where I have around 10 kinds of Images Folder Dataset, for this task I am using Pretrained model( MobileNet_v2 ) but the problem is I am not able to change the FC layer of it. There is not model.fc attribute. Can anyone help me to do this. Thanks

Kila answered 31/7, 2019 at 7:16 Comment(0)
C
9

Do something like below:

import torch
model = torch.hub.load('pytorch/vision', 'mobilenet_v2', pretrained=True)
print(model.classifier)

model.classifier[1] = torch.nn.Linear(in_features=model.classifier[1].in_features, out_features=10)
print(model.classifier)

output:

Sequential(
  (0): Dropout(p=0.2)
  (1): Linear(in_features=1280, out_features=1000, bias=True)
)
Sequential(
  (0): Dropout(p=0.2)
  (1): Linear(in_features=1280, out_features=10, bias=True)
)

Note: you would need torch >= 1.1.0 to use torch.hub.

Chokeberry answered 31/7, 2019 at 12:42 Comment(0)
J
8

From the MobileNet V2 source code it looks like this model has a sequential model called classifier in the end. Therefore, you should be able to change the final layer of the classifier like this:

import torch.nn as nn
import torchvision.models as models
model = models.mobilenet_v2()
model.classifier[1] = nn.Linear(model.last_channel, 10)

Unfortunately, I cannot test this code right now.
This is also a good reference, on how to finetune models.

Jar answered 31/7, 2019 at 8:25 Comment(3)
Actually, module torchvision.models has no attribute mobilnet_v2. Present models are [resnet, alexnet, vgg, squeezenet, densenet, inception]Chokeberry
@AnubhavSingh, actually it does since a recent torchvision update. pytorch.org/docs/stable/torchvision/models.htmlAlda
Oh I see. My bad. Thank you so much .Chokeberry
M
3

MobilenetV2 implementation asks for num_classes (default=1000) as input and provides self.classifier as an attribute which is a torch.nn.Linear layer with output dimension of num_classes. You can use this attribute for your fine-tuning. You can have a look at the code yourself for better understanding.

import torchvision.models as models
model = models.mobilnet_v2(num_classes=10)
Marlie answered 31/7, 2019 at 8:26 Comment(1)
It's good to know there are these extra parameters! But it should be pointed out that using the num_classes parameter prevents from downloading the pre-trained model which is pretty much what this question is about. Also, here is an updated link to the code.Rebbecarebbecca
F
0

By looking into the last layer in models.mobilenet_v2, you can see the following:

(classifier): Sequential(
(0): Dropout(p=0.2, inplace=False)
(1): Linear(in_features=1280, out_features=1000, bias=True)

To edit the out_features from 1000 to any number of classes num_classes:

from torchvision import models
import torch.nn as nn
model_ft = models.mobilenet_v2(pretrained=True)
num_ftrs = model_ft.classifier[1].in_features
model_ft.classifier[1] = nn.Linear(num_ftrs, num_classes)
Frasch answered 25/4, 2022 at 0:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.