SenseCraft
Perceptual and signal losses for image and video models, with our own feature nets, a compose layer that returns the gradient at the prediction directly, and a measured precision and speed record for every term.
Work in progress.
Install
pip install sensecraft # torch, safetensors, pytorch-msssim
pip install "sensecraft[dinov3]" # adds huggingface_hub for the DINOv3 terms
Python 3.13. Pretrained weights are converted once to safetensors under ~/.cache/sensecraft
(SENSECRAFT_CACHE moves it). VGG, AlexNet and SqueezeNet come from the torchvision URLs; the LPIPS
heads ship in the package. DINOv3 checkpoints are gated on Hugging Face, so their first use needs a
token.
Use
import torch
from sensecraft import Loss, Term
loss = Loss(
[
Term("mse"),
Term("lpips", 0.5, net="vgg"),
Term("dino_vit", 250, model="base", use_gram=False),
Term("ssim", monitor=True), # logged, not in the total
],
dims=2, # 3 for (B, T, C, H, W) video
input_range=(-1, 1),
).prepare("cuda", torch.bfloat16, torch.channels_last, compile="term")
out = loss(fake, real) # out.total, out.terms
out = loss.value_and_seed(fake, real) # also out.seed = d total / d fake, no graph kept
- Each term reads its images in the range it was trained for;
Lossconverts frominput_range. - Image terms run per frame on video; video terms see the clip.
prepareputs the feature nets in the compute dtype and layout and compiles by scope:off,termorloss. Distances stay in fp32.- Terms that need fp32 (FFT, SSIM) run with autocast off.
- Nothing reads the host: totals and terms stay on the device.
Metrics on the same units, without gradient:
from sensecraft.metrics import lpips, ms_ssim, psnr, ssim
psnr(fake, real, data_range=2.0)
Terms
| Group | Names |
|---|---|
| Perceptual | lpips (vgg, alex, squeeze), dino_vit, dino_convnext, convnext |
| Pixel | mse, l1, huber, smooth_l1, rmse, mape, smape, log_cosh, quantile, cauchy, geman_mcclure, welsch, tukey, wing, charbonnier, gaussian_noise |
| Frequency | fft, patch_fft |
| Edge | sobel, laplacian, canny, gradient, high_freq, multi_scale_gradient, structure_tensor |
| Structure | ssim, ms_ssim |
| Video | ssim3d, stssim, tssim, fdb, temporal_accel, temporal_fft, patch_fft_3d, temporal_gradient |
A new term is an nn.Module with forward(input, target) -> scalar, registered with
@register(name, Range.UNIT, dims="2d", fp32=False).
Layout
src/sensecraft/
nets/ feature nets, features only: VGG16, AlexNet, SqueezeNet, ConvNeXt, DINOv3 ViT and ConvNeXt
weights/ upstream sources, conversion to cached safetensors, the LPIPS heads
distance/ LPIPS and feature distances, in fp32
perceptual/ terms: a net and a distance
pixel/ pixel regression terms
signal/ FFT, edge, SSIM and video terms
kernels/ fused ops with plain PyTorch fallbacks
compose.py Term, Loss, value_and_seed
prepare.py dtype, layout and compile scope
metrics.py metrics on device tensors
measure/ the precision and speed chain, see measure/README.md
checks/ behavior checks run by hand, see checks/README.md
Precision
Every perceptual term matches upstream at fp64 (S1, error at the level of fp64 rounding), and every fixed term matches 0.3.13 exactly at fp64 (S5). Against fp64, at the same dtype as upstream:
| Term | dtype | Value gradient cosine, ours | upstream |
|---|---|---|---|
| LPIPS SqueezeNet | fp16 | 0.992 | 0.607 |
| LPIPS VGG | bf16 | 0.988 | 0.985 |
| LPIPS VGG | fp16 | 0.995 | 0.995 |
The LPIPS distance runs in fp32: in fp16 the channel sum of squares overflows, and in bf16 the difference of two close unit vectors cancels. Value error drops 3 to 5x in bf16 and from 28% to 0.02% for SqueezeNet in fp16. DINOv3 rows are measured on random stand-in weights until the gated checkpoints are available.
Changed on purpose against 0.3.13: tssim reads [-1, 1] clips, which its formulas assume; ssim3d
and stssim read [0, 1] clips instead of guessing the range from the input's minimum.
Speed
Batch 32, 256 px, one RTX 5090, bf16, channels-last, compiled per term, forward and the gradient at the prediction:
| Term | 0.3.13 | now |
|---|---|---|
| LPIPS VGG | 29.5 ms | 23.3 ms |
| DINOv3 ViT-B | 21.5 ms | 20.6 ms |
| ConvNeXt small | 8.6 ms | |
| DINOv3 ConvNeXt tiny | 8.1 ms |
LPIPS runs every conv, bias and ReLU as one cuDNN op with an input-only backward, since feature net
weights never take gradients; its heads are a channel sum inside the fused distance, and VGG's pools
keep no indices (peak memory 2.8 to 1.8 GB). LPIPS runs channels-last inside whatever layout it
is given; from NCHW input that is 36 ms otherwise. DINOv3 embeds patches with one matmul. The full
numbers, per-layer floor ratios and the changes that were tried and dropped are in measure/.
License
Apache-2.0. The LPIPS heads and the pretrained nets keep their own licenses; see LICENSE-NOTICES.
Metadata
Release files for sensecraft 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sensecraft-0.4.0.tar.gz | 56.6 kB | With provenance | Details for sensecraft-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sensecraft-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for sensecraft-0.4.0-py3-none-any.whl |
Total release size: 121.0 kB
Release files / sensecraft-0.4.0.tar.gz
| Download URL | sensecraft-0.4.0.tar.gz |
|---|---|
| Size | 56.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
87afb10becc15ca4c9703392660f1edac19151227b17a77c18860bfcdf90d443
|
|
BLAKE2b-256 checksum How to use checksums |
e8cc055ee2efce44ffc9282297085c87022bf159cc4e838e468573e5c5283691
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logRelease files / sensecraft-0.4.0-py3-none-any.whl
| Download URL | sensecraft-0.4.0-py3-none-any.whl |
|---|---|
| Size | 64.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c36819ae0dda07cebdbfd7b03314660ce6ff5b6ed1a38d5c2b7bc195e367a332
|
|
BLAKE2b-256 checksum How to use checksums |
348f232d7ee693b4512ecb602db3d299b0bed5282933cf50c1c276f9edd1f2f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logSecurity for sensecraft 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for sensecraft-0.4.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.