2

I'm using the EfficientNet pre-trained model for my image classification project in Pytorch, and my purpose is to change the number of classes which is initially 1000 to 4. However, for that when I try adding a model._fc layer, I keep on seeing this error "EfficientNet' object has no attribute 'classifier". Here is my code (Config.NUM_CLASSES = 4):

elif Config.MODEL_NAME == 'efficientnet-b3':
      
    from efficientnet_pytorch import EfficientNet
    model = EfficientNet.from_pretrained('efficientnet-b3')
    model._fc= torch.nn.Linear(in_features=model.classifier.in_features, **out_features=Config.NUM_CLASSES**, bias=True)

The situation is different when I add model._fc to the end of the Resnet part, it clearly changes the number of output classes to 4 in Resnet-18. Here is the code for that:

if Config.MODEL_NAME == 'resnet18': model = models.resnet50(pretrained=True) model.fc = torch.nn.Linear(in_features=model.fc.in_features, out_features=Config.NUM_CLASSES, bias=True)

The solution is available for TensorFlow and Keras, and I would really appreciate it if anyone could help me with that in PyTorch.

Regards,

Far

FarnooshAzour
  • 23
  • 1
  • 6

2 Answers2

4

Torchvision >= 0.11 includes EfficientNet, and it does have a classifier attribute. To get the in_features :

import torchvision

model = torchvision.models.efficientnet_b5()
num_ftrs = model.classifier[1].in_features
Lei Huayi
  • 121
  • 1
  • 5
0

EfficentNet class doesn't have attribute classifier, you need to change in_features=model.classifier.in_features to in_features=model._fc.in_features.

import torchvision.models as models

NUM_CLASSES = 4

#EfficientNet
from efficientnet_pytorch import EfficientNet
efficientnet = EfficientNet.from_pretrained('efficientnet-b3')
efficientnet ._fc= torch.nn.Linear(in_features=efficientnet._fc.in_features, out_features=NUM_CLASSES, bias=True)

#mobilenet_v2
mobilenet = models.mobilenet_v2(pretrained=True)
mobilenet.classifier = nn.Sequential(nn.Dropout(p=0.2, inplace=False),
                  nn.Linear(in_features=mobilenet.classifier[1].in_features, out_features=NUM_CLASSES, bias=True))

#inception_v3
inception = models.inception_v3(pretrained=True)
inception.fc =  nn.Linear(in_features=inception.fc.in_features, out_features=NUM_CLASSES, bias=True)


Trong Van
  • 374
  • 1
  • 13
  • Thank you very much@Trong Van. It worked. I'm really new to this world. I wonder if you knew the same thing with the other two models MobileNet and InceptionNet. When I add your line of code to them I get this error. "HTTP Error 403: rate limit exceeded.Here is the code: – FarnooshAzour Jun 19 '21 at 16:27
  • elif Config.MODEL_NAME == 'mobilenet-v2': model = torch.hub.load('pytorch/vision:v0.9.0', 'mobilenet_v2', pretrained=True) model._fc= torch.nn.Linear(in_features=model._fc.in_features, out_features=4, bias=True) print(model) – FarnooshAzour Jun 19 '21 at 16:27
  • elif Config.MODEL_NAME == 'inception_v3': model = torch.hub.load('pytorch/vision:v0.9.0', 'inception_v3',aux_logits=False, pretrained=True) model._fc= torch.nn.Linear(in_features=model._fc.in_features, out_features=4, bias=True) print(model) – FarnooshAzour Jun 19 '21 at 16:32
  • I never use `torch.hub` before. But it seems error, check [here](https://github.com/pytorch/pytorch/issues/33233). I suggest using `torch.vision.models` to download wide-know models. Also, you should check the model attribute, every model's coding style is different from others, so the classifier name may differ. I will edit my answer for inception and mobinet. You can use this as an example – Trong Van Jun 19 '21 at 16:44
  • @Tong Van. I really appreciate your help. Thanks a lot. – FarnooshAzour Jun 21 '21 at 18:19