PyTorch supplied pretrained ResNet with different depths. The ResNet has the “BasicBlock” or “Bottleneck” structure. I want to ask that how to get separate conv feature maps from these pretrained ResNet (for example, restnet18 or resnet50)?
your question is not clear at all. What do you mean by “get separate conv feature maps”.
Also, there are a few posts explaining how to extract intermediate outputs of resnet, please read those.
What I mean is “Extracting image features” from different layers in ResNet, and I read this post.
I just wonder how this can be done in ResNet. One possible way is to construct a partial model which only use the front layers for forwarding. Are there other ways?
By the way, the Torch version of ResNet has a nice script
to extract features from ResNet. If PyTorch also has a corresponding one will be better.
In your case, attaching a hook to the layer you want seems easier.
model = models.resnet18() outputs =  def hook(module, input, output): outputs.append(output) model.layer2.conv1.register_forward_hook (hook)
(Typing from the phone, the code is an approximation and might contain errors)
Why do we have output as one of the inputs in the hook?
Here is what i do:
class Resnet50Extractor(nn.Module): def __init__(self, submodule, extracted_layer): super(Resnet50Extractor, self).__init__() self.submodule = submodule self.extracted_layer = extracted_layer def forward(self, x): if self.extracted_layer == 'maxpool': modules = list(self.submodule.children())[:4] elif self.extracted_layer == 'inner-layer-3': modules = list(self.submodule.children())[:6] third_module = list(self.submodule.children()) third_module_modules = list(third_module.children())[:3] # take the first three inner modules third_module = nn.Sequential(*third_module_modules) modules.append(third_module) elif self.extracted_layer == 'layer-3': modules = list(self.submodule.children())[:7] else: # after avg-pool modules = list(self.submodule.children())[:9] self.submodule = nn.Sequential(*modules) x = self.submodule(x) return x
then call like below:
model_ft = models.resnet50(pretrained=True) extractor = Resnet50Extractor(model_ft, extracted_layer) features = extractor(input_tensor)
is there any pros or cons between using your method comparing to