Skip to content
This repository was archived by the owner on Jan 8, 2021. It is now read-only.

Commit 625bdb4

Browse files
committed
Another PSNR approach
1 parent 2c6f4df commit 625bdb4

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

psnr.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import torch
2-
from math import log10
32

43

5-
class PSNR(torch.nn.Module):
4+
class PSNR:
5+
"""Peak Signal to Noise Ratio
6+
img1 and img2 have range [0, 255]"""
7+
68
def __init__(self):
7-
super(PSNR, self).__init__()
8-
self.criterion = torch.nn.MSELoss()
9-
def forward(self, prediction, target):
10-
mse = self.criterion(prediction, target)
11-
psnr = 10 * log10(1 / mse.item())
12-
return psnr
9+
self.name = "PSNR"
10+
11+
@staticmethod
12+
def __call__(img1, img2):
13+
mse = torch.mean((img1 - img2) ** 2)
14+
return 20 * torch.log10(255.0 / torch.sqrt(mse))

0 commit comments

Comments
 (0)