π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Use separable filter for SSIM calculation to speed up. (#2383)
Dogan Kurt3b386f497547
diff --git a/kornia/metrics/ssim.py b/kornia/metrics/ssim.pyindex 14fe50b5c..e193bf070 100644--- a/kornia/metrics/ssim.py+++ b/kornia/metrics/ssim.py@@ -3,7 +3,7 @@ from typing import Listimport torchimport torch.nn as nn-from kornia.filters import filter2d, get_gaussian_kernel2d+from kornia.filters import filter2d_separable, get_gaussian_kernel1dfrom kornia.filters.filter import _compute_padding@@ -75,19 +75,19 @@ def ssim(raise ValueError(f"img1 and img2 shapes must be the same. Got: {img1.shape} and {img2.shape}")# prepare kernel- kernel: torch.Tensor = get_gaussian_kernel2d((window_size, window_size), (1.5, 1.5))+ kernel: torch.Tensor = get_gaussian_kernel1d(window_size, 1.5, device=img1.device, dtype=img1.dtype)# compute coefficientsC1: float = (0.01 * max_val) ** 2C2: float = (0.03 * max_val) ** 2# compute local mean per channel- mu1: torch.Tensor = filter2d(img1, kernel)- mu2: torch.Tensor = filter2d(img2, kernel)+ mu1: torch.Tensor = filter2d_separable(img1, kernel, kernel)+ mu2: torch.Tensor = filter2d_separable(img2, kernel, kernel)cropping_shape: List[int] = []if padding == 'valid':- height, width = kernel.shape[-2:]+ height = width = kernel.shape[-1]cropping_shape = _compute_padding([height, width])mu1 = _crop(mu1, cropping_shape)mu2 = _crop(mu2, cropping_shape)@@ -98,9 +98,9 @@ def ssim(mu2_sq = mu2**2mu1_mu2 = mu1 * mu2- mu_img1_sq = filter2d(img1**2, kernel)- mu_img2_sq = filter2d(img2**2, kernel)- mu_img1_img2 = filter2d(img1 * img2, kernel)+ mu_img1_sq = filter2d_separable(img1**2, kernel, kernel)+ mu_img2_sq = filter2d_separable(img2**2, kernel, kernel)+ mu_img1_img2 = filter2d_separable(img1 * img2, kernel, kernel)if padding == 'valid':mu_img1_sq = _crop(mu_img1_sq, cropping_shape)