GAN评价指标代码(FID、LPIPS、MS-SSIM

  • 写在前面
    • FID
    • LPIPS
    • MS-SSIM
  • 写在后面

写在前面

科研需要,对GAN生成的图片要做定量评价,因此总结一些自己要用到的一些评价指标。

FID

官方链接:https://github.com/mseitzer/pytorch-fid

描述:FID越小,表示生成图片越多样、质量越好。

步骤:
(1)先下载pytorch-fid

pip install pytorch-fid

(2)执行命令

python -m pytorch_fid path/dataset1/test/images path/dataset2/test/images

参数:
path/dataset1/test/images是真实图像路径
path/dataset2/test/images是生成图像路径

注意:
有个额外的参数--dims N,其中 N 是特征的维度,一共有64、192、768、2048(默认)这四种,表示的是使用Inception的哪一层去测试,一般默认都是2048即最终平均池化特征去测试。
但是官方说如果测试的样本少于2048个可以用–dims这个参数,但这会改变FID分数的大小,因此只有–dims相同时跑出来的分数才有可比性。
不过我看TensorFlow那边的FID说,建议使用10,000的最小样本量来计算 FID,否则会低估生成器的真实FID。所以如果样本量太少建议不使用FID这个指标。

LPIPS

官方链接:https://github.com/richzhang/PerceptualSimilarity
描述:LPIPS越高意味着图片与原图更多不同,越低意味着与原图更相似

步骤:
(1)下载LPIPS代码
(2)执行命令

python lpips_2dirs.py -d0 imgs/ex_dir0 -d1 imgs/ex_dir1 -o imgs/example_dists.txt --use_gpu

参数:
-d0是真实图像路径
-d1是生成图像路径
-o是输出的结果保存位置

MS-SSIM

链接:https://blog.csdn.net/m0_63642362/article/details/123297405
描述:接近1的值表示更好的图像质量,接近0的值表示较差的质量

代码
把生成图片放在imgs/fake_img路径下,把真实图片放在imgs/real_img路径下,然后运行即可

import os
import cv2
import paddle
from paddle_msssim import ssim, ms_ssim
def file_name(file_dir):
    img_path_list = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
             img_path_list.append((os.path.join(root, file),file))
    return img_path_list
def imread(img_path):
    img = cv2.imread(img_path)
    return paddle.to_tensor(img.transpose(2, 0, 1)[None, ...], dtype=paddle.float32)
if __name__ == '__main__':
    file_dir = 'imgs/fake_img'  # 伪造图像路径
    target_dir = 'imgs/real_img' # 真实图像路径
    img_path_list = file_name(file_dir)
    target_path_list = file_name(target_dir)
    d = 0
    for i in range(img_path_list.__len__()):
        (img_path, img_name) = img_path_list[i]
        (target_path, target_name) = target_path_list[i]
        print(img_path)
        print(target_path)
        fake = imread(img_path)
        real = imread(target_path)
        distance = ms_ssim(real, fake).cpu().numpy()
        print(distance)
        d += distance
print('average ms_ssim')
print(d/img_path_list.__len__())

写在后面

你笑我徒劳,而我一个人在路上,走完的四季,确实你永远体会不到的风景。
-----《愿有人陪你颠沛流离》

GAN评价指标代码(FID、LPIPS、MS-SSIM)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。