Metadata-Version: 2.1
Name: torch-featurelayer
Version: 0.1.2
Summary: Useful utility functions and wrappers for hooking onto layers within PyTorch models for feature extraction.
Author-email: spencerwooo <spencer.woo@outlook.com>
License: MIT License
        
        Copyright (c) 2024 Spencer (Shangbo Wu)
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Repository, https://github.com/spencerwooo/torch-featurelayer
Project-URL: Documentation, https://github.com/spencerwooo/torch-featurelayer/blob/main/README.md
Keywords: torch,pytorch,torchvision,feature-extraction
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: GPU :: NVIDIA CUDA
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch
Provides-Extra: dev
Requires-Dist: mypy ; extra == 'dev'
Requires-Dist: ruff ; extra == 'dev'
Provides-Extra: test
Requires-Dist: torchvision <0.18,>=0.15 ; extra == 'test'
Requires-Dist: pytest ; extra == 'test'

[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![image](https://img.shields.io/pypi/v/torch-featurelayer.svg)](https://pypi.python.org/pypi/torch-featurelayer)
[![image](https://img.shields.io/pypi/l/torch-featurelayer.svg)](https://pypi.python.org/pypi/torch-featurelayer)
[![image](https://img.shields.io/pypi/pyversions/torch-featurelayer.svg)](https://pypi.python.org/pypi/torch-featurelayer)
[![lint](https://github.com/spencerwooo/torch-featurelayer/actions/workflows/lint.yml/badge.svg)](https://github.com/spencerwooo/torch-featurelayer/actions/workflows/lint.yml)
[![test](https://github.com/spencerwooo/torch-featurelayer/actions/workflows/test.yml/badge.svg)](https://github.com/spencerwooo/torch-featurelayer/actions/workflows/test.yml)

# torch-featurelayer

🧠 Simple utility functions and wrappers for hooking onto layers within PyTorch models for feature extraction.

> [!TIP]
> This library is intended to be a simplified and well-documented implementation for extracting a PyTorch model's intermediate layer output(s). For a more sophisticated and complete implementation, either consider using [`torchvision.models.feature_extraction`](https://pytorch.org/vision/stable/feature_extraction.html), or check the official [`torch.fx`](https://pytorch.org/docs/stable/fx.html).

## Install

```shell
pip install torch-featurelayer
```

## Usage

Imports:

```python
import torch
from torchvision.models import vgg11
from torch_featurelayer import FeatureLayer
```

Load a pretrained VGG-11 model:

```python
model = vgg11(weights='DEFAULT').eval()
```

Hook onto layer `features.15` of the model:

```python
layer_path = 'features.15'
hooked_model = FeatureLayer(model, layer_path)
```

Forward pass an input tensor through the model:

```python
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:

```python
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](./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 `FeatureLayer` instance.

    - `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 `FeatureLayers` instance.

    - `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

[MIT](./LICENSE)
