Skip to content

Commit

Permalink
Add multiple layer extract example
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Apr 15, 2024
1 parent bac18b1 commit 504adbe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions examples/extract_multiple_layers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import torch
from torchvision.models import resnet50

from torch_featurelayer import FeatureLayers

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Load a pretrained ResNet-50 model
model = resnet50(weights='DEFAULT').eval().to(device)

# Hook onto layers ['layer1', 'layer2', 'layer3', 'layer4'] of the model
layer_paths = ['layer1', 'layer2', 'layer3', 'layer4']
hooked_model = FeatureLayers(model, layer_paths)

# Forward pass an input tensor through the model
x = torch.randn(1, 3, 224, 224).to(device)
feature_outputs, output = hooked_model(x)

# Print the output shapes
for layer_path, feature_output in feature_outputs.items():
print(f'{layer_path} output shape: {feature_output.shape}')
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file was autogenerated by uv via the following command:
# uv pip compile pyproject.toml
# uv pip compile pyproject.toml -o requirements.txt
filelock==3.13.4
# via torch
fsspec==2024.3.1
Expand Down

0 comments on commit 504adbe

Please sign in to comment.