Following the receptive field through a CNN

I have an idea, and I was wondering if anyone could point me in the right direction. I’d like to:

  1. Run an image through the CNN, keeping the activations at each layer.
  2. For every pixel, collect every filter activation that was influenced by that pixel.
  3. The final result would be a tensor of shape [image height, image width, # of filter channels in the entire network].

I was going to use network.modules() to get a list, and then find all the instances of Conv2d and MaxPool2d (etc.), and go from there… but squeezenet, for example, doesn’t appear to just apply things in the order I get from modules(). Is there some way I can traverse the computation graph, even though it is dynamic? I can figure out how to do it manually, but I’d like to implement a general-purpose function that takes in an arbitrary network.

because pytorch networks are code, it’s hard to build a general purpose function to do this.

If you want the activations at every layer you could register a forward hook on every module and store them in a list with .append(). Naively I’d expect that they would then be in the order in which they executed?

handles = []
variable_storage = []
for module in net.modules():
    handles.append(module.register_forward_hook(lambda input, output: 

Not sure if this will help, but I have made an implementation that allows to compute the RF numerically.
Here it is:

Hello @rogetrullo

could you compute the receptive field region of each feature location in a feature map?and is it possible for you to share some code snippet?

Thank you.