Pytorch, Pre-trained model: How to use feature and classifier at the same time
I'm using vgg16 extracting image feature vector. I want to get 114096 vector from the 2nd-to-last layer.

My code:
def get_model():
    model = models.vgg16(pretrained=True)#.features[:].classifier[:4]
    model = model.eval()
    # model.cuda()  # send the model to GPU, DO NOT include this line if you haven't a GPU
    return model

But I can only get 111000 vector from the last layer.
I know how to use feathers and classifier, but I don't know how to use them at the same time.
use classifier only:

use feathers only:

use them at the same time:

log:
Traceback (most recent call last):
  File &quot;/mnt/c/Users/sunji/PycharmProjects/image_cluster_pytorch/main.py&quot;, line 7, in &lt;module&gt;
    model = calc.get_model()
  File &quot;/mnt/c/Users/sunji/PycharmProjects/image_cluster_pytorch/imagecluster/calc.py&quot;, line 17, in get_model
    model = models.vgg16(pretrained=True).features[:].classifier[:4]
  File &quot;/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py&quot;, line 771, in __getattr__
    raise ModuleAttributeError(&quot;'{}' object has no attribute '{}'&quot;.format(
torch.nn.modules.module.ModuleAttributeError: 'Sequential' object has no attribute 'classifier'


I've find the solution myself.
Sorry for bothering to Stack overflow.
here is the code:
def get_model():
    model = models.vgg16(pretrained=True)
    model.features = model.features[:]
    model.classifier = model.classifier[:4]

    model = model.eval()
    # model.cuda()  # send the model to GPU, DO NOT include this line if you haven't a GPU
    return model

result:

I think this is the right answer.
