def compute_lpips(self, source, target): im1 = lpips.im2tensor( lpips.load_image(self.base_path + '/train_images/' + source)) im2 = lpips.im2tensor( lpips.load_image(self.base_path + '/train_images/' + target)) dist = self.loss_fn.forward(im1, im2) return dist.detach().numpy()
default='./out/', help='path to save files') opt = parser.parse_args() os.makedirs(opt.out, exist_ok=True) data = [] ## Initializing the model loss_fn = lpips.LPIPS(net=opt.net, version=opt.version) if (opt.use_gpu): loss_fn.cuda() # Load images img0 = lpips.im2tensor(lpips.load_image(opt.path)) # RGB image from [-1,1] if (opt.use_gpu): img0 = img0.cuda() files = os.listdir(opt.folder) for (ff, file) in enumerate(files[:-1]): img1 = lpips.im2tensor(lpips.load_image(os.path.join( opt.folder, file))) # RGB image from [-1,1] if (opt.use_gpu): img1 = img1.cuda() # Compute distance dist01 = loss_fn.forward(img0, img1)
loss_fn = lpips.LPIPS(net='alex', spatial=spatial) # Can also set net = 'squeeze' or 'vgg' # loss_fn = lpips.LPIPS(net='alex', spatial=spatial, lpips=False) # Can also set net = 'squeeze' or 'vgg' if(use_gpu): loss_fn.cuda() ## Example usage with dummy tensors dummy_im0 = torch.zeros(1,3,64,64) # image should be RGB, normalized to [-1,1] dummy_im1 = torch.zeros(1,3,64,64) if(use_gpu): dummy_im0 = dummy_im0.cuda() dummy_im1 = dummy_im1.cuda() dist = loss_fn.forward(dummy_im0,dummy_im1) ## Example usage with images ex_ref = lpips.im2tensor(lpips.load_image('./imgs/ex_ref.png')) ex_p0 = lpips.im2tensor(lpips.load_image('./imgs/ex_p0.png')) ex_p1 = lpips.im2tensor(lpips.load_image('./imgs/ex_p1.png')) if(use_gpu): ex_ref = ex_ref.cuda() ex_p0 = ex_p0.cuda() ex_p1 = ex_p1.cuda() ex_d0 = loss_fn.forward(ex_ref,ex_p0) ex_d1 = loss_fn.forward(ex_ref,ex_p1) if not spatial: print('Distances: (%.3f, %.3f)'%(ex_d0, ex_d1)) else: print('Distances: (%.3f, %.3f)'%(ex_d0.mean(), ex_d1.mean())) # The mean distance is approximately the same as the non-spatial distance
list_avg_dist = [] f = open(opt.out, 'w') for i in range(200): # crawl directories img_dir = os.path.join(opt.dir, str(i)) files = os.listdir(img_dir) if opt.N is not None: files = files[:opt.N] F = len(files) dists = [] for (ff, file) in enumerate(files[:-1]): img0 = lpips.im2tensor(lpips.load_image(os.path.join( img_dir, file))) # RGB image from [-1,1] if (opt.use_gpu): img0 = img0.cuda() files1 = files[ff + 1:] for file1 in files1: img1 = lpips.im2tensor( lpips.load_image(os.path.join(img_dir, file1))) if (opt.use_gpu): img1 = img1.cuda() # Compute distance dist01 = loss_fn.forward(img0, img1) print('(%s,%s): %.3f' % (file, file1, dist01))
opt = parser.parse_args() ## Initializing the model loss_fn = lpips.LPIPS(net='alex', version=opt.version) if (opt.use_gpu): loss_fn.cuda() # crawl directories f = open(opt.out, 'w') files = os.listdir(opt.dir0) dists = [] for file in files: if (os.path.exists(os.path.join(opt.dir1, file))): # Load images img0 = lpips.im2tensor(lpips.load_image(os.path.join( opt.dir0, file))) # RGB image from [-1,1] img1 = lpips.im2tensor(lpips.load_image(os.path.join(opt.dir1, file))) if (opt.use_gpu): img0 = img0.cuda() img1 = img1.cuda() # Compute distance dist01 = loss_fn.forward(img0, img1) print('%s: %.3f' % (file, dist01)) f.writelines('%s: %.6f\n' % (file, dist01)) dists.append(dist01.cpu().detach().numpy()) mean = np.mean(np.array(dists)) print(mean) f.close()
loss_fn = lpips.LPIPS(net='alex', version=opt.version) if (opt.use_gpu): loss_fn.cuda() # Crawl directories f = open(opt.out, 'w') aligned_images = sorted(os.listdir(opt.dir0)) generated_images = sorted(os.listdir(opt.dir1)) for idx, image in enumerate(aligned_images): if (idx == 0): #print("\n") pass if (image[:-4] == generated_images[idx][:-4]): # Load images img0 = lpips.im2tensor(lpips.load_image(os.path.join( opt.dir0, image))) # RGB image from [-1,1] img1 = lpips.im2tensor( lpips.load_image(os.path.join(opt.dir1, generated_images[idx]))) if (opt.use_gpu): img0 = img0.cuda() img1 = img1.cuda() # Compute distance dist01 = loss_fn.forward(img0, img1) #print("LPIPS score (distance) for ", image[:-4], " is:\t", round(dist01, 2), "\n") #print('LPIPS score (distance) for %s is:\t%.2f\n'%(image[:-4], dist01)) f.writelines('LPIPS score (distance) for %s is:\t%.2f\n' % (image[:-4], dist01)) f.close()
def main(): # Configurations # GT - Ground-truth; # Gen: Generated / Restored / Recovered images folder_GT = '/data/wcz93762/houselee/OriRealSR(V3)/Canon/Test/HRx4' folder_Gen = '/home/yzj6850/HouseLee/Test-SR/no_pre_realsr_66000' f = open('realsr_ours_Result_new.txt', 'a+') loss_fn = lpips.LPIPS(net='alex') loss_fn.cuda() crop_border = 4 suffix = '' # suffix for Gen images test_Y = False # True: test Y channel only; False: test RGB channels PSNR_all = [] # PSNR_all_2 = [] SSIM_all = [] LPIPS_all = [] # SSIM_all_2 = [] img_list = sorted(glob.glob(folder_GT + '/*')) if test_Y: print('Testing Y channel.') else: print('Testing RGB channels.') for i, img_path in enumerate(img_list): base_name = os.path.splitext(os.path.basename(img_path))[0] #Test LPIPS img0 = lpips.im2tensor(lpips.load_image(img_path)) img1 = lpips.im2tensor( lpips.load_image( os.path.join(folder_Gen, base_name + suffix + '.png'))) img0 = img0.cuda() img1 = img1.cuda() dist01 = loss_fn.forward(img0, img1) lp = dist01.cpu().detach().numpy() LPIPS_all.append(lp) im_GT = cv2.imread(img_path) / 255. im_Gen = cv2.imread( os.path.join(folder_Gen, base_name + suffix + '.png')) / 255. # im_bic = cv2.imread(os.path.join(folder_bic, base_name + suffix + '.png')) / 255. if test_Y and im_GT.shape[ 2] == 3: # evaluate on Y channel in YCbCr color space im_GT_in = bgr2ycbcr(im_GT) im_Gen_in = bgr2ycbcr(im_Gen) # im_bic_in = bgr2ycbcr(im_bic) else: im_GT_in = im_GT im_Gen_in = im_Gen # im_bic_in = im_bic # crop borders if im_GT_in.ndim == 3: cropped_GT = im_GT_in[crop_border:-crop_border, crop_border:-crop_border, :] cropped_Gen = im_Gen_in[crop_border:-crop_border, crop_border:-crop_border, :] # cropped_bic = im_bic_in[crop_border:-crop_border, crop_border:-crop_border, :] elif im_GT_in.ndim == 2: cropped_GT = im_GT_in[crop_border:-crop_border, crop_border:-crop_border] cropped_Gen = im_Gen_in[crop_border:-crop_border, crop_border:-crop_border] # cropped_bic = im_bic_in[crop_border:-crop_border, crop_border:-crop_border] else: raise ValueError( 'Wrong image dimension: {}. Should be 2 or 3.'.format( im_GT_in.ndim)) # calculate PSNR and SSIM PSNR = calculate_psnr(cropped_GT * 255, cropped_Gen * 255) # PSNR2 = calculate_psnr(cropped_GT * 255, cropped_bic * 255) SSIM = calculate_ssim(cropped_GT * 255, cropped_Gen * 255) # SSIM2 = calculate_ssim(cropped_GT * 255, cropped_bic * 255) #select pred_better and pred_worse patches # if PSNR1-PSNR2 >= 0.7: #if len(glob.glob(folder_pred = '*.png')) <= 200: # shutil.copy(folder_Gen + '/' + base_name + suffix + '.png', folder_pred) # if PSNR1-PSNR2 <= 0.3: #if len(glob.glob(folder_ori = '*.png')) <= 200: # shutil.copy(folder_Gen + '/' + base_name + suffix + '.png', folder_ori) print( '{:3d} - {:25}. \tPSNR: {:.6f} dB, \tSSIM: {:.6f}, \tLPIPS: {:.6f}' .format(i + 1, base_name, PSNR, SSIM, lp[0][0][0][0])) f.writelines( '{:3d} - {:25}. \tPSNR: {:.6f} dB, \tSSIM: {:.6f}, \tLPIPS: {:.6f}\n' .format(i + 1, base_name, PSNR, SSIM, lp[0][0][0][0])) PSNR_all.append(PSNR) SSIM_all.append(SSIM) avg_lpips = sum(LPIPS_all) / len(LPIPS_all) print('Average: PSNR: {:.6f} dB, SSIM: {:.6f}, LPIPS: {:.6f}'.format( sum(PSNR_all) / len(PSNR_all), sum(SSIM_all) / len(SSIM_all), avg_lpips[0][0][0][0])) f.writelines( 'Average: PSNR: {:.6f} dB, SSIM: {:.6f}, LPIPS: {:.6f}'.format( sum(PSNR_all) / len(PSNR_all), sum(SSIM_all) / len(SSIM_all), avg_lpips[0][0][0][0])) # print('{:3d} - {:13}. \tPSNR1: {:.6f} dB, \tSSIM1:{:.6f}, \tPSNR2: {:.6f}, \tSSIM2:{:.6f}'.format( # i + 1, base_name, PSNR1, SSIM1, PSNR2, SSIM2)) # f.writelines('{:3d} - {:13}. \tPSNR1: {:.6f} dB, \tSSIM1:{:.6f}, \tPSNR2: {:.6f}, \tSSIM2:{:.6f}\n'.format( # i + 1, base_name, PSNR1, SSIM1, PSNR2, SSIM2)) # PSNR_all_1.append(PSNR1) # PSNR_all_2.append(PSNR2) # SSIM_all_1.append(SSIM1) # SSIM_all_2.append(SSIM2) # SSIM_all.append(SSIM) # print('Average: PSNR1: {:.6f} dB, SSIM1: {:.6f}, PSNR2: {:.6f}dB, SSIM2: {:.6f}'.format( # sum(PSNR_all_1) / len(PSNR_all_1), # sum(SSIM_all_1) / len(SSIM_all_1), # sum(PSNR_all_2) / len(PSNR_all_2), # sum(SSIM_all_2) / len(SSIM_all_2))) # f.writelines('Average: PSNR1: {:.6f} dB, SSIM1: {:.6f}, PSNR2: {:.6f}dB, SSIM2: {:.6f}'.format( # sum(PSNR_all_1) / len(PSNR_all_1), # sum(SSIM_all_1) / len(SSIM_all_1), # sum(PSNR_all_2) / len(PSNR_all_2), # sum(SSIM_all_2) / len(SSIM_all_2))) f.close()
loss_fn = lpips.LPIPS(net='vgg').cuda( ) # closer to "traditional" perceptual loss, when used for optimization total_lpips = 0 widgets = [ 'LPIPS :', Percentage(), ' ', Bar('#'), ' ', Timer(), ' ', ETA(), ' ', FileTransferSpeed() ] pbar = ProgressBar(widgets=widgets) for file in tqdm.tqdm(files): # Load images img0 = lpips.im2tensor(lpips.load_image(os.path.join( input1, file))) # RGB image from [-1,1] img1 = lpips.im2tensor(lpips.load_image(os.path.join(input2, file))) img0 = img0.cuda() img1 = img1.cuda() # Compute distance with torch.no_grad(): dist01 = loss_fn(img0, img1) total_lpips += dist01 lpips = total_lpips / 5000 print("lpips: ", lpips) score_lpips = pow(1 - 2 * (min(max(lpips, 0.2), 0.7) - 0.2), 0.5) if isinstance(score_lpips, torch.Tensor): score_lpips = score_lpips.item()
os.makedirs(opt.out, exist_ok=True) data = [] ## Initializing the model loss_fn = lpips.LPIPS(net=opt.net, version=opt.version) if (opt.use_gpu): loss_fn.cuda() smallest_file = opt.path smallest_dist = 3.0 img0 = lpips.im2tensor( lpips.load_image(os.path.join(opt.folder, smallest_file))) files = os.listdir(opt.folder) while (len(files) > 1): for (ff, file) in enumerate(files[:-1]): img1 = lpips.im2tensor(lpips.load_image(os.path.join( opt.folder, file))) # RGB image from [-1,1] if (opt.use_gpu): img0 = img0.cuda() img1 = img1.cuda() # Compute distance dist01 = loss_fn.forward(img0, img1) # print('%s: %.3f'%(file,dist01))
parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--ref_path', type=str, default='./imgs/ex_ref.png') parser.add_argument('--pred_path', type=str, default='./imgs/ex_p1.png') parser.add_argument('--use_gpu', action='store_true', help='turn on flag to use GPU') opt = parser.parse_args() loss_fn = lpips.LPIPS(net='vgg') if (opt.use_gpu): loss_fn.cuda() ref = lpips.im2tensor(lpips.load_image(opt.ref_path)) pred = Variable(lpips.im2tensor(lpips.load_image(opt.pred_path)), requires_grad=True) if (opt.use_gpu): with torch.no_grad(): ref = ref.cuda() pred = pred.cuda() optimizer = torch.optim.Adam([ pred, ], lr=1e-3, betas=(0.9, 0.999)) plt.ion() fig = plt.figure(1) ax = fig.add_subplot(131) ax.imshow(lpips.tensor2im(ref))
import argparse import lpips parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-p0','--path0', type=str, default='./imgs/ex_ref.png') parser.add_argument('-p1','--path1', type=str, default='./imgs/ex_p0.png') parser.add_argument('-v','--version', type=str, default='0.1') parser.add_argument('--use_gpu', action='store_true', help='turn on flag to use GPU') opt = parser.parse_args() ## Initializing the model loss_fn = lpips.LPIPS(net='alex',version=opt.version) if(opt.use_gpu): loss_fn.cuda() # Load images img0 = lpips.im2tensor(lpips.load_image(opt.path0)) # RGB image from [-1,1] img1 = lpips.im2tensor(lpips.load_image(opt.path1)) if(opt.use_gpu): img0 = img0.cuda() img1 = img1.cuda() # Compute distance dist01 = loss_fn.forward(img0,img1) print('Distance: %.3f'%dist01)