基于pytorch 的psnr和ssim计算:
import math
import numpy as np
from pytorch_msssim import ssim, ms_ssim, SSIM, MS_SSIM
import datetime
import os
import sysdef calc_psnr(sr, hr):diff = (sr - hr) / 255.00mse = diff.pow(2).mean()psnr = -10 * math.log10(mse) return float(psnr)def calc_ssim(sr, hr):# def ssim(# X,# Y,# data_range=255,# size_average=True,# win_size=11,# win_sigma=1.5,# win=None,# K=(0.01, 0.03),# nonnegative_ssim=False,# )ssim_val = ssim(sr, hr, data_range=255, size_average=True)return float(ssim_val)def cur_timestamp_str():#2024-0103-1121now = datetime.datetime.now()year = str(now.year)month = str(now.month).zfill(2)day = str(now.day).zfill(2)hour = str(now.hour).zfill(2)minute = str(now.minute).zfill(2)content = "{}-{}{}-{}{}".format(year, month, day, hour, minute)return content
关于ssim的计算,以及作为损失函数介绍,参考:
https://pypi.org/project/pytorch-msssim/#description