How can I extract intermediate layer output from loaded CNN model?

Hello,

I have created a “recorder” for such goals part of this package:

It is very simple to record from multiple layers of PyTorch models, including CNNs.

An example to record output from all conv layers of VGG16:


model = torch.hub.load('pytorch/vision:v0.10.0', 'vgg16', pretrained = True)
# Only conv layers
layer_nr = [0, 2, 5, 7, 10, 12, 14, 17, 19, 21, 24, 26, 28]

# Get layers from model
layers = [list(model.features.named_children())[lnr][1] for lnr in layer_nr]

# Assign a recorder to each layer from modelutils in torchknickknacks
from torchknickknacks import modelutils
recorders = [modelutils.Recorder(layer, record_output = True, backward = False) for layer in layers]

# Pass some data through the model
X = torch.rand(32,3,224,224)
out = model(X)

# Get the output of each recorder (output of each layer)
rec = [r.recording.detach().clone() for r in recorders]

The Recorder class is based on Understanding Pytorch hooks | Kaggle