Fast and differentiable MS-SSIM and SSIM for pytorch.
Slowing down. Last release over 3 years ago, little activity since. Fine for existing code; think twice for a new project.
pip install pytorch-msssimPytorch MS-SSIM
Fast and differentiable MS-SSIM and SSIM for pytorch.
Structural Similarity (SSIM):
Multi-Scale Structural Similarity (MS-SSIM):
Why it is faster than other versions?
Gaussian kernels used in SSIM & MS-SSIM are seperable. A separable filter in image processing can be written as product of two more simple filters. Typically a 2-dimensional convolution operation is separated into two 1-dimensional filters. This reduces the computational costs on an $N\times M$ image with a $m\times n$ filter from $\mathcal{O}(M\cdot N \cdot m \cdot n)$ down to $\mathcal{O}(M\cdot N \cdot (m+n))$. More importantly, seperated kernels are more contiguous and thus cache-friendly than 2-D kernels, which effectively accelerates the computing of SSIM/MS-SSIM.
Update
2020.08.21 (v0.2.1)
3D image support from @FynnBe!
2020.04.30 (v0.2)
Now (v0.2), ssim & ms-ssim can produce consistent results as tensorflow and skimage. A benchmark (pytorch-msssim, tensorflow and skimage) can be found in the Tests section.
Installation
pip install pytorch-msssim
Usage
1. Basic Usage
from pytorch_msssim import ssim, ms_ssim, SSIM, MS_SSIM
# X: (N,3,H,W) a batch of non-negative RGB images (0~255)
# Y: (N,3,H,W)
# calculate ssim & ms-ssim for each image
ssim_val = ssim( X, Y, data_range=255, size_average=False) # return (N,)
ms_ssim_val = ms_ssim( X, Y, data_range=255, size_average=False ) #(N,)
# set 'size_average=True' to get a scalar value as loss. see tests/tests_loss.py for more details
ssim_loss = 1 - ssim( X, Y, data_range=255, size_average=True) # return a scalar
ms_ssim_loss = 1 - ms_ssim( X, Y, data_range=255, size_average=True )
# reuse the gaussian kernel with SSIM & MS_SSIM.
ssim_module = SSIM(data_range=255, size_average=True, channel=3) # channel=1 for grayscale images
ms_ssim_module = MS_SSIM(data_range=255, size_average=True, channel=3)
ssim_loss = 1 - ssim_module(X, Y)
ms_ssim_loss = 1 - ms_ssim_module(X, Y)
ssim=1.0000
ssim=0.4225
ssim=0.1924
left: the original image, right: the reconstructed image