Differentiable SSIM, frozen in super-old PyTorch
It turns the SSIM image-quality metric into a differentiable PyTorch loss so you can backpropagate perceptual similarity.

What it does
The repo provides a pytorch_ssim module that computes the Structural Similarity Index between two images and exposes it as both a functional call and a torch.nn.Module. Because the implementation is differentiable, it can be dropped into an optimizer loop as a loss function that rewards perceptual likeness rather than raw pixel distance. The README demonstrates this by starting from random noise and running gradient descent until the result resembles an Einstein portrait.
The interesting bit Back when it worked, the trick was simply making SSIM differentiable: most classic metrics are evaluation-only, but treating perceptual similarity as a loss opens the door to tasks like image reconstruction and denoising. The included animation shows random pixels slowly resolving into a recognizable face via pure SSIM optimization.
Key highlights
- Exposes both
pytorch_ssim.ssim()and a reusableSSIMmodule. - Differentiable, so it supports
.backward()and standard PyTorch optimizers. - GPU-ready: the examples pass tensors to
cuda()if available. - Demonstrates optimization-by-perception by maximizing SSIM against a reference image.
Caveats
- Explicitly unmaintained; the author states it no longer works on modern PyTorch.
- Uses deprecated patterns like
Variableand.data[0], so expect breakage on current versions. - No recent commits or compatibility fixes; treat it as reference code rather than a dependency.
Verdict Worth a skim if you are writing your own differentiable SSIM loss and want a minimal conceptual template. Otherwise, look elsewhere for production-ready perceptual losses.
Frequently asked
- What is Po-Hsun-Su/pytorch-ssim?
- It turns the SSIM image-quality metric into a differentiable PyTorch loss so you can backpropagate perceptual similarity.
- Is pytorch-ssim open source?
- Yes — Po-Hsun-Su/pytorch-ssim is an open-source project tracked on heatdrop.
- What language is pytorch-ssim written in?
- Po-Hsun-Su/pytorch-ssim is primarily written in Python.
- How popular is pytorch-ssim?
- Po-Hsun-Su/pytorch-ssim has 1.9k stars on GitHub.
- Where can I find pytorch-ssim?
- Po-Hsun-Su/pytorch-ssim is on GitHub at https://github.com/Po-Hsun-Su/pytorch-ssim.