torch-featurelayer


Nametorch-featurelayer JSON
Version 0.1.1 PyPI version JSON
download
home_pageNone
SummaryUseful utility functions and wrappers for hooking onto layers within PyTorch models for feature extraction.
upload_time2024-04-15 09:09:32
maintainerNone
docs_urlNone
authorNone
requires_python<3.13,>=3.10
licenseMIT 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.
keywords torch pytorch torchvision feature-extraction
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![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/ci.yml/badge.svg)](https://github.com/spencerwooo/torch-featurelayer/actions/workflows/ci.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). 

## Usage

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

# 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](./examples/) directory for more.

## API

> `torch_featurelayer.FeatureLayer(model: torch.nn.Module, feature_layer_path: str)`

> `torch_featurelayer.FeatureLayers(model: torch.nn.Module, feature_layer_paths: list[str])`

> `torch_featurelayer.get_layer_candidates(module: nn.Module, max_depth: int = 1)`

## License

[MIT](./LICENSE)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "torch-featurelayer",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.13,>=3.10",
    "maintainer_email": null,
    "keywords": "torch, pytorch, torchvision, feature-extraction",
    "author": null,
    "author_email": "spencerwooo <spencer.woo@outlook.com>",
    "download_url": "https://files.pythonhosted.org/packages/df/78/ae8b38a166a50ba18eee8c860b4c27f5f515d202f44d614ac775221dbea8/torch_featurelayer-0.1.1.tar.gz",
    "platform": null,
    "description": "[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![image](https://img.shields.io/pypi/v/torch-featurelayer.svg)](https://pypi.python.org/pypi/torch-featurelayer)\n[![image](https://img.shields.io/pypi/l/torch-featurelayer.svg)](https://pypi.python.org/pypi/torch-featurelayer)\n[![image](https://img.shields.io/pypi/pyversions/torch-featurelayer.svg)](https://pypi.python.org/pypi/torch-featurelayer)\n[![lint](https://github.com/spencerwooo/torch-featurelayer/actions/workflows/ci.yml/badge.svg)](https://github.com/spencerwooo/torch-featurelayer/actions/workflows/ci.yml)\n\n# torch-featurelayer\n\n\ud83e\udde0 Simple utility functions and wrappers for hooking onto layers within PyTorch models for feature extraction.\n\n> [!TIP]\n> 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). \n\n## Usage\n\n```python\nimport torch\nfrom torchvision.models import vgg11\n\nfrom torch_featurelayer import FeatureLayer\n\n# Load a pretrained VGG-11 model\nmodel = vgg11(weights='DEFAULT').eval()\n\n# Hook onto layer `features.15` of the model\nlayer_path = 'features.15'\nhooked_model = FeatureLayer(model, layer_path)\n\n# Forward pass an input tensor through the model\nx = torch.randn(1, 3, 224, 224)\nfeature_output, output = hooked_model(x)\n\n# Print the output shape\nprint(f'Feature layer output shape: {feature_output.shape}')  # [1, 512, 14, 14]\nprint(f'Model output shape: {output.shape}')  # [1, 1000]\n```\n\nCheck the [examples](./examples/) directory for more.\n\n## API\n\n> `torch_featurelayer.FeatureLayer(model: torch.nn.Module, feature_layer_path: str)`\n\n> `torch_featurelayer.FeatureLayers(model: torch.nn.Module, feature_layer_paths: list[str])`\n\n> `torch_featurelayer.get_layer_candidates(module: nn.Module, max_depth: int = 1)`\n\n## License\n\n[MIT](./LICENSE)\n",
    "bugtrack_url": null,
    "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. ",
    "summary": "Useful utility functions and wrappers for hooking onto layers within PyTorch models for feature extraction.",
    "version": "0.1.1",
    "project_urls": {
        "Documentation": "https://github.com/spencerwooo/torch-featurelayer/blob/main/README.md",
        "Repository": "https://github.com/spencerwooo/torch-featurelayer"
    },
    "split_keywords": [
        "torch",
        " pytorch",
        " torchvision",
        " feature-extraction"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cdf590af9f8d9258aa03509c31ebbea50fbd962b9c13baadb4d91a3beddb9de3",
                "md5": "ec3e29e70a04c4a2dbf151501192fe3a",
                "sha256": "970bd6a58c4854636d42b0d769b25a795ff187d49efd22c63cdc01f38de9a144"
            },
            "downloads": -1,
            "filename": "torch_featurelayer-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ec3e29e70a04c4a2dbf151501192fe3a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.13,>=3.10",
            "size": 7163,
            "upload_time": "2024-04-15T09:09:31",
            "upload_time_iso_8601": "2024-04-15T09:09:31.926893Z",
            "url": "https://files.pythonhosted.org/packages/cd/f5/90af9f8d9258aa03509c31ebbea50fbd962b9c13baadb4d91a3beddb9de3/torch_featurelayer-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "df78ae8b38a166a50ba18eee8c860b4c27f5f515d202f44d614ac775221dbea8",
                "md5": "42be29ab09876d8eda493494cef32081",
                "sha256": "ce8e0e36051fd299686802790a396770f15000f3a5a83bc02eaafc23fe4b7551"
            },
            "downloads": -1,
            "filename": "torch_featurelayer-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "42be29ab09876d8eda493494cef32081",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.13,>=3.10",
            "size": 6073,
            "upload_time": "2024-04-15T09:09:32",
            "upload_time_iso_8601": "2024-04-15T09:09:32.819007Z",
            "url": "https://files.pythonhosted.org/packages/df/78/ae8b38a166a50ba18eee8c860b4c27f5f515d202f44d614ac775221dbea8/torch_featurelayer-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-15 09:09:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "spencerwooo",
    "github_project": "torch-featurelayer",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "torch-featurelayer"
}
        
Elapsed time: 4.70430s