赞
踩
LPIPS是一种用于评价图像之间的感知相似性的指标,它是基于深度网络特征的线性加权距离度量。它的全称是Learned Perceptual Image Patch Similarity,即学习的感知图像块相似性。
LPIPS的计算方法是将两个输入图像送入一个预训练的深度网络(如VGG、AlexNet、SqueezeNet等),提取每个层的输出特征,并进行归一化处理。然后对每个层的特征进行线性加权,并计算L2距离,最后取平均得到LPIPS值。公式如下:
使用这个指标需要安装lpips包,首先请确保你安装了pytorch。
当使用torch<1.8时,此度量是不可编写脚本的。如果报错,请更新您的pytorch安装。
以pip-install torchmetrics[image]或pip-install-lips的形式安装
楼主用的是:
pip install lpips #安装lpips
LPIPS接受以下输入:
img1
(Tensor): tensor with images of shape (N, 3, H, W)
img2
(Tensor): tensor with images of shape (N, 3, H, W)
正向和计算的输出,LPIPS返回以下输出:
lpips
(Tensor): returns float scalar tensor with average LPIPS value over samples
如:
>>>
>>> import torch >>> _ = torch.manual_seed(123) >>> from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity >>> lpips = LearnedPerceptualImagePatchSimilarity(net_type='vgg') >>> # LPIPS needs the images to be in the [-1, 1] range. >>> img1 = (torch.rand(10, 3, 100, 100) * 2) - 1 >>> img2 = (torch.rand(10, 3, 100, 100) * 2) - 1 >>> lpips(img1, img2) tensor(0.3493, grad_fn=<SqueezeBackward0>)
- import torch
- from torchvision import transforms
- from PIL import Image
- from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
- # 加载图像
- _ = torch.manual_seed(123)
- lpips = LearnedPerceptualImagePatchSimilarity(net_type='vgg') #可为‘vgg','alex',’squeeze'
- image_path1 = " " # 替换为您的图像路径
- image = Image.open(image_path1)
-
- image_path2 = " " # 替换为您的图像路径
- image2 = Image.open(image_path2)
- # 转换为张量
- transform = transforms.ToTensor()
- img1 = transform(image)
- img2 = transform(image2)
- image_mask = torch.randn(1,3,512,512)
- mask = torch.zeros_like(image_mask)
- img1 = mask.copy_(img1).repeat(1, 1, 1, 1)
- img2 = mask.copy_(img2).repeat(1, 1, 1, 1)
- img1 = (img1 * 2) - 1
- img2 = (img2 * 2) - 1
-
- print(lpips(img1, img2))
以上就是全部内容
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。