def write_sgd_summary(slm_phase, out_amp, target_amp, k, writer=None, path=None, s=0., prefix='test'): """tensorboard summary for SGD :param slm_phase: Use it if you want to save intermediate phases during optimization. :param out_amp: PyTorch Tensor, Field amplitude at the image plane. :param target_amp: PyTorch Tensor, Ground Truth target Amplitude. :param k: iteration number. :param writer: SummaryWriter instance. :param path: path to save image files. :param s: scale for SGD algorithm. :param prefix: :return: """ loss = nn.MSELoss().to(out_amp.device) loss_value = loss(s * out_amp, target_amp) psnr_value = psnr(target_amp.squeeze().cpu().detach().numpy(), (s * out_amp).squeeze().cpu().detach().numpy()) ssim_value = ssim(target_amp.squeeze().cpu().detach().numpy(), (s * out_amp).squeeze().cpu().detach().numpy()) s_min = (target_amp * out_amp).mean() / (out_amp**2).mean() psnr_value_min = psnr(target_amp.squeeze().cpu().detach().numpy(), (s_min * out_amp).squeeze().cpu().detach().numpy()) ssim_value_min = ssim(target_amp.squeeze().cpu().detach().numpy(), (s_min * out_amp).squeeze().cpu().detach().numpy()) if writer is not None: writer.add_image(f'{prefix}_Recon/amp', (s * out_amp).squeeze(0), k) writer.add_scalar(f'{prefix}_loss', loss_value, k) writer.add_scalar(f'{prefix}_psnr', psnr_value, k) writer.add_scalar(f'{prefix}_ssim', ssim_value, k) writer.add_scalar(f'{prefix}_psnr/scaled', psnr_value_min, k) writer.add_scalar(f'{prefix}_ssim/scaled', ssim_value_min, k) writer.add_scalar(f'{prefix}_scalar', s, k)
def test(valid_gp, valid_gt): avg = 0 gp_av = 0 print('------------------ Validation----------------------') model.eval() with torch.no_grad(): for j in range(len(valid_gt)): test_x, shape = get_test_image(valid_gp[j], SIZE) test_x = torch.from_numpy(test_x).to(device) for i in range(len(test_x)): prediction = model(test_x[i:i + 1]) test_x[i] = test_x[i] + prediction img = get_img(test_x.data.cpu().numpy(), shape, valid_gp[j].shape, SIZE) crit = psnr(valid_gt[j], img) gp_t = psnr(valid_gt[j], valid_gp[j]) gp_av += gp_t avg += crit if (j % 50 == 0): print("PSNR ---> GPAEN {:.8f}, GP {:.8f}".format(crit, gp_t)) avg /= len(valid_gt) gp_av /= len(valid_gt) print("===> Avg. PSNR : {:.8f}".format(avg)) print("===> Avg. GP: {:.8f}".format(gp_av)) validation_loss.append(avg)
def PSNR_metric(img1, img2): img1 = np.asarray(img1) img2 = np.asarray(img2) pixel_max = np.max(np.maximum(img1, img2)) pixel_min = np.min(np.minimum(img1, img2)) pixel_diff = pixel_max - pixel_min return (psnr(img1, img2, data_range=pixel_diff))
def dataset_stats(dataset, det): print(f'Dataset {det}:') print(f'\tfeatures: {dataset["keV"].shape}') print(f'\tclean spectra: {dataset["spectrum"].shape}') print(f'\tnoisy spectra: {dataset["noisy_spectrum"].shape}') print(f'\tnoise: {dataset["noise"].shape}') print(f'\tmin Compton scale: {np.min(dataset["compton_scale"])}') print(f'\tmax Compton scale: {np.max(dataset["compton_scale"])}') print(f'\tmin Noise scale: {np.min(dataset["noise_scale"])}') print(f'\tmax Noise scale: {np.max(dataset["noise_scale"])}') noisy_spectra = dataset['noisy_spectrum'] clean_spectra = dataset['spectrum'] min_psnr = 9999.0 max_psnr = 0.0 for clean, noisy in zip(clean_spectra, noisy_spectra): noisy_psnr = psnr(clean, noisy) if noisy_psnr < min_psnr: min_psnr = noisy_psnr if noisy_psnr > max_psnr: max_psnr = noisy_psnr print(f'\tmax PSNR {max_psnr:.2f} dB') print(f'\tmin PSNR {min_psnr:.2f} dB')
def write_gs_summary(slm_field, recon_field, target_amp, k, writer, roi=(880, 1600), prefix='test'): """tensorboard summary for GS""" _, slm_phase = rect_to_polar(slm_field[..., 0], slm_field[..., 1]) recon_amp, recon_phase = rect_to_polar(recon_field[..., 0], recon_field[..., 1]) loss = nn.MSELoss().to(recon_amp.device) recon_amp = crop_image(recon_amp, target_shape=roi, stacked_complex=False) target_amp = crop_image(target_amp, target_shape=roi, stacked_complex=False) recon_amp *= (torch.sum(recon_amp * target_amp, (-2, -1), keepdim=True) / torch.sum(recon_amp * recon_amp, (-2, -1), keepdim=True)) loss_value = loss(recon_amp, target_amp) psnr_value = psnr(target_amp.squeeze().cpu().detach().numpy(), recon_amp.squeeze().cpu().detach().numpy()) ssim_value = ssim(target_amp.squeeze().cpu().detach().numpy(), recon_amp.squeeze().cpu().detach().numpy()) if writer is not None: writer.add_image(f'{prefix}_Recon/amp', recon_amp.squeeze(0), k) writer.add_scalar(f'{prefix}_loss', loss_value, k) writer.add_scalar(f'{prefix}_psnr', psnr_value, k) writer.add_scalar(f'{prefix}_ssim', ssim_value, k)
def PSNR(img_n, img_f): PSNR = psnr(img_f, img_n, data_range=max(img_f.max(), img_n.max()) - min(img_f.min(), img_n.min())) print(f' [*] PSNR @ {PSNR}') return PSNR
def PSNR(ground_truth_images: np.ndarray, noisy_images: np.ndarray) -> List[float]: """ Calculate the medium PSNR over the ground truth images and noisy images. """ validate_inputs(ground_truth_images, noisy_images) psnr_acumulated = [] quantity_of_images = ground_truth_images.shape[0] if need_to_normalize(ground_truth_images): ground_truth_images = normalize(ground_truth_images, \ interval=(0,255), data_type='int') if need_to_normalize(noisy_images): noisy_images = normalize(noisy_images, \ interval=(0,255), data_type='int') for i in range(quantity_of_images): psnr_image = psnr(ground_truth_images[i, :, :, 0], noisy_images[i, :, :, 0], data_range=256) psnr_acumulated.append(psnr_image) # psnr_acumulated = np.array(psnr_acumulated) # return psnr_acumulated.mean() return psnr_acumulated
def batch_PSNR(img, imclean, data_range): Img = img.data.cpu().numpy().astype(np.float32) Iclean = imclean.data.cpu().numpy().astype(np.float32) PSNR = 0 for i in range(Img.shape[0]): PSNR += psnr(Iclean[i, :, :, :], Img[i, :, :, :], data_range=data_range) return (PSNR / Img.shape[0])
def psnr_of_batch(clean_imgs, denoised_imgs): clean_imgs = clean_imgs.data.cpu().numpy().astype(np.float32) denoised_imgs = denoised_imgs.data.cpu().numpy().astype(np.float32) batch_psnr = 0 for i in range(clean_imgs.shape[0]): batch_psnr += psnr(clean_imgs[i, :, :, :], denoised_imgs[i, :, :, :], data_range=1) return batch_psnr / clean_imgs.shape[0]
def calculate_image_similarity(input_image, enhanced_image): L = input_image.max() - input_image.min() sim = ssim(input_image, enhanced_image, gaussian_weights=True, sigma=1.5, win_size=11, data_range=L) peak = psnr(input_image, enhanced_image, data_range=L) ambe = np.abs(np.mean(input_image)-np.mean(enhanced_image)) / L return sim, peak, ambe
def get_psnr_ssim(recon_amp, target_amp, multichannel=False): """get PSNR and SSIM metrics""" psnrs, ssims = {}, {} # amplitude psnrs['amp'] = psnr(target_amp, recon_amp) ssims['amp'] = ssim(target_amp, recon_amp, multichannel=multichannel) # linear target_linear = target_amp**2 recon_linear = recon_amp**2 psnrs['lin'] = psnr(target_linear, recon_linear) ssims['lin'] = ssim(target_linear, recon_linear, multichannel=multichannel) # srgb target_srgb = srgb_lin2gamma(np.clip(target_linear, 0.0, 1.0)) recon_srgb = srgb_lin2gamma(np.clip(recon_linear, 0.0, 1.0)) psnrs['srgb'] = psnr(target_srgb, recon_srgb) ssims['srgb'] = ssim(target_srgb, recon_srgb, multichannel=multichannel) return psnrs, ssims
def compare_image(metric, ref, target): if metric == 'ssim': return ssim(ref, target, multichannel=True) if metric == 'psnr': return psnr(ref, target) if metric == 'mse': return mse(ref, target) return None
def validation_step(self, batch, batch_idx: int): inputs, targets = batch logits = self(inputs) loss = self.criterion(logits.view(-1), targets.view(-1)) self.log("val_loss", loss, sync_dist=True, on_step=True, on_epoch=True) if self.current_epoch % 75 == 0 and batch_idx == 0: log_all_info( module=self, img=inputs[0], target=targets[0], preb=logits[0], loss=loss, batch_idx=batch_idx, state="val", input_img_type=self.hparams.X_image, target_img_type=self.hparams.y_image, ) inputs = inputs.cpu().detach().numpy().squeeze() targets = targets.cpu().detach().numpy().squeeze() predicts = logits.cpu().detach().numpy().squeeze() mse = np.square(np.subtract(targets, predicts)).mean() ssim_ = ssim(targets, predicts, data_range=predicts.max() - predicts.min()) psnr_ = psnr(targets, predicts, data_range=predicts.max() - predicts.min()) if batch_idx <= 5: np.savez(f"{batch_idx}.npz", inputs=inputs, target=targets, predict=predicts) if self.hparams.in_channels >= 2: brain_mask = inputs[0] == inputs[0][0][0][0] elif self.hparams.in_channels == 1: brain_mask = inputs == inputs[0][0][0] pred_clip = np.clip(predicts, -self.clip_min, self.clip_max) - min( -self.clip_min, np.min(predicts) ) targ_clip = np.clip(targets, -self.clip_min, self.clip_max) - min( -self.clip_min, np.min(targets) ) pred_255 = np.floor(256 * (pred_clip / (self.clip_min + self.clip_max))) targ_255 = np.floor(256 * (targ_clip / (self.clip_min + self.clip_max))) pred_255[brain_mask] = 0 targ_255[brain_mask] = 0 diff_255 = np.absolute(pred_255.ravel() - targ_255.ravel()) mae = np.mean(diff_255) # diff_255_mask = np.absolute(pred_255[~brain_mask].ravel() - targ_255[~brain_mask].ravel()) # mae_mask = np.mean(diff_255_mask) return {"MAE": mae, "MSE": mse, "SSIM": ssim_, "PSNR": psnr_}
def get_psnr_ssim(pred, gt): # evaluating the psnr and ssim on Y channel of the image. pred1 = cv2.cvtColor(pred, cv2.COLOR_RGB2YUV) y_pred, _, _ = cv2.split(pred1) gt1 = cv2.cvtColor(gt, cv2.COLOR_RGB2YUV) y_gt, _, _ = cv2.split(gt1) curr_psnr = psnr(y_gt, y_pred) curr_ssim = ssim(y_gt, y_pred) return curr_psnr, curr_ssim
def psnr_b(img1, img2): img1 = np.swapaxes(img1, -3, -1) img2 = np.swapaxes(img2, -3, -1) ds = len(np.shape(img1)) #print(img1.shape) if ds > 4: keep_shape = [ img1.shape[i - 1] for i in np.flip(range(ds, ds - 3, -1)) ] return ssim(img1.reshape((-1, *keep_shape)), img2.reshape((-1, *keep_shape))) else: return psnr(img1, img2)
def test(valid_gp, valid_gt, direc): avg = 0 print('------------------ Test ----------------------') for j in range(len(valid_gt)): img = np.zeros(valid_gp[j].shape, dtype=np.float32) for k in range(ntrans): im1 = trans(valid_gp[j], k) test_x, shape = get_test_image(im1) test_x = torch.from_numpy(test_x) #.to(device) for i in range(len(test_x) // 64): patch = get_batch(test_x, 64, i).to(device) patch = (patch + model(patch)) place_batch(test_x, patch, 64, i) im1 = get_img(test_x.numpy(), shape, im1.shape) im1 = itrans(im1, k) img = img + im1 * (1. / ntrans) cv2.imwrite(OUTPATH + direc[j], img) crit = psnr(valid_gt[j], img) crit2 = psnr(valid_gt[j], valid_gp[j]) gain.append(crit - crit2) validation_loss.append(crit) gp.append(crit2) print("Image", direc[j], "GP", crit2, "GP+CNN", crit)
def lpips_analysis(gt, srs, scale): from collections import OrderedDict results = OrderedDict() gt = imread(gt) h, w, _ = gt.shape gt = gt[:(h // 8) * 8, :(w // 8) * 8] srs = [imread(sr) for sr in srs] lpipses_sp = [] lpipses_gl = [] lrpsnrs = [] n_samples = len(srs) for sample_idx in tqdm.trange(n_samples): sr = srs[sample_idx] h1, w1, _ = gt.shape sr = sr[:h1, :w1] lpips_sp = loss_fn_alex_sp(2 * t(sr) - 1, 2 * t(gt) - 1) lpipses_sp.append(lpips_sp) lpipses_gl.append(lpips_sp.mean().item()) imgA_lr = imresize(sr, 1 / scale) imgB_lr = imresize(gt, 1 / scale) lrpsnr = psnr(imgA_lr, imgB_lr) lrpsnrs.append(lrpsnr) lpips_gl = np.min(lpipses_gl) results['LPIPS_mean'] = np.mean(lpipses_gl) results['LRPSNR_worst'] = np.min(lrpsnrs) results['LRPSNR_mean'] = np.mean(lrpsnrs) lpipses_stacked = torch.stack([l[0, 0, :, :] for l in lpipses_sp], dim=2) lpips_best_sp, _ = torch.min(lpipses_stacked, dim=2) lpips_loc = lpips_best_sp.mean().item() score = (lpips_gl - lpips_loc) / lpips_gl * 100 results['score'] = score dprint(results) return results
def compute_avg_SSIM_PSNR_numpy(u_true, u_gen, n_mesh, data_range): # assumes images are size n_samples x n_features**2 and are detached n_samples = u_true.shape[0] u_true = u_true.reshape(n_samples, n_mesh, n_mesh) u_gen = u_gen.reshape(n_samples, n_mesh, n_mesh) ssim_val = 0 psnr_val = 0 for j in range(n_samples): ssim_val = ssim_val + ssim( u_true[j, :, :], u_gen[j, :, :], data_range=data_range) psnr_val = psnr_val + psnr( u_true[j, :, :], u_gen[j, :, :], data_range=data_range) return ssim_val / n_samples, psnr_val / n_samples
def cal_psnrssim(img_gt, img_hat): """[summary] Args: img_gt ([type]): H W C img_hat ([type]): [description] Returns: [type]: [description] """ # img_hat=img_hat*img_gt.mean()/img_hat.mean() H, W, C = img_gt.shape img_hat_psnr = psnr(img_gt, img_hat, data_range=img_gt.max()) img_hat_ssim = ssim(img_gt, img_hat, data_range=img_gt.max(), multichannel=True) return img_hat_psnr, img_hat_ssim
def test_single(net, img, target, image_name, model_name, test_image): net.eval() tens = transforms.ToTensor() h, w, c = img.shape inp = tens(img).float() inp = inp.view((1, c, h, w)) output = net(inp) o = output.view((c, h * 4, w * 4)) o = o.data.numpy() o = np.swapaxes(o, 0, 1) o = np.swapaxes(o, 1, 2) bicub_res = rescale(img, (4, 4, 1), anti_aliasing=True) result = np.clip(o + bicub_res, 0., 1.) result = np.clip(correct_color_shift(target, result, samples=100), 0., 1.) if result.shape != target.shape: w1, h1, c1 = result.shape w2, h2, c2 = target.shape if w1 < w2: target = target[0:w1, :, :] elif w1 > w2: result = result[0:w2, :, :] if h1 < h2: target = target[:, 0:h1, :] elif h1 > h2: result = result[:, 0:h2, :] # PSNR score = psnr(result, target) * 1.10326 sim = ssim(result, target, multichannel=True) if image_name == test_image: fig, ax1 = plt.subplots(1, 1) ax1.imshow(result) ax1.set_title(model_name) plt.show() #plt.imsave('results_reconstr/{m}/{n}'.format(m=os.path.splitext(model_name)[0], n=image_name), result) """if model_name == 'ENet-E.pth': io.imsave('quality_assessment/E/{x}'.format(x=image_name), result) elif model_name == 'ENet-PAT.pth': io.imsave('quality_assessment/PAT/{x}'.format(x=image_name), result)""" #print('Image name: %s PSNR: %f SSIM: %f' % (image_name, score, sim)) return score, sim
def quality(truth, recon): """ ALEX NOTE: Modified it so it uses sci-kit image's psnr, and also added data_range parameters """ # for fixed images truth and reconstruction, evaluates average l2 value and ssim score amount_images = truth.shape[0] # recon = cut_image(recon) l2 = np.average(np.sqrt(np.sum(np.square(truth - recon), axis=(1, 2, 3)))) psn = 0 for k in range(amount_images): # psn = psn + psnr(truth[k,...,0], cut_image(recon[k,...,0]), data_range=1) psn = psn + psnr(truth[k, ..., 0], recon[k, ..., 0], data_range=1) psn = psn / amount_images ssi = 0 for k in range(amount_images): # ssi = ssi + ssim(truth[k,...,0], cut_image(recon[k,...,0]), data_range=1) ssi = ssi + ssim(truth[k, ..., 0], recon[k, ..., 0], data_range=1) ssi = ssi / amount_images return [l2, psn, ssi]
def evaluate(args): path_in = args.data_dir_in path_tar = args.data_dir_tar file_in = sorted(os.listdir(path_in)) file_tar = sorted(os.listdir(path_tar)) len_list_in = len(file_in) # calculate PSNR, SSIM psnr_avg = 0 ssim_avg = 0 ssim_avg_self = 0 # SSIM_func = SSIM().cuda() for i in range(len_list_in): list_in = os.path.join(path_in, file_in[i]) # list_tar = os.path.join(path_tar, file_tar[i//15]) list_tar = os.path.join(path_tar, file_tar[i]) img_in = cv2.imread(list_in) img_tar = cv2.imread(list_tar) mse = ((img_in - img_tar)**2).mean() # psnr_tmp = 10 * log10(255 * 255 / (mse + 10 ** (-10))) # psnr_avg += psnr_tmp psnr_tmp = psnr(img_in, img_tar, data_range=255) psnr_avg += psnr_tmp ssim_tmp = ssim(img_in, img_tar, data_range=255, multichannel=True) ssim_avg += ssim_tmp # img_in_torch, img_tar_torch = RGB_np2tensor(img_in, img_tar) # c, h, w = img_in_torch.shape # img_in_torch = torch.reshape(img_in_torch, (1, c, h, w)) # img_tar_torch = torch.reshape(img_tar_torch, (1, c, h, w)) # ssim_tmp_self = SSIM_func(img_in_torch, img_tar_torch) # ssim_avg_self += ssim_tmp_self print('%s: PSNR = %2.5f, SSIM = %2.5f' % (file_in[i], psnr_tmp, ssim_tmp)) psnr_avg = psnr_avg / len_list_in ssim_avg = ssim_avg / len_list_in # ssim_avg_self = ssim_avg_self / len_list_in print('avg psnr = %2.5f, avg SSIM = %1.5f' % (psnr_avg, ssim_avg))
def compute_metric(self): self.send_work_count_msg(len(self.opts.video_names)) psnr_all = np.zeros(len(self.opts.video_names)) for v in range(len(self.opts.video_names)): video_name = self.opts.video_names[v] num_frames = self.opts.video_frame_counts[v] for t in range(num_frames): cur_gt_frame = get_gt_frame(self.opts.gt_root, video_name, t) cur_gt_frame = pil_rgb_to_numpy(cur_gt_frame) cur_comp_frame = get_comp_frame(self.opts.gt_root, self.opts.pred_root, video_name, t) cur_comp_frame = pil_rgb_to_numpy(cur_comp_frame) psnr_all[v] += psnr(cur_gt_frame, cur_comp_frame, data_range=1) / num_frames self.send_update_msg(1) self.send_result_msg(psnr_all)
def compute_statistics(data, p, A, reg_func, parametrisation, params): recons = [] chunks = data['y'].split(10) for chunk in chunks: S, alpha, eps = parametrisation(torch.tensor(p, device=chunk.device), params) if 'xinit' in params['alg_params']['ll_sol'] and params['alg_params'][ 'll_sol']['xinit'].shape[0] != chunk.shape[0]: del params['alg_params']['ll_sol']['xinit'] chunk_recon = lower_level_solver(chunk, S, alpha, eps, A, reg_func, params['alg_params']) recons.append(chunk_recon.to('cpu')) recons = torch.cat(recons, dim=0) ssims = [] psnrs = [] for i in range(recons.shape[0]): abs_recon = torch.sqrt(torch.sum(recons[i, :, :, :]**2, dim=2)).numpy() abs_clean = torch.sqrt(torch.sum(data['x'][i, :, :, :]**2, dim=2)).cpu().numpy() ssims.append(ssim(abs_clean, abs_recon)) psnrs.append(psnr(abs_clean, abs_recon)) results = {'recons': recons, 'ssims': ssims, 'psnrs': psnrs} return results
def compute_metric(self): self.send_work_count_msg(len(self.opts.video_names)) pcons_psnr_mask_all = np.zeros(len(self.opts.video_names)) for v in range(len(self.opts.video_names)): video_name = self.opts.video_names[v] num_frames = self.opts.video_frame_counts[v] comp_frame_a = get_comp_frame(self.opts.gt_root, self.opts.pred_root, video_name, 0) comp_frame_a = pil_rgb_to_numpy(comp_frame_a) mask_frame_a = get_mask_frame(self.opts.gt_root, video_name, 0) mask_frame_a = pil_binary_to_numpy(mask_frame_a) for t in range(1, num_frames): comp_frame_b = get_comp_frame(self.opts.gt_root, self.opts.pred_root, video_name, t) comp_frame_b = pil_rgb_to_numpy(comp_frame_b) mask_frame_b = get_mask_frame(self.opts.gt_root, video_name, t) mask_frame_b = pil_binary_to_numpy(mask_frame_b) # Extract a patch around the center of mass of the missing region (or a corner of the image if the # center is too close to the edge) rows, cols = np.where(mask_frame_a == 0) a_sy = floor(rows.mean() - self.opts.sim_cons_ps / 2) a_sx = floor(cols.mean() - self.opts.sim_cons_ps / 2) a_sy = np.clip(a_sy, 0, comp_frame_a.shape[0] - self.opts.sim_cons_ps) a_sx = np.clip(a_sx, 0, comp_frame_a.shape[1] - self.opts.sim_cons_ps) ### measure video consistency by finding patch in next frame comp_frame_a_patch = comp_frame_a[a_sy:a_sy + self.opts.sim_cons_ps, a_sx:a_sx + self.opts.sim_cons_ps] best_patch_psnr = 0.0 best_b_sy = None best_b_sx = None for b_sy in range(a_sy - self.opts.sim_cons_sw, a_sy + self.opts.sim_cons_sw): for b_sx in range(a_sx - self.opts.sim_cons_sw, a_sx + self.opts.sim_cons_sw): comp_frame_b_patch = comp_frame_b[ b_sy:b_sy + self.opts.sim_cons_ps, b_sx:b_sx + self.opts.sim_cons_ps] if comp_frame_a_patch.shape != comp_frame_b_patch.shape: # Invalid patch at given location in comp_frame_b, so skip continue patch_psnr = psnr(comp_frame_a_patch, comp_frame_b_patch) if patch_psnr > best_patch_psnr: best_patch_psnr = patch_psnr best_b_sy = b_sy best_b_sx = b_sx best_comp_frame_b_patch = comp_frame_b[best_b_sy:best_b_sy + self.opts.sim_cons_ps, best_b_sx:best_b_sx + self.opts.sim_cons_ps] pcons_psnr_mask_all[v] += psnr( comp_frame_a_patch, best_comp_frame_b_patch) / (num_frames - 1) comp_frame_a = comp_frame_b mask_frame_a = mask_frame_b self.send_update_msg(1) self.send_result_msg(pcons_psnr_mask_all)
def compute_psnr(im1, im2): p = psnr(im1, im2) return p
def test_synthetic(args): my_model = Deraining(args) my_model = nn.DataParallel(my_model) my_model.cuda() my_model.load_state_dict(torch.load(args.pretrained_model)) # print("model: \n",my_model) test_dataloader = get_testdataset(args) my_model.eval() avg_psnr = 0 avg_ssim = 0 count = 0 total_time = 0 for idx, (rain_img, keypoints_in, clean_img_LR, clean_img_HR, rain_img_name) in enumerate(test_dataloader): count = count + 1 with torch.no_grad(): rain_img = Variable(rain_img.cuda()) clean_img_HR = Variable(clean_img_HR.cuda()) keypoints_in = Variable(keypoints_in.cuda()) start = time.time() output, out_combine, clean_layer, add_layer, mul_layer, add_res, mul_res = my_model( rain_img, keypoints_in) end = time.time() print("infer time: ", (end - start)) total_time += end - start # add_res = rain_img-clean_img_HR#-rain_img # mul_res = rain_img/clean_img_HR#/rain_img save_dir = args.save_dir_syn_data # mean = [0.485, 0.456, 0.406] # std = [0.229, 0.224, 0.225] mean = [0, 0, 0] std = [1, 1, 1] output = output.cpu() output = output.data.squeeze(0) for t, m, s in zip(output, mean, std): t.mul_(s).add_(m) output = output.numpy() output *= 255.0 output = output.clip(0, 255) output = output.transpose(1, 2, 0) out = np.uint8(output) ensure_dir(save_dir + '/out_img') cv2.imwrite(save_dir + '/out_img/out_%s' % (rain_img_name[0]), out) # cv2.cvtColor(out, cv2.COLOR_BGR2RGB)) out_combine = out_combine.cpu() out_combine = out_combine.data.squeeze(0) for t, m, s in zip(out_combine, mean, std): t.mul_(s).add_(m) out_combine = out_combine.numpy() out_combine *= 255.0 out_combine = out_combine.clip(0, 255) out_combine = out_combine.transpose(1, 2, 0) comb = np.uint8(out_combine) ensure_dir(save_dir + '/comb_img') cv2.imwrite(save_dir + '/comb_img/comb_%s' % (rain_img_name[0]), comb) # cv2.cvtColor(comb, cv2.COLOR_BGR2RGB)) clean_layer = clean_layer.cpu() clean_layer = clean_layer.data.squeeze(0) for t, m, s in zip(clean_layer, mean, std): t.mul_(s).add_(m) clean_layer = clean_layer.numpy() clean_layer *= 255.0 clean_layer += 30 clean_layer = clean_layer.clip(0, 255) clean_layer = clean_layer.transpose(1, 2, 0) clean = np.uint8(clean_layer) ensure_dir(save_dir + '/clean_img') cv2.imwrite(save_dir + '/clean_img/clean_%s' % (rain_img_name[0]), clean) # cv2.cvtColor(out, cv2.COLOR_BGR2RGB)) add_layer = add_layer.cpu() add_layer = add_layer.data.squeeze(0) for t, m, s in zip(add_layer, mean, std): t.mul_(s).add_(m) add_layer = add_layer.numpy() add_layer *= 255.0 add_layer += 30 add_layer = add_layer.clip(0, 255) add_layer = add_layer.transpose(1, 2, 0) add = np.uint8(add_layer) ensure_dir(save_dir + '/add_img') cv2.imwrite(save_dir + '/add_img/add_%s' % (rain_img_name[0]), add) # cv2.cvtColor(out, cv2.COLOR_BGR2RGB)) mul_layer = mul_layer.cpu() mul_layer = mul_layer.data.squeeze(0) for t, m, s in zip(mul_layer, mean, std): t.mul_(s).add_(m) mul_layer = mul_layer.numpy() mul_layer *= 255.0 mul_layer += 30 mul_layer = mul_layer.clip(0, 255) mul_layer = mul_layer.transpose(1, 2, 0) mul = np.uint8(mul_layer) ensure_dir(save_dir + '/mul_img') cv2.imwrite(save_dir + '/mul_img/mul_%s' % (rain_img_name[0]), mul) # cv2.cvtColor(out, cv2.COLOR_BGR2RGB)) add_res = add_res.cpu() add_res = add_res.data.squeeze(0) for t, m, s in zip(add_res, mean, std): t.mul_(s).add_(m) add_res = add_res.numpy() add_res *= 255.0 add_res = add_res.clip(0, 255) add_res = add_res.transpose(1, 2, 0) a_res = np.uint8(add_res) ensure_dir(save_dir + '/add_res_img') cv2.imwrite(save_dir + '/add_res_img/add_%s' % (rain_img_name[0]), a_res) # cv2.cvtColor(out, cv2.COLOR_BGR2RGB)) mul_res = mul_res.cpu() mul_res = mul_res.data.squeeze(0) for t, m, s in zip(mul_res, mean, std): t.mul_(s).add_(m) mul_res = mul_res.numpy() mul_res *= 255.0 mul_res = mul_res.clip(0, 255) mul_res = mul_res.transpose(1, 2, 0) m_res = np.uint8(mul_res) ensure_dir(save_dir + '/mul_res_img') cv2.imwrite(save_dir + '/mul_res_img/mul_%s' % (rain_img_name[0]), m_res) # cv2.cvtColor(out, cv2.COLOR_BGR2RGB)) # =========== Target Image =============== clean_img_HR = clean_img_HR.cpu() clean_img_HR = clean_img_HR.data.squeeze(0) for t, m, s in zip(clean_img_HR, mean, std): t.mul_(s).add_(m) clean_img_HR = clean_img_HR.numpy() # clean_img - ground truth clean_img_HR *= 255.0 clean_img_HR = clean_img_HR.clip(0, 255) clean_img_HR = clean_img_HR.transpose(1, 2, 0) clean_img_HR = np.uint8(clean_img_HR) cv2.imwrite('results/GT/GT_%s' % (rain_img_name[0]), clean_img_HR) psnr_val = psnr(out, clean_img_HR, data_range=255) avg_psnr += psnr_val ssim_val = ssim(out, clean_img_HR, data_range=255, multichannel=True, gaussian_weights=True) avg_ssim += ssim_val log = "{}:\t PSNR = {:.5f}, SSIM = {:.5f}".format( rain_img_name[0], psnr_val, ssim_val) # print('%s: PSNR = %2.5f, SSIM = %2.5f' %(rain_img_name, psnr_val, ssim_val)) print(log) avg_psnr /= (count) avg_ssim /= (count) print('AVG PSNR = %2.5f, Average SSIM = %2.5f' % (avg_psnr, avg_ssim)) print('total time: ', total_time)
def PSNR(img1: Image, img2: Image): img1 = np.array(img1) img2 = np.array(img2) return psnr(img1, img2)
def main(): os.chdir(os.path.dirname(os.path.abspath(__file__))) if not len(sys.argv) == 2: raise Exception('Error!: two arguments are required') filename = sys.argv[1] body = re.split(r'\.', filename)[0] img = readimg(filename) os.makedirs("./images/" + body, exist_ok=True) plt.figure(figsize=(6, 4)) plt.subplot(1, 2, 1) plt.imshow(img, vmin=0, vmax=255) plt.title("original") plt.gray() image = image_JPEG(N) qdct_coef, compressed_img = image.compress(img) print("Compress image in conventional JPEG") print("\nnumber of dct with zero as its coefficient", image.sparse_num, "/", img.size) plt.subplot(1, 2, 2) plt.imshow(compressed_img, vmin=0, vmax=255) plt.title("jpeg") print("\nentropy :", calc_entropy(qdct_coef)) img = readimg(filename) print("PSNR :", psnr(img, compressed_img, data_range=255)) print("SSIM :", ssim(img, compressed_img, data_range=255)) cv2.imwrite('./images/' + body + "/" + body + '_jpeg_compress.bmp', compressed_img) print("\n\nCompress images in JPEG format with L1 regularization") lams = [0.01, 0.1, 1, 5] for i, lam in enumerate(lams): print("\n------------------------------------\n") sparse = image_sparse(N) dct_coef, compressed_img = sparse.compress(img, lam) print("lambda=", lam) print("number of L1 with zero as its coefficient", sparse.sparse_num, "/", img.size) plt.subplot(2, 2, i + 1) plt.imshow(compressed_img, vmin=0, vmax=255) plt.title("lambda={}".format(lam)) print("\nentropy :", calc_entropy(dct_coef)) img = readimg(filename) print("PSNR :", psnr(img, compressed_img, data_range=255)) print("SSIM :", ssim(img, compressed_img, data_range=255)) cv2.imwrite( './images/' + body + "/" + body + '_jpeg_compress_with_lasso(lambda=' + str(lam) + ').bmp', compressed_img) plt.suptitle("JPEG format with L1 regularization") plt.show()
#cv2.imwrite('img_deblu.png', img_deblu) #cv2.imwrite('img_sharp.png', img_sharp) img_sharp = img_sharp[:, :, [2, 1, 0]] img_deblu = img_deblu[:, :, [2, 1, 0]] # plt.figure() # plt.imshow(img_sharp/255) # plt.title('sharp') # # plt.figure() # plt.imshow(img_deblu/255) # plt.title('denoise') # plt.show() psnr_n = psnr(img_sharp, img_deblu, data_range=255) ssim_n = ssim(img_deblu / 255, img_sharp / 255, gaussian_weights=True, multichannel=True, use_sample_covariance=False, sigma=1.5) if name_sharp[-7:-4] == "001": print(name_sharp, (psnr_n, ssim_n)) sharp = Image.fromarray(np.uint8(img_sharp)) deblu = Image.fromarray(np.uint8(img_deblu)) sharp_ts = TF.to_tensor(sharp).unsqueeze(0) deblu_ts = TF.to_tensor(deblu).unsqueeze(0) sharp_ts, deblu_ts = sharp_ts/255.0, deblu_ts/255.0 vif_n = piq.vif_p(deblu_ts, sharp_ts) vsi_n = piq.vsi(deblu_ts, sharp_ts) haar_n = piq.haarpsi(deblu_ts, sharp_ts)