def display_perturbation(runner, f, im_nbr=0, cmap='gray'): r = runner.r[-1] batch = runner.x0[-1] # mask = runner.mask[0]; fx = f(batch) fxr = f(batch + r) x_i1 = from_lasagne_format(batch) r_i1 = from_lasagne_format(r) fx_i1 = from_lasagne_format(fx) fxr_i1 = from_lasagne_format(fxr) x_i = x_i1[im_nbr] r_i = r_i1[im_nbr] fx_i = fx_i1[im_nbr] fxr_i = fxr_i1[im_nbr] nx = l2_norm_of_tensor(x_i) nr = l2_norm_of_tensor(r_i) nf_diff = l2_norm_of_tensor(fx_i - fxr_i) print('|f(x)-f(x+r)|: %g, |r|: %g, |f(x)-f(x+r)|/|r|: %g |r|/|x|: %g' % \ (abs(nf_diff), nr, abs(nf_diff)/nr, nr/nx )) plt.subplot(231) plt.matshow(abs(x_i), cmap=cmap, fignum=False) plt.title('|x|') plt.colorbar() plt.subplot(232) plt.matshow(abs(x_i + r_i), cmap=cmap, fignum=False) plt.title('|x+r|') plt.colorbar() plt.subplot(233) plt.matshow(abs(r_i), cmap=cmap, fignum=False) plt.title('|r|') plt.colorbar() plt.subplot(234) plt.matshow(abs(fx_i), cmap=cmap, fignum=False) plt.title('|f(x)|') plt.colorbar() plt.subplot(235) plt.matshow(abs(fxr_i), cmap=cmap, fignum=False) plt.title('|f(x+r)|') plt.colorbar() plt.subplot(236) plt.matshow(abs(fxr_i - fx_i), cmap=cmap, fignum=False) plt.title('|f(x+r)-f(x)|') plt.colorbar() plt.show(block=True)
def export_data(self, fname, r_idx): """ Save the images and perturbations as .mat files. """ r = self.r[r_idx] x0 = self.x0[0] mask = self.mask[0] r = from_lasagne_format(r) x0 = from_lasagne_format(x0) scipy.io.savemat(fname + 'x0.mat', mdict={'x0': x0}) scipy.io.savemat(fname + 'r.mat', mdict={'r': r}) scipy.io.savemat(fname + 'mask.mat', mdict={'mask': mask})
def find_adversarial_perturbation(self, f, dQ, batch, only_real=False): """ Search for adversarial perturbation. An extension of Runner.find_adversarial_perturbation(...) with the additional parameter ``only_real``, which makes the algorithm only search for real adversarial perturbations. :param only_real: Search only for real perturbations. """ if only_real: r_is_empty = not self.r # isempty(r) if (r_is_empty): ps_factor = self.perp_start_factor if (self.perp_start == 'rand'): rr = ps_factor * np.random.rand( *batch.shape).astype('float32') elif (self.perp_start == 'randn'): rr = ps_factor * np.random.randn( *batch.shape).astype('float32') elif (self.perp_start == 'ones'): rr = ps_factor * np.ones(batch.shape).astype('float32') else: # "off" rr = ps_factor * np.zeros(batch.shape).astype('float32') rr = to_lasagne_format(np.real(from_lasagne_format(rr))) self.r.append(rr) Runner.find_adversarial_perturbation(self, f, dQ, batch)
def hand_f(f, im, mask): im = from_lasagne_format(im) im_und, k_und = cs.undersample(im, mask, centred=False, norm='ortho') im_und_l = to_lasagne_format(im_und) k_und_l = to_lasagne_format(k_und) mask_l = to_lasagne_format(mask, mask=True) pred = f(im_und_l, mask_l, k_und_l) return pred
def convert_runner_to_matlab_format(runner_id, data_path=deep_mri_runner_path): fname = join(data_path, 'data', 'runner_%d.npz' % runner_id) fname_out = join(data_path, 'data_mat', 'runner_%d.mat' % runner_id) data = np.load(fname) out_dict = {} for key in data.keys(): out_dict[key] = data[key] r1 = out_dict['r'] n, batch_size, ch, Ny, Nx = r1.shape r = np.zeros([n, batch_size, Ny, Nx], dtype='complex64') for i in range(n): r[i] = from_lasagne_format(r1[i]) out_dict['x0'] = from_lasagne_format(data['x0']) out_dict['r'] = r sio.savemat(fname_out, out_dict)
def hand_dQ(df, im, r, mask, pred, la): imr = from_lasagne_format(im + r) im_und, k_und = cs.undersample(imr, mask, centred=False, norm='ortho') im_und_l = to_lasagne_format(im_und) k_und_l = to_lasagne_format(k_und) mask_l = to_lasagne_format(mask, mask=True) dr = df(im_und_l, mask_l, k_und_l, pred) dr = dr - la * r return dr
learning_rate: %g ps: %s ps_factor: %g ws: %s ws_factor: %g max_itr: %g """ % (runner_id, runner.la, runner.momentum, runner.learning_rate, runner.perp_start, runner.perp_start_factor, runner.warm_start, runner.warm_start_factor, runner.max_itr)) max_itr = runner.max_itr pert_list = [] print(len(runner.r)) for i in range(len(runner.r)): pert = from_lasagne_format(runner.r[i]) pert_im = pert[im_nbr] print('i: %d, norm: %g' % (i, np.linalg.norm(pert_im, 'fro'))) pert_list.append(pert_im) relevant_perturbations = [0, 4, 8, 12] N = pert_im.shape[0] X = np.zeros([len(relevant_perturbations)] + list(pert_im.shape), dtype=pert_im.dtype) for i in range(len(relevant_perturbations)): X[i, :, :] = pert_list[relevant_perturbations[i]] vmax = np.amax(abs(X)) vmin = np.amin(abs(X)) print('vmin: ', vmin)
validate_batches += 1 if args.debug and validate_batches == 20: break # Testing vis = [] test_err = 0 base_psnr = 0 test_psnr = 0 test_batches = 0 for im in iterate_minibatch(test, batch_size, shuffle=False): im_und, k_und, mask, im_gnd = prep_input(im, acc=acc) err, pred = val_fn(im_und, mask, k_und, im_gnd) test_err += err for im_i, und_i, pred_i in zip(im, from_lasagne_format(im_und), from_lasagne_format(pred)): base_psnr += complex_psnr(im_i, und_i, peak='max') test_psnr += complex_psnr(im_i, pred_i, peak='max') test_batches += 1 if save_fig and test_batches % save_every == 0: vis.append((im[0], from_lasagne_format(pred)[0], from_lasagne_format(im_und)[0], from_lasagne_format(mask, mask=True)[0])) if args.debug and test_batches == 20: break t_end = time.time()
# f(input_var, mask, k_space) largest_nth_psnr_value = 100 array_of_worst_psnr = largest_nth_psnr_value * np.ones(nbr_of_saved_worst) array_of_worst_pred = np.zeros([nbr_of_saved_worst, N, N], dtype='complex64') array_of_worst_noisy_images = np.zeros([nbr_of_saved_worst, N, N], dtype='complex64') array_of_worst_raw_noise = np.zeros([nbr_of_saved_worst, batch_size, N, N], dtype='complex64') array_of_psnr_values = np.zeros(nbr_itr) for itr in range(nbr_itr): print('%3d/%3d' % (itr, nbr_itr)) noise_raw = np.random.normal(0, 10, size=input_shape) noise_raw_complex = from_lasagne_format(noise_raw) noise_und, k_und = cs.undersample(noise_raw_complex, mask, centred=False, norm='ortho') #noise_raw = np.random.uniform(size=r_list[0].shape).astype('float32') #noise_raw_complex = from_lasagne_format(noise_raw) #k_und = mask*noise_raw_complex noise_und = np.fft.ifft2(k_und, norm='ortho') norm_noise_und = l2_norm_of_tensor(noise_und) scaled_noise2 = (factor * norm_r1 * noise_und) / norm_noise_und data = from_lasagne_format(x0) im2_noisy = data + scaled_noise2
subsamp = np.zeros(n) psnr_arr = np.zeros([n, batch_size]) # Do first iteration outside loop; undersampling_rate = us_rate[0] mask, idx = inc_cartesian_mask((batch_size, Nx, Ny), undersampling_rate, sample_n=8) im_und, k_und = cs.undersample(im, mask, centred=False, norm='ortho') im_und_l = to_lasagne_format(im_und) k_und_l = to_lasagne_format(k_und) mask_l = to_lasagne_format(mask, mask=True) pred = f(im_und_l, mask_l, k_und_l) pred = from_lasagne_format(pred) psnr_values = np.zeros(batch_size) for i in range(batch_size): psnr_arr[0, i] = compute_psnr(pred[i], im[i]) print('psnr_arr: ', psnr_arr[0]) subsamp[0] = 1. / undersampling_rate amask = np.squeeze(mask[0, :, :]) plt.imsave(os.path.join(mask_dest, "mask_k_%d.png" % 0), amask, cmap='gray') set_of_all_masks = np.zeros([len(us_rate), N, N]) set_of_all_masks[0, :, :] = amask
norm_r2 = l2_norm_of_tensor(r2) norm_r3 = l2_norm_of_tensor(r3) print(norm_r2 / norm_r1) batch_size = x0.shape[0] # 2 input_shape = [batch_size, 2, N, N] # Load and compile network net_config, net = load_network(input_shape, network_path) f = compile_f(net, net_config) # f(input_var, mask, k_space) noise_raw = np.random.normal(0, 1, size=r_list[0].shape) noise_raw_complex = from_lasagne_format(noise_raw) noise_und, k_und = cs.undersample(noise_raw_complex, mask, centred=False, norm='ortho') norm_noise_und = l2_norm_of_tensor(noise_und) scaled_noise1 = (factor * norm_r1 * noise_und) / norm_noise_und scaled_noise2 = (factor * norm_r2 * noise_und) / norm_noise_und scaled_noise3 = (factor * norm_r3 * noise_und) / norm_noise_und data = from_lasagne_format(x0) r1_no_l = from_lasagne_format(r1) r2_no_l = from_lasagne_format(r2) r3_no_l = from_lasagne_format(r3) im1_noisy = data + r1_no_l + scaled_noise1
if args.debug and validate_batches == 20: break vis = [] test_err = 0 base_psnr = 0 test_psnr = 0 test_batches = 0 for im in iterate_minibatch(test, batch_size, shuffle=False): im_und, k_und, mask, im_gnd = prep_input(im, acc=acc) err, pred = val_fn(im_und, mask, k_und, im_gnd) test_err += err for im_i, und_i, pred_i in zip(im, from_lasagne_format(im_und), from_lasagne_format(pred)): base_psnr += complex_psnr(im_i, und_i, peak='max') test_psnr += complex_psnr(im_i, pred_i, peak='max') test_batches += 1 if save_fig and test_batches % save_every == 0: vis.append((im[0], from_lasagne_format(pred)[0], from_lasagne_format(im_und)[0], from_lasagne_format(mask, mask=True)[0])) if args.debug and test_batches == 20: break t_end = time.time()
def save_runner(self, f, network_name='DeepMRI', data_path=deep_mri_runner_path): """ Save the current runner object. Reads the current runner count and store the runner object at data_path/data/ as 'runner_count.npz'. In addition the file 'runner_description.txt' (located at data_path) is updated with statistics from the run. :param f: Neural Network. :param network_name: Network Name (str). :param data_path: Where to store the runner_object. The data_path directory should contain a subdirectory called 'data', where the object is stored. :returns: Runner count (int). """ count = self._read_count(data_path) fname = join('data', 'runner_%d.npz' % (count)) fname = join(data_path, fname) #print("len(r): ", len(self.r)) np.savez(fname, x0=self.x0[0], r=self.r, v=self.v, mask=self.mask[0], la=self.la, lr=self.learning_rate, momentum=self.momentum, smoothing_eps=self.smoothing_eps, ws=self.warm_start, wsf=self.warm_start_factor, ps=self.perp_start, psf=self.perp_start_factor, max_itr=self.max_itr, max_r_norm=self.max_r_norm, max_diff_norm=self.max_diff_norm, optimizer=self.optimizer, backlog=self.backlog) x0 = self.x0[0] r = self.r[-1] mask = self.mask[0] batch_size = x0.shape[0] # Evaluate the networks fx = f(x0) fxr = f(x0 + r) x_i1 = from_lasagne_format(x0) r_i1 = from_lasagne_format(r) fx_i1 = from_lasagne_format(fx) fxr_i1 = from_lasagne_format(fxr) fname = join(data_path, "runner_description.txt") outfile = open(fname, 'a') outfile.write(""" -------------------------- %03d: %s -------------------------- opt: %s, mom: %g, learn_rate: %g, smooth_esp: %g, la: %g, max_r_norm: %g, max_diff_norm: %g, max_itr: %d ws: %s, wsf: %g, ps: %s psf: %g ---- ---- """ % (count, network_name, self.optimizer, self.momentum, self.learning_rate, self.smoothing_eps, self.la, self.max_r_norm, self.max_diff_norm, self.max_itr, self.warm_start, self.warm_start_factor, self.perp_start, self.perp_start_factor)) for im_nbr in range(batch_size): x_i = x_i1[im_nbr] r_i = r_i1[im_nbr] fx_i = fx_i1[im_nbr] fxr_i = fxr_i1[im_nbr] nx = l2_norm_of_tensor(x_i) nr = l2_norm_of_tensor(r_i) n_diff = l2_norm_of_tensor(fx_i - fxr_i) next_str = '%02d: |f(x)-f(x+r)|: %g, |r|: %g, |f(x)-f(x+r)|/|r|: %g |r|/|x|: %g\n' % \ (im_nbr, abs(n_diff), nr, abs(n_diff)/nr, nr/nx ) outfile.write(next_str) outfile.close() return count
bd = 5; fID = open(os.path.join(dest, 'description.txt'), 'w'); # Make first perturbation equal to 0; a = np.zeros(runner.r[0].shape, dtype= runner.r[0].dtype); perturbation_list = list(runner.r); perturbation_list[0] = a; # Remove our initial guess perturbation r_0. for i in range(len(runner.r)): #r = to_lasagne_format(np.real(from_lasagne_format(runner.r[i]))); r = perturbation_list[i]; fx = f(x0); fxr = f(x0 + r); x_i1 = from_lasagne_format(x0); r_i1 = from_lasagne_format(r); fx_i1 = from_lasagne_format(fx); fxr_i1 = from_lasagne_format(fxr); x_i = x_i1[im_nbr]; r_i = r_i1[im_nbr]; xpr = x_i+r_i; fx_i = fx_i1[im_nbr]; fxr_i = fxr_i1[im_nbr]; nx = l2_norm_of_tensor(x_i); nr = l2_norm_of_tensor(r_i); nf_diff = l2_norm_of_tensor(fx_i-fxr_i); next_str = '%2d: |f(x)-f(x+r)|: %g, |r|: %g, |f(x)-f(x+r)|/|r|: %g |r|/|x|: %g' % \