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 example_fan_2d_shortscan(): # Volume Parameters: volume_size = 256 volume_shape = [volume_size, volume_size] volume_spacing = [1, 1] # Detector Parameters: detector_shape = 500 detector_spacing = 1 # Trajectory Parameters: number_of_projections = 250 angular_range = None # will get set to pi + 2 * fan_angle source_detector_distance = 1200 source_isocenter_distance = 750 # Create Geometry class geometry = GeometryFan2D(volume_shape, volume_spacing, detector_shape, detector_spacing, number_of_projections, angular_range, source_detector_distance, source_isocenter_distance) geometry.angular_range = np.pi + 2 * geometry.fan_angle # fan_angle gets defined by sdd and detector_shape geometry.set_trajectory(circular_trajectory_2d(geometry)) # Create Phantom phantom = shepp_logan_enhanced(volume_shape) # Add required batch dimension phantom = np.expand_dims(phantom, axis=0) # Build up Reconstruction Pipeline # Create Sinogram of Phantom sinogram = fan_projection2d(phantom, geometry) # Redundancy Weighting: Create Weights Image and pointwise multiply redundancy_weights = weights.parker_weights_2d(geometry) sinogram_redun_weighted = sinogram * redundancy_weights # Filtering: Create 2D Filter and pointwise multiply reco_filter = filters.ram_lak_2D(geometry) sino_freq = tf.signal.fft( tf.cast(sinogram_redun_weighted, 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)) # Final Backprojection reco = fan_backprojection2d(sinogram_filtered, geometry) plt.figure() plt.imshow(np.squeeze(reco), cmap=plt.get_cmap('gist_gray')) plt.axis('off') plt.savefig('2d_fan_short_scan_reco.png', dpi=150, transparent=False, bbox_inches='tight')
def example_fan_2d_shortscan(): # Volume Parameters: volume_size = 256 volume_shape = [volume_size, volume_size] volume_spacing = [1,1] # Detector Parameters: detector_shape = 500 detector_spacing = 1 # Trajectory Parameters: number_of_projections = 250 angular_range = None # will get set to pi + 2 * fan_angle source_detector_distance = 1200 source_isocenter_distance = 750 # Create Geometry class geometry = GeometryFan2D(volume_shape, volume_spacing, detector_shape, detector_spacing, number_of_projections, angular_range, source_detector_distance, source_isocenter_distance) geometry.angular_range = np.pi + 2*geometry.fan_angle # fan_angle gets defined by sdd and detector_shape geometry.central_ray_vectors = circular_trajectory_2d(geometry) # Create Phantom phantom = shepp_logan_enhanced(volume_shape) phantom = np.expand_dims(phantom,axis=0) # Build up Reconstruction Pipeline with tf.Session() as sess: # Create Sinogram of Phantom result = fan_projection2d(phantom, geometry) sinogram = result.eval() # Redundancy Weighting: Create Weights Image and pointwise multiply redundancy_weights = weights.parker_weights_2d(geometry) sinogram_redun_weighted = sinogram * redundancy_weights # Filtering: Create 2D Filter and pointwise multiply the_filter = filters.ram_lak_2D(geometry) sino_fft = np.fft.fft(sinogram_redun_weighted, axis=-1) sino_filtered_fft = np.multiply(sino_fft, the_filter) sinogram_filtered = np.fft.ifft(sino_filtered_fft, axis=-1) # Final Backprojection result_back_proj = fan_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_fan_short_scan_reco.png', dpi=150, transparent=False, bbox_inches='tight')
def example_fan_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 source_detector_distance = 1200 source_isocenter_distance = 750 # create Geometry class geometry = GeometryFan2D(volume_shape, volume_spacing, detector_shape, detector_spacing, number_of_projections, angular_range, source_detector_distance, source_isocenter_distance) geometry.set_central_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 = fan_projection2d(phantom, geometry) sinogram = result.eval() #TODO: Add Cosine weighting #TODO: Add redundancy weighting for 360 degree reco_filter = filters.ramp_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 = fan_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_fan_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_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')
def iterative_reconstruction(): # ------------------ Declare Parameters ------------------ parser = argparse.ArgumentParser(description='') parser.add_argument('--lr', dest='learning_rate', type=float, default=1e-2, help='initial learning rate for adam') parser.add_argument('--epoch', dest='num_epochs', type=int, default=1000000, help='# of epoch') args = parser.parse_args() # Volume Parameters: volume_size = 512 volume_shape = [volume_size, volume_size] volume_spacing = [0.5, 0.5] # Detector Parameters: detector_shape = 625 detector_spacing = 0.5 # Trajectory Parameters: number_of_projections = 30 angular_range = np.radians(200) # 200 * np.pi / 180 # 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)) phantom = shepp_logan.shepp_logan_enhanced(volume_shape) config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.5 config.gpu_options.allow_growth = True # ------------------ Call Layers ------------------ with tf.Session(config=config) as sess: acquired_sinogram = generate_sinogram(phantom,projection_2d.parallel_projection2d,geometry) acquired_sinogram = acquired_sinogram + np.random.normal( loc=np.mean(np.abs(acquired_sinogram)), scale=np.std(acquired_sinogram), size=acquired_sinogram.shape) * 0.02 zero_vector = np.zeros(np.shape(phantom), dtype=np.float32) iter_pipeline = pipeline(sess, args, geometry) iter_pipeline.train(zero_vector,np.asarray(acquired_sinogram)) plt.figure() plt.imshow(iter_pipeline.result[0], cmap=plt.get_cmap('gist_gray')) plt.axis('off') plt.savefig('iter_tv_reco.png', dpi=150, transparent=False, bbox_inches='tight')
def example_fan_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 source_detector_distance = 1200 source_isocenter_distance = 750 # create Geometry class geometry = GeometryFan2D(volume_shape, volume_spacing, detector_shape, detector_spacing, number_of_projections, angular_range, source_detector_distance, source_isocenter_distance) 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 = fan_projection2d(phantom, geometry) #TODO: Add Cosine weighting #TODO: Add redundancy weighting for 360 degree reco_filter = filters.ramp_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 = fan_backprojection2d(sinogram_filtered, geometry) plt.figure() plt.imshow(np.squeeze(reco), cmap=plt.get_cmap('gist_gray')) plt.axis('off') plt.savefig('2d_fan_reco.png', dpi=150, transparent=False, bbox_inches='tight')
def get_test_data(number_of_samples=1): data = np.empty((number_of_samples,) + tuple(GEOMETRY.sinogram_shape)) labels = np.empty((number_of_samples,) + tuple(GEOMETRY.volume_shape)) with tf.Session() as sess: # get shepp logan 2d if number_of_samples == 1: labels[0] = shepp_logan.shepp_logan_enhanced(GEOMETRY.volume_shape) data[0] = generate_sinogram(np.expand_dims(labels[0],axis=0), parallel_projection2d, GEOMETRY) # every slice of shepp logan 3d with number_of_samples as Z-dimension as own image else: labels = shepp_logan.shepp_logan_3d((number_of_samples,) + tuple(GEOMETRY.sinogram_shape)) for i in range(number_of_samples): data[i] = generate_sinogram_parallel_2d(np.expand_dims(labels[i], axis=0), GEOMETRY) return data, labels
def iterative_reconstruction(): # ------------------ Declare Parameters ------------------ parser = argparse.ArgumentParser(description='') parser.add_argument('--lr', dest='learning_rate', type=float, default=1e-2, help='initial learning rate for adam') parser.add_argument('--epoch', dest='num_epochs', type=int, default=100000, help='# of epoch') args = parser.parse_args() # Volume Parameters: volume_size = 256 volume_shape = [volume_size - 1, volume_size] #, volume_size+1] volume_spacing = [1, 1] #, 1] # Detector Parameters: detector_shape = [375] #, 375] detector_spacing = [1] #,1] # Trajectory Parameters: number_of_projections = 30 angular_range = np.radians(200) # 200 * np.pi / 180 # create Geometry class geometry = GeometryParallel2D(volume_shape, volume_spacing, detector_shape, detector_spacing, number_of_projections, angular_range) #, 1200.0, 750.0 ) matrices = circular_trajectory.circular_trajectory_2d(geometry) geometry.set_trajectory(matrices) phantom = shepp_logan.shepp_logan_enhanced(volume_shape).astype( dtype=np.float32) # Add required batch dimension phantom = np.expand_dims(phantom, axis=0) # ------------------ Call Layers ------------------ acquired_sinogram = generate_sinogram(phantom, projection_2d.parallel_projection2d, geometry) # acquired_sinogram = acquired_sinogram + np.random.normal( # loc=np.mean(np.abs(acquired_sinogram)), scale=np.std(acquired_sinogram), size=acquired_sinogram.shape) * 0.02 zero_vector = np.zeros(np.shape(phantom), dtype=np.float32) iter_pipeline = pipeline(args, geometry) iter_pipeline.train(zero_vector, np.asarray(acquired_sinogram)) plt.figure() plt.imshow(np.squeeze(iter_pipeline.result), cmap=plt.get_cmap('gist_gray')) plt.axis('off') plt.savefig('iter_tv_reco.png', dpi=150, transparent=False, bbox_inches='tight')