torch-featurelayer
🧠 Simple utility functions and wrappers for hooking onto layers within PyTorch models for feature extraction.
Install
pip install torch-featurelayer
Usage
Imports:
import torch
from torchvision.models import vgg11
from torch_featurelayer import FeatureLayer
Load a pretrained VGG-11 model:
model = vgg11(weights='DEFAULT').eval()
Hook onto layer features.15 of the model:
layer_path = 'features.15'
hooked_model = FeatureLayer(model, layer_path)
Forward pass an input tensor through the model:
x = torch.randn(1, 3, 224, 224)
feature_output, output = hooked_model(x)
feature_output is the output of layer features.15. Print the output shape:
print(f'Feature layer output shape: {feature_output.shape}') # [1, 512, 14, 14]
print(f'Model output shape: {output.shape}') # [1, 1000]
Check the examples directory for more.
API
torch_featurelayer.FeatureLayer
The FeatureLayer class wraps a model and provides a hook to access the output of a specific feature layer.
-
__init__(self, model: torch.nn.Module, feature_layer_path: str)Initializes the
FeatureLayerinstance.model: The model containing the feature layer.feature_layer_path: The path to the feature layer in the model.
-
__call__(self, *args: Any, **kwargs: Any) -> tuple[torch.Tensor | None, torch.Tensor]Performs a forward pass through the model and updates the hooked feature layer.
*args: Variable length argument list.**kwargs: Arbitrary keyword arguments.
Returns a tuple containing the feature layer output and the model output.
torch_featurelayer.FeatureLayers
The FeatureLayers class wraps a model and provides hooks to access the output of multiple feature layers.
-
__init__(self, model: torch.nn.Module, feature_layer_paths: list[str])Initializes the
FeatureLayersinstance.model: The model containing the feature layers.feature_layer_paths: A list of paths to the feature layers in the model.
-
__call__(self, *args: Any, **kwargs: Any) -> tuple[dict[str, torch.Tensor | None], torch.Tensor]Performs a forward pass through the model and updates the hooked feature layers.
*args: Variable length argument list.**kwargs: Arbitrary keyword arguments.
Returns a tuple containing the feature layer outputs and the model output.
torch_featurelayer.get_layer_candidates(module: torch.nn.Module, max_depth: int = 1) -> Generator[str, None, None]
The get_layer_candidates function returns a generator of layer paths for a given model up to a specified depth.
model: The model to get layer paths from.max_depth: The maximum depth to traverse in the model's layers.
Returns a generator of layer paths.
License
Metadata
Release files for torch-featurelayer 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torch_featurelayer-0.1.2.tar.gz | 8.2 kB | Without provenance | Details for torch_featurelayer-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_featurelayer-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for torch_featurelayer-0.1.2-py3-none-any.whl |
Total release size: 15.9 kB
Release files / torch_featurelayer-0.1.2.tar.gz
| Download URL | torch_featurelayer-0.1.2.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
42c6cf350febc2c7b5b172d83d1d0624645893e01ab0ceee7a21c8469c0a9052
|
|
BLAKE2b-256 checksum How to use checksums |
7c0f2e0b893125ef4196a59cf7fa7aa139265687ce09e06a3c5d1fe6a02347c7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.10
|
Release files / torch_featurelayer-0.1.2-py3-none-any.whl
| Download URL | torch_featurelayer-0.1.2-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3638311e0b9ff972ecb184b0bcb39b82721972da93d922ea36ef65cfc43e8a2e
|
|
BLAKE2b-256 checksum How to use checksums |
c2b2118c64dbd60e13a87116bf68a7a6d4ff09ed51fd547fa64a51878c911a60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.10
|
Security for torch-featurelayer 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.