Closed Linranran closed 6 years ago
@Linranran I have the same question, too. Have you figured out that? I am new with pytorch and will be happy that if you can give me some suggestions.
@liangbh6 you can change a little at https://github.com/Cysu/open-reid/blob/master/reid/models/resnet.py ,just return one more variable .
def forward(self, x):
for name, module in self.base._modules.items():
if name == 'avgpool':
break
x = module(x)
you can mark the x and return it a in the end. you will get two features when put images into the model ,and chose the feature you wants.
i make it by this way.and pytorch have the function hook
,but i am not clear how to use it .
@Linranran It works. Thank you for kind help! I think it's a very simple way to achieve that~
Hi, I don't really understand your method, do u mean for example i wan to extract the features from pool 5, i do this?
def forward(self, x):
for name, module in self.base._modules.items():
if name == 'avgpool':
return x
@zarakikun no ,do not do it this way.just mark the pool5 variable,and return it at the end .if you do it in your approach you will distroy the initial code and have to change a lot related code.you just marke the pool5 variable and return it in the end .and chose the returned variable that your want
@Linranran would you mind showing me a snippet example of how you would retrieve for example pool5 features? thank you!
@zarakikun like this
def forward(self, x): for name, module in self.base._modules.items(): if name == 'avgpool': break x = module(x) if self.cut_at_pooling: return x x = F.avg_pool2d(x, x.size()[2:]) x = x.view(x.size(0), -1) pool5=x if self.has_embedding: x = self.feat(x) x = self.feat_bn(x) if self.norm: x = F.normalize(x) elif self.has_embedding: x = F.relu(x) if self.dropout > 0: x = self.drop(x) if self.num_classes > 0: x = self.classifier(x) return x ,pool5
Dear @Linranran,
Thank you for your answer. But I think if one want extract the correct & discriminative feature vector from a pre-trained model must return a variable after x = self.feat(x)
layer. Am I correct? What do you think?
@ahkarami No, you don't have to. Just
outs = []
x = self.feat(x)
outs.append(x)
...
return outs
Dear @Cysu, Thank you very much for your answer.
hi,i have trained a resnet50 on my dataset.and i want to extract the feature that on pool5,how to use the parameters that on function of outputs = extract_cnn_feature(model, imgs, ?? ) . what should the third parameters look like ?