How can I visualize the output of a particular layer from my custom CNN model?

Say if I have a very basic pre-trained 3 layer CNN model. If I want to check what kind of feature it is outputting from the 1st or 2nd layer what do I have to do? My thinking right now is just slicing the network at the point of interest, and rebuild it with nn.Sequencial() into an individual network, then I will start the inference process from there. However I am not sure if this is the best practice because this means each time I want to check a new layer I have to slice the original model and turn that into a new model to run inference.

Hi,
You should have a look at hookers
https://pytorch.org/docs/stable/nn.html?highlight=hook#torch.nn.Module.register_forward_hook
They are registered after the model is defined. (Good idea to look in the forum :slight_smile: )

1 Like

Thank you for this information. Still a little bit confused on how to use it. Apparently you need to create your own “hooking function”, then pass the function into the register_forward_hook method to grab the output? E.g. Layer1.register_forward_hook( “hooking function”).

Edited: This is useful too.

https://pytorch.org/tutorials/beginner/former_torchies/nn_tutorial.html#forward-and-backward-function-hooks

Edited 2: Just did a quick look through their example they didn’t use ANY hooks? Did they remove the tutorial? This is the linked example, unless I am bind I don’t see anything about getting the output from conv2.

Final edited: Idk why my google search led me to the old tutorial here is the updated one…
https://pytorch.org/tutorials/beginner/former_torchies/nnft_tutorial.html?highlight=hook