My personal library of reusable Lightning components
Project description
lit-wsl
My personal library of reusable Pytorch Lightning components
Features
Installation
With pip:
python -m pip install lit-wsl
With uv:
uv add lit-wsl
How to use it
IntermediateLayerGetter
Capture intermediate layer outputs during forward pass:
import torch
from torchvision.models import resnet18
from lit_wsl.models.intermediate_layer_getter import IntermediateLayerGetter
model = resnet18(pretrained=True)
# Specify which layers to capture: {layer_name: output_name}
return_layers = {"layer2": "feat1", "layer4": "feat2"}
layer_getter = IntermediateLayerGetter(model, return_layers, keep_output=True)
x = torch.randn(1, 3, 224, 224)
intermediate_outputs, final_output = layer_getter(x)
# intermediate_outputs is OrderedDict with keys "feat1" and "feat2"
print(intermediate_outputs["feat1"].shape) # torch.Size([1, 128, 28, 28])
WeightRenamer
Rename keys in checkpoint files:
from lit_wsl.models.weight_renamer import WeightRenamer
# Load checkpoint
renamer = WeightRenamer("old_model.pth")
# Remove common prefix
renamer.remove_prefix("model.")
# Rename specific keys
renamer.rename_keys({
"backbone.conv1": "encoder.conv1",
"head.fc": "classifier.fc"
})
# Save modified checkpoint
renamer.save("renamed_model.pth")
WeightMapper
Automatically map weights between different model architectures:
import torch
from lit_wsl.mapper.weight_mapper import WeightMapper
from lit_wsl.models.weight_renamer import WeightRenamer
# Define your models (with different layer names)
old_model = OldModelArchitecture()
new_model = NewModelArchitecture()
# Analyze and suggest mapping
mapper = WeightMapper(old_model, new_model)
mapping, unmatched = mapper.suggest_mapping(threshold=0.6)
# Apply mapping to checkpoint
renamer = WeightRenamer("old_weights.pth")
renamer.rename_keys(mapping)
renamer.save("adapted_weights.pth")
# Load adapted weights
new_model.load_state_dict(torch.load("adapted_weights.pth"))
Docs
uv run mkdocs build -f ./mkdocs.yml -d ./_build/
Update template
copier update --trust -A --vcs-ref=HEAD
Credits
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
lit_wsl-0.3.3.tar.gz
(54.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
lit_wsl-0.3.3-py3-none-any.whl
(61.8 kB
view details)
File details
Details for the file lit_wsl-0.3.3.tar.gz.
File metadata
- Download URL: lit_wsl-0.3.3.tar.gz
- Upload date:
- Size: 54.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
afa33b188241dbeeeed8a2067d6d9c20ccdf2b381e78e85ff5a117c792abad03
|
|
| MD5 |
ae4616176ad0e925b2bf4134d70a23b2
|
|
| BLAKE2b-256 |
961a5b492e2eb18b6794b8d3ae7033f3e624943ae4cd6840fe89787a4ddd64bc
|
File details
Details for the file lit_wsl-0.3.3-py3-none-any.whl.
File metadata
- Download URL: lit_wsl-0.3.3-py3-none-any.whl
- Upload date:
- Size: 61.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73589d88c04d7cbfdf4fa0341897d10b880e07583a791761cbda594a552f60f4
|
|
| MD5 |
984f44b0db54dd97de8937e2313fbc03
|
|
| BLAKE2b-256 |
23c131827d8f69625a1018a4a5b2637869cc0b133b6fb6870d94f79b5d2c7f91
|