def example_parallel_2d(): # ------------------ Declare Parameters ------------------ # Volume Parameters: volume_size = 64 volume_shape = [volume_size, volume_size] volume_spacing = [1, 1] # Detector Parameters: detector_shape = 100 detector_spacing = 1 # Trajectory Parameters: number_of_projections = 90 angular_range = np.pi # create Geometry class geometry = GeometryParallel2D(volume_shape, volume_spacing, detector_shape, detector_spacing, number_of_projections, angular_range) geometry.set_trajectory(circular_trajectory.circular_trajectory_2d(geometry)) # Get Phantom phantom = shepp_logan.shepp_logan_enhanced(volume_shape).astype(np.float32) # Add required batch dimension phantom = np.expand_dims(phantom, axis=0) sino = parallel_projection2d(phantom,geometry) @tf.function def test_func_proj(x): return parallel_projection2d(x,geometry) @tf.function def test_func_reco(x): return parallel_backprojection2d(x,geometry) proj_theoretical, proj_numerical = tf.test.compute_gradient(test_func_proj, [sino]) reco_theoretical, reco_numerical = tf.test.compute_gradient(test_func_reco, [sino])
def bench_parallel_backward(phantom, det_count, num_angles, warmup, repeats): geometry = create_parallel_geometry(det_count, num_angles) sino = parallel_projection2d(phantom, geometry) f = lambda x: parallel_backprojection2d(x, geometry) return benchmark(f, sino, warmup, repeats)
def forwardproject(self, imgs: TensorLike): imgs_tf = tf.convert_to_tensor(imgs, dtype=tf.float32) # This row is needed if rank of output is not changed in parallel_projection2d gradient. imgs_tf = tf.reshape(imgs_tf, shape=tf.shape(imgs_tf)[:3]) sinos_3D = parallel_projection2d( imgs_tf, self._geometry) # removes channeldimension sinos_4D = tf.expand_dims(sinos_3D, axis=-1) return sinos_4D
def bench_parallel_backward(batch, size, det_count, num_angles, *bench_args): with tf.device('/GPU:0'): phantom = tf.random.normal((batch, size, size)) geometry = create_parallel_geometry(size, det_count, num_angles) sino = parallel_projection2d(phantom, geometry) def f(x): return parallel_backprojection2d(x, geometry) return benchmark(f, sino, *bench_args)
def example_parallel_2d(): # ------------------ Declare Parameters ------------------ # Volume Parameters: volume_size = 256 volume_shape = [volume_size, volume_size] volume_spacing = [1, 1] # Detector Parameters: detector_shape = 800 detector_spacing = 1 # Trajectory Parameters: number_of_projections = 360 angular_range = 2 * np.pi # create Geometry class geometry = GeometryParallel2D(volume_shape, volume_spacing, detector_shape, detector_spacing, number_of_projections, angular_range) geometry.set_ray_vectors( circular_trajectory.circular_trajectory_2d(geometry)) # Get Phantom phantom = shepp_logan.shepp_logan_enhanced(volume_shape) phantom = np.expand_dims(phantom, axis=0) # ------------------ Call Layers ------------------ with tf.Session() as sess: result = parallel_projection2d(phantom, geometry) sinogram = result.eval() #sinogram = sinogram + np.random.normal( # loc=np.mean(np.abs(sinogram)), scale=np.std(sinogram), size=sinogram.shape) * 0.02 reco_filter = filters.ram_lak_2D(geometry) sino_freq = np.fft.fft(sinogram, axis=1) sino_filtered_freq = np.multiply(sino_freq, reco_filter) sinogram_filtered = np.fft.ifft(sino_filtered_freq, axis=1) result_back_proj = parallel_backprojection2d(sinogram_filtered, geometry) reco = result_back_proj.eval() plt.figure() plt.imshow(np.squeeze(reco), cmap=plt.get_cmap('gist_gray')) plt.axis('off') plt.savefig('2d_par_reco.png', dpi=150, transparent=False, bbox_inches='tight')
def example_parallel_2d(): # ------------------ Declare Parameters ------------------ # Volume Parameters: volume_size = 256 volume_shape = [volume_size, volume_size] volume_spacing = [1, 1] # Detector Parameters: detector_shape = 800 detector_spacing = 1 # Trajectory Parameters: number_of_projections = 360 angular_range = 2 * np.pi # create Geometry class geometry = GeometryParallel2D(volume_shape, volume_spacing, detector_shape, detector_spacing, number_of_projections, angular_range) geometry.set_trajectory( circular_trajectory.circular_trajectory_2d(geometry)) # Get Phantom phantom = shepp_logan.shepp_logan_enhanced(volume_shape) # Add required batch dimension phantom = np.expand_dims(phantom, axis=0) # ------------------ Call Layers ------------------ sinogram = parallel_projection2d(phantom, geometry) #sinogram = sinogram + np.random.normal( # loc=np.mean(np.abs(sinogram)), scale=np.std(sinogram), size=sinogram.shape) * 0.02 reco_filter = filters.ram_lak_2D(geometry) sino_freq = tf.signal.fft(tf.cast(sinogram, dtype=tf.complex64)) sino_filtered_freq = tf.multiply(sino_freq, tf.cast(reco_filter, dtype=tf.complex64)) sinogram_filtered = tf.math.real(tf.signal.ifft(sino_filtered_freq)) reco = parallel_backprojection2d(sinogram_filtered, geometry) plt.figure() plt.imshow(np.squeeze(reco), cmap=plt.get_cmap('gist_gray')) plt.axis('off') plt.savefig('2d_par_reco.png', dpi=150, transparent=False, bbox_inches='tight')
angular_range_inpainted) geometry_inpainted.set_trajectory( circular_trajectory.circular_trajectory_2d(geometry_inpainted)) dataset_paths = [x[2] for x in os.walk('./train_dataset')] dataset_paths = [args.tfolder + s for s in dataset_paths[0]] current_it = 0 for epoch in range(number_epochs): for dataset_path in dataset_paths: #Get and prepare training array X = prepare_training_array(dataset_path, img_size) for idx in range(X.shape[0]): if idx == 0: inpainted_sinogram_0 = parallel_projection2d( np.expand_dims(X[0], 0), geometry_inpainted) else: inpainted_sinogram_0 = parallel_projection2d( np.expand_dims(X[idx - 1], 0), geometry_inpainted) inpainted_sinogram_0 = tf.keras.layers.LayerNormalization()( inpainted_sinogram_0) inpainted_sinogram_0 = tf.expand_dims(inpainted_sinogram_0, axis=-1) inpainted_sinogram_1 = parallel_projection2d( np.expand_dims(X[idx], axis=0), geometry_inpainted) inpainted_sinogram_1 = tf.keras.layers.LayerNormalization()( inpainted_sinogram_1) inpainted_sinogram_1 = tf.expand_dims(inpainted_sinogram_1, axis=-1)
def model(self, input_volume): self.updated_reco = tf.add(input_volume, self.reco) self.current_sino = projection_2d.parallel_projection2d(self.updated_reco, self.geometry) return self.current_sino, self.reco
def test_func_proj(x): return parallel_projection2d(x,geometry)
def call(self, x): self.updated_reco = tf.add(x, self.reco) self.current_sino = projection_2d.parallel_projection2d( self.updated_reco, self.geometry) return self.current_sino, self.reco
circular_trajectory.circular_trajectory_2d(geometry_inpainted)) reco_filter = filters.ram_lak_2D(geometry) epoch = 0 for dataset_path in [ args.recon, ]: #['./test_dataset/series_5-area_0_SIRT30i_2n.h5', ]:# ['./test_dataset/20170710.h5', ] #['./test_dataset/clean4test.h5', ] ['./test_dataset/phantom_00016_recon.h5', ] ['./dataset/TIQ.h5', ] #Get and prepare training array X = prepare_training_array(dataset_path, img_size) for idx in range(0, X.shape[0], 1): #for biological TEM tomo # for idx in range(X.shape[0]): tf.print('Proccessed_index:', idx, output_stream=sys.stdout) if idx == 0: original_sinogram_0 = parallel_projection2d( np.expand_dims(X[0], axis=0), geometry) inpainted_sinogram_0 = parallel_projection2d( np.expand_dims(X[0], 0), geometry_inpainted) else: original_sinogram_0 = parallel_projection2d( np.expand_dims(X[idx - 1], axis=0), geometry) inpainted_sinogram_0 = parallel_projection2d( np.expand_dims(X[idx - 1], 0), geometry_inpainted) original_sinogram_0 = tf.keras.layers.LayerNormalization()( original_sinogram_0) inpainted_sinogram_0 = tf.keras.layers.LayerNormalization()( inpainted_sinogram_0) original_sinogram_0 = tf.expand_dims(original_sinogram_0, axis=-1)
def f(x): return parallel_projection2d(x, geometry)