-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiqa_estimator.py
61 lines (44 loc) · 2.09 KB
/
iqa_estimator.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
from os import path
import numpy as np
from keras import backend as K
from scipy.stats import spearmanr
from utils import get_preprocessed_patches
class IQAEstimator:
def __init__(self, model, test_images, num_patches_for_image=8):
self.tid2013_images = []
self.num_patches_for_image = num_patches_for_image
self.test_images = test_images
self.model = model
self._prepare_test_patches()
def full_test_set_eval(self):
for test_img in self.test_images:
test_img['estimated_iqa'] = 0
for img_to_compare in self.test_images:
if test_img != img_to_compare:
test_img['estimated_iqa'] += self._compare_image_patches(
test_img['patches'],
img_to_compare['patches']
)
rho, p_val = spearmanr(
[img['iqa'] for img in self.test_images],
[img['estimated_iqa'] for img in self.test_images]
)
print(f'Spearman rank-order correlation coefficients are: rho {rho}, p-val: {p_val}')
def _prepare_test_patches(self):
for test_img in self.test_images:
test_img['patches'] = get_preprocessed_patches(test_img['path'], self.num_patches_for_image)
def _compare_image_patches(self, patches, test_patches):
patches_batch, test_patches_batch = [], []
for p in patches:
for p_test in test_patches:
patches_batch.append(p)
test_patches_batch.append(p_test)
patches_batch = np.moveaxis(np.array(patches_batch), 1, 3)
test_patches_batch = np.moveaxis(np.array(test_patches_batch), 1, 3)
pred = self.model.predict([patches_batch, test_patches_batch])
# Make average over all image patches
# higher_iqa = sum([p[0] for p in pred]) / len(pred)
# lower_iqa = sum([p[1] for p in pred]) / len(pred)
# return [higher_iqa, lower_iqa]
# The original paper sums ones (one of tested image has higher iqa, zero elsewhere)
return sum([int(p[0] > p[1]) for p in pred])