学习感知图像块相似度(Learned Perceptual Image Patch Similarity, LPIPS)也称为“感知损失”(perceptual loss),用于度量两张图像之间的差别。来源于CVPR2018《The Unreasonable Effectiveness of Deep Features as a Perceptual Metric》,该度量标准学习生成图像到Ground Truth的反向映射强制生成器学习从假图像中重构真实图像的反向映射,并优先处理它们之间的感知相似度。LPIPS 比传统方法(比如L2/PSNR, SSIM, FSIM)更符合人类的感知情况。LPIPS的值越低表示两张图像越相似,反之,则差异越大。
d为 x0与x之间的距离。从L层提取特征堆(feature stack)并在通道维度中进行单位规格化(unit-normalize)。利用向量WL 来放缩激活通道数,最终计算L2距离。最后在空间上平均,在通道上求和。
import lpips
class util_of_lpips():
def __init__(self, net, use_gpu=False):
'''
Parameters
----------
net: str
抽取特征的网络,['alex', 'vgg']
use_gpu: bool
是否使用GPU,默认不使用
Returns
-------
References
-------
https://github.com/richzhang/PerceptualSimilarity/blob/master/lpips_2imgs.py
'''
## Initializing the model
self.loss_fn = lpips.LPIPS(net=net)
self.use_gpu = use_gpu
if use_gpu:
self.loss_fn.cuda()
def calc_lpips(self, img1_path, img2_path):
'''
Parameters
----------
img1_path : str
图像1的路径.
img2_path : str
图像2的路径.
Returns
-------
dist01 : torch.Tensor
学习的感知图像块相似度(Learned Perceptual Image Patch Similarity, LPIPS).
References
-------
https://github.com/richzhang/PerceptualSimilarity/blob/master/lpips_2imgs.py
'''
# Load images
img0 = lpips.im2tensor(lpips.load_image(img1_path)) # RGB image from [-1,1]
img1 = lpips.im2tensor(lpips.load_image(img2_path))
if self.use_gpu:
img0 = img0.cuda()
img1 = img1.cuda()
dist01 = self.loss_fn.forward(img0, img1)
return dist01