Extracting output from an intermediate layer of a pre-trained network

I want to extract features from the penultimate layer in ResNet18 in PyTorch. I have seen some posts in this discussion forum, suggesting to use hooks to get the output. But I am not sure if I fully understand how it works and will it work if I want to back-propagate gradients.

Is there any alternative away to do that like in Tensorflow “model.get_layer(layer_name).output”?

I tried using the code below to get the output from the penultimate layer

rn18 = models.resnet18(pretrained=True)

Getting the name of the modules

cnames = []
for n,c in rn18.named_children():
    cnames += [n]

cnames = cnmaes[:-1]

Then I tried building a new net using the modules

new_net = nn.Sequential(nn.ModuleList([rn18._modules[n] for n in cnames]))

But when I tried to get a summary of the model


I am getting an error

TypeError: forward() takes 1 positional argument but 2 were given

EDIT: Problem Solved
rn18._modules is an OrderedDict

So, popping the layers which are not required from the dict (like, rn18._modules.pop('fc')) and then doing
nn.Sequential(rn18._modules) does the job. I get the output from the penultimate layer of the pretrained network.

I think the issue was that I was giving nn.ModuleList as input to the nn.Sequential, whereas giving an OrderedDict as input worked fine.

This also works

        self.children_list = []
        for n,c in self.pretrained.named_children():
            if n != self.output_layer:
        self.net = nn.Sequential(*self.children_list)
        self.pretrained = None