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.shear_grid_mask(im.shape[1:], acc, sample_low_freq=True, sample_n=4) mask = np.repeat(mask[np.newaxis], im.shape[0], axis=0) im_und, k_und = cs.undersample(im, mask, centred=False, norm='ortho') im_gnd_l = torch.from_numpy(to_tensor_format(im)) im_und_l = torch.from_numpy(to_tensor_format(im_und)) k_und_l = torch.from_numpy(to_tensor_format(k_und)) mask_l = torch.from_numpy(to_tensor_format(mask, mask=True)) im = im.transpose(0, 2, 3, 1) xf_gnd = fftshift(fft(ifftshift(im, axes=-1), norm='ortho'), axes=-1) xf_gnd = xf_gnd.transpose(0, 3, 1, 2) xf_gnd_l = torch.from_numpy(to_tensor_format(xf_gnd)) return im_und_l, k_und_l, mask_l, im_gnd_l, xf_gnd_l
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 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
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 im_und2, k_und2 = cs.undersample(im2_noisy, mask,
net_config, net = load_network(input_shape, src_network_weights) 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, 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
arr2 = sitk.GetArrayFromImage(img2) arr3 = sitk.GetArrayFromImage(img3) arr4 = sitk.GetArrayFromImage(img4) arr5 = sitk.GetArrayFromImage(img5) arr6 = sitk.GetArrayFromImage(img6) #combining array arr = np.concatenate((arr1, arr2, arr3, arr4, arr5, arr6), axis=0) np.save('image_array', arr) #fft to numpy array fft_arr = np.fft.fft2(arr) fshift_out = np.fft.fftshift(fft_arr) #cartesian mask mask_1 = cs.cartesian_mask(arr.shape, 4, sample_n=10, centred=False) im_und, k_und = cs.undersample(arr, mask_1, centred=False, norm='ortho') #normalising the input and output data features_IMG = tf.keras.utils.normalize(im_und, axis=1) x_train = np.expand_dims(features_IMG, 3) print(np.shape(x_train)) features_KS = tf.keras.utils.normalize(k_und, axis=1) x_ks_train = np.expand_dims(features_KS, 3) Target = tf.keras.utils.normalize(arr, axis=1) y_train = np.expand_dims(Target, 3) mask_data = np.expand_dims(mask_1[1], 3) mask_ksp = mask_data.astype(int) inv_mask = np.bitwise_not(mask_ksp) print(np.shape(mask_data)) #storing data