|
| 1 | +from typing import Union, List, Tuple |
| 2 | + |
| 3 | +from nnunetv2.configuration import ANISO_THRESHOLD |
| 4 | +from nnunetv2.experiment_planning.experiment_planners.default_experiment_planner import ExperimentPlanner |
| 5 | +from nnunetv2.experiment_planning.experiment_planners.residual_unets.residual_encoder_unet_planners import \ |
| 6 | + nnUNetPlannerResEncL |
| 7 | +from nnunetv2.preprocessing.resampling.resample_torch import resample_torch_fornnunet |
| 8 | + |
| 9 | + |
| 10 | +class nnUNetPlannerResEncL_torchres(nnUNetPlannerResEncL): |
| 11 | + def __init__(self, dataset_name_or_id: Union[str, int], |
| 12 | + gpu_memory_target_in_gb: float = 24, |
| 13 | + preprocessor_name: str = 'DefaultPreprocessor', plans_name: str = 'nnUNetResEncUNetLPlans_torchres', |
| 14 | + overwrite_target_spacing: Union[List[float], Tuple[float, ...]] = None, |
| 15 | + suppress_transpose: bool = False): |
| 16 | + super().__init__(dataset_name_or_id, gpu_memory_target_in_gb, preprocessor_name, plans_name, |
| 17 | + overwrite_target_spacing, suppress_transpose) |
| 18 | + |
| 19 | + def generate_data_identifier(self, configuration_name: str) -> str: |
| 20 | + """ |
| 21 | + configurations are unique within each plans file but different plans file can have configurations with the |
| 22 | + same name. In order to distinguish the associated data we need a data identifier that reflects not just the |
| 23 | + config but also the plans it originates from |
| 24 | + """ |
| 25 | + return self.plans_identifier + '_' + configuration_name |
| 26 | + |
| 27 | + def determine_resampling(self, *args, **kwargs): |
| 28 | + """ |
| 29 | + returns what functions to use for resampling data and seg, respectively. Also returns kwargs |
| 30 | + resampling function must be callable(data, current_spacing, new_spacing, **kwargs) |
| 31 | +
|
| 32 | + determine_resampling is called within get_plans_for_configuration to allow for different functions for each |
| 33 | + configuration |
| 34 | + """ |
| 35 | + resampling_data = resample_torch_fornnunet |
| 36 | + resampling_data_kwargs = { |
| 37 | + "is_seg": False, |
| 38 | + 'force_separate_z': False, |
| 39 | + 'memefficient_seg_resampling': False |
| 40 | + } |
| 41 | + resampling_seg = resample_torch_fornnunet |
| 42 | + resampling_seg_kwargs = { |
| 43 | + "is_seg": True, |
| 44 | + 'force_separate_z': False, |
| 45 | + 'memefficient_seg_resampling': False |
| 46 | + } |
| 47 | + return resampling_data, resampling_data_kwargs, resampling_seg, resampling_seg_kwargs |
| 48 | + |
| 49 | + def determine_segmentation_softmax_export_fn(self, *args, **kwargs): |
| 50 | + """ |
| 51 | + function must be callable(data, new_shape, current_spacing, new_spacing, **kwargs). The new_shape should be |
| 52 | + used as target. current_spacing and new_spacing are merely there in case we want to use it somehow |
| 53 | +
|
| 54 | + determine_segmentation_softmax_export_fn is called within get_plans_for_configuration to allow for different |
| 55 | + functions for each configuration |
| 56 | +
|
| 57 | + """ |
| 58 | + resampling_fn = resample_torch_fornnunet |
| 59 | + resampling_fn_kwargs = { |
| 60 | + "is_seg": False, |
| 61 | + 'force_separate_z': False, |
| 62 | + 'memefficient_seg_resampling': False |
| 63 | + } |
| 64 | + return resampling_fn, resampling_fn_kwargs |
| 65 | + |
| 66 | + |
| 67 | +class nnUNetPlannerResEncL_torchres_sepz(nnUNetPlannerResEncL): |
| 68 | + def __init__(self, dataset_name_or_id: Union[str, int], |
| 69 | + gpu_memory_target_in_gb: float = 24, |
| 70 | + preprocessor_name: str = 'DefaultPreprocessor', plans_name: str = 'nnUNetResEncUNetLPlans_torchres_sepz', |
| 71 | + overwrite_target_spacing: Union[List[float], Tuple[float, ...]] = None, |
| 72 | + suppress_transpose: bool = False): |
| 73 | + super().__init__(dataset_name_or_id, gpu_memory_target_in_gb, preprocessor_name, plans_name, |
| 74 | + overwrite_target_spacing, suppress_transpose) |
| 75 | + |
| 76 | + def generate_data_identifier(self, configuration_name: str) -> str: |
| 77 | + """ |
| 78 | + configurations are unique within each plans file but different plans file can have configurations with the |
| 79 | + same name. In order to distinguish the associated data we need a data identifier that reflects not just the |
| 80 | + config but also the plans it originates from |
| 81 | + """ |
| 82 | + return self.plans_identifier + '_' + configuration_name |
| 83 | + |
| 84 | + def determine_resampling(self, *args, **kwargs): |
| 85 | + """ |
| 86 | + returns what functions to use for resampling data and seg, respectively. Also returns kwargs |
| 87 | + resampling function must be callable(data, current_spacing, new_spacing, **kwargs) |
| 88 | +
|
| 89 | + determine_resampling is called within get_plans_for_configuration to allow for different functions for each |
| 90 | + configuration |
| 91 | + """ |
| 92 | + resampling_data = resample_torch_fornnunet |
| 93 | + resampling_data_kwargs = { |
| 94 | + "is_seg": False, |
| 95 | + 'force_separate_z': None, |
| 96 | + 'memefficient_seg_resampling': False, |
| 97 | + 'separate_z_anisotropy_threshold': ANISO_THRESHOLD |
| 98 | + } |
| 99 | + resampling_seg = resample_torch_fornnunet |
| 100 | + resampling_seg_kwargs = { |
| 101 | + "is_seg": True, |
| 102 | + 'force_separate_z': None, |
| 103 | + 'memefficient_seg_resampling': False, |
| 104 | + 'separate_z_anisotropy_threshold': ANISO_THRESHOLD |
| 105 | + } |
| 106 | + return resampling_data, resampling_data_kwargs, resampling_seg, resampling_seg_kwargs |
| 107 | + |
| 108 | + def determine_segmentation_softmax_export_fn(self, *args, **kwargs): |
| 109 | + """ |
| 110 | + function must be callable(data, new_shape, current_spacing, new_spacing, **kwargs). The new_shape should be |
| 111 | + used as target. current_spacing and new_spacing are merely there in case we want to use it somehow |
| 112 | +
|
| 113 | + determine_segmentation_softmax_export_fn is called within get_plans_for_configuration to allow for different |
| 114 | + functions for each configuration |
| 115 | +
|
| 116 | + """ |
| 117 | + resampling_fn = resample_torch_fornnunet |
| 118 | + resampling_fn_kwargs = { |
| 119 | + "is_seg": False, |
| 120 | + 'force_separate_z': None, |
| 121 | + 'memefficient_seg_resampling': False, |
| 122 | + 'separate_z_anisotropy_threshold': ANISO_THRESHOLD |
| 123 | + } |
| 124 | + return resampling_fn, resampling_fn_kwargs |
| 125 | + |
| 126 | + |
| 127 | +class nnUNetPlanner_torchres(ExperimentPlanner): |
| 128 | + def __init__(self, dataset_name_or_id: Union[str, int], |
| 129 | + gpu_memory_target_in_gb: float = 8, |
| 130 | + preprocessor_name: str = 'DefaultPreprocessor', plans_name: str = 'nnUNetPlans_torchres', |
| 131 | + overwrite_target_spacing: Union[List[float], Tuple[float, ...]] = None, |
| 132 | + suppress_transpose: bool = False): |
| 133 | + super().__init__(dataset_name_or_id, gpu_memory_target_in_gb, preprocessor_name, plans_name, |
| 134 | + overwrite_target_spacing, suppress_transpose) |
| 135 | + |
| 136 | + def generate_data_identifier(self, configuration_name: str) -> str: |
| 137 | + """ |
| 138 | + configurations are unique within each plans file but different plans file can have configurations with the |
| 139 | + same name. In order to distinguish the associated data we need a data identifier that reflects not just the |
| 140 | + config but also the plans it originates from |
| 141 | + """ |
| 142 | + return self.plans_identifier + '_' + configuration_name |
| 143 | + |
| 144 | + def determine_resampling(self, *args, **kwargs): |
| 145 | + """ |
| 146 | + returns what functions to use for resampling data and seg, respectively. Also returns kwargs |
| 147 | + resampling function must be callable(data, current_spacing, new_spacing, **kwargs) |
| 148 | +
|
| 149 | + determine_resampling is called within get_plans_for_configuration to allow for different functions for each |
| 150 | + configuration |
| 151 | + """ |
| 152 | + resampling_data = resample_torch_fornnunet |
| 153 | + resampling_data_kwargs = { |
| 154 | + "is_seg": False, |
| 155 | + 'force_separate_z': False, |
| 156 | + 'memefficient_seg_resampling': False |
| 157 | + } |
| 158 | + resampling_seg = resample_torch_fornnunet |
| 159 | + resampling_seg_kwargs = { |
| 160 | + "is_seg": True, |
| 161 | + 'force_separate_z': False, |
| 162 | + 'memefficient_seg_resampling': False |
| 163 | + } |
| 164 | + return resampling_data, resampling_data_kwargs, resampling_seg, resampling_seg_kwargs |
| 165 | + |
| 166 | + def determine_segmentation_softmax_export_fn(self, *args, **kwargs): |
| 167 | + """ |
| 168 | + function must be callable(data, new_shape, current_spacing, new_spacing, **kwargs). The new_shape should be |
| 169 | + used as target. current_spacing and new_spacing are merely there in case we want to use it somehow |
| 170 | +
|
| 171 | + determine_segmentation_softmax_export_fn is called within get_plans_for_configuration to allow for different |
| 172 | + functions for each configuration |
| 173 | +
|
| 174 | + """ |
| 175 | + resampling_fn = resample_torch_fornnunet |
| 176 | + resampling_fn_kwargs = { |
| 177 | + "is_seg": False, |
| 178 | + 'force_separate_z': False, |
| 179 | + 'memefficient_seg_resampling': False |
| 180 | + } |
| 181 | + return resampling_fn, resampling_fn_kwargs |
0 commit comments