You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can access intermediate representations in two ways:
Probing the model
You can conveniently access intermediate representations of a forward pass using the ptrnets.utils.mlayer.probe_model function Example:
importtorchfromptrnetsimportresnet50fromptrnets.utils.mlayerimportprobe_modelmodel=resnet50(pretrained=True)
available_layers= [nameforname, _inmodel.named_modules()]
layer_name="layer2.1"assertlayer_nameinavailable_layers, f"Layer {layer_name} not available. Choose from {available_layers}"model_probe=probe_model(model, layer_name)
x=torch.rand(1, 3, 224, 224)
output=model_probe(x)
Note: if the input is not large enough to do a full forward pass through the network, you might need to use a try-except block to catch the RuntimeError.
Clipping the model
ptrnets.utils.mlayer.clip_model creates a copy of the model up to a specific layer. Because the model is smaller, a forward pass can run faster.
However, the output is only guaranteed to be the same as the original model's if the architecture is fully sequential up until that layer.
Example:
importtorchfromptrnetsimportvgg16fromptrnets.utils.mlayerimportclip_model, probe_modelmodel=vgg16(pretrained=True)
available_layers= [nameforname, _inmodel.named_modules()]
layer_name="features.18"assertlayer_nameinavailable_layers, f"Layer {layer_name} not available. Choose from {available_layers}"model_clipped=clip_model(model, layer_name) # Creates new model up to the layerx=torch.rand(1, 3, 224, 224)
output=model_clipped(x)
asserttorch.allclose(output, probe_model(model, layer_name)(x)), "Output of clipped model is not the same as the original model"
Contributing
Pull requests are welcome.
Please see instructions here.
About
Collection of pretrained networks in pytorch readily available for transfer learning tasks (e.g. neural system identification)