model = antspynet.create_deep_back_projection_network_model_3d( (*input_image_list[0].shape, 1), number_of_outputs=1, number_of_base_filters=64, number_of_feature_filters=256, number_of_back_projection_stages=7, convolution_kernel_size=(3, 3, 3), strides=(2, 2, 2), number_of_loss_functions=1) print("Loading weights file") start_time = time.time() weights_file_name = "./mriSuperResolutionWeights.h5" if not os.path.exists(weights_file_name): weights_file_name = antspynet.get_pretrained_network( "mriSuperResolution", weights_file_name) model.load_weights(weights_file_name) end_time = time.time() elapsed_time = end_time - start_time print(" (elapsed time: ", elapsed_time, " seconds)") number_of_image_volumes = len(input_image_list) output_image_list = list() for i in range(number_of_image_volumes): print("Applying super resolution to image", i, "of", number_of_image_volumes) start_time = time.time() input_image = ants.iMath(input_image_list[i], "TruncateIntensity", 0.0001,
start_time = time.time() # image_resampled = ants.resample_image(image_standard, shape_initial_stage, True, 0) image_resampled = image_standard end_time = time.time() elapsed_time = end_time - start_time print(" (elapsed time: ", elapsed_time, " seconds)") # Build model and load weights for first pass print(" Initial step 5: load weights.") start_time = time.time() model_initial_stage = antspynet.create_hippmapp3r_unet_model_3d( (*shape_initial_stage, 1), True) weights_file_name = "./hippMapp3rInitialWeights.h5" if not os.path.exists(weights_file_name): weights_file_name = antspynet.get_pretrained_network( "hippMapp3rInitial", weights_file_name) model_initial_stage.load_weights(weights_file_name) end_time = time.time() elapsed_time = end_time - start_time print(" (elapsed time: ", elapsed_time, " seconds)") # Create initial segmentation image print(" Initial step 6: prediction.") start_time = time.time() data_initial_stage = image_resampled.numpy() data_initial_stage = np.expand_dims(data_initial_stage, 0) data_initial_stage = np.expand_dims(data_initial_stage, -1) prediction_initial_stage = np.squeeze( model_initial_stage.predict(data_initial_stage))
unet_model = antspynet.create_unet_model_3d( (*resampled_image_size, channel_size), number_of_outputs=number_of_classification_labels, number_of_layers=4, number_of_filters_at_base_layer=16, dropout_rate=0.0, convolution_kernel_size=(7, 7, 5), deconvolution_kernel_size=(7, 7, 5)) print("Loading weights file") start_time = time.time() weights_file_name = "./lungSegmentationWeights.h5" if not os.path.exists(weights_file_name): weights_file_name = antspynet.get_pretrained_network( "protonLungMri", weights_file_name) unet_model.load_weights(weights_file_name) end_time = time.time() elapsed_time = end_time - start_time print(" (elapsed time: ", elapsed_time, " seconds)") start_time_total = time.time() print("Reading ", input_file_name) start_time = time.time() image = ants.image_read(input_file_name) end_time = time.time() elapsed_time = end_time - start_time print(" (elapsed time: ", elapsed_time, " seconds)")
print(" (elapsed time: ", elapsed_time, " seconds)") resampled_image_size = reorient_template.shape unet_model = antspynet.create_unet_model_3d( (*resampled_image_size, channel_size), number_of_outputs = number_of_classification_labels, number_of_layers = 4, number_of_filters_at_base_layer = 8, dropout_rate = 0.0, convolution_kernel_size = (3, 3, 3), deconvolution_kernel_size = (2, 2, 2), weight_decay = 1e-5 ) print( "Loading weights file" ) start_time = time.time() weights_file_name = "./brainExtractionWeights.h5" if not os.path.exists(weights_file_name): weights_file_name = antspynet.get_pretrained_network("brainExtraction", weights_file_name) unet_model.load_weights(weights_file_name) end_time = time.time() elapsed_time = end_time - start_time print(" (elapsed time: ", elapsed_time, " seconds)") start_time_total = time.time() print( "Reading ", input_file_name ) start_time = time.time() image = ants.image_read(input_file_name) image = (image - image.min()) / (image.max() - image.min()) end_time = time.time() elapsed_time = end_time - start_time print(" (elapsed time: ", elapsed_time, " seconds)")
unet_model = antspynet.create_unet_model_3d( (*patch_size, channel_size), number_of_outputs=number_of_classification_labels, number_of_layers=4, number_of_filters_at_base_layer=16, dropout_rate=0.0, convolution_kernel_size=(3, 3, 3), deconvolution_kernel_size=(2, 2, 2), weight_decay=1e-5) print("Loading weights file") start_time = time.time() weights_file_name = "./brainSegmentationPatchBasedWeights.h5" if not os.path.exists(weights_file_name): weights_file_name = antspynet.get_pretrained_network( "brainSegmentationPatchBased", weights_file_name) unet_model.load_weights(weights_file_name) end_time = time.time() elapsed_time = end_time - start_time print(" (elapsed time: ", elapsed_time, " seconds)") # Process input start_time_total = time.time() print("Reading ", input_file_name) start_time = time.time() image = ants.image_read(input_file_name) mask = ants.image_read(input_mask_file_name) mask = ants.threshold_image(mask, 0.4999, 1.0001, 1, 0)
image_mean = np.mean(thresholded_nonzero) image_sd = np.std(thresholded_nonzero) image_standard = (image_n4 - image_mean) / image_sd image_standard = image_standard * thresholded_mask # Resample image print(" Initial step 4: resample to (160, 160, 128).") image_resampled = ants.resample_image(image_standard, shape_initial_stage, True, 0) # Build model and load weights for first pass print(" Initial step 5: generate first network and download weights.") model_initial_stage = antspynet.create_hippmapp3r_unet_model_3d( (*shape_initial_stage, 1), True) model_initial_stage.load_weights( antspynet.get_pretrained_network("hippMapp3rInitial")) # Create initial segmentation image print(" Initial step 6: prediction and write to disk.") data_initial_stage = image_resampled.numpy() data_initial_stage = np.expand_dims(data_initial_stage, 0) data_initial_stage = np.expand_dims(data_initial_stage, -1) prediction_initial_stage = np.squeeze( model_initial_stage.predict(data_initial_stage)) prediction_initial_stage[np.where(prediction_initial_stage >= 0.5)] = 1 prediction_initial_stage[np.where(prediction_initial_stage < 0.5)] = 0 mask_initial_stage = ants.from_numpy(prediction_initial_stage, origin=image_resampled.origin, spacing=image_resampled.spacing, direction=image_resampled.direction)