I want to extract features(activations) from the 4 layers of resnet 18. Is my implementation correct?

Hi, I want to extract activations from the 4 resnet blocks/layers and use them. Is my code correct?

Resnet modules:

for num, layer in enumerate(resnet_18._modules.keys()):
    print(num, layer)

The above code outputs:

0 conv1
1 bn1
2 relu
3 maxpool
4 layer1
5 layer2
6 layer3
7 layer4
8 avgpool
9 fc

Custom Model:

class ResNet18(nn.Module):
    def __init__(self, layer_numbers):
        self.layer_numbers = layer_numbers # indices of layers whose activations are needed
        self.layer_acts = OrderedDict()
        self.resnet_18 = models.resnet18(pretrained=True)
        self.forward_hooks = []
        #register hooks
        for layer_num, layer_name in enumerate(self.resnet_18._modules.keys()):
            if layer_num in layer_numbers:
                self.forward_hooks.append(getattr(self.resnet_18, layer_name).register_forward_hook(self.get_activations(layer_name)))
    #Defining hook to get intermediate features     
    def get_activations(self, layer_name):
        def hook(module, input, output):
            self.layer_acts[layer_name] = output
        return hook
    #forward pass
    def forward(self, x):
        out = self.resnet_18(x)
        #don't need output values
        return self.layer_acts

Model Usage:

required_layers = [4, 5, 6, 7]
model = ResNet18(layer_numbers=required_layers)

Now, if I pass my image to the model, will I be getting correct layer activations for the 4 layers?

Thanks in advance.

1 Like

The code looks alright and you can check the results by accessing the dict after a forward pass where the activations would be stored.
To compare your approach you would also directly access the desired layers and register the hooks e.g. via model.layer4.register_forward_hook(...).