-
Notifications
You must be signed in to change notification settings - Fork 5
/
psnr_ssim.py
59 lines (47 loc) · 1.66 KB
/
psnr_ssim.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
import pytorch_ssim
import torch
from torch.autograd import Variable
import data_loader_test
import math
maxDataNum = 992
def denorm(x):
out = (x + 1) / 2
return out.clamp(0, 1)
def psnr2(input, target):
#print(torch.min(input))
#print(torch.max(input))
input = torch.abs(input - target).cuda()
MSE = torch.mean(input * input)
PSNR = 10 * math.log10((255 * 255) / MSE)
return PSNR
gt_dataset = data_loader_test.InpaintingDataset(root_dir='./demo/gt/')
#gt_dataset = data_loader_test.InpaintingDataset(root_dir='../FaceForensics/frame/RandomHoles/gt/')
gt_loader = torch.utils.data.DataLoader(gt_dataset,
batch_size=1, shuffle=False)
out_dataset = data_loader_test.InpaintingDataset(root_dir='./demo/output/')
out_loader = torch.utils.data.DataLoader(out_dataset,
batch_size=1, shuffle=False)
cnt=0
ssim=0
psnr=0
for i, (sample1,sample2) in enumerate(zip(gt_loader,out_loader)):
gt=Variable(sample1['image']).cuda()
out=Variable(sample2['image']).cuda()
ssim+=pytorch_ssim.ssim(gt,out)
psnr+=psnr2(denorm(gt)*255,denorm(out)*255)
print(cnt/maxDataNum*100)
cnt=cnt+1
ssim=ssim/cnt
psnr=psnr/cnt
print('ssim_average : %.4f'%ssim)
print('psnr_average : %.4f'%psnr)
'''
img1 = Variable(torch.rand(1, 1, 256, 256))
img2 = Variable(torch.rand(1, 1, 256, 256))
if torch.cuda.is_available():
img1 = img1.cuda()
img2 = img2.cuda()
print('pytorch_ssim: %.2f'%pytorch_ssim.ssim(img1, img2))
ssim_loss = pytorch_ssim.SSIM(window_size = 11)
print('ssimloss: %.2f'%ssim_loss(img1, img2))
'''