diff --git a/data/input/CHGJ007/gt/CHGJ007_ct_gtvt.nii.gz b/data/input/CHGJ007/gt/CHGJ007_ct_gtvt.nii.gz new file mode 100644 index 0000000..143397d Binary files /dev/null and b/data/input/CHGJ007/gt/CHGJ007_ct_gtvt.nii.gz differ diff --git a/data/input/CHGJ007/pet/CHGJ007_pt.nii.gz b/data/input/CHGJ007/pet/CHGJ007_pt.nii.gz new file mode 100644 index 0000000..e540134 Binary files /dev/null and b/data/input/CHGJ007/pet/CHGJ007_pt.nii.gz differ diff --git a/data/input/CHGJ010/gt/CHGJ010_ct_gtvt.nii.gz b/data/input/CHGJ010/gt/CHGJ010_ct_gtvt.nii.gz new file mode 100644 index 0000000..8f90303 Binary files /dev/null and b/data/input/CHGJ010/gt/CHGJ010_ct_gtvt.nii.gz differ diff --git a/data/input/CHGJ010/pet/CHGJ010_pt.nii.gz b/data/input/CHGJ010/pet/CHGJ010_pt.nii.gz new file mode 100644 index 0000000..6ec6d8d Binary files /dev/null and b/data/input/CHGJ010/pet/CHGJ010_pt.nii.gz differ diff --git a/data/input/CHGJ015/gt/CHGJ015_ct_gtvt.nii.gz b/data/input/CHGJ015/gt/CHGJ015_ct_gtvt.nii.gz new file mode 100644 index 0000000..e6ddab1 Binary files /dev/null and b/data/input/CHGJ015/gt/CHGJ015_ct_gtvt.nii.gz differ diff --git a/data/input/CHGJ015/pet/CHGJ015_pt.nii.gz b/data/input/CHGJ015/pet/CHGJ015_pt.nii.gz new file mode 100644 index 0000000..698777b Binary files /dev/null and b/data/input/CHGJ015/pet/CHGJ015_pt.nii.gz differ diff --git a/data/predicted/CHGJ015/CHGJ015_ground_truth.nii b/data/predicted/CHGJ015/CHGJ015_ground_truth.nii new file mode 100644 index 0000000..15f0194 Binary files /dev/null and b/data/predicted/CHGJ015/CHGJ015_ground_truth.nii differ diff --git a/data/predicted/CHGJ015/CHGJ015_image.nii b/data/predicted/CHGJ015/CHGJ015_image.nii new file mode 100644 index 0000000..ed40624 Binary files /dev/null and b/data/predicted/CHGJ015/CHGJ015_image.nii differ diff --git a/data/predicted/CHGJ015/CHGJ015_predicted.nii b/data/predicted/CHGJ015/CHGJ015_predicted.nii new file mode 100644 index 0000000..ac1ed42 Binary files /dev/null and b/data/predicted/CHGJ015/CHGJ015_predicted.nii differ diff --git a/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ007/ground_truth.nii b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ007/ground_truth.nii new file mode 100644 index 0000000..c289b89 Binary files /dev/null and b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ007/ground_truth.nii differ diff --git a/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ007/pet.nii b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ007/pet.nii new file mode 100644 index 0000000..32466d5 Binary files /dev/null and b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ007/pet.nii differ diff --git a/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ010/ground_truth.nii b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ010/ground_truth.nii new file mode 100644 index 0000000..b013338 Binary files /dev/null and b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ010/ground_truth.nii differ diff --git a/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ010/pet.nii b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ010/pet.nii new file mode 100644 index 0000000..c067378 Binary files /dev/null and b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ010/pet.nii differ diff --git a/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ015/ground_truth.nii b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ015/ground_truth.nii new file mode 100644 index 0000000..df6dd2a Binary files /dev/null and b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ015/ground_truth.nii differ diff --git a/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ015/pet.nii b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ015/pet.nii new file mode 100644 index 0000000..788a3d0 Binary files /dev/null and b/data/preprocessed/SalviaRun_default_3d_dir_/CHGJ015/pet.nii differ diff --git a/src/LFBNet/__pycache__/__init__.cpython-310.pyc b/src/LFBNet/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..fc01cd7 Binary files /dev/null and b/src/LFBNet/__pycache__/__init__.cpython-310.pyc differ diff --git a/src/LFBNet/__pycache__/data_loader.cpython-310.pyc b/src/LFBNet/__pycache__/data_loader.cpython-310.pyc new file mode 100644 index 0000000..b10a3b1 Binary files /dev/null and b/src/LFBNet/__pycache__/data_loader.cpython-310.pyc differ diff --git a/src/LFBNet/data_loader.py b/src/LFBNet/data_loader.py index 087823f..deb9919 100644 --- a/src/LFBNet/data_loader.py +++ b/src/LFBNet/data_loader.py @@ -146,26 +146,30 @@ def get_nii_files_path(data_directory: str) -> List[ndarray]: for path in list(nii_paths): # get the base name: means the file name identifier_base_name = str(os.path.basename(path)).split('.')[0] - if "pet_sagittal" == str(identifier_base_name): - pet_saggital = np.asanyarray(nib.load(path).dataobj) - pet_saggital = np.expand_dims(pet_saggital, axis=0) - - elif "pet_coronal" == str(identifier_base_name): - pet_coronal = np.asanyarray(nib.load(path).dataobj) - pet_coronal = np.expand_dims(pet_coronal, axis=0) - - if "ground_truth_sagittal" == str(identifier_base_name): - gt_saggital = np.asanyarray(nib.load(path).dataobj) - gt_saggital = np.expand_dims(gt_saggital, axis=0) - - elif "ground_truth_coronal" == str(identifier_base_name): - gt_coronal = np.asanyarray(nib.load(path).dataobj) - gt_coronal = np.expand_dims(gt_coronal, axis=0) - - # concatenate coronal and sagita images - # show - pet = np.concatenate((pet_saggital, pet_coronal), axis=0) - gt = np.concatenate((gt_saggital, gt_coronal), axis=0) + # if "pet_sagittal" == str(identifier_base_name): + # pet_saggital = np.asanyarray(nib.load(path).dataobj) + # pet_saggital = np.expand_dims(pet_saggital, axis=0) + + # elif "pet_coronal" == str(identifier_base_name): + # pet_coronal = np.asanyarray(nib.load(path).dataobj) + # pet_coronal = np.expand_dims(pet_coronal, axis=0) + + # if "ground_truth_sagittal" == str(identifier_base_name): + # gt_saggital = np.asanyarray(nib.load(path).dataobj) + # gt_saggital = np.expand_dims(gt_saggital, axis=0) + + # elif "ground_truth_coronal" == str(identifier_base_name): + # gt_coronal = np.asanyarray(nib.load(path).dataobj) + # gt_coronal = np.expand_dims(gt_coronal, axis=0) + + if "pet" == str(identifier_base_name): + pet = np.asanyarray(nib.load(path).dataobj) + pet= np.expand_dims(pet, axis=0) + + elif "ground_truth" == str(identifier_base_name): + gt = np.asanyarray(nib.load(path).dataobj) + gt = np.expand_dims(gt, axis=0) + return [pet, gt] @staticmethod diff --git a/src/LFBNet/losses/__pycache__/__init__.cpython-310.pyc b/src/LFBNet/losses/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..516ac0b Binary files /dev/null and b/src/LFBNet/losses/__pycache__/__init__.cpython-310.pyc differ diff --git a/src/LFBNet/losses/__pycache__/losses.cpython-310.pyc b/src/LFBNet/losses/__pycache__/losses.cpython-310.pyc new file mode 100644 index 0000000..4f161f2 Binary files /dev/null and b/src/LFBNet/losses/__pycache__/losses.cpython-310.pyc differ diff --git a/src/LFBNet/network_architecture/__pycache__/__init__.cpython-310.pyc b/src/LFBNet/network_architecture/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..826e5e9 Binary files /dev/null and b/src/LFBNet/network_architecture/__pycache__/__init__.cpython-310.pyc differ diff --git a/src/LFBNet/network_architecture/__pycache__/get_conv_blocks.cpython-310.pyc b/src/LFBNet/network_architecture/__pycache__/get_conv_blocks.cpython-310.pyc new file mode 100644 index 0000000..fe5c3ef Binary files /dev/null and b/src/LFBNet/network_architecture/__pycache__/get_conv_blocks.cpython-310.pyc differ diff --git a/src/LFBNet/network_architecture/__pycache__/lfbnet.cpython-310.pyc b/src/LFBNet/network_architecture/__pycache__/lfbnet.cpython-310.pyc new file mode 100644 index 0000000..6566c49 Binary files /dev/null and b/src/LFBNet/network_architecture/__pycache__/lfbnet.cpython-310.pyc differ diff --git a/src/LFBNet/network_architecture/__pycache__/losses.cpython-310.pyc b/src/LFBNet/network_architecture/__pycache__/losses.cpython-310.pyc new file mode 100644 index 0000000..11200f8 Binary files /dev/null and b/src/LFBNet/network_architecture/__pycache__/losses.cpython-310.pyc differ diff --git a/src/LFBNet/network_architecture/get_conv_blocks.py b/src/LFBNet/network_architecture/get_conv_blocks.py index ae20c41..2464a2d 100644 --- a/src/LFBNet/network_architecture/get_conv_blocks.py +++ b/src/LFBNet/network_architecture/get_conv_blocks.py @@ -212,12 +212,12 @@ class UpConvLayer: """ def __init__(self, stage_input: ndarray, num_output_features: int, kernel_size: int = None, strides: int = None, - conv_upsampling: str = '2D'): + conv_upsampling: str = '3D'):########2D if strides is None: - self.strides = [2, 2] + self.strides = [2, 2,2] #############[2,2] if kernel_size is None: - self.kernel_size = [2, 2] + self.kernel_size = [2, 2,2] #########[2,2] if conv_upsampling == "3D": self.conv_upsampling = Conv3DTranspose diff --git a/src/LFBNet/network_architecture/lfbnet.py b/src/LFBNet/network_architecture/lfbnet.py index 5036a31..398acc7 100644 --- a/src/LFBNet/network_architecture/lfbnet.py +++ b/src/LFBNet/network_architecture/lfbnet.py @@ -30,13 +30,15 @@ # CUDA_VISIBLE_DEVICES = 1 # os.environ["CUDA_VISIBLE_DEVICES"] = "1" -# local import +# # local import +# from get_conv_blocks import StackedConvLayerABlock, UpConvLayer +# from losses import LossMetric from src.LFBNet.network_architecture.get_conv_blocks import StackedConvLayerABlock, UpConvLayer from src.LFBNet.losses.losses import LossMetric # function to set/configure default parameters for lfbnet. -def get_default_config(dimension: int = 2, dropout_ratio: float = 0.5, non_linear_activation: str = 'elu', +def get_default_config(dimension: int = 3, dropout_ratio: float = 0.5, non_linear_activation: str = 'elu', batch_norm: bool = True, strides: int = 1, pooling: bool = True, pool_size:int =2, default_skips: bool = True, kernel_size: int = 3, kernel_initializer: str = 'he_normal', use_bias: bool = False, padding: str = 'same', num_conv_per_block: int = 2, skip_encoder=None, use_residual: bool = True, @@ -126,9 +128,11 @@ def __init__(self, input_image_shape: ndarray = None, num_output_class: int = 1, skipped_input: skipped values from the encoder and to be connected to the decoder. num_conv_per_block: a series of consecutive convolution, batch normalization, activation operations. """ - + if input_image_shape is None: - input_image_shape = [128, 256, 1] + input_image_shape = [64, 64, 64,1] + #######[128, 128, 256,1] + ##################[128, 256, 1] self.img_shape = input_image_shape self.channels_out = num_output_class @@ -141,7 +145,9 @@ def __init__(self, input_image_shape: ndarray = None, num_output_class: int = 1, # add the at last the number of features : base_num_features * latent_dim_input_ratio in feature space self.latent_dim[-1] = int(base_num_features * latent_dim_input_ratio) - self.optimizer = tf.keras.optimizers.Adam(lr=3e-4) + # self.optimizer = tf.keras.optimizers.Adam(lr=3e-4) + self.optimizer = tf.keras.optimizers.legacy.Adam(learning_rate=3e-4) + # if conv_config is not given: take the default values if conv_config is None: @@ -175,10 +181,12 @@ def __init__(self, input_image_shape: ndarray = None, num_output_class: int = 1, for stage in range(num_layers): skipped_input.append( [int(decoder_input_shape[0] * (2 ** stage)), int(decoder_input_shape[1] * (2 ** stage)), + int(decoder_input_shape[2] * (2 ** stage)), #############I added this int(base_num_features * (2 ** (num_layers - (1 + stage))))]) + - # print("skipped_connections setup") - # print(skipped_input) + print("skipped_connections setup") + print(skipped_input) self.skipped_input = skipped_input @@ -232,13 +240,13 @@ def __init__(self, input_image_shape: ndarray = None, num_output_class: int = 1, encoder_output.insert(1, img_input_latent) decoder_output = self.forward_decoder([encoder_output[i] for i in range(len(encoder_output))]) + """ """ # combined model training both encoder and decoder together self.combine_and_train = Model(inputs=[img_input, img_input_latent], outputs=[decoder_output]) - # print('Forward Encoder and decoder network combined summary: \n ') # self.combine_and_train.summary() @@ -258,10 +266,10 @@ def __init__(self, input_image_shape: ndarray = None, num_output_class: int = 1, self.feedback_latent = Model(inputs=[self.fcn_feedback.input], outputs=[self.fcn_feedback.get_layer('latent_space_fcn').output]) + + """ - """ - - """ + """ def define_forward_encoder(self): """ forward system's encoder model. @@ -309,13 +317,12 @@ def define_forward_decoder(self): # set the two inputs from the two encoders inputs_forward_encoder = inputs inputs_feedback_encoder = Input(shape=self.latent_dim, name='input_from_feedback') - + # change the input dimension into input tensors skip_input = [] for skip in range(len(self.skipped_input) - 1): skip_input.append( Input(shape=np.asarray(self.skipped_input[skip + 1]), name='input_from_encoder' + str(skip))) - ''' The bottleneck need to do the feedback connection: To use U-net-based segmentation jump or comment this block @@ -323,7 +330,7 @@ def define_forward_decoder(self): concatenate_encoder_feedback = self.conv_config['merging_strategy']( [inputs_forward_encoder, inputs_feedback_encoder]) - + fused_bottle_neck = StackedConvLayerABlock(concatenate_encoder_feedback, int(self.base_num_features * (2 ** (self.num_layers - 1))), conv_config=self.conv_config, num_conv_per_block=self.conv_config[ @@ -339,14 +346,15 @@ def define_forward_decoder(self): # apply drop out at the bottleneck fused_bottle_neck = Dropout(self.conv_config['dropout_ratio'])(fused_bottle_neck) - + current_up_conv = fused_bottle_neck for decoder_stage in range(self.num_layers - 1): # decrease the number of features per block: (self.num_decoder_stage-decoder_stage) num_output_features = int(self.base_num_features * (2 ** (self.num_layers - (2 + decoder_stage)))) current_up_conv = UpConvLayer(current_up_conv, num_output_features=num_output_features, - conv_upsampling="2D").Up_conv_layer() + conv_upsampling="3D").Up_conv_layer() #####2D + # Need skipp connections: if self.conv_config['merging_strategy'] is not None: skipped_ = skip_input[decoder_stage] @@ -370,7 +378,6 @@ def define_forward_decoder(self): current_up_conv = self.conv_config['conv'](self.num_classes, kernel_size=1, activation=activation, kernel_initializer='he_normal', use_bias=False, padding='same', name='final_output_layer')(current_up_conv) - skip_input.insert(0, inputs) skip_input.insert(1, inputs_feedback_encoder) @@ -406,7 +413,7 @@ def define_feedback_fcn_network(self): # up convolution block current_up_conv = UpConvLayer(current_up_conv, num_output_features=num_output_features, - conv_upsampling="2D").Up_conv_layer() + conv_upsampling="3D").Up_conv_layer() ####2D # convolution blocks current_up_conv = StackedConvLayerABlock(current_up_conv, num_output_features, conv_config=self.conv_config, @@ -437,5 +444,15 @@ def define_feedback_fcn_network(self): props = get_default_config() model = LfbNet() print("network summary \n") + print(f"{model.combine_and_train.summary()}\n") + + + print(f"{model.forward_encoder.summary()}\n") + + print(f"{model.forward_decoder.summary()}\n") + + print(f"{model.feedback_latent.summary()}\n") + + print(f"{model.fcn_feedback.summary()}\n") diff --git a/src/LFBNet/postprocessing/__pycache__/__init__.cpython-310.pyc b/src/LFBNet/postprocessing/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..5cc7fd8 Binary files /dev/null and b/src/LFBNet/postprocessing/__pycache__/__init__.cpython-310.pyc differ diff --git a/src/LFBNet/postprocessing/__pycache__/postprocessing.cpython-310.pyc b/src/LFBNet/postprocessing/__pycache__/postprocessing.cpython-310.pyc new file mode 100644 index 0000000..5e2a445 Binary files /dev/null and b/src/LFBNet/postprocessing/__pycache__/postprocessing.cpython-310.pyc differ diff --git a/src/LFBNet/preprocessing/__pycache__/__init__.cpython-310.pyc b/src/LFBNet/preprocessing/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..2ee5c2c Binary files /dev/null and b/src/LFBNet/preprocessing/__pycache__/__init__.cpython-310.pyc differ diff --git a/src/LFBNet/preprocessing/__pycache__/preprocessing.cpython-310.pyc b/src/LFBNet/preprocessing/__pycache__/preprocessing.cpython-310.pyc new file mode 100644 index 0000000..4acb888 Binary files /dev/null and b/src/LFBNet/preprocessing/__pycache__/preprocessing.cpython-310.pyc differ diff --git a/src/LFBNet/preprocessing/preprocessing.py b/src/LFBNet/preprocessing/preprocessing.py index 5214362..874535c 100644 --- a/src/LFBNet/preprocessing/preprocessing.py +++ b/src/LFBNet/preprocessing/preprocessing.py @@ -230,7 +230,7 @@ def crop_nii_to_desired_resolution(data: ndarray = None, cropped_resolution: Lis pass if cropped_resolution is not None: - cropped_resolution = [128, 128, 256] + cropped_resolution = [64,64,64]#[54,54,54]#[128, 128, 256] print("\n Initial data size \t Cropped data size ") print(data.shape, "\t", end=" ") @@ -392,7 +392,7 @@ def read_pet_gt_resize_crop_save_as_3d_andor_mip( data_path: str = None, data_name: str = None, saving_dir: str = None, save_3D: bool = False, crop: bool = True, output_resolution: List[int] = None, desired_spacing: List[float] = None, generate_mip: bool = False ): - """ Read pet and ground truth images from teh input data path. It also apply resize, and cropping operations. + """ Read pet and ground truth images from the input data path. It also apply resize, and cropping operations. Args: data_path: directory to the raw 3D pet and ground truth .nii fies. @@ -413,7 +413,7 @@ def read_pet_gt_resize_crop_save_as_3d_andor_mip( rows, columns, depths = output_resolution else: # default values # output resized and cropped image resolution= - output_resolution = [128, 128, 256] + output_resolution = [128, 128, 256]####################### if data_name is None: data_name = "unknown_data" @@ -465,6 +465,7 @@ def create_directory(directory_to_create: list): # all patient ids case_ids = os.listdir(data_path) + # print(f"===== {data_path} case_ids {case_ids}") if not len(case_ids): # reise exception if the directory is empty raise Exception("Directory %s is empty" % data_path) @@ -606,7 +607,9 @@ def create_directory(directory_to_create: list): [pet_mip], affine, path_save=saving_dir_mip, identifier=str(image_name), name=['pet_' + str(naming_)] ) - return saving_dir_mip + + ###################return saving_dir_mip + return saving_dir_mip if generate_mip else saving_dir_3d # Read .nii files using itk @@ -615,9 +618,10 @@ def create_directory(directory_to_create: list): # input_path = r"F:\Data\Remarc\REMARC/" # data_ = "remarc" # - input_path = r"F:\Data\Vienna\No_ground_truth/" + input_path = 'data/input'#r"F:\Data\Vienna\No_ground_truth/" data_ = "LNH" saving_dir_mip = read_pet_gt_resize_crop_save_as_3d_andor_mip( data_path=input_path, data_name=data_, saving_dir=None, save_3D=True, crop=True, - output_resolution=[128, 128, 256], desired_spacing=None, generate_mip=True + output_resolution=[128, 128, 256], desired_spacing=None, generate_mip=False + #################output_resolution=[128, 128, 256], desired_spacing=None, generate_mip=True ) diff --git a/src/LFBNet/utilities/__pycache__/__init__.cpython-310.pyc b/src/LFBNet/utilities/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..c7e9cb1 Binary files /dev/null and b/src/LFBNet/utilities/__pycache__/__init__.cpython-310.pyc differ diff --git a/src/LFBNet/utilities/__pycache__/compute_surrogate_features.cpython-310.pyc b/src/LFBNet/utilities/__pycache__/compute_surrogate_features.cpython-310.pyc new file mode 100644 index 0000000..a7cb38a Binary files /dev/null and b/src/LFBNet/utilities/__pycache__/compute_surrogate_features.cpython-310.pyc differ diff --git a/src/LFBNet/utilities/__pycache__/train_valid_paths.cpython-310.pyc b/src/LFBNet/utilities/__pycache__/train_valid_paths.cpython-310.pyc new file mode 100644 index 0000000..4e2b9b2 Binary files /dev/null and b/src/LFBNet/utilities/__pycache__/train_valid_paths.cpython-310.pyc differ diff --git a/src/run/__pycache__/__init__.cpython-310.pyc b/src/run/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..40c4476 Binary files /dev/null and b/src/run/__pycache__/__init__.cpython-310.pyc differ diff --git a/src/run/__pycache__/parse_argument.cpython-310.pyc b/src/run/__pycache__/parse_argument.cpython-310.pyc new file mode 100644 index 0000000..93281d5 Binary files /dev/null and b/src/run/__pycache__/parse_argument.cpython-310.pyc differ diff --git a/src/run/__pycache__/trainer.cpython-310.pyc b/src/run/__pycache__/trainer.cpython-310.pyc new file mode 100644 index 0000000..bde768b Binary files /dev/null and b/src/run/__pycache__/trainer.cpython-310.pyc differ diff --git a/src/run/trainer.py b/src/run/trainer.py index 04a7d2e..24bc02e 100644 --- a/src/run/trainer.py +++ b/src/run/trainer.py @@ -64,7 +64,7 @@ def default_training_parameters( - num_epochs: int = 5000, batch_size: int = 16, early_stop: int = None, fold_number: int = None, + num_epochs: int = 4, batch_size: int = 1, early_stop: int = None, fold_number: int = None, model_name_save: List[str] = None, loss: str = None, metric: str = None ) -> dict: """ Configure default parameters for training. @@ -213,7 +213,8 @@ def __init__( self.latent_dim = self.model.latent_dim self.h_at_zero_time = np.zeros( (int(self.config_trainer['batch_size']), int(self.latent_dim[0]), int(self.latent_dim[1]), - int(self.latent_dim[2])), np.float32 + int(self.latent_dim[2]), ############i added + int(self.latent_dim[3])), np.float32 ########## ) @staticmethod @@ -261,6 +262,7 @@ def train(self): # training if self.task == 'train': # training + for current_epoch in range(self.config_trainer['num_epochs']): feedback_loss_dice = [] forward_loss_dice = [] @@ -280,12 +282,13 @@ def train(self): batch_input_data, batch_output_data = self.load_dataset( directory_=self.folder_preprocessed_train, ids_to_read=kk - ) + ) assert len(batch_input_data) > 0, "batch of data not loaded correctly" # shuffle within the batch index_batch = np.random.permutation(int(batch_input_data.shape[0])) + batch_input_data = batch_input_data[index_batch] batch_output_data = batch_output_data[index_batch] @@ -300,10 +303,13 @@ def train(self): # Train forward models if current_epoch % 2 == 0: # step 1: train the forward network encoder and decoder + loss, dice = self.model.combine_and_train.train_on_batch( [batch_input, self.h_at_zero_time], [batch_output] ) # self.h_at_zero_time + forward_loss_dice.append([loss, dice]) + else: predicted_decoder = self.model.combine_and_train.predict( @@ -325,6 +331,7 @@ def train(self): [output for output in forward_encoder_output], [batch_output] ) forward_decoder_loss_dice.append([loss, dice]) + forward_loss_dice = np.array(forward_loss_dice) feedback_loss_dice = np.array(feedback_loss_dice) @@ -336,11 +343,13 @@ def train(self): 'Training_forward_system: >%d, ' ' fwd_loss = %.3f, fwd_dice=%0.3f, ' % (current_epoch, loss, dice) ) + else: + loss_forward, dice_forward = np.mean(forward_decoder_loss_dice, axis=0) loss_feedback, dice_feedback = np.mean(feedback_loss_dice, axis=0) - + print( 'Training_forward_decoder_and_feedback_system: >%d, ' 'fwd_decoder_loss=%03f, ' @@ -505,7 +514,7 @@ def evaluation( # latent feedback variable h0 # replace the first number of batches with the number of input images from the first channel h0_input = np.zeros( - (len(input_image), int(self.latent_dim[0]), int(self.latent_dim[1]), int(self.latent_dim[2])), np.float32 + (len(input_image), int(self.latent_dim[0]), int(self.latent_dim[1]), int(self.latent_dim[2]), int(self.latent_dim[3])), np.float32 ) # step 0: @@ -554,7 +563,7 @@ def evaluation( binary.specificity(NetworkTrainer.threshold_image(predicted), NetworkTrainer.threshold_image(ground_truth)) ) # all = np.concatenate((ground_truth, predicted, input_image), axis=0) - # display_image(all) + # NetworkTrainer.display_image(all) # Sometimes save predictions if self.save_all: @@ -591,6 +600,8 @@ def display_image(im_display: ndarray): plt.subplot(3, 2, n + 1) plt.imshow(im) # chart formatting plt.show() + # temp_file_path = 'temp_plot.png' + # plt.savefig(temp_file_path, bbox_inches='tight', pad_inches=0.5) @staticmethod # binary.dc, sen, and specificty works only on binary images diff --git a/src/train.py b/src/train.py index 6918991..656959d 100644 --- a/src/train.py +++ b/src/train.py @@ -63,6 +63,7 @@ def main(): # get input and output data directories input_dir = args.input_dir + # print(f"==========={input_dir}") train_valid_paths.directory_exist(input_dir) # CHECK: check if the input directory has files # data identifier, or name @@ -102,14 +103,17 @@ def main(): # STEP 1: read the raw .nii files in suv and resize, crop in 3D form, generate MIP, and save # get the directory path to the generated and saved MIPS, if it already exists, go for training or testing dir_mip = [] + dir_3D = [] # path to the training and validation data path_train_valid = dict(train=None, test=None) # preprocessing stage: preprocessing_params = dict(data_path=input_dir, data_name=dataset_name, saving_dir=preprocessing_dir, save_3D=True, - output_resolution=[128, 128, 256], desired_spacing=desired_spacing, generate_mip=True) + output_resolution=[64, 64, 64], desired_spacing=desired_spacing, generate_mip=False) + ###out_resolu=[128,128,256] + # dir_mip = preprocessing.read_pet_gt_resize_crop_save_as_3d_andor_mip(**preprocessing_params) + dir_3D = preprocessing.read_pet_gt_resize_crop_save_as_3d_andor_mip(**preprocessing_params) - dir_mip = preprocessing.read_pet_gt_resize_crop_save_as_3d_andor_mip(**preprocessing_params) # training or validation/testing from the input argument task task = args.task # true training and false testing or validation @@ -123,11 +127,11 @@ def main(): train_ids, valid_ids = trainer.get_training_and_validation_ids_from_csv(train_valid_ids_path_csv) else: # generate csv file for the validation and training data by dividing the data into training and validation - path_train_valid = dict(train=dir_mip) + path_train_valid = dict(train=dir_3D) train_ids, valid_ids = train_valid_paths.get_train_valid_ids_from_folder(path_train_valid=path_train_valid) - + # train or test on the given input arguments - trainer_params = dict(folder_preprocessed_train=dir_mip, folder_preprocessed_valid=dir_mip, + trainer_params = dict(folder_preprocessed_train=dir_3D, folder_preprocessed_valid=dir_3D, ids_to_read_train=train_ids, ids_to_read_valid=valid_ids, task=task, predicted_directory=output_dir) @@ -145,10 +149,10 @@ def main(): else: # generate csv file for the validation and training data by dividing the data into training and validation - path_train_valid = dict(train=dir_mip) + path_train_valid = dict(train=dir_3D) train_ids, valid_ids = train_valid_paths.get_train_valid_ids_from_folder(path_train_valid=path_train_valid) - trainer_params = dict(folder_preprocessed_train=dir_mip, folder_preprocessed_valid=dir_mip, + trainer_params = dict(folder_preprocessed_train=dir_3D, folder_preprocessed_valid=dir_3D, ids_to_read_train=train_ids, ids_to_read_valid=valid_ids, task=task, predicted_directory=output_dir, save_predicted=True) diff --git a/src/weight/feedback_system.h5 b/src/weight/feedback_system.h5 deleted file mode 100644 index 3f85920..0000000 Binary files a/src/weight/feedback_system.h5 and /dev/null differ diff --git a/src/weight/forward_system.h5 b/src/weight/forward_system.h5 deleted file mode 100644 index 6123d02..0000000 Binary files a/src/weight/forward_system.h5 and /dev/null differ diff --git a/test_env.py b/test_env.py index 5ca2c6a..6b2ef00 100644 --- a/test_env.py +++ b/test_env.py @@ -58,13 +58,13 @@ def main(): # If input directory path is not given use the default "/input_data" if input_dir is None: - input_dir = "/input" + input_dir = "data/input" if not os.path.exists(input_dir): os.mkdir(input_dir) # if output path is not given in the argument, create "/output" if preprocessing_data_dir is None: - preprocessing_data_dir = "/output" + preprocessing_data_dir = "data/output" if not os.path.exists(preprocessing_data_dir): os.mkdir(preprocessing_data_dir) @@ -105,5 +105,5 @@ def main(): # check if __name__ == '__main__': - print("\n Running the integrated framework for testing use case... \n\n") + # print("\n Running the integrated framework for testing use case... \n\n") main()