def get_metrics(GT, reconstructed, mode=False): if mode: pass _psnr = psnr(np.array(GT), np.array(reconstructed)) _ssim = ssim(np.array(GT), np.array(reconstructed)) #_niqe = niqe(np.array(reconstructed)) return _psnr, _ssim[0], 0
def image_quality_evaluation(sr_filename: str, hr_filename: str, device: torch.device = "cpu"): """Image quality evaluation function. Args: sr_filename (str): Image file name after super resolution. hr_filename (str): Original high resolution image file name. device (optional, torch.device): Selection of data processing equipment in PyTorch. (Default: ``cpu``). Returns: If the `simple` variable is set to ``False`` return `mse, rmse, psnr, ssim, msssim, niqe, sam, vifp, lpips`, else return `psnr, ssim`. """ # Reference sources from `https://github.com/richzhang/PerceptualSimilarity` lpips_loss = lpips.LPIPS(net="vgg", verbose=False).to(device) # Evaluate performance sr = cv2.imread(sr_filename) hr = cv2.imread(hr_filename) # For LPIPS evaluation sr_tensor = opencv2tensor(sr, device) hr_tensor = opencv2tensor(hr, device) # Complete estimate. mse_value = mse(sr, hr) rmse_value = rmse(sr, hr) psnr_value = psnr(sr, hr) ssim_value = ssim(sr, hr) msssim_value = msssim(sr, hr) niqe_value = niqe(sr_filename) sam_value = sam(sr, hr) vifp_value = vifp(sr, hr) lpips_value = lpips_loss(sr_tensor, hr_tensor) return mse_value, rmse_value, psnr_value, ssim_value, msssim_value, niqe_value, sam_value, vifp_value, lpips_value
def evaluate(GT, P): score = {'rmse': 1e9, 'psnr': 0, 'ssim': 0, 'hough': 1e9} for ratio in np.arange(1.0, 1.3, 0.05): GT_enlarge = enlarge_and_crop(GT, ratio, 256) score['rmse'] = min(score['rmse'], rmse(GT_enlarge, P)) score['psnr'] = max(score['psnr'], psnr(GT_enlarge, P)) score['ssim'] = max(score['ssim'], ssim(GT_enlarge, P)[0]) score['hough'] = min(score['hough'], Hough_score(GT_enlarge, P)) print(score) return score
def get_metrics(compression_model: BaseModel, original_image: np.ndarray) -> Dict[str, float]: compressed_image = compression_model.compress(original_image) decompressed_image = compression_model.decompress(compressed_image) return { "mse": mse(original_image, decompressed_image), "ssim": ssim(original_image, decompressed_image)[0], "vif-p": vifp(original_image, decompressed_image), "psnr-b": psnrb(original_image, decompressed_image), "psnr": psnr(original_image, decompressed_image), "original_image_size": asizeof.asizeof(original_image), "compressed_image_size": asizeof.asizeof(compressed_image), "compression_ratio": asizeof.asizeof(compressed_image) / asizeof.asizeof(original_image) }
def plot_predict(low_reso_imgs, high_reso_imgs, srgan_model, idx, n_imgs): plt.figure(figsize=(12, 12)) plt.tight_layout() n_imgs = n_imgs for i in range(0, n_imgs * 3, 3): #idx = np.random.randint(0,low_reso_imgs.shape[0]-1) idx = idx plt.subplot(n_imgs, 3, i + 1) h_img = high_reso_imgs[idx] plt.imshow(h_img) plt.grid('off') plt.axis('off') #plt.title('Source') plt.title('Original') plt.subplot(n_imgs, 3, i + 2) plt.imshow( cv2.resize(low_reso_imgs[idx], (256, 256), interpolation=cv2.INTER_LINEAR)) plt.grid('off') plt.axis('off') #plt.title('X4 (bicubic)') plt.title('low resolution (bicubic)') img = srgan_model.generator.predict( np.expand_dims(low_reso_imgs[idx], axis=0) / 127.5 - 1) img_unnorm = (img + 1) * 127.5 plt.subplot(n_imgs, 3, i + 3) plt.imshow(np.squeeze(img_unnorm, axis=0).astype(np.uint8)) prd_val_psnr = psnr(h_img, img_unnorm[0]) prd_val_ssim = ssim(h_img, img_unnorm[0]) plt.title('SRGAN_result') plt.ylabel('PSNR/SSIM') plt.xlabel(str(prd_val_psnr) + '/' + str(prd_val_ssim)) img_rgb = cv2.cvtColor( np.squeeze(img_unnorm, axis=0).astype(np.uint8), cv2.COLOR_BGR2RGB) cv2.imwrite('predict_img' + str(idx) + '.png', img_rgb) #plt.grid('off') #plt.axis('off') plt.savefig('predicted_' + str(idx) + '.png')
def obtain_similarity_metrics(GT_img, distorted_img): # MEAN SQUARED ERROR mse_value = mse(GT_img, distorted_img) # STRUCTURAL SIMILARITY ssim_value = ssim(GT_img, distorted_img) # PEAK SIGNAL TO NOISE RATIO psnr_value = psnr(GT_img, distorted_img) # ROOT MEAN SQUARED ERROR rmse_value = rmse(GT_img, distorted_img) # VISUAL INFORMATION FIDELITY vif_value = vifp(GT_img, distorted_img) # UNIVERSAL IMAGE QUALITY INDEX uqi_value = uqi(GT_img, distorted_img) # MULTI-SCALE STRUCTURAL SIMILARITY INDEX msssim_value = msssim(GT_img, distorted_img) # PSNR-HVS-M & PSNR-HVS p_hvs_m, p_hvs = psnrhmam.color_psnrhma(GT_img, distorted_img) return mse_value, ssim_value, psnr_value, rmse_value, vif_value, uqi_value, msssim_value, p_hvs_m, p_hvs
vutils.save_image(lr, "lr.png") vutils.save_image(sr, "sr.png") vutils.save_image(hr, "hr.png") # Evaluate performance src_img = cv2.imread("sr.png") dst_img = cv2.imread("hr.png") # Reference sources from `https://github.com/richzhang/PerceptualSimilarity` lpips_loss = lpips.LPIPS(net="vgg").to(device) mse_value = mse(src_img, dst_img) rmse_value = rmse(src_img, dst_img) psnr_value = psnr(src_img, dst_img) ssim_value = ssim(src_img, dst_img) ms_ssim_value = msssim(src_img, dst_img) # 30.00+000j niqe_value = cal_niqe("sr.png") sam_value = sam(src_img, dst_img) vif_value = vifp(src_img, dst_img) lpips_value = lpips_loss(sr, hr) print("\n") print("====================== Performance summary ======================") print( f"MSE: {mse_value:.2f}\n" f"RMSE: {rmse_value:.2f}\n" f"PSNR: {psnr_value:.2f}\n" f"SSIM: {ssim_value[0]:.4f}\n" f"MS-SSIM: {ms_ssim_value.real:.4f}\n" f"NIQE: {niqe_value:.2f}\n"
def calculate_ssim_psnr(self, img1, img2): self.input1 = cv2.imread(img1) self.input2 = cv2.imread(img2) self.ssim_result.append(ssim(self.input1, self.input2)) self.psnr_result.append(psnr(self.input1, self.input2))
#Spatial correlation coefficient full_ref.scc(ref_img, img, win=[[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], ws=8) #Structural similarity index """calculates structural similarity index (ssim). :param GT: first (original) input image. :param P: second (deformed) input image. :param ws: sliding window size (default = 8). :param K1: First constant for SSIM (default = 0.01). :param K2: Second constant for SSIM (default = 0.03). :param MAX: Maximum value of datarange (if None, MAX is calculated using image dtype). :returns: tuple -- ssim value, cs value. """ ssim_img = full_ref.ssim(ref_img, img, ws=11, K1=0.01, K2=0.03, MAX=None, fltr_specs=None, mode='valid') print("SSIM: structural similarity index = ", ssim_img) ############################################################################## #Universal image quality index """calculates universal image quality index (uqi). :param GT: first (original) input image. :param P: second (deformed) input image. :param ws: sliding window size (default = 8). :returns: float -- uqi value. """ UQI_img = full_ref.uqi(ref_img, img, ws=8) print("UQI: universal image quality index = ", UQI_img)
sr_img = util.tensor2img(visuals['SR']) # uint8 #pdb.set_trace() if need_HR: # load GT image and calculate psnr gt_img = util.tensor2img(visuals['HR']) crop_border = test_loader.dataset.opt['scale'] cropped_sr_img = sr_img[crop_border:-crop_border, crop_border:-crop_border, :] cropped_gt_img = gt_img[crop_border:-crop_border, crop_border:-crop_border, :] #psnr = util.psnr(cropped_sr_img, cropped_gt_img) psnr = fr.psnr(cropped_sr_img, cropped_gt_img, MAX=1.0) #ssim = util.ssim(cropped_sr_img, cropped_gt_img, multichannel=True) ssim = fr.ssim(cropped_sr_img, cropped_gt_img, MAX=1.0)[0] #ssim = fr.uqi(cropped_sr_img, cropped_gt_img) #niqe = nr.niqe(cropped_sr_img) test_results['psnr'].append(psnr) test_results['ssim'].append(ssim) #test_results['niqe'].append(niqe) #pdb.set_trace() if gt_img.shape[2] == 3: # RGB image cropped_sr_img_y = bgr2ycbcr(cropped_sr_img, only_y=True) cropped_gt_img_y = bgr2ycbcr(cropped_gt_img, only_y=True) #psnr_y = util.psnr(cropped_sr_img_y, cropped_gt_img_y) #ssim_y = util.ssim(cropped_sr_img_y, cropped_gt_img_y, multichannel=False) psnr_y = fr.psnr(cropped_sr_img_y, cropped_gt_img_y, MAX=1.0) ssim_y = fr.ssim(cropped_sr_img_y, cropped_gt_img_y, MAX=1.0)[0]
#save os.makedirs("test_data/fake_visible", exist_ok = True) cv2.imwrite("test_data/fake_visible/" + vis_path , fake_A[0][:,:,::-1] * 255) totol_metric_dict_matched = {"mse":0.0,"rmse":0.0,"uqi":0.0,"ssim":0.0,"psnr":0.0,"psnrb":0.0,"vifp":0.0} #参数指标 true_path = "test_data/visible" fake_path = "test_data/fake_visiblee" lenth = len(os.listdir(true_path)) for true_name,fake_name in zip(os.listdir(true_path),os.listdir(fake_path)): true = cv2.imread(os.path.join(true_path,true_name)) fake = cv2.imread(os.path.join(fake_path,fake_name)) metric_dict_matched = {"mse":mse(fake,true),"rmse":rmse(fake,true),"uqi":uqi(fake,true),"ssim":ssim(fake,true)[0] \ ,"psnr":psnr(fake,true),"psnrb":psnrb(fake,true),"vifp":vifp(fake,true)} for key,value in metric_dict_matched.items(): totol_metric_dict_matched[key] = totol_metric_dict_matched[key]+value for key,value in totol_metric_dict_matched.items(): totol_metric_dict_matched[key] /= lenth print(totol_metric_dict_matched) #path = ["train_data/" + method + "_infrared","train_data/" + method + "_visible"] path = [true_path,fake_path] fid_value = fid.calculate_fid_given_paths(path, inception_path = None, low_profile=False) print("FID: ", fid_value) print("done")
out_img_cb = cb.resize(out_image_y.size, Image.BICUBIC) out_img_cr = cr.resize(out_image_y.size, Image.BICUBIC) out_img = Image.merge("YCbCr", [out_image_y, out_img_cb, out_img_cr]).convert("RGB") # before converting the result in RGB out_img.save(f"result/{filename}") # Evaluate performance src_img = cv2.imread(f"result/{filename}") dst_img = cv2.imread(f"{target}/{filename}") total_mse_value += mse(src_img, dst_img) total_rmse_value += rmse(src_img, dst_img) total_psnr_value += psnr(src_img, dst_img) total_ssim_value += ssim(src_img, dst_img) total_ms_ssim_value += msssim(src_img, dst_img) total_niqe_value += cal_niqe(f"result/{filename}") total_sam_value += sam(src_img, dst_img) total_vif_value += vifp(src_img, dst_img) total_file += 1 print(f"Avg MSE: {total_mse_value / total_file:.2f}\n" f"Avg RMSE: {total_rmse_value / total_file:.2f}\n" f"Avg PSNR: {total_psnr_value / total_file:.2f}\n" f"Avg SSIM: {total_ssim_value / total_file:.4f}\n" f"Avg MS-SSIM: {total_ms_ssim_value / total_file:.4f}\n" f"Avg NIQE: {total_niqe_value / total_file:.2f}\n" f"Avg SAM: {total_sam_value / total_file:.4f}\n" f"Avg VIF: {total_vif_value / total_file:.4f}")
from sewar.full_ref import sam from sewar.full_ref import msssim from sewar.full_ref import vifp img1 = cv2.imread("ssim/png_source.png") img2 = cv2.imread("ssim/png_sr.png") print("Metricas\n") uqi = uqi(img1, img2) print("uqi: ", uqi) psnr = psnr(img1, img2) print("psnr: ", psnr) ssim = ssim(img1, img2) print("ssim: ", ssim) mse = mse(img1, img2) print("mse: ", mse) rmse_sw = rmse_sw(img1, img2) # print("rmse_sw: ", rmse_sw) ergas = ergas(img1, img2) print("ergas: ", ergas) scc = scc(img1, img2) print("scc: ", scc) rase = rase(img1, img2)
def compute_SSIM(self): """ Compute Universal Quality Image Index between two images """ self.SSIM = ssim(self.I1, self.I2) return ()
counter = 0 for img_name in test_image_names: img_name = img_name[0:-4] # for real vs foggy # img_name = img_name[0:-19] # print(img_name) normal_image = scipy.misc.imread(first_images_dir + str(img_name) + '_fake_B.png', mode="RGB") foggy_image = scipy.misc.imread(foggy_images_dir + str(img_name) + '_fake_B.png', mode="RGB") diff_mse = mse(normal_image, foggy_image) diff_ssim = ssim(normal_image, foggy_image) diff_psnr = psnr(normal_image, foggy_image) diff_mse_list += [diff_mse] diff_psnr_list += [diff_psnr] diff_ssim_list += [diff_ssim] # print(diff_mse_list) print(diff_ssim_list) # print(diff_psnr_list) #Get average, max and min print("MSE stats: ") print(np.mean(np.array(diff_mse_list))) print(max(diff_mse_list)) print(min(diff_mse_list))