def compute_nrmse(img1: np.ndarray, img2: np.ndarray) -> float: """ Computes normalized root mean squared error. See normalized_root_mse docs: https://scikit-image.org/docs/stable/api/skimage.metrics.html#skimage.metrics.normalized_root_mse. Parameters ---------- img1: Image 1. img2: Image 2. Returns ------- Normalized root mean squared error. """ return nrmse(img1, img2, normalization='Euclidean')
def compare_image(metric, ref, image): error = None if metric == 'ssim': error = ssim(image, ref, multichannel=True) if metric == 'mse': error = mse(image, ref) if metric == 'rmse': error = rmse(image, ref) if metric == 'nrmse': error = nrmse(image, ref) return error
def reconstructionAttack(model, alpha=5000, beta=100, gamma=0.01, delta=0.1, save=True, show=False): dae = False if (model.name == 'DAESoftMax'): dae = True # reload model model.load_state_dict(torch.load('models/' + model.name + '_model.pt')) # performance measures startTime = time.time() timeStr = time.strftime("%H:%M:%S", time.localtime(startTime)) mse_all, nrmsev_all, ssmv_all, epochs = [], [], [], [] # SDG criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=delta) test_x = get_orig() rec_x = np.zeros((40, 112, 92), dtype='float32') process = False print('DAE Flag is', dae) # if DAE images have different size and process is true encoder = Autoencoder(0) if (dae): test_x = encoder.encode(torch.Tensor(test_x)) rec_x = np.zeros((40, 300), dtype='float32') for c in classes: print('\nReconstructing class', c) best_x, best_cost = '', 1 if (dae): img = np.zeros_like(test_x[0].detach().numpy()) else: img = np.zeros_like(test_x[0]) ssmv, msev, nrmsev = 0, 0, 0 rec, orig = '', '' if (dae): process = True best_x = img = np.zeros((1, 300), dtype='float32') else: np.zeros_like(test_x[0]) b = beta for i in range(alpha): best_cost, best_x, b, img, stop = invert(model, img, criterion, optimizer, delta, c_to_i(c), best_cost, best_x, i, b, beta, gamma, processing=process) if stop: epochs.append(i) break if (dae): orig = test_x[c_to_i(c)].detach().numpy() rec = best_x.reshape(300) rec_x[c_to_i(c)] = rec else: orig = test_x[c_to_i(c)] rec = best_x.reshape(112, 92) rec_x[c_to_i(c)] = rec ssmv = ssm(rec, orig) msev = mse(rec, orig) nrmsev = nrmse(rec, orig) mse_all.append(msev) nrmsev_all.append(nrmsev) ssmv_all.append(ssmv) if (show or save): if (dae): rec = encoder.decode(torch.Tensor([rec])).view( 112, 92).detach().numpy() orig = encoder.decode(torch.Tensor([orig])).view( 112, 92).detach().numpy() fig = plt.figure(figsize=(10, 4)) fig.suptitle("SSM: {:.1e}, NRMSE: {:.1f}".format(ssmv, nrmsev)) ax1 = fig.add_subplot(1, 2, 1) ax1.imshow(rec, cmap='gray') ax2 = fig.add_subplot(1, 2, 2) ax2.imshow(orig, cmap='gray') plt.savefig(f'./data/results/' + model.name + '_class_' + c + '.png') if show: plt.show() endTime = time.time() dur = endTime - startTime print("Duration in sec: " + str(int(dur))) # Calculating means performance values of all images print("\nAverage performance", model.name) print('MSE mean', np.mean(mse_all), 'with std of ', np.std(mse_all)) print('NRMSE mean', np.mean(nrmsev_all), 'with std of +/-', np.std(nrmsev_all)) print('SSM mean', np.mean(ssmv_all), 'with std of +/-', np.std(ssmv_all)) print('Epochs mean', np.mean(epochs), 'with std of +/-', np.std(epochs))
def process(tensor): encoder = Autoencoder(0) img = encoder.decode(tensor) img = img.view(112, 92) img2 = denoise_nl_means(img.detach().numpy(), patch_size=5, patch_distance=5, h=0.3) img3 = unsharp_mask(img2, radius=2, amount=1, preserve_range=True) output = encoder.encode(torch.Tensor([img3])) return output img = torch.Tensor(img) # ¿processing evtl. hier if not img.requires_grad: img.requires_grad = True optim.zero_grad() pred = model(img) loss = crit(pred, torch.LongTensor([c])) loss.backward() img = torch.clamp(img - lr * img.grad, 0, 255) if (processing): img = process(img) if loss.detach().numpy() < best_loss and i > 4: best_loss = loss.detach().numpy() best_x = img.detach().numpy() np_a = np.array([ np.clip(x + np.random.normal(2, 2), 0, 255) for x in img.detach().numpy() ]) return best_loss, best_x, np_a #.reshape(1, -1) startTime = time.time() timeStr = time.strftime("%H:%M:%S", time.localtime(startTime)) print(f'Starting at {timeStr} to invert {model.name}...') encoder = Autoencoder(0) model.load_state_dict(torch.load(f'./models/{model.name}_model.pt')) crit = torch.nn.CrossEntropyLoss() optim = torch.optim.SGD(model.parameters(), lr=lrMod) ssm_vs, nrmse_vs = [], [] original_imgs = torch.Tensor(get_orig()) test_x = encoder.encode(original_imgs) rec_x = np.zeros((40, 300), dtype='float32') for i, c in enumerate(classes): best_loss = float('inf') best_x = img = np.zeros((1, 300), dtype='float32') for epoch in range(nStep): # clear_output(wait=True) # print("Starting at " + timeStr + " to invert " + model.name + "...") # print(f'class {c} ({i+1}/{len(classes)})') # print(f'\tepoch {epoch}') best_loss, best_x, img = invertClass(model, crit, optim, img, lrInv, c_to_i(c), best_loss, best_x, epoch, processing) if (verbose and epoch % 5 == 0): print(f'epoch: {epoch}, best_loss. {best_loss}') orig = test_x[c_to_i(c)].detach().numpy() rec = best_x.reshape(300) rec_x[c_to_i(c)] = rec ssm_v = ssm(rec, orig) nrmse_v = nrmse(rec, orig) ssm_vs.append(ssm_v) nrmse_vs.append(nrmse_v) if (show or save): encoder = Autoencoder(0) rec_show = encoder.decode(torch.Tensor([rec])).view( 112, 92).detach().numpy() orig_show = encoder.decode(torch.Tensor([orig])).view( 112, 92).detach().numpy() fig = plt.figure(figsize=(10, 4)) fig.suptitle("SSM: {:.1e}, NRMSE: {:.1f}".format(ssm_v, nrmse_v)) ax1 = fig.add_subplot(1, 2, 1) ax1.imshow(rec_show, cmap='gray') ax2 = fig.add_subplot(1, 2, 2) ax2.imshow(orig_show, cmap='gray') if show: plt.show() if save: plt.savefig(f'./data/results/class_{c}.png') # if (c=='s12'): break endTime = time.time() dur = endTime - startTime timeStr = time.strftime("%H:%M:%S", time.localtime(endTime)) print(f'Finished at {timeStr}, duration in sec: {int(dur)}') if plot: fig = plt.figure(figsize=(10, 3)) fig.suptitle(f'Model: {model.name}') ax1 = fig.add_subplot(1, 2, 1) ax1.plot(ssm_vs) ax1.set_ylabel('Structural Similarity') ax1.set_xlabel('class index') ax2 = fig.add_subplot(1, 2, 2) ax2.plot(nrmse_vs) ax2.set_ylabel('Normalized Root MSE') ax2.set_xlabel('class index') plt.show() print("SSM: mean {:.2e}, std {:.2e}".format(np.mean(ssm_vs), np.std(ssm_vs))) print("NRMSE: mean {:.2e}, std {:.2e}".format(np.mean(nrmse_vs), np.std(nrmse_vs))) # print(test_x.shape) # print(torch.Tensor(rec_x).shape) encoder = Autoencoder(0) test_imgs = original_imgs[0:5] rec_imgs = encoder.decode(torch.Tensor(rec_x[0:5])).view( 5, 112, 92).detach().numpy() show_images(np.concatenate((test_imgs, rec_imgs), axis=0), f'Model: {model.name}')
def main(): parser = argparse.ArgumentParser( description= "Compare multiple image using metric on different estimators") parser.add_argument('--json', type=str, help="json with all build figure data", required=True) args = parser.parse_args() p_json = args.json # extract data from json configuration json_data = None with open(p_json, 'r') as json_file: json_data = json.load(json_file) reference = json_data["reference"] estimators = json_data["estimators"] metric = json_data["metric"] p_folder = os.path.join(json_data['output'], json_data['nsamples'], 'processing') p_output = os.path.join(json_data['output'], json_data['nsamples'], 'metrics') print(f"Comparisons of {reference} with {estimators}") if not os.path.exists(p_output): os.makedirs(p_output) counter = 0 for i, est in enumerate(estimators): # get current expected method for estimator method = json_data['methods'][i] default_estimator_path = os.path.join(p_folder, method, reference) # expected images path have same name images = os.listdir(default_estimator_path) # prepare output filename counter_str = str(counter) while len(counter_str) < 3: counter_str = "0" + counter_str output_filename = os.path.join( p_output, f"{counter_str}_{method}_{reference}_{est}_{metric}.csv") output_file = open(output_filename, 'w') for img in sorted(images): est1_path_image = os.path.join(p_folder, method, reference, img) est2_path_image = os.path.join(p_folder, method, est, img) img_rgb_1 = np.array(Image.open(est1_path_image)) img_rgb_2 = np.array(Image.open(est2_path_image)) scene_name = img.replace('.png', '') if metric == 'ssim': sentence = "{0};{1};{2};{3}\n".format( scene_name, img, est, ssim(img_rgb_1, img_rgb_2, multichannel=True)) output_file.write(sentence) if metric == 'rmse': sentence = "{0};{1};{2};{3}\n".format( scene_name, img, est, rmse(img_rgb_1, img_rgb_2)) output_file.write(sentence) if metric == 'nrmse': sentence = "{0};{1};{2};{3}\n".format( scene_name, img, est, nrmse(img_rgb_1, img_rgb_2)) output_file.write(sentence) if metric == 'mse': sentence = "{0};{1};{2};{3}\n".format( scene_name, img, est, mse(img_rgb_1, img_rgb_2)) output_file.write(sentence) if metric == 'psnr': sentence = "{0};{1};{2};{3}\n".format( scene_name, img, est, psnr(img_rgb_1, img_rgb_2)) output_file.write(sentence) if metric == 'rmse_ssim': sentence = "{0};{1};{2};{3}\n".format( scene_name, img, est, rmse(img_rgb_1, img_rgb_2) / ssim(img_rgb_1, img_rgb_2, multichannel=True)) output_file.write(sentence) if metric == 'firefly': sentence = "{0};{1};{2};{3}\n".format( scene_name, img, est, firefly_error(img_rgb_1, img_rgb_2)) output_file.write(sentence) counter += 1 output_file.close()