'VGG' object has no attribute 'fc'

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

moskomule: <p><code>print(model)</code> shows its architecture.<br>
Indeed there are some fc layers in vgg.</p>

micklexqg: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/2/2c49591a6173b9695c21c7c395e07a6f8d4d5eaf.png" data-download-href="https://discuss.pytorch.org/uploads/default/2c49591a6173b9695c21c7c395e07a6f8d4d5eaf" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/2/2c49591a6173b9695c21c7c395e07a6f8d4d5eaf.png" alt="image" data-base62-sha1="6jM9GoG0l9Mb1gkxTHK9Zyj0eAf" width="429" height="500" data-dominant-color="343232"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">464×540 26.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
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)?</p>

moskomule: <p><code>model.features</code> returns <code>nn.Sequential</code> so referring each layer like <code>list</code>.</p>
<pre><code class="lang-auto">&gt;&gt;&gt; model.classifier[0]
Linear (25088 -&gt; 4096)
</code></pre>

micklexqg: <p>which could return 4096? model.classifier[0]?</p>

Akash_Goel: <p>model.classifier[0].out_features<br>
will return 4096 for you <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

micklexqg: <p>yeah, i have tried, thank you!</p>

204161013: <p>HELLO <a class="mention" href="/u/micklexqg">@micklexqg</a> DID YOU GOT THE SOLUTUON TO THIS<br>
PLEASE POST IT.</p>

Vinay_Verma: <p>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.<br>
For example -</p>
<pre><code class="lang-auto">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)
  )
)
</code></pre>
<p>if you want to change classifier’s 0th layer then do -</p>
<pre><code class="lang-auto">num_ftrs = model.classifier[0].in_features
model.classifier[0] = nn.Linear(num_ftrs, num_classes)
</code></pre>

204161013: <p>ok thanks vinay, also can you tell me that what is ACC@5 mean here , how can i get  that<br>
lnk  - <a href="https://pytorch.org/vision/stable/models.html" class="inline-onebox" rel="noopener nofollow ugc">Models and pre-trained weights — Torchvision 0.16 documentation</a><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/e/fe99ad4265655463f6fb414af10e18d1d5f180ff.png" data-download-href="https://discuss.pytorch.org/uploads/default/fe99ad4265655463f6fb414af10e18d1d5f180ff" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/fe99ad4265655463f6fb414af10e18d1d5f180ff_2_531x500.png" alt="image" data-base62-sha1="AkiwLA8DazpQnM0T1FFMQntHEUT" width="531" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/fe99ad4265655463f6fb414af10e18d1d5f180ff_2_531x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/fe99ad4265655463f6fb414af10e18d1d5f180ff_2_796x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/f/e/fe99ad4265655463f6fb414af10e18d1d5f180ff.png 2x" data-dominant-color="F7F8F9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">819×770 24.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Vinay_Verma: <p>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.<br>
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.</p>
<p>If you have less than 5 classes, then obviously you cant use top 5 accuracy metric.<br>
Welcome.</p>

204161013: <p>ok cool,</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/drive/1dCXCn5sFJKJRoqpHZVW9pYBQ6vDd5SIs?usp=sharing">
  <header class="source">
      <img src="https://colab.research.google.com/img/favicon.ico?vrz=colab-20210923-060057-RC00_398452826" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/drive/1dCXCn5sFJKJRoqpHZVW9pYBQ6vDd5SIs?usp=sharing" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="260" height="260">

<h3><a href="https://colab.research.google.com/drive/1dCXCn5sFJKJRoqpHZVW9pYBQ6vDd5SIs?usp=sharing" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
(I have 3 features) getting an accuracy of only 57 percent , can you help me out<br>
this is the model i am using of tranfer learning  taken from the link</p>
<p><a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html</a></p>

ashwin: <p>Hello , Try changing the loss function  from</p>
<pre><code class="lang-auto">criterion = nn.CrossEntropyLoss()
</code></pre>
<p>to</p>
<pre><code class="lang-auto">criterion= torch.nn.NLLLoss()
</code></pre>

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

204161013: <p>like I am changing to diiferent models so that i can get maximum accuracy , i am taking from this<br>
<a href="https://pytorch.org/vision/stable/models.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/vision/stable/models.html</a></p>