-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmeasure_full.py
199 lines (136 loc) · 4.73 KB
/
measure_full.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
from skimage.metrics import peak_signal_noise_ratio as psnr
import torch
import tqdm
import sys
import os
import numpy as np
import pandas as pd
import scipy.signal
import cv2
from setuptools import glob
from imresize import imresize
if True:
sys.path.insert(0, "./PerceptualSimilarity")
from lpips import lpips
def fiFindByWildcard(wildcard):
return glob.glob(os.path.expanduser(wildcard), recursive=True)
def dprint(d):
out = []
for k, v in d.items():
out.append(f"{k}: {v:0.4f}")
print(", ".join(out))
def t(array):
return torch.Tensor(np.expand_dims(array.transpose([2, 0, 1]), axis=0).astype(np.float32)) / 255
def imread(path):
img = cv2.imread(path, cv2.IMREAD_COLOR)
return img[:, :, [2, 1, 0]]
def lpips_analysis(gt, srs, scale):
from collections import OrderedDict
results = OrderedDict()
gt_img = imread(gt)
h, w, _ = gt_img.shape
gt_img = gt_img[:(h//8)*8, :(w//8)*8]
print("Compare GT", gt, gt_img.shape)
np.random.shuffle(srs)
sr_imgs = [imread(sr) for sr in srs]
for sr_img, sr in zip(sr_imgs, srs):
print(" with SR", sr, sr_img.shape)
n_samples = len(sr_imgs)
lpipses_sp = []
lpipses_gl = []
mses_sp = []
mses_gl = []
kernel = np.ones((16, 16)) / (16 * 16)
for sample_idx in tqdm.tqdm(range(n_samples)):
sr = sr_imgs[sample_idx]
h1, w1, _ = gt_img.shape
sr = sr[:h1, :w1]
# LPIPS
lpips_sp = loss_fn_alex_sp(2 * t(sr) - 1, 2 * t(gt_img) - 1)
lpipses_sp.append(lpips_sp)
lpipses_gl.append(lpips_sp.mean().item())
# MSE
mse_sp_rgb = ((sr * 1.0) - (gt_img * 1.0)) ** 2
assert mse_sp_rgb.shape[2] == 3
mse_sp = mse_sp_rgb.mean(axis=2)
mse_sp = scipy.signal.convolve2d(mse_sp, kernel)
mse_sp = torch.Tensor(mse_sp)
mses_sp.append(mse_sp)
mses_gl.append(mse_sp.mean())
for n in range(1, n_samples + 1):
# LPIPS
lpips_gl = np.min(lpipses_gl[:n])
results[f'LPIPS_mean_n{n}'] = np.mean(lpipses_gl[:n])
lpipses_stacked = torch.stack(
[l[0, 0, :, :] for l in lpipses_sp[: n]], dim=2)
lpips_best_sp, _ = torch.min(lpipses_stacked, dim=2)
lpips_loc = lpips_best_sp.mean().item()
results[f'LPIPS_best_loc_n{n}'] = lpips_loc
lpips_diff = (lpips_gl - lpips_loc)
results[f'LPIPS_diff_n{n}'] = lpips_diff
score = lpips_diff / lpips_gl * 100
results[f'LPIPS_score_n{n}'] = score
# MSE
mse_gl = np.min(mses_gl[:n])
results[f'MSE_mean_n{n}'] = np.mean(mses_gl[:n])
mses_stacked = torch.stack(mses_sp[: n], dim=2)
mse_best_sp, _ = torch.min(mses_stacked, dim=2)
mse_loc = mse_best_sp.mean().item()
results[f'MSE_best_loc_n{n}'] = mse_loc
mse_diff = (mse_gl - mse_loc)
results[f'MSE_diff_n{n}'] = mse_diff
score = mse_diff / mse_gl * 100
results[f'MSE_score_n{n}'] = score
dprint(results)
return results
name, gt_dir, srs_dir, n_samples, scale, n_max = sys.argv[1:]
gt_dir = os.path.expanduser(gt_dir)
srs_dir = os.path.expanduser(srs_dir)
n_samples = int(n_samples)
n_max = int(n_max)
scale = int(scale)
########################################
# Get Paths
########################################
gt_imgs_raw = fiFindByWildcard(os.path.join(gt_dir, '*.png'))
srs_imgs_raw = fiFindByWildcard(os.path.join(srs_dir, '*.png'))
gt_imgs = []
srs_imgs = []
print("Start evaluation")
for img_idx in range(n_max):
gt = os.path.expanduser(os.path.join(gt_dir, f'{901 + img_idx:04d}.png'))
if gt in gt_imgs_raw:
gt_imgs.append(gt)
else:
raise RuntimeError("Not Found: ", gt)
if n_samples > 1:
srs_imgs.append([])
for i in range(n_samples):
off = 0
if os.path.isfile(os.path.join(srs_dir, '000901_sample00000.png')):
off = 901
sr = os.path.join(
srs_dir, f'{off + img_idx:06d}_sample{i:05d}.png')
if sr in srs_imgs_raw:
srs_imgs[-1].append(sr)
else:
raise RuntimeError("Not Found: ", sr)
else:
srs_imgs.append([])
sr = os.path.join(srs_dir, f'{img_idx:06d}.png')
if sr in srs_imgs_raw:
srs_imgs[-1].append(sr)
else:
raise RuntimeError("Not Found: ", sr)
print("Found required images.")
loss_fn_alex_sp = lpips.LPIPS(spatial=True)
results = []
for img_idx in range(n_max):
print(img_idx)
results.append(lpips_analysis(gt_imgs[img_idx], srs_imgs[img_idx], scale))
df = pd.DataFrame(results)
df_mean = df.mean()
df.to_csv(f"./{name}_full.csv")
df_mean.to_csv(f"./{name}_full_mean.csv")
print()
print(df_mean.to_string())