赞
踩
目录
由于论文需要,我将测试代码进行了修改,融入了LPIPS等指标,关于LPIPS介绍,可以查看博客:
代码中有两大部分需要进行修改,我在代码中进行了标注。
需要安装一些包,如pip install lpips
有疑问欢迎联系交流。
- # -*- coding:utf-8 _*-
- import argparse
- import os
- import cv2
- import pandas as pd
- import lpips
- import torch
- import torchvision.transforms as transforms
- from skimage.metrics import peak_signal_noise_ratio
- from skimage.metrics import structural_similarity
-
- """
- author&wechat:Alocus
- QQ:1913434222
- data:2022.02.27
- """
- #########################需要修改的部分(1)#######################
- #abs_path = r'C:\Users\Administrator\Desktop\test\metric' #绝对路径,存放csv文件位置
- abs_path = os.getcwd()
- test_set_name = r'Result' #用于生成csv结果文件名字
-
- #result_dir = r'C:\Users\Administrator\Desktop\test\metric\haze' #存放结果的文件夹路径及名字
- result_dir = os.path.join(abs_path,'EPDN_result' )
- #GT_dir = r'C:\Users\Administrator\Desktop\test\metric\GT' #存放GT的文件夹路径及名字
- GT_dir = os.path.join(abs_path,'test-label' )
- split = '_' #从result中分割出GT名字的符号,如10222_01_0.8411.png 中第一个符号为_,分割后GT为10222.png。 如何实际二者相同,请选择GT中没有的符号。
- ###########################################################
-
- #存储列表
- result_images =[] #结果图片名字列表
- GT_image = [] #GT图片名字列表
- image_number = [] #图片读取求指标的id
- image_name = [] #图片读取求指标的id 对应的图片名
- psnr_number = [] #psnr值列表
- ssim_number = [] #ssim值列表
- lpips_number = [] #lpips值列表
- for root, _, fnames in sorted(os.walk(result_dir)):
- for fname in fnames:
- #path = os.path.join(root, fname)
- result_images.append(fname) ##结果图片名字列表
-
- for root, _, fnames in sorted(os.walk(GT_dir)):
- for fname in fnames:
- #path = os.path.join(root, fname)
- GT_image.append(fname) #GT图片名字列表
-
- for i in range(len(result_images)):
- print("now"+str(i))
- name_extension = result_images[i] #result图片全名
- (name, extension) = os.path.splitext(name_extension) #分离名字和后缀
-
- ###########拼接出对应的GT中的全名###需要修改的部分(2)#########################
- index = name.find(split) #根据编名方式,提取出GT部分名字结束下标
- if index == -1: #没有找到。则直接按照结果的名字拼接成GT名字
- index = len(name)
- find_name = name[:index] #提取出在GT中对应的图片名
- GT_name = 'label'+find_name[7:11]+".png" #拼接结果
-
- result_name_path =os.path.join(result_dir,name_extension)#拼接result 路径和名字
- GT_name_path =os.path.join(GT_dir,GT_name) #拼接GT路径和名字
- # print(result_name_path)
- # print(GT_name_path)
-
- #开始计算指标
- result = cv2.imread(result_name_path)
- GT = cv2.imread(GT_name_path)
- psnr = peak_signal_noise_ratio(GT,result)
- ssim= structural_similarity(GT,result, multichannel=True)
-
- loss_fn_alex = lpips.LPIPS(net='alex',version=0.1) # best forward scores
- loss_fn_vgg = lpips.LPIPS(net='vgg',version=0.1) # closer to "traditional" perceptual loss, when used for optimization
- test1_res = result
- test1_label = GT
- transf = transforms.ToTensor()
- test1_label = transf(test1_label)
- test1_res = transf(test1_res)
- test1_ress = test1_res.to(torch.float32).unsqueeze(0)
- test1_labell = test1_label.to(torch.float32).unsqueeze(0)
- lpips_loss = loss_fn_alex(test1_ress, test1_labell)
-
- #计算结果存入列表
- image_number.append(str(i))
- image_name.append(name)
- psnr_number.append(psnr)
- ssim_number.append(ssim)
- lpips_number.append(lpips_loss)
- #计算列表中指标值的平均值函数
- def ave(lis):
- s = 0
- total_num = len(lis)
- for i in lis:
- s = s + i
- return s/total_num
- #计算列表中指标值的平均值,并加入列表
- total = 'total(' + str(len(image_number)) + ')'
- image_number.append(total)
- image_name.append('average')
- psnr_ave = ave(psnr_number)
- ssim_ave = ave(ssim_number)
- lpips_ave = ave(lpips_number)
- psnr_number.append(psnr_ave)
- ssim_number.append(ssim_ave)
- lpips_number.append(lpips_ave)
- #生成csv文件
- dit = {'image_number':image_number, 'result_name':image_name, 'psnr':psnr_number,'ssim':ssim_number,'lpips':lpips_number}
- df = pd.DataFrame(dit)
- csv_name = test_set_name + '_ssim&psnr&lpips.csv' #拼接csv名字
- csv_path = os.path.join(abs_path,csv_name) #csv全路径
- df.to_csv(csv_path,columns=['image_number','result_name','psnr','ssim','lpips'],index=False,sep=',')
-
- print('————————————————————————————finish————————————————————————————')
- print('csv_save_path:',csv_path) ##csv全路径
- print('result_photos_num:',len(result_images)) #result_photos_num
- print('GT_photos_num:',len(GT_image)) #GT_photos_num
- print('psnr_ave:',psnr_ave) #psnr_ave
- print('ssim_ave:',ssim_ave) #ssim_ave
- print('lpips_ave:',lpips_ave) #ssim_ave
注:有小伙伴测试时还是出现问题 ,我打包了个测试文件,图片可以按其命名放入对应文件夹,或者修改对应代码。
链接:https://pan.baidu.com/s/1xcScdzQofvQPkuVPy1FAVw?pwd=LUCK
提取码:LUCK
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。