深度学习:GAN所生成fake image如何使用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>)

示例代码

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

以上就是全部内容

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Inception Score评价GAN网络生成图片质量的Python代码: ```python import tensorflow as tf import numpy as np import os import functools from tensorflow.python.ops import array_ops from tensorflow.python.ops import functional_ops slim = tf.contrib.slim def inception_score(images, num_classes=1000, batch_size=32, splits=10): """ 计算Inception Score :param images: 生成图片的张量,shape为[batch_size, height, width, channels],像素值范围为[0, 255] :param num_classes: Inception网络的类别数,默认为1000 :param batch_size: 计算Inception Score时的batch大小,默认为32 :param splits: 分割数量,默认为10 :return: Inception Score """ assert (images.shape[1] == images.shape[2]) # 保证图片为正方形 inception_images = tf.image.resize_bilinear(images, [299, 299]) inception_images = tf.divide(inception_images, 255.0) logits = [] for i in range(0, images.shape[0], batch_size): batch = inception_images[i:i + batch_size, :, :, :] logit = functional_ops.softmax( functional_ops.in_top_k( predictions=tf.cast(batch, tf.float32), targets=tf.constant(np.arange(num_classes)), k=1) ) logits.append(logit) logits = array_ops.concat(logits, 0) scores = [] for i in range(splits): part = logits[ (i * logits.shape[0] // splits): ((i + 1) * logits.shape[0] // splits), :] kl = part * (tf.log(part) - tf.log(tf.reduce_mean(part, 0, keepdims=True))) kl = tf.reduce_mean(tf.reduce_sum(kl, 1)) scores.append(tf.exp(kl)) return tf.reduce_mean(scores) def get_inception_score(sess, images_ph, fake_images): """ 计算Inception Score :param sess: TensorFlow会话 :param images_ph: 真实图片的占位符 :param fake_images: 生成图片的张量 :return: Inception Score """ assert (fake_images.shape[1] == fake_images.shape[2]) # 保证图片为正方形 fake_images = (fake_images + 1.0) / 2.0 # 将像素值从[-1, 1]转换为[0, 1] num_images = fake_images.shape[0] num_batches = int(np.ceil(num_images / 32)) scores = [] for i in range(num_batches): batch_fake_images = fake_images[i * 32:(i + 1) * 32] score = sess.run(inception_score(images_ph, fake_images=batch_fake_images)) scores.append(score) return np.mean(scores) if __name__ == '__main__': # 在此处定义生成生成的图片张量fake_images # ... # 加载Inception模型 inception_path = os.path.join('inception', 'inception_v3.ckpt') sess = tf.Session() images_ph = tf.placeholder(tf.float32, shape=[None, None, None, 3]) with slim.arg_scope(inception_v3_arg_scope()): _, end_points = inception_v3(images_ph, num_classes=1000, is_training=False) inception_variables = slim.get_variables(scope='InceptionV3') saver = tf.train.Saver(var_list=inception_variables) saver.restore(sess, inception_path) # 计算Inception Score inception_score = get_inception_score(sess, images_ph, fake_images) print('Inception Score:', inception_score) sess.close() ``` 需要注意的是,这里假设生成生成的图片像素值范围为[-1, 1],因此需要先将像素值从[-1, 1]转换为[0, 1]。同时,需要将Inception模型文件(`inception_v3.ckpt`)和代码文件放在同一个目录下。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值