当前位置:   article > 正文

深度学习:GAN所生成fake image如何使用LPIPs评价指标_lpips如何使用

lpips如何使用

一.LPIPS的定义

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>)

示例代码

  1. import torch
  2. from torchvision import transforms
  3. from PIL import Image
  4. from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
  5. # 加载图像
  6. _ = torch.manual_seed(123)
  7. lpips = LearnedPerceptualImagePatchSimilarity(net_type='vgg') #可为‘vgg','alex',’squeeze'
  8. image_path1 = " " # 替换为您的图像路径
  9. image = Image.open(image_path1)
  10. image_path2 = " " # 替换为您的图像路径
  11. image2 = Image.open(image_path2)
  12. # 转换为张量
  13. transform = transforms.ToTensor()
  14. img1 = transform(image)
  15. img2 = transform(image2)
  16. image_mask = torch.randn(1,3,512,512)
  17. mask = torch.zeros_like(image_mask)
  18. img1 = mask.copy_(img1).repeat(1, 1, 1, 1)
  19. img2 = mask.copy_(img2).repeat(1, 1, 1, 1)
  20. img1 = (img1 * 2) - 1
  21. img2 = (img2 * 2) - 1
  22. print(lpips(img1, img2))

以上就是全部内容

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号