TorchHook
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 | 中文
Metadata
Release files for TorchHook 0.2.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| TorchHook-0.2.7.tar.gz | 9.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| TorchHook-0.2.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.8 kB
Release files / TorchHook-0.2.7.tar.gz
| Download URL | TorchHook-0.2.7.tar.gz |
|---|---|
| Size | 9.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
51d578f8704c95f234c89fe6d2eafc62673d52212c0c82eb14b512bcdf446e4a
|
|
BLAKE2b-256 checksum How to use checksums |
ccc2b7ccb3b95c8c65604ab8bae95b0221823aa005fffaa3fd9859be04b62e7b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.18
|
Release files / TorchHook-0.2.7-py3-none-any.whl
| Download URL | TorchHook-0.2.7-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
71c0e3ebf6e9ac377f1cacebeb1244f3071a6d85aa72dc06892af99f0631ab0d
|
|
BLAKE2b-256 checksum How to use checksums |
75ba17277d7a3934fbcb97eac8870aa153ce9957086e0562c84e4c49d13511db
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.18
|