I have an idea, and I was wondering if anyone could point me in the right direction. I’d like to:
Run an image through the CNN, keeping the activations at each layer.
For every pixel, collect every filter activation that was influenced by that pixel.
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.
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:
variable_storage.push_back(output))