Beginner question: I was trying to use PyTorch Hook to get the layer output of pretrained model. I’ve tried two approaches both with some issues:
method 1:
net = EfficientNet.from_pretrained('efficientnet-b7')

visualisation = {}

def hook_fn(m, i, o):
  visualisation[m] = o 

def get_all_layers(net):
  for name, layer in net._modules.items():
    #If it is a sequential, don't register a hook on it
    # but recursively register hook on all it's module children
    if isinstance(layer, nn.Sequential):
      pass
    else:
      # it's a non sequential. Register a hook
      layer.register_forward_hook(hook_fn)

get_all_layers(net)

  
out = net(torch.randn(2,3,224,224))

# Just to check whether we got all layers
visualisation.keys()

This is from the tutorial 16. However, there’re two issues:

the result only has 8 keys/items - was expecting it to have a lot more layers.
EfficientNet seems to require batch input, but say if I want to store the last but second layer’s output for 10 different images respectively in a list or array, how can I do so?

method 2:
I was trying to understand how to use the Hook class to extract a last but second layer output from a bunch of images without saving every layer, and if I’m passing input to a pre-trained model, do I still need a backward step?
class Hook():
    def __init__(self, module, backward=False):
        if backward==False:
            self.hook = module.register_forward_hook(self.hook_fn)
        else:
            self.hook = module.register_backward_hook(self.hook_fn)
    def hook_fn(self, module, input, output):
        self.input = input
        self.output = output
    def close(self):
        self.hook.remove()

Thanks in advance.