import os import torch import lpips from PIL import Image # 初始化LPIPS模型 loss_fn = lpips.LPIPS(net='vgg') def calculate_lpips(folder1, folder2, ext1='.jpg', ext2='.png'): # 获取文件夹中的文件名 filenames = [f for f in os.listdir(folder1) if f.endswith(ext1)] lpips_values = [] for filename in filenames: # 加载图像 img1 = Image.open(os.path.join(folder1, filename)) img2 = Image.open(os.path.join(folder2, filename.replace(ext1, ext2))) # 转换为张量 img1_tensor = torch.from_numpy(np.array(img1)).unsqueeze(0).float() img2_tensor = torch.from_numpy(np.array(img2)).unsqueeze(0).float() # 计算LPIPS lpips_value = loss_fn(img1_tensor, img2_tensor) lpips_values.append(lpips_value.item()) return lpips_values # 使用函数 folder1 = 'path_to_folder1' folder2 = 'path_to_folder2' lpips_values = calculate_lpips(folder1, folder2)
标签:folder2,folder1,111,import,img2,img1,lpips From: https://www.cnblogs.com/yyhappy/p/18083845