From 2cf45d5fe5c79081f83b3d04342500cebfb3a58e Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Thu, 22 Feb 2024 10:29:13 +0800 Subject: [PATCH 1/3] Update test_nnunetv2runner (#7483) Fixes #7013 #7478 ### Description replace `predict_from_raw_data` with `nnUNetPredictor` in test_nnunetv2runner ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> Signed-off-by: Mathijs de Boer --- monai/apps/nnunet/nnunetv2_runner.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/monai/apps/nnunet/nnunetv2_runner.py b/monai/apps/nnunet/nnunetv2_runner.py index e62809403e..44b3c24256 100644 --- a/monai/apps/nnunet/nnunetv2_runner.py +++ b/monai/apps/nnunet/nnunetv2_runner.py @@ -37,6 +37,7 @@ class nnUNetV2Runner: # noqa: N801 """ ``nnUNetV2Runner`` provides an interface in MONAI to use `nnU-Net` V2 library to analyze, train, and evaluate neural networks for medical image segmentation tasks. + A version of nnunetv2 higher than 2.2 is needed for this class. ``nnUNetV2Runner`` can be used in two ways: @@ -770,7 +771,7 @@ def find_best_configuration( def predict( self, list_of_lists_or_source_folder: str | list[list[str]], - output_folder: str, + output_folder: str | None | list[str], model_training_output_dir: str, use_folds: tuple[int, ...] | str | None = None, tile_step_size: float = 0.5, @@ -824,7 +825,7 @@ def predict( """ os.environ["CUDA_VISIBLE_DEVICES"] = f"{gpu_id}" - from nnunetv2.inference.predict_from_raw_data import predict_from_raw_data + from nnunetv2.inference.predict_from_raw_data import nnUNetPredictor n_processes_preprocessing = ( self.default_num_processes if num_processes_preprocessing < 0 else num_processes_preprocessing @@ -832,20 +833,21 @@ def predict( n_processes_segmentation_export = ( self.default_num_processes if num_processes_segmentation_export < 0 else num_processes_segmentation_export ) - - predict_from_raw_data( - list_of_lists_or_source_folder=list_of_lists_or_source_folder, - output_folder=output_folder, - model_training_output_dir=model_training_output_dir, - use_folds=use_folds, + predictor = nnUNetPredictor( tile_step_size=tile_step_size, use_gaussian=use_gaussian, use_mirroring=use_mirroring, - perform_everything_on_gpu=perform_everything_on_gpu, + perform_everything_on_device=perform_everything_on_gpu, verbose=verbose, + ) + predictor.initialize_from_trained_model_folder( + model_training_output_dir=model_training_output_dir, use_folds=use_folds, checkpoint_name=checkpoint_name + ) + predictor.predict_from_files( + list_of_lists_or_source_folder=list_of_lists_or_source_folder, + output_folder_or_list_of_truncated_output_files=output_folder, save_probabilities=save_probabilities, overwrite=overwrite, - checkpoint_name=checkpoint_name, num_processes_preprocessing=n_processes_preprocessing, num_processes_segmentation_export=n_processes_segmentation_export, folder_with_segs_from_prev_stage=folder_with_segs_from_prev_stage, From 8c440c73a1b3a9b1a40ff3b75261dfede4b38a91 Mon Sep 17 00:00:00 2001 From: Mathijs de Boer Date: Thu, 22 Feb 2024 14:11:58 +0100 Subject: [PATCH 2/3] Add __repr__ and __str__ to Metrics baseclass Signed-off-by: Mathijs de Boer --- monai/metrics/metric.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/monai/metrics/metric.py b/monai/metrics/metric.py index a6dc1a49a2..08cd153526 100644 --- a/monai/metrics/metric.py +++ b/monai/metrics/metric.py @@ -37,6 +37,12 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any: """ raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.") + def __repr__(self): + return self.__class__.__name__ + + def __str__(self): + return self.__class__.__name__ + class IterationMetric(Metric): """ From dd88f37ef119465a1d88bc9076aa63df2dfb8b0b Mon Sep 17 00:00:00 2001 From: Mathijs de Boer Date: Tue, 27 Feb 2024 14:44:26 +0100 Subject: [PATCH 3/3] Remove __repr__ Signed-off-by: Mathijs de Boer --- monai/metrics/metric.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/monai/metrics/metric.py b/monai/metrics/metric.py index 08cd153526..249b2dc951 100644 --- a/monai/metrics/metric.py +++ b/monai/metrics/metric.py @@ -37,9 +37,6 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any: """ raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.") - def __repr__(self): - return self.__class__.__name__ - def __str__(self): return self.__class__.__name__