图像计算常用的指标 PSNR, MAE, MSE, SSIM(python 代码)
import numpy as np
import math
def psnr(img1, img2):
mse = np.mean( (img1 - img2) ** 2 )
if mse == 0:
return 100
PIXEL_MAX = 255.0
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
def mse(img1, img2):
mse = np.mean( (img1 - img2) ** 2 )
return mse
def mae(img1, img2):
mae = np.mean( abs(img1 - img2) )
return mae
def ssim(y_true , y_pred):
u_true = np.mean(y_true)
u_pred = np.mean(y_pred)
var_true = np.var(y_true)
var_pred = np.var(y_pred)
std_true = np.sqrt(var_true)
std_pred = np.sqrt(var_pred)
c1 = np.square(0.01*7)
c2 = np.square(0.03*7)
ssim = (2 * u_true * u_pred + c1) * (2 * std_pred * std_true + c2)
denom = (u_true ** 2 + u_pred ** 2 + c1) * (var_pred + var_true + c2)
return ssim/denom
from skimage.measure import compare_ssim as ssim
ssim(img1,img2)
ssim(img1,img1,multichannel=True)
batch train in tensorflow
mse = np.square(x - gx).sum()
def batch_mae_frame_float(gen_frames, gt_frames):
if gen_frames.ndim == 3:
axis = (1, 2)
elif gen_frames.ndim == 4:
axis = (1, 2, 3)
x = np.float32(gen_frames)
y = np.float32(gt_frames)
mae = np.sum(np.absolute(x - y), axis=axis, dtype=np.float32)
return np.mean(mae)
def batch_psnr(gen_frames, gt_frames):
if gen_frames.ndim == 3:
axis = (1, 2)
elif gen_frames.ndim == 4:
axis = (1, 2, 3)
x = np.int32(gen_frames)
y = np.int32(gt_frames)
num_pixels = float(np.size(gen_frames[0]))
mse = np.sum((x - y) ** 2, axis=axis, dtype=np.float32) / num_pixels
psnr = 20 * np.log10(255) - 10 * np.log10(mse)
return np.mean(psnr)
for b in range(configs.batch_size):
score, _ = compare_ssim(gx[b], x[b], full=True, multichannel=True)
sim[i] += score
frame_mse = img_mse[i] / (batch_id * configs.batch_size * configs.n_gpu))
ssim = np.asarray(ssim, dtype=np.float32) / (configs.batch_size * batch_id)
psnr = np.asarray(psnr, dtype=np.float32) / batch_id
fmae = np.asarray(fmae, dtype=np.float32) / batch_id