1

I am making the model using the fine-tuning method and the model is VGG-16. But I got the following error 'Sequential' object has no attribute 'in_features' I used classifier so I change classifier into fc but got this error 'Sequential' object has no attribute 'fc'. Can somebody guide me on what I am doing wrong? I have attached the screenshot of the error as well.

**ERROR:'Sequential' object has no attribute 'in_features'**
[![enter image description here][1]][1]

Traceback (most recent call last):
  File "ct_pretrained.py", line 186, in <module>
    model = build_model().cuda()
  File "ct_pretrained.py", line 42, in build_model
    return models.VGG(is_emr=is_emr)
  File "/data/torch/models/vgg.py", line 19, in __init__
    num_ftrs = self.axial_model.classifier.in_features
  File "/root/miniconda/lib/python3.8/site-packages/torch/nn/modules/module.py",line 778, in __getattr__                       
    raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
torch.nn.modules.module.ModuleAttributeError: 'Sequential' object has no attribute 'in_features'                  

**ERROR:'VGG' object has no attribute 'fc'**
[![enter image description here][2]][2] 
Traceback (most recent call last):
  File "ct_pretrained.py", line 186, in <module>
    model = build_model().cuda()
  File "ct_pretrained.py", line 42, in build_model
    return models.VGG(is_emr=is_emr)
  File "/data/torch/models/vgg.py", line 19, in __init__
    num_ftrs = self.axial_model.fc.in_features
  File "/root/miniconda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 778, in __getattr__
    raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
torch.nn.modules.module.ModuleAttributeError: 'VGG' object has no attribute 'fc'



   

     import torch
        import torch.nn as nn
        from torchvision import models
        
        __all__ = ['VGG']
        
        class VGG(nn.Module):
        
            def __init__(self, is_emr=False, mode='sum'):
                super().__init__()
                self.is_emr = is_emr
                self.mode = mode
                in_dim = 45
        
                self.axial_model = models.vgg16(pretrained=True)
                out_channels = self.axial_model.features[0].out_channels
                self.axial_model.features[0] = nn.Conv2d(1, out_channels, kernel_size=7, stride=1, padding=0, bias=False)
                self.axial_model.features[3] = nn.MaxPool2d(1)
                num_ftrs = self.axial_model.classifier.in_features #error in this line of code
                self.axial_model.classifier = nn.Linear(num_ftrs, 15)
        
        
        
                self.sa_co_model = models.vgg16(pretrained=True)
                self.sa_co_model.features[0] = nn.Conv2d(1, out_channels, kernel_size=7, stride=1, padding=(3,0), bias=False)
                self.sa_co_model.features[3] = nn.MaxPool2d(1)
                self.sa_co_model.classifier = nn.Linear(num_ftrs, 15)
        
                if self.is_emr:
                    self.emr_model = EMRModel()
                    if self.mode == 'concat': in_dim = 90
        
                self.classifier = Classifier(in_dim)
        
            def forward(self, axial, sagittal, coronal, emr):
                axial = axial[:,:,:-3,:-3]
                sagittal = sagittal[:,:,:,:-3]
                coronal = coronal[:,:,:,:-3]
        
                axial_feature = self.axial_model(axial)
                sagittal_feature = self.sa_co_model(sagittal)
                coronal_feature = self.sa_co_model(coronal)
                out = torch.cat([axial_feature, sagittal_feature, coronal_feature], dim=1)
        
                if self.is_emr:
                    emr_feature = self.emr_model(emr)
                    if self.mode == 'concat':
                        out = torch.cat([out, emr_feature], dim=1)
                    elif self.mode == 'sum':
                        out += emr_feature
        
                out = self.classifier(out)
        
                return out 
Farah Jabeen
  • 141
  • 1
  • 2
  • 6

1 Answers1

0

The classifier sequential object does not have a variable called in_features. If you want to do it dynamically, you will need to access the layer in the classifier, rather than the entire classifier: num_ftrs = self.axial.model.classifier[0].in_features. This accesses the first layer of the sequential object, namely the one that determines how many features go into the entire sequential object.

However, you can easily replace the classifier layer with another layer by determining the necessary number of features by hand. Looking at the pytorch sourcecode for VGG16, you can see the classifier takes 512 * 7 * 7 features as input.

Kroshtan
  • 637
  • 5
  • 17