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 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
def prep_input(im, acc=4): """Undersample the batch, then reformat them into what the network accepts. Parameters ---------- gauss_ivar: float - controls the undersampling rate. higher the value, more undersampling """ mask = cs.cartesian_mask(im.shape, acc, sample_n=8) im_und, k_und = cs.undersample(im, mask, centred=False, norm='ortho') im_gnd_l = to_lasagne_format(im) im_und_l = to_lasagne_format(im_und) k_und_l = to_lasagne_format(k_und) mask_l = to_lasagne_format(mask, mask=True) return im_und_l, k_und_l, mask_l, im_gnd_l
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 prep_input(im, acc=6): """Undersample the batch, then reformat them into what the network accepts. Parameters ---------- gauss_ivar: float - controls the undersampling rate. higher the value, more undersampling """ #mask = cs.cartesian_mask(im.shape, acc, sample_n=8)##########笛卡儿采样 mask = loadmat(join('./data/mask/mask_radial_015_HFinCenter.mat') )['mask_radial_015_HFinCenter'] im_und, k_und = cs.undersample(im, mask, centred=False, norm='ortho') # print(type(k_und),k_und.shape) # np.save('k_und.mat',k_und) # assert False im_gnd_l = to_lasagne_format(im) im_und_l = to_lasagne_format(im_und) k_und_l = to_lasagne_format(k_und) mask_l = to_lasagne_format(mask, mask=True) return im_und_l, k_und_l, mask_l, im_gnd_l
#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 im_und2, k_und2 = cs.undersample(im2_noisy, mask, centred=False, norm='ortho') mask_l = to_lasagne_format(mask, mask=True) im_und_l2 = to_lasagne_format(im_und2) k_und_l2 = to_lasagne_format(k_und2) pred2 = f(im_und_l2, mask_l, k_und_l2) pred2 = from_lasagne_format(pred2) pred2_single = pred2[im_nbr, :, :] im2_noisy_single = im2_noisy[im_nbr, :, :] psnr_value = compute_psnr(pred2_single, im2_noisy_single) array_of_psnr_values[itr] = psnr_value if psnr_value < largest_nth_psnr_value: idx = np.argmax(array_of_worst_psnr) array_of_worst_psnr[idx] = psnr_value array_of_worst_pred[idx, :, :] = pred2_single
im3_noisy = data + r3_no_l + scaled_noise3 im_und1, k_und1 = cs.undersample(im1_noisy, mask, centred=False, norm='ortho') im_und2, k_und2 = cs.undersample(im2_noisy, mask, centred=False, norm='ortho') im_und3, k_und3 = cs.undersample(im3_noisy, mask, centred=False, norm='ortho') mask_l = to_lasagne_format(mask, mask=True) im_und_l1 = to_lasagne_format(im_und1) k_und_l1 = to_lasagne_format(k_und1) im_und_l2 = to_lasagne_format(im_und2) k_und_l2 = to_lasagne_format(k_und2) im_und_l3 = to_lasagne_format(im_und3) k_und_l3 = to_lasagne_format(k_und3) pred1 = f(im_und_l1, mask_l, k_und_l1) pred1 = from_lasagne_format(pred1) pred2 = f(im_und_l2, mask_l, k_und_l2) pred2 = from_lasagne_format(pred2) pred3 = f(im_und_l3, mask_l, k_und_l3) pred3 = from_lasagne_format(pred3)
# 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) us_rate = range(14,1,-1); n = len(us_rate); subsamp = np.zeros(n); psnr_arr = np.zeros(n); # 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); #print("im.shape: ", im.shape); #print("pred.shape: ", pred.shape); psnr_values = np.zeros(batch_size); for i in range(batch_size): psnr_values[i] = compute_psnr(pred[i], im[i]); subsamp[0] = 1./undersampling_rate; psnr_arr[0] = np.mean(psnr_values); amask = np.squeeze(mask[0,:,:]);
Nx = 256 Ny = 256 input_shape = (batch_size, 2, Nx, Ny) A = scipy.io.loadmat(mask_data_path) mask = A['mask'] net_config, net = load_network(input_shape, network_path) val_fn = compile_f(net, net_config) f = lambda im: hand_f(val_fn, im, mask) dest = 'plots_symbol' cmap = 'gray' fsize_list = [10] for j in range(len(fsize_list)): fsize = fsize_list[j] fname = "deep_symb_fsize_%02d.mat" % (fsize) A = scipy.io.loadmat(join(symbol_data_path, fname)) data = A['Y'] data1 = to_lasagne_format(data) pred = f(data1) pred = from_lasagne_format(pred) im = abs(pred[0, :, :]) fname = 'DeepMRINet_symb_rec_fsize_%02d.png' % (fsize) fname = join(dest, fname) mpimg.imsave(fname, im, cmap=cmap)