forked from xahidbuffon/FUnIE-GAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure_ssim_psnr.py
65 lines (54 loc) · 2.1 KB
/
measure_ssim_psnr.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
"""
# > Script for measuring quantitative performances in terms of
# - Structural Similarity Metric (SSIM)
# - Peak Signal to Noise Ratio (PSNR)
# > Maintainer: https://github.com/xahidbuffon
"""
## python libs
import numpy as np
from PIL import Image
from glob import glob
from os.path import join
from ntpath import basename
## local libs
from imqual_utils import getSSIM, getPSNR
## compares avg ssim and psnr
def SSIMs_PSNRs(gtr_dir, gen_dir, im_res=(256, 256)):
"""
- gtr_dir contain ground-truths
- gen_dir contain generated images
"""
gtr_paths = sorted(glob(join(gtr_dir, "*.*")))
gen_paths = sorted(glob(join(gen_dir, "*.*")))
ssims, psnrs = [], []
for gtr_path, gen_path in zip(gtr_paths, gen_paths):
gtr_f = basename(gtr_path).split('.')[0]
gen_f = basename(gen_path).split('.')[0]
if (gtr_f==gen_f):
# assumes same filenames
r_im = Image.open(gtr_path).resize(im_res)
g_im = Image.open(gen_path).resize(im_res)
# get ssim on RGB channels
ssim = getSSIM(np.array(r_im), np.array(g_im))
ssims.append(ssim)
# get psnt on L channel (SOTA norm)
r_im = r_im.convert("L"); g_im = g_im.convert("L")
psnr = getPSNR(np.array(r_im), np.array(g_im))
psnrs.append(psnr)
return np.array(ssims), np.array(psnrs)
"""
Get datasets from
- http://irvlab.cs.umn.edu/resources/euvp-dataset
- http://irvlab.cs.umn.edu/resources/ufo-120-dataset
"""
gtr_dir = "/home/xahid/datasets/released/EUVP/test_samples/GTr/"
#gtr_dir = "/home/xahid/datasets/released/UFO-120/TEST/hr/"
## generated im paths
gen_dir = "eval_data/euvp_test/funie-gan/"
#gen_dir = "eval_data/ufo_test/deep-sesr/"
### compute SSIM and PSNR
SSIM_measures, PSNR_measures = SSIMs_PSNRs(gtr_dir, gen_dir)
print ("SSIM on {0} samples".format(len(SSIM_measures)))
print ("Mean: {0} std: {1}".format(np.mean(SSIM_measures), np.std(SSIM_measures)))
print ("PSNR on {0} samples".format(len(PSNR_measures)))
print ("Mean: {0} std: {1}".format(np.mean(PSNR_measures), np.std(PSNR_measures)))