-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
216 lines (184 loc) · 7.54 KB
/
metrics.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import numpy as np
from scipy.signal import convolve2d
from skimage.measure import compare_psnr, compare_ssim
def compare_ergas(x_true, x_pred, ratio):
"""
Calculate ERGAS, ERGAS offers a global indication of the quality of fused image.The ideal value is 0.
:param x_true:
:param x_pred:
:param ratio: upsample ratio
:return:
"""
x_true, x_pred = img_2d_mat(x_true=x_true, x_pred=x_pred)
sum_ergas = 0
for i in range(x_true.shape[0]):
vec_x = x_true[i]
vec_y = x_pred[i]
err = vec_x - vec_y
r_mse = np.mean(np.power(err, 2))
tmp = r_mse / (np.mean(vec_x) ** 2)
sum_ergas += tmp
return (100 / ratio) * np.sqrt(sum_ergas / x_true.shape[0])
def compare_sam(x_true, x_pred):
"""
:param x_true: [H, W, C
:param x_pred: [H, W, C]
:return: sam_deg
"""
num = 0
sum_sam = 0
x_true, x_pred = x_true.astype(np.float64), x_pred.astype(np.float64)
for x in range(x_true.shape[0]):
for y in range(x_true.shape[1]):
tmp_pred = x_pred[x, y].ravel()
tmp_true = x_true[x, y].ravel()
if np.linalg.norm(tmp_true) != 0 and np.linalg.norm(tmp_pred) != 0:
sum_sam += np.arccos(
np.inner(tmp_pred, tmp_true) / (np.linalg.norm(tmp_true) * np.linalg.norm(tmp_pred)))
num += 1
sam_deg = (sum_sam / num) * 180 / np.pi
return sam_deg
def compare_corr(x_true, x_pred):
"""
Calculate the cross correlation between x_pred and x_true.
CC is a spatial measure.
"""
x_true, x_pred = img_2d_mat(x_true=x_true, x_pred=x_pred)
x_true = x_true - np.mean(x_true, axis=1).reshape(-1, 1)
x_pred = x_pred - np.mean(x_pred, axis=1).reshape(-1, 1)
numerator = np.sum(x_true * x_pred, axis=1).reshape(-1, 1)
denominator = np.sqrt(np.sum(x_true * x_true, axis=1) * np.sum(x_pred * x_pred, axis=1)).reshape(-1, 1)
return (numerator / denominator).mean()
def img_2d_mat(x_true, x_pred):
"""
:param x_true: [H, W, C]
:param x_pred: [H, W, C]
:return: a matrix which shape is [C, H * W]
"""
h, w, c = x_true.shape
x_true, x_pred = x_true.astype(np.float32), x_pred.astype(np.float32)
x_mat = np.zeros((c, h * w), dtype=np.float32)
y_mat = np.zeros((c, h * w), dtype=np.float32)
for i in range(c):
x_mat[i] = x_true[:, :, i].reshape((1, -1))
y_mat[i] = x_pred[:, :, i].reshape((1, -1))
return x_mat, y_mat
def compare_rmse(x_true, x_pred):
"""
Calculate Root mean squared error
:param x_true:
:param x_pred:
:return:
"""
x_true, x_pred = x_true.astype(np.float32), x_pred.astype(np.float32)
return np.linalg.norm(x_true - x_pred) / (np.sqrt(x_true.shape[0] * x_true.shape[1] * x_true.shape[2]))
def compare_mpsnr(x_true, x_pred, data_range):
"""
:param x_true: Input image must have three dimension [H, W, C]
:param x_pred:
:return:
"""
x_true, x_pred = x_true.astype(np.float32), x_pred.astype(np.float32)
channels = x_true.shape[2]
total_psnr = [compare_psnr(im_true=x_true[:, :, k], im_test=x_pred[:, :, k], data_range=data_range)
for k in range(channels)]
return np.mean(total_psnr)
def compare_mssim(x_true, x_pred, data_range, multidimension):
"""
:param x_true:
:param x_pred:
:param data_range:
:param multidimension:
:return:
"""
mssim = [compare_ssim(X=x_true[:, :, i], Y=x_pred[:, :, i], data_range=data_range, multidimension=multidimension)
for i in range(x_true.shape[2])]
return np.mean(mssim)
def compare_sid(x_true, x_pred):
"""
SID is an information theoretic measure for spectral similarity and discriminability.
:param x_true:
:param x_pred:
:return:
"""
x_true, x_pred = x_true.astype(np.float32), x_pred.astype(np.float32)
N = x_true.shape[2]
err = np.zeros(N)
for i in range(N):
err[i] = abs(np.sum(x_pred[:, :, i] * np.log10((x_pred[:, :, i] + 1e-3) / (x_true[:, :, i] + 1e-3))) +
np.sum(x_true[:, :, i] * np.log10((x_true[:, :, i] + 1e-3) / (x_pred[:, :, i] + 1e-3))))
return np.mean(err / (x_true.shape[1] * x_true.shape[0]))
def compare_appsa(x_true, x_pred):
"""
:param x_true:
:param x_pred:
:return:
"""
x_true, x_pred = x_true.astype(np.float32), x_pred.astype(np.float32)
nom = np.sum(x_true * x_pred, axis=2)
denom = np.linalg.norm(x_true, axis=2) * np.linalg.norm(x_pred, axis=2)
cos = np.where((nom / (denom + 1e-3)) > 1, 1, (nom / (denom + 1e-3)))
appsa = np.arccos(cos)
return np.sum(appsa) / (x_true.shape[1] * x_true.shape[0])
def compare_mare(x_true, x_pred):
"""
:param x_true:
:param x_pred:
:return:
"""
x_true, x_pred = x_true.astype(np.float32), x_pred.astype(np.float32)
diff = x_true - x_pred
abs_diff = np.abs(diff)
relative_abs_diff = np.divide(abs_diff, x_true + 1) # added epsilon to avoid division by zero.
return np.mean(relative_abs_diff)
def img_qi(img1, img2, block_size=8):
N = block_size ** 2
sum2_filter = np.ones((block_size, block_size))
img1_sq = img1 * img1
img2_sq = img2 * img2
img12 = img1 * img2
img1_sum = convolve2d(img1, np.rot90(sum2_filter), mode='valid')
img2_sum = convolve2d(img2, np.rot90(sum2_filter), mode='valid')
img1_sq_sum = convolve2d(img1_sq, np.rot90(sum2_filter), mode='valid')
img2_sq_sum = convolve2d(img2_sq, np.rot90(sum2_filter), mode='valid')
img12_sum = convolve2d(img12, np.rot90(sum2_filter), mode='valid')
img12_sum_mul = img1_sum * img2_sum
img12_sq_sum_mul = img1_sum * img1_sum + img2_sum * img2_sum
numerator = 4 * (N * img12_sum - img12_sum_mul) * img12_sum_mul
denominator1 = N * (img1_sq_sum + img2_sq_sum) - img12_sq_sum_mul
denominator = denominator1 * img12_sq_sum_mul
quality_map = np.ones(denominator.shape)
index = (denominator1 == 0) & (img12_sq_sum_mul != 0)
quality_map[index] = 2 * img12_sum_mul[index] / img12_sq_sum_mul[index]
index = (denominator != 0)
quality_map[index] = numerator[index] / denominator[index]
return quality_map.mean()
def compare_qave(x_true, x_pred, block_size=8):
n_bands = x_true.shape[2]
q_orig = np.zeros(n_bands)
for idim in range(n_bands):
q_orig[idim] = img_qi(x_true[:, :, idim], x_pred[:, :, idim], block_size)
return q_orig.mean()
def quality_assessment(x_true, x_pred, data_range, ratio, multi_dimension=False, block_size=8):
"""
:param multi_dimension:
:param ratio:
:param data_range:
:param x_true:
:param x_pred:
:param block_size
:return:
"""
result = {'MPSNR' : compare_mpsnr(x_true=x_true, x_pred=x_pred, data_range=data_range),
'MSSIM' : compare_mssim(x_true=x_true, x_pred=x_pred, data_range=data_range, multidimension=multi_dimension),
'ERGAS' : compare_ergas(x_true=x_true, x_pred=x_pred, ratio=ratio),
'SAM' : compare_sam(x_true=x_true, x_pred=x_pred),
# 'SID': compare_sid(x_true=x_true, x_pred=x_pred),
'CrossCorrelation': compare_corr(x_true=x_true, x_pred=x_pred),
'RMSE' : compare_rmse(x_true=x_true, x_pred=x_pred),
"UIQI": compare_qave(x_true=x_true, x_pred=x_pred, block_size=block_size),
# 'APPSA': compare_appsa(x_true=x_true, x_pred=x_pred),
# 'MARE': compare_mare(x_true=x_true, x_pred=x_pred),
# "QAVE": compare_qave(x_true=x_true, x_pred=x_pred, block_size=block_size)
}
return result