How to extract features of an image from a trained model

Hey guys, pardon my silly question. Of what use is the FC layer? How do you generalize to localization/detection of objects using the last fclayer features?

In this line new_classifier = nn.Sequential(*list(model.classifier.children())[:-1]) from @fmassa 's post, I wonder if it was meant to read

  new_classifier = nn.Sequential(*list(model.children())[:-1])

I checked the methods of the resnet18 model and it seemed to have

 |  children(self)
 |      Returns an iterator over immediate children modules.

as a direct method. Am I wrong?

resnet18 doesn’t have a classifier field, and the solution I gave was for vgg.

2 Likes

@fmassa In order to get the fc7 features of a resnet, do I need to write a class like you did with Inception, or is there a more straightforward way of doing so?

Just using:

new_classifier = nn.Sequential(*list(model.children())[:-1])
model = new_classifier

seems to work. Right?

Also, is there a way of getting both the fc7 features and the results of the softmax?

Hey @apaszke, @fmassa, if I download a resnet18 without weights for example,

resnet = torchvision.models.resnet18(pretrained=False)

Now, say I want to change the shape of the last layer to 512->2.
Which is the appropriate way to reshape the last fc layer? This:

resnet.add_module('9', nn.Linear(512, 2))?

@lakehanne

resnet.fc = nn.Linear(512, 2)
2 Likes

can you please tell me how are you loading model in different file??

Has there been any updates on a feature that would allow iteration over graphs by name?

Hi fmassa! Where can I get to know model.classifier? Why isn’t it new_classifier = nn.Sequential(*list(model.children())[:-1]) ?

It depends on your model, but usually printing it is a first way to inspect (roughly) what models are used inside.
But you can always refer back to the python implementation to have a idea of the structure of the model.

Sentence of “new_classifier = nn.Sequential(*list(model.children())[:-1])” goes right!

Hi @fmassa how can i get a certain conv layer’s feature in DenseNet model? since i do not know the exact name of this layer. thanks~

@visonpon you can check the source code for exact layer names: https://github.com/pytorch/vision/blob/master/torchvision/models/densenet.py

For any model, you can use model.children() to see it’s layers. Once you know the number of layers you want to retain (based on which layer you want to get features from), use the nn.Sequential() function to create a new model. Then do a model(x) on input x to get output features. Read this thread a little more thoroughly, all of these commands have been written many times here.

3 Likes

I am wondering where I can check the attributes of the models. For example, I was trying to check the features from Documents and cannot find where is the definition of feature, a tuple with four dimensions. Thanks!

Does this method copy layers’ parameters at the same time??

Would this work when using multi-gpus through nn.parallel.data_parallel?

What do you mean by ‘attributes’ of models? If you mean weights/parameters then you can access them using .parameters()

Weird question,

When we extract fc7 features, are we supposed to do a relu on them or not? I always thought that it makes sense to do so (considering that in the training we do that), but I am getting better results in some task by not applying relu on them. How does this make sense?

1 Like

When you do the relu you are filtering negative values that can carry useful information, that’s the reason I guess