Easy access to pretrained models for system identification
Project description
ptrnets
Collection of pretrained networks in pytorch readily available for transfer learning tasks like neural system identification.
Installation
pip install ptrnets
Usage
Find a list of all available models like this:
from ptrnets import AVAILABLE_MODELS
print(AVAILABLE_MODELS)
Import a model like this:
from ptrnets import simclr_resnet50x2
model = simclr_resnet50x2(pretrained=True)
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:
import torch
from ptrnets import resnet50
from ptrnets.utils.mlayer import probe_model
model = resnet50(pretrained=True)
available_layers = [name for name, _ in model.named_modules()]
layer_name = "layer2.1"
assert layer_name in available_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:
import torch
from ptrnets import vgg16
from ptrnets.utils.mlayer import clip_model, probe_model
model = vgg16(pretrained=True)
available_layers = [name for name, _ in model.named_modules()]
layer_name = "features.18"
assert layer_name in available_layers, f"Layer {layer_name} not available. Choose from {available_layers}"
model_clipped = clip_model(model, layer_name) # Creates new model up to the layer
x = torch.rand(1, 3, 224, 224)
output = model_clipped(x)
assert torch.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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ptrnets-0.1.1.tar.gz.
File metadata
- Download URL: ptrnets-0.1.1.tar.gz
- Upload date:
- Size: 28.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bc62be98e923e3acd169acdacca67e1fad4443952f0492b569e582e3585abce
|
|
| MD5 |
959f5eed30129e71052f3bf745e482bc
|
|
| BLAKE2b-256 |
70037592904543c9e927439f92bfa1bffa2357d97780e0ce40be5ffc8955fc4e
|
File details
Details for the file ptrnets-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ptrnets-0.1.1-py3-none-any.whl
- Upload date:
- Size: 36.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44437c270aabf8977aba9bab42096294df0a3bfff182ab1ded76dbb6e20800d9
|
|
| MD5 |
5ba7df00489ae9361a21f610563971c7
|
|
| BLAKE2b-256 |
f4c7ba5f1681e10740c1b8b79eb75599ba7e60c63469368b20a6a13d5ebc0c68
|