Skip to content

Commit 99275f6

Browse files
authored
Add files via upload
1 parent 2e05ee0 commit 99275f6

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

evaluation.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import glob
2+
import os
3+
import time
4+
from collections import OrderedDict
5+
6+
import numpy as np
7+
import torch
8+
import cv2
9+
import argparse
10+
11+
from natsort import natsort
12+
from skimage.metrics import structural_similarity as ssim
13+
from skimage.metrics import peak_signal_noise_ratio as psnr
14+
import lpips
15+
16+
17+
class Measure():
18+
def __init__(self, net='alex', use_gpu=False):
19+
self.device = 'cuda' if use_gpu else 'cpu'
20+
self.model = lpips.LPIPS(net=net)
21+
self.model.to(self.device)
22+
23+
def measure(self, imgA, imgB):
24+
return [float(f(imgA, imgB)) for f in [self.psnr, self.ssim, self.lpips]]
25+
26+
def lpips(self, imgA, imgB, model=None):
27+
tA = t(imgA).to(self.device)
28+
tB = t(imgB).to(self.device)
29+
dist01 = self.model.forward(tA, tB).item()
30+
return dist01
31+
32+
def ssim(self, imgA, imgB):
33+
# multichannel: If True, treat the last dimension of the array as channels. Similarity calculations are done independently for each channel then averaged.
34+
score, diff = ssim(imgA, imgB, full=True, multichannel=True)
35+
return score
36+
37+
def psnr(self, imgA, imgB):
38+
psnr_val = psnr(imgA, imgB)
39+
return psnr_val
40+
41+
42+
def t(img):
43+
def to_4d(img):
44+
assert len(img.shape) == 3
45+
assert img.dtype == np.uint8
46+
img_new = np.expand_dims(img, axis=0)
47+
assert len(img_new.shape) == 4
48+
return img_new
49+
50+
def to_CHW(img):
51+
return np.transpose(img, [2, 0, 1])
52+
53+
def to_tensor(img):
54+
return torch.Tensor(img)
55+
56+
return to_tensor(to_4d(to_CHW(img))) / 127.5 - 1
57+
58+
59+
def fiFindByWildcard(wildcard):
60+
return natsort.natsorted(glob.glob(wildcard, recursive=True))
61+
62+
63+
def imread(path):
64+
return cv2.imread(path)[:, :, [2, 1, 0]]
65+
66+
67+
def format_result(psnr, ssim, lpips):
68+
return f'{psnr:0.2f}, {ssim:0.3f}, {lpips:0.3f}'
69+
70+
def measure_dirs(dirA, dirB, use_gpu, verbose=False):
71+
if verbose:
72+
vprint = lambda x: print(x)
73+
else:
74+
vprint = lambda x: None
75+
76+
77+
t_init = time.time()
78+
79+
paths_A = fiFindByWildcard(os.path.join(dirA, f'*.{type}'))
80+
paths_B = fiFindByWildcard(os.path.join(dirB, f'*.{type}'))
81+
82+
vprint("Comparing: ")
83+
vprint(dirA)
84+
vprint(dirB)
85+
86+
measure = Measure(use_gpu=use_gpu)
87+
88+
results = []
89+
for pathA, pathB in zip(paths_A, paths_B):
90+
result = OrderedDict()
91+
92+
t = time.time()
93+
result['psnr'], result['ssim'], result['lpips'] = measure.measure(imread(pathA), imread(pathB))
94+
d = time.time() - t
95+
vprint(f"{pathA.split('/')[-1]}, {pathB.split('/')[-1]}, {format_result(**result)}, {d:0.1f}")
96+
97+
results.append(result)
98+
99+
psnr = np.mean([result['psnr'] for result in results])
100+
ssim = np.mean([result['ssim'] for result in results])
101+
lpips = np.mean([result['lpips'] for result in results])
102+
103+
vprint(f"Final Result: {format_result(psnr, ssim, lpips)}, {time.time() - t_init:0.1f}s")
104+
105+
106+
if __name__ == "__main__":
107+
parser = argparse.ArgumentParser()
108+
parser.add_argument('-dirA', default='D:/NCHU/paper submit/ICIP 2022/evaluation/gt_mit', type=str)
109+
parser.add_argument('-dirB', default='D:/NCHU/paper submit/ICIP 2022/evaluation/enhanced_mit', type=str)
110+
parser.add_argument('-type', default='png')
111+
parser.add_argument('--use_gpu', default=True)
112+
args = parser.parse_args()
113+
114+
dirA = args.dirA
115+
dirB = args.dirB
116+
type = args.type
117+
use_gpu = args.use_gpu
118+
119+
if len(dirA) > 0 and len(dirB) > 0:
120+
measure_dirs(dirA, dirB, use_gpu=use_gpu, verbose=True)

0 commit comments

Comments
 (0)