'VGG' object has no attribute 'fc'

I am training with vgg net, but came across the error: ‘VGG’ object has no attribute ‘fc’.
but there are no similar errors with inception v3 and resnet. I have trained them early. and now is with vgg.
the part code is follow:
model = models.vgg16(pretrained=True)
num_ftrs = model.fc.in_features #(error is here )
model.fc = nn.Linear(num_ftrs, num)

print(model) shows its architecture.
Indeed there are some fc layers in vgg.


this is the partial architecture, but i don’t know how to use it. how to refer the num_ftrs(just like the given code above)?

model.features returns nn.Sequential so referring each layer like list.

>>> model.classifier[0]
Linear (25088 -> 4096)

which could return 4096? model.classifier[0]?

model.classifier[0].out_features
will return 4096 for you :slight_smile:

yeah, i have tried, thank you!

HELLO @micklexqg DID YOU GOT THE SOLUTUON TO THIS
PLEASE POST IT.

Which model you are using? You can find or modify the layer by knowing the structure of the model. Try loading the model in jupyter notebook if possible, print(model). Using the hierarchy you can reach to the required layer and modify that.
For example -

vgg = models.vgg16(pretrained=True)

VGG(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU(inplace=True)
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU(inplace=True)
    (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU(inplace=True)
    (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace=True)
    (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (13): ReLU(inplace=True)
    (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (15): ReLU(inplace=True)
    (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (18): ReLU(inplace=True)
    (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (20): ReLU(inplace=True)
    (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (22): ReLU(inplace=True)
    (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (25): ReLU(inplace=True)
    (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (27): ReLU(inplace=True)
    (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (29): ReLU(inplace=True)
    (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
  (classifier): Sequential(
    (0): Linear(in_features=25088, out_features=4096, bias=True)
    (1): ReLU(inplace=True)
    (2): Dropout(p=0.5, inplace=False)
    (3): Linear(in_features=4096, out_features=4096, bias=True)
    (4): ReLU(inplace=True)
    (5): Dropout(p=0.5, inplace=False)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
  )
)

if you want to change classifier’s 0th layer then do -

num_ftrs = model.classifier[0].in_features
model.classifier[0] = nn.Linear(num_ftrs, num_classes)

ok thanks vinay, also can you tell me that what is ACC@5 mean here , how can i get that
lnk - Models and pre-trained weights — Torchvision 0.16 documentation

There are the metrics to compare models, You can read it as, Top 1 Accuracy, Top 5 Accuracy. In Imagenet there are 1000 classes. If the image you gave is of “Cat” and cat comes in Top 5 predictions then it is acc@5. If you want to be strict and want the class you are detecting, “Cat” in this case should come as 1st detection with highest confidence. Then it is acc@1.
Based on above theory, you can see acc@5 is more than acc@1 for all the models. Predicted class is Correct if we look at top 5 predictions but wrong if we take first prediction as result.

If you have less than 5 classes, then obviously you cant use top 5 accuracy metric.
Welcome.

ok cool,

(I have 3 features) getting an accuracy of only 57 percent , can you help me out
this is the model i am using of tranfer learning taken from the link

https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

Hello , Try changing the loss function from

criterion = nn.CrossEntropyLoss()

to

criterion= torch.nn.NLLLoss()

hello ashwin,
Thanks for your suggestion , i did it but
now the test accuracy has been decreased to 45% , you can check by opening collab that i shared,

like I am changing to diiferent models so that i can get maximum accuracy , i am taking from this
https://pytorch.org/vision/stable/models.html