当前位置:   article > 正文

#保姆级教学 「图像评价指标」(MSE、LPIPS)——理论+代码_lpips loss公式

lpips loss公式
  •  均方误差MSE

给定一个大小为m*n的原图I和生成图K,计算均方误(MSE)定义为:的干净图像和噪声图像,均方误差定义为:   

在这里插入图片描述

  1. #原图为I,生成图为K
  2. #pytorch ——直接调用torch.nn.MSELoss()函数
  3. function = torch.nn.MSELoss()
  4. mse_loss = funciton(I, K)
  5. #tensorflow 1.x
  6. mse_loss = tf.keras.losses.MSE(I, K)
  7. #tensorflow 2.x
  8. mse_loss = tf.losses.MSE(I, K)
  • LPIPS

 学习感知图像块相似度(Learned Perceptual Image Patch Similarity, LPIPS)也称为“感知损失”(perceptual loss),用于度量两张图像之间的差别,来源于论文《The Unreasonable Effectiveness of Deep Features as a Perceptual Metric》。

论文地址:

https://arxiv.org/pdf/1801.03924.pdf

代码地址:

pytorch:https://github.com/richzhang/PerceptualSimilarity

tensorflow:https://github.com/alexlee-gk/lpips-tensorflow

        计算相似度需逐层计算网络输出的对应channel的Cos Distance,然后对得到的distance进行平均(所有层,空间维度),LPIPS主要是把两个Cos Distance作为网络的输入,然后用Cross Entropy Loss训练网络学习2AFC。

        计算xx_{0} 之间的距离d_{0}:给定不同的BaseNet F,首先计算深度嵌入,规格化通道维度中的激活,用向量w缩放每个通道,取L_{2 }距离,然后对空间维度和所有层次求平均。

在这里插入图片描述

        从l层提取特征堆并在通道维度中进行单元标准化。通过w_{l }缩放激活通道维并计算 L_{2 }距离

,在空间上取平均,在层上求和。 

  1. #pytorch 求LPIPS
  2. import torch
  3. import lpips
  4. import os
  5. use_gpu = False # Whether to use GPU
  6. spatial = True # Return a spatial map of perceptual distance.
  7. # Linearly calibrated models (LPIPS)
  8. loss_fn = lpips.LPIPS(net='alex', spatial=spatial) # Can also set net = 'squeeze' or 'vgg'
  9. # loss_fn = lpips.LPIPS(net='alex', spatial=spatial, lpips=False) # Can also set net = 'squeeze' or 'vgg'
  10. if(use_gpu):
  11. loss_fn.cuda()
  12. ## Example usage with dummy tensors
  13. rood_path = r'D:\Project\results\faces'
  14. img0_path_list = []
  15. img1_path_list = []
  16. ## path in net is already exist
  17. '''
  18. for root, _, fnames in sorted(os.walk(rood_path, followlinks=True)):
  19. for fname in fnames:
  20. path = os.path.join(root, fname)
  21. if '_generated' in fname:
  22. im0_path_list.append(path)
  23. elif '_real' in fname:
  24. im1_path_list.append(path)
  25. '''
  26. dist_ = []
  27. for i in range(len(img0_path_list)):
  28. dummy_img0 = lpips.im2tensor(lpips.load_image(img0_path_list[i]))
  29. dummy_img1 = lpips.im2tensor(lpips.load_image(img1_path_list[i]))
  30. if(use_gpu):
  31. dummy_img0 = dummy_img0.cuda()
  32. dummy_img1 = dummy_img1.cuda()
  33. dist = loss_fn.forward(dummy_img0, dummy_img1)
  34. dist_.append(dist.mean().item())
  35. print('Avarage Distances: %.3f' % (sum(dist_)/len(img0_path_list)))

需要注意的是tensorflow版本需要下载.pb数据文件

http://rail.eecs.berkeley.edu/models/lpips/

  1. #tensorflow 求LPIPS
  2. import numpy as np
  3. import tensorflow as tf
  4. import lpips_tf
  5. batch_size = 32
  6. image_shape = (batch_size, 64, 64, 3)
  7. image0 = np.random.random(image_shape) #read real image
  8. image1 = np.random.random(image_shape) #read generate image
  9. image0_ph = tf.placeholder(tf.float32)
  10. image1_ph = tf.placeholder(tf.float32)
  11. distance_t = lpips_tf.lpips(image0_ph, image1_ph, model='net-lin', net='alex')
  12. with tf.Session() as session:
  13. distance = session.run(distance_t, feed_dict={image0_ph: image0, image1_ph: image1})

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/192933
推荐阅读
相关标签
  

闽ICP备14008679号