Create test_metrics/test_psnr_ssim.py
Browse files
basicsr/metrics/test_metrics/test_psnr_ssim.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
from basicsr.metrics import calculate_psnr, calculate_ssim
|
| 5 |
+
from basicsr.metrics.psnr_ssim import calculate_psnr_pt, calculate_ssim_pt
|
| 6 |
+
from basicsr.utils import img2tensor
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def test(img_path, img_path2, crop_border, test_y_channel=False):
|
| 10 |
+
img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
|
| 11 |
+
img2 = cv2.imread(img_path2, cv2.IMREAD_UNCHANGED)
|
| 12 |
+
|
| 13 |
+
# --------------------- Numpy ---------------------
|
| 14 |
+
psnr = calculate_psnr(img, img2, crop_border=crop_border, input_order='HWC', test_y_channel=test_y_channel)
|
| 15 |
+
ssim = calculate_ssim(img, img2, crop_border=crop_border, input_order='HWC', test_y_channel=test_y_channel)
|
| 16 |
+
print(f'\tNumpy\tPSNR: {psnr:.6f} dB, \tSSIM: {ssim:.6f}')
|
| 17 |
+
|
| 18 |
+
# --------------------- PyTorch (CPU) ---------------------
|
| 19 |
+
img = img2tensor(img / 255., bgr2rgb=True, float32=True).unsqueeze_(0)
|
| 20 |
+
img2 = img2tensor(img2 / 255., bgr2rgb=True, float32=True).unsqueeze_(0)
|
| 21 |
+
|
| 22 |
+
psnr_pth = calculate_psnr_pt(img, img2, crop_border=crop_border, test_y_channel=test_y_channel)
|
| 23 |
+
ssim_pth = calculate_ssim_pt(img, img2, crop_border=crop_border, test_y_channel=test_y_channel)
|
| 24 |
+
print(f'\tTensor (CPU) \tPSNR: {psnr_pth[0]:.6f} dB, \tSSIM: {ssim_pth[0]:.6f}')
|
| 25 |
+
|
| 26 |
+
# --------------------- PyTorch (GPU) ---------------------
|
| 27 |
+
img = img.cuda()
|
| 28 |
+
img2 = img2.cuda()
|
| 29 |
+
psnr_pth = calculate_psnr_pt(img, img2, crop_border=crop_border, test_y_channel=test_y_channel)
|
| 30 |
+
ssim_pth = calculate_ssim_pt(img, img2, crop_border=crop_border, test_y_channel=test_y_channel)
|
| 31 |
+
print(f'\tTensor (GPU) \tPSNR: {psnr_pth[0]:.6f} dB, \tSSIM: {ssim_pth[0]:.6f}')
|
| 32 |
+
|
| 33 |
+
psnr_pth = calculate_psnr_pt(
|
| 34 |
+
torch.repeat_interleave(img, 2, dim=0),
|
| 35 |
+
torch.repeat_interleave(img2, 2, dim=0),
|
| 36 |
+
crop_border=crop_border,
|
| 37 |
+
test_y_channel=test_y_channel)
|
| 38 |
+
ssim_pth = calculate_ssim_pt(
|
| 39 |
+
torch.repeat_interleave(img, 2, dim=0),
|
| 40 |
+
torch.repeat_interleave(img2, 2, dim=0),
|
| 41 |
+
crop_border=crop_border,
|
| 42 |
+
test_y_channel=test_y_channel)
|
| 43 |
+
print(f'\tTensor (GPU batch) \tPSNR: {psnr_pth[0]:.6f}, {psnr_pth[1]:.6f} dB,'
|
| 44 |
+
f'\tSSIM: {ssim_pth[0]:.6f}, {ssim_pth[1]:.6f}')
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
if __name__ == '__main__':
|
| 48 |
+
test('tests/data/bic/baboon.png', 'tests/data/gt/baboon.png', crop_border=4, test_y_channel=False)
|
| 49 |
+
test('tests/data/bic/baboon.png', 'tests/data/gt/baboon.png', crop_border=4, test_y_channel=True)
|
| 50 |
+
|
| 51 |
+
test('tests/data/bic/comic.png', 'tests/data/gt/comic.png', crop_border=4, test_y_channel=False)
|
| 52 |
+
test('tests/data/bic/comic.png', 'tests/data/gt/comic.png', crop_border=4, test_y_channel=True)
|