简体   繁体   中英

How to extract fc7 features from AlexNet in pytorch as numpy array?

I want to extract the 4096 -dimensional feature vector from the fc7 layer of my finetuned AlexNet. My goal is to use this layer for clustering later on. This is how I extract it:

alexnet = models.alexnet(pretrained=True);
fc7 = alexnet.classifier[6];

However, when I print it, fc7 is a Linear object:

Linear(in_features=4096, out_features=1000, bias=True)

What I am looking for is how to turn this Linear object into a numpy array so that I can do further manipulations on it. What I am thinking of is to call its method 'def forward(self, input)' , but am not sure which input to provide? Should I provide the input image or should I provide the output the fc6 layer?

And I want the 4096 -dim input array and get rid of the 1000 output array (presumably, since I don't think it will help me for clustering).

This could be done by creating a new model with all the same layers (and associated parameters) as alexnet except for the last layer.

new_model = models.alexnet(pretrained=True)
new_classifier = nn.Sequential(*list(new_model.classifier.children())[:-1])
new_model.classifier = new_classifier

You should now be able to provide the input image to new_model and extract a 4096-dimensional feature vector.

If you do need a particular layer as a numpy array for some reason, you could do the following: fc7.weight.data.numpy() .

(on PyTorch 0.4.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM