Python计算图像评价指标--Lpips

https://github.com/richzhang/PerceptualSimilarity

源代码链接如上

我的方法是首先安装lpips包

pip install lpips

安装成功之后,新建.py文件命名为lpips1文件

# coding=gbk
import cv2
import lpips
import torchvision.transforms as transforms
import torch

# 载入指标模型
loss_fn_alex = lpips.LPIPS(net='alex')  # best forward scores
loss_fn_vgg = lpips.LPIPS(net='vgg')  # closer to "traditional" perceptual loss, when used for optimization

# 读取图片
test1 = cv2.imread('1.png')
test2 = cv2.imread('2.png')
# 分割目标图片
test1_org = test1[:, :512, :] / 255  # 原始图片
test1_res = test1[:, 512:1024, :] / 255  # 模型输出结果
test1_label = test1[:, 1024:1536, :] / 255  # label图片

test2_org = test2[:, :512, :] / 255
test2_res = test2[:, 512:1024, :] / 255
test2_label = test2[:, 1024:1536, :] / 255

# 转为tensor
transf = transforms.ToTensor()

test1_org = transf(test1_org)
test1_res = transf(test1_res)
test1_label = transf(test1_label)
test2_org = transf(test2_org)
test2_res = transf(test2_res)
test2_label = transf(test2_label)

# 转换数据类型
test1_orgg = test1_org.to(torch.float32)
test1_ress = test1_res.to(torch.float32)
test1_labell = test1_label.to(torch.float32)
test2_orgg = test2_org.to(torch.float32)
test2_ress = test2_res.to(torch.float32)
test2_labell = test2_label.to(torch.float32)

# 测试
d11 = loss_fn_alex(test1_ress, test1_labell)
d12 = loss_fn_alex(test1_ress, test2_labell)
print('d11:', d11)
print('d12:', d12)

d22 = loss_fn_alex(test2_ress, test2_labell)
d21 = loss_fn_alex(test2_ress, test1_labell)
print('d22:', d22)
print('d121:', d21)

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值