Skip to main content

TouchHook: A PyTorch hook management library

Project description

TorchHook Logo

TorchHook

PyPI version License: MIT Downloads Python Version

English Blog | 中文博客 | 中文文档

TorchHook is a lightweight, easy-to-use Python library designed to simplify the process of extracting intermediate features from PyTorch models. It provides a clean API to manage PyTorch hooks for capturing layer outputs without the boilerplate code.

Key Features

  • Easy Hook Registration: Quickly register hooks for desired model layers by name or object.
  • Flexible Feature Extraction: Retrieve captured features easily.
  • Highly Customizable: Define custom hook logic or output transformations.
  • Resource Management: Automatic cleanup of registered hooks.

Installation

pip install torchhook

Or install from the local source:

git clone https://github.com/zzaiyan/TorchHook.git
cd TorchHook
pip install .

Quick Start

import torch
import torchvision.models as models
from torchhook import HookManager

# 1. Load your model
model = models.resnet18()
model.eval()

# 2. Initialize HookManager
hook_manager = HookManager(model, max_size=1) # Keep only the latest feature per hook

# 3. Register layers
hook_manager.add(layer_name='conv1')
hook_manager.add(layer_name='layer4.1.relu')
hook_manager.add(layer_name='fully_connected', layer=model.fc) # Optional: pass layer object

# 4. Forward pass
dummy_input = torch.randn(1, 3, 224, 224)
with torch.no_grad():
    output = model(dummy_input)

# 5. Get features
features_conv1 = hook_manager.get('conv1')
features_relu = hook_manager.get('layer4.1.relu')
all_features = hook_manager.get_all() # Get all features as a dict

print(f"Conv1 feature shape: {features_conv1[0].shape}")
print(f"Layer 4.1 ReLU feature shape: {features_relu[0].shape}")

# 6. Summary (Optional)
hook_manager.summary()

# 7. Clean up hooks (Important!)
hook_manager.clear_hooks()

For advanced usage like custom hooks and output transformations, please refer to the blog posts: English | 中文

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchhook-0.2.6.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchhook-0.2.6-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file torchhook-0.2.6.tar.gz.

File metadata

  • Download URL: torchhook-0.2.6.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for torchhook-0.2.6.tar.gz
Algorithm Hash digest
SHA256 7f767cb9aa6fd091ae7c74b0b5fc9778eae90699d9c92952920b1619799b9847
MD5 ab286b23555adb84c43d686dc14b8d21
BLAKE2b-256 fd3879ae9dbe030b648f6f2982f08fa9ff0681eda8aabf58dfb28a54b3c82b6c

See more details on using hashes here.

File details

Details for the file torchhook-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: torchhook-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for torchhook-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7848b8d8449846ccfc4ca95c49cd82d20f850a068df27eac8f9de1493c676d7c
MD5 85c00a9a2ebd3c4923b6356c8973a9ab
BLAKE2b-256 de4870e13b11f21297d95fa031b1ded07700f4a573ddf2af39720aa15426f89d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page