-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcalc_ssim.py
78 lines (57 loc) · 2.4 KB
/
calc_ssim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import torch
import cv2
import numpy as np
def generate_1d_gaussian_kernel():
return cv2.getGaussianKernel(11, 1.5)
def generate_2d_gaussian_kernel():
kernel = generate_1d_gaussian_kernel()
return np.outer(kernel, kernel.transpose())
def generate_3d_gaussian_kernel():
kernel = generate_1d_gaussian_kernel()
window = generate_2d_gaussian_kernel()
return np.stack([window * k for k in kernel], axis=0)
class SSIM():
def __init__(self, device='cpu'):
self.device = device
conv3d = torch.nn.Conv3d(1, 1, (11, 11, 11), stride=1, padding=(5, 5, 5), bias=False, padding_mode='replicate')
conv3d.weight.requires_grad = False
conv3d.weight[0, 0, :, :, :] = torch.tensor(generate_3d_gaussian_kernel())
self.conv3d = conv3d.to(device)
conv2d = torch.nn.Conv2d(1, 1, (11, 11), stride=1, padding=(5, 5), bias=False, padding_mode='replicate')
conv2d.weight.requires_grad = False
conv2d.weight[0, 0, :, :] = torch.tensor(generate_2d_gaussian_kernel())
self.conv2d = conv2d.to(device)
def calc(self, img1, img2):
assert len(img1.shape) == len(img2.shape)
with torch.no_grad():
img1 = torch.tensor(img1).to(self.device).float()
img2 = torch.tensor(img2).to(self.device).float()
if len(img1.shape) == 2:
conv = self.conv2d
elif len(img1.shape) == 3:
conv = self.conv3d
else:
raise not NotImplementedError('only support 2d / 3d images.')
return self._ssim(img1, img2, conv)
def _ssim(self, img1, img2, conv):
img1 = img1.unsqueeze(0).unsqueeze(0)
img2 = img2.unsqueeze(0).unsqueeze(0)
C1 = (0.01 * 255) ** 2
C2 = (0.03 * 255) ** 2
mu1 = conv(img1)
mu2 = conv(img2)
mu1_sq = mu1 ** 2
mu2_sq = mu2 ** 2
mu1_mu2 = mu1 * mu2
sigma1_sq = conv(img1 ** 2) - mu1_sq
sigma2_sq = conv(img2 ** 2) - mu2_sq
sigma12 = conv(img1 * img2) - mu1_mu2
ssim_map = ((2 * mu1_mu2 + C1) *
(2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
(sigma1_sq + sigma2_sq + C2))
return float(ssim_map.mean())
device = 'cpu'
calculator = SSIM(device=device)
tgt = cv2.imread('example/car_gt.jpeg')
inp = cv2.imread('example/car_pred.jpeg')
print(calculator.calc(inp, tgt))