1、感知指标:LPIPS 和 FID
在计算机视觉领域,特别是图像生成和图像质量评估中,感知指标被用来衡量生成图像与真实图像之间的视觉相似度。以下是两种常用的感知指标:LPIPS 和 FID。
1.1、LPIPS(越小越好)
LPIPS(Learned Perceptual Image Patch Similarity)
LPIPS 是一种感知相似度度量方法,它试图模拟人类视觉系统对图像差异的感知。LPIPS 不仅仅是基于像素级别的差异(如均方误差 MSE),而是通过学习的方式捕捉图像的更高层次特征,从而更好地反映人眼对图像质量的主观评价。
示例代码:
import torch
from torchvision import transforms
from lpips import LPIPS
from PIL import Image
# 初始化LPIPS模型
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
loss_fn_alex = LPIPS(net='alex').to(device) # 使用AlexNet作为特征提取器
# loss_fn_vgg = LPIPS(net='vgg').to(device) # 使用VGG作为特征提取器
# 图像预处理
preprocess = transforms.Compose([
transforms.Resize((400, 600)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# 准备输入图像
image_path01 = r'your_path/your_image.png'
image_path02 = r'your_path/your_image.png'
image1 = preprocess(Image.open(image_path01)).unsqueeze(0).to(device)
image2 = preprocess(Image.open(image_path02)).unsqueeze(0).to(device)
# 计算LPIPS得分
with torch.no_grad():
dist = loss_fn_alex(image1, image2)
# 输出结果
print('LPIPS score:\n', dist.item())