【问题标题】:How to add the last classification layer in EfficieNet pre-trained model in Pytorch?如何在 Pytorch 的 EfficieNet 预训练模型中添加最后一个分类层?
【发布时间】:2021-09-03 21:49:56
【问题描述】:

我在 Pytorch 中的 图像分类 项目中使用 EfficientNet 预训练模型,我的目的是将最初为 1000 的类数更改为 4 个。 但是,为此,当我尝试添加 model._fc 层时,我不断看到此错误“EfficientNet”对象没有属性“分类器”。这是我的代码(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)

当我在 Resnet 部分的末尾添加 model._fc 时情况有所不同,它显然将 Resnet-18 中的输出类数更改为 4。这是代码:

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)

solution 可用于 TensorFlow 和 Keras,如果有人能在 PyTorch 中帮助我,我将不胜感激。

问候,

【问题讨论】:

  • 能否提供预训练模型的链接?
  • 当然,这里是Link

标签: deep-learning pytorch conv-neural-network transfer-learning image-classification


【解决方案1】:

Torchvision >= 0.11 包括 EfficientNet,并且它确实具有分类器属性。获取 in_features :

import torchvision

model = torchvision.models.efficientnet_b5()
num_ftrs = model.classifier[1].in_features

【讨论】:

    【解决方案2】:

    EfficentNet 类没有属性classifier,需要将in_features=model.classifier.in_features 改为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。有效。我对这个世界真的很陌生。我想知道您是否对其他两个模型 MobileNet 和 InceptionNet 也有同样的了解。当我将您的代码行添加到他们时,我收到此错误。 “HTTP 错误 403:超出速率限制。代码如下:
    • 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)
    • elif Config.MODEL_NAME == 'inception_v3': model = torch.hub.load('pytorch/vision:v0.9.0', 'inception_v3',aux_logits=False, pretrained=True) 模型。 _fc= torch.nn.Linear(in_features=model._fc.in_features, out_features=4, bias=True) 打印(模型)
    • 我以前从未使用过torch.hub。但这似乎是错误的,请检查here。我建议使用torch.vision.models 下载广为人知的模型。另外,您应该检查模型属性,每个模型的编码风格都不同,因此分类器名称可能会有所不同。我将为 inception 和 mobinet 编辑我的答案。你可以以此为例
    • @Tong Van。我真的很感谢你的帮助。非常感谢。
    猜你喜欢
    • 2020-07-27
    • 2021-07-16
    • 2020-02-07
    • 2019-07-17
    • 2021-10-29
    • 2021-04-05
    • 2022-08-10
    • 2021-10-07
    • 2019-09-09
    相关资源
    最近更新 更多