-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetrics.py
67 lines (50 loc) · 1.5 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
import math
import cv2
import csv
import os
import sys
import glob
import fnmatch
from IQA_pytorch import SSIM, utils
from PIL import Image
import numpy as np
# target folder
target_path = './data/train_check/'
# ground-truth folder
gt_path = './data/test_gt/'
file_list = os.listdir(target_path)
def findFile(filePath, fileName):
ans = None
for f_name in os.listdir(filePath):
fileName = os.path.splitext(fileName)[0]
if fnmatch.fnmatch(f_name, fileName + ".*"):
ans = f_name
if ans == None:
print("There is no Ground-Truth matched with input file")
return ans
def read_img(file_name):
return cv2.imread(file_name)
def cal_PSNR(image):
gt_file = gt_path + str(findFile(gt_path, os.path.basename(image)))
s = read_img(image) / 255.0
r = read_img(gt_file) / 255.0
mse = np.mean(np.square(s - r))
psnr = 10 * math.log10(1 / mse)
return psnr
def cal_SSIM(image):
tg_file = image
gt_file = gt_path + os.path.basename(tg_file)
gt_file = gt_path + str(findFile(gt_path, os.path.basename(tg_file)))
ref = utils.prepare_image(Image.open(gt_file).convert("RGB")).cuda()
dist = utils.prepare_image(Image.open(tg_file).convert("RGB")).cuda()
model = SSIM(channels=3).cuda()
ssim = model(dist, ref, as_loss=False)
return ssim
def PSNR1(ss,rr):
ss = ss.cpu()
rr = rr.cpu()
s = ss.detach().numpy()
r = rr.detach().numpy()
mse = np.mean(np.square(s - r))
psnr = 10 * math.log10(1 / mse)
return psnr