|
| 1 | +# Copyright 2002 MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +from monai.deploy.operators.monai_bundle_inference_operator import MonaiBundleInferenceOperator, get_bundle_config |
| 13 | +from monai.deploy.utils.importutil import optional_import |
| 14 | +from typing import Any, Dict, Tuple, Union |
| 15 | +from monai.deploy.core import Image |
| 16 | +from pathlib import Path |
| 17 | +MONAI_UTILS = "monai.utils" |
| 18 | +nibabel, _ = optional_import("nibabel", "3.2.1") |
| 19 | +torch, _ = optional_import("torch", "1.10.2") |
| 20 | + |
| 21 | +NdarrayOrTensor, _ = optional_import("monai.config", name="NdarrayOrTensor") |
| 22 | +MetaTensor, _ = optional_import("monai.data.meta_tensor", name="MetaTensor") |
| 23 | +PostFix, _ = optional_import("monai.utils.enums", name="PostFix") # For the default meta_key_postfix |
| 24 | +first, _ = optional_import("monai.utils.misc", name="first") |
| 25 | +ensure_tuple, _ = optional_import(MONAI_UTILS, name="ensure_tuple") |
| 26 | +convert_to_dst_type, _ = optional_import(MONAI_UTILS, name="convert_to_dst_type") |
| 27 | +Key, _ = optional_import(MONAI_UTILS, name="ImageMetaKey") |
| 28 | +MetaKeys, _ = optional_import(MONAI_UTILS, name="MetaKeys") |
| 29 | +SpaceKeys, _ = optional_import(MONAI_UTILS, name="SpaceKeys") |
| 30 | +Compose_, _ = optional_import("monai.transforms", name="Compose") |
| 31 | +ConfigParser_, _ = optional_import("monai.bundle", name="ConfigParser") |
| 32 | +MapTransform_, _ = optional_import("monai.transforms", name="MapTransform") |
| 33 | +SimpleInferer, _ = optional_import("monai.inferers", name="SimpleInferer") |
| 34 | + |
| 35 | +Compose: Any = Compose_ |
| 36 | +MapTransform: Any = MapTransform_ |
| 37 | +ConfigParser: Any = ConfigParser_ |
| 38 | +__all__ = ["MonainnUNetBundleInferenceOperator"] |
| 39 | + |
| 40 | + |
| 41 | +class MonainnUNetBundleInferenceOperator(MonaiBundleInferenceOperator): |
| 42 | + """ |
| 43 | + A specialized operator for performing inference using the MONAI nnUNet bundle. |
| 44 | + This operator extends the `MonaiBundleInferenceOperator` to support nnUNet-specific |
| 45 | + configurations and prediction logic. It initializes the nnUNet predictor and provides |
| 46 | + a method for performing inference on input data. |
| 47 | + |
| 48 | + Attributes |
| 49 | + ---------- |
| 50 | + _nnunet_predictor : torch.nn.Module |
| 51 | + The nnUNet predictor module used for inference. |
| 52 | + |
| 53 | + Methods |
| 54 | + ------- |
| 55 | + _init_config(config_names) |
| 56 | + Initializes the configuration for the nnUNet bundle, including parsing the bundle |
| 57 | + configuration and setting up the nnUNet predictor. |
| 58 | + predict(data, *args, **kwargs) |
| 59 | + Performs inference on the input data using the nnUNet predictor. |
| 60 | + """ |
| 61 | + |
| 62 | + def __init__( |
| 63 | + self, |
| 64 | + *args, |
| 65 | + **kwargs, |
| 66 | + ): |
| 67 | + |
| 68 | + |
| 69 | + super().__init__(*args, **kwargs) |
| 70 | + |
| 71 | + self._nnunet_predictor : torch.nn.Module = None |
| 72 | + |
| 73 | + |
| 74 | + def _init_config(self, config_names): |
| 75 | + |
| 76 | + super()._init_config(config_names) |
| 77 | + parser = get_bundle_config(str(self._bundle_path), config_names) |
| 78 | + parser['bundle_root'] = str(Path(self._bundle_path).parent.parent.parent) |
| 79 | + self._parser = parser |
| 80 | + |
| 81 | + self._nnunet_predictor = parser.get_parsed_content("network_def") |
| 82 | + |
| 83 | + def predict(self, data: Any, *args, **kwargs) -> Union[Image, Any, Tuple[Any, ...], Dict[Any, Any]]: |
| 84 | + """Predicts output using the inferer.""" |
| 85 | + |
| 86 | + self._nnunet_predictor.predictor.network = self._model_network |
| 87 | + #os.environ['nnUNet_def_n_proc'] = "1" |
| 88 | + return self._nnunet_predictor(torch.unsqueeze(data, 0)) |
0 commit comments