How to extract features of an image from a trained model


#63

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?


#64

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?


(Francisco Massa) #65

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


(Ismail Elezi) #66

@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?


#67

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))?


(Ismail Elezi) #68

@lakehanne

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

(Sameer Ahmed) #69

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


(Shaayaan Sayed) #70

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


#71

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


(Francisco Massa) #72

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.


#73

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


(Visonpon) #74

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~


#75

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


(Spandan Madan) #76

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.


(Anderson Peng) #77

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!


(Guoshuangshuang) #78

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


(kgl) #79

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


(Spandan Madan) #80

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


(Ismail Elezi) #81

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?


(Federico Pala) #82

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