|
| 1 | +# Copyright (c) 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 typing import Callable, Dict, Sequence, Union |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import torch |
| 16 | + |
| 17 | +from monai.data import decollate_batch, list_data_collate |
| 18 | +from monai.engines import SupervisedEvaluator, SupervisedTrainer |
| 19 | +from monai.engines.utils import IterationEvents |
| 20 | +from monai.transforms import Compose |
| 21 | +from monai.utils.enums import CommonKeys |
| 22 | + |
| 23 | + |
| 24 | +class Interaction: |
| 25 | + """ |
| 26 | + Ignite process_function used to introduce interactions (simulation of clicks) for DeepEdit Training/Evaluation. |
| 27 | +
|
| 28 | + More details about this can be found at: |
| 29 | +
|
| 30 | + Diaz-Pinto et al., MONAI Label: A framework for AI-assisted Interactive |
| 31 | + Labeling of 3D Medical Images. (2022) https://arxiv.org/abs/2203.12362 |
| 32 | +
|
| 33 | + Args: |
| 34 | + deepgrow_probability: probability of simulating clicks in an iteration |
| 35 | + transforms: execute additional transformation during every iteration (before train). |
| 36 | + Typically, several Tensor based transforms composed by `Compose`. |
| 37 | + train: True for training mode or False for evaluation mode |
| 38 | + click_probability_key: key to click/interaction probability |
| 39 | + label_names: Dict of label names |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + deepgrow_probability: float, |
| 45 | + transforms: Union[Sequence[Callable], Callable], |
| 46 | + train: bool, |
| 47 | + label_names: Dict[str, int], |
| 48 | + click_probability_key: str = "probability", |
| 49 | + ) -> None: |
| 50 | + |
| 51 | + self.deepgrow_probability = deepgrow_probability |
| 52 | + self.transforms = Compose(transforms) if not isinstance(transforms, Compose) else transforms |
| 53 | + self.train = train |
| 54 | + self.label_names = label_names |
| 55 | + self.click_probability_key = click_probability_key |
| 56 | + |
| 57 | + def __call__(self, engine: Union[SupervisedTrainer, SupervisedEvaluator], batchdata: Dict[str, torch.Tensor]): |
| 58 | + |
| 59 | + if batchdata is None: |
| 60 | + raise ValueError("Must provide batch data for current iteration.") |
| 61 | + |
| 62 | + if np.random.choice([True, False], p=[self.deepgrow_probability, 1 - self.deepgrow_probability]): |
| 63 | + |
| 64 | + # Run the inner loop only once |
| 65 | + inputs, _ = engine.prepare_batch(batchdata) |
| 66 | + inputs = inputs.to(engine.state.device) |
| 67 | + |
| 68 | + engine.fire_event(IterationEvents.INNER_ITERATION_STARTED) |
| 69 | + |
| 70 | + engine.network.eval() |
| 71 | + with torch.no_grad(): |
| 72 | + if engine.amp: |
| 73 | + with torch.cuda.amp.autocast(): |
| 74 | + predictions = engine.inferer(inputs, engine.network) |
| 75 | + else: |
| 76 | + predictions = engine.inferer(inputs, engine.network) |
| 77 | + batchdata.update({CommonKeys.PRED: predictions}) |
| 78 | + |
| 79 | + # decollate/collate batchdata to execute click transforms |
| 80 | + batchdata_list = decollate_batch(batchdata, detach=True) |
| 81 | + |
| 82 | + for i in range(len(batchdata_list)): |
| 83 | + batchdata_list[i][self.click_probability_key] = 1.0 |
| 84 | + batchdata_list[i] = self.transforms(batchdata_list[i]) |
| 85 | + |
| 86 | + batchdata = list_data_collate(batchdata_list) |
| 87 | + |
| 88 | + engine.fire_event(IterationEvents.INNER_ITERATION_COMPLETED) |
| 89 | + |
| 90 | + else: |
| 91 | + # zero out input guidance channels |
| 92 | + batchdata_list = decollate_batch(batchdata, detach=True) |
| 93 | + for i in range(1, len(batchdata_list[0][CommonKeys.IMAGE])): |
| 94 | + batchdata_list[0][CommonKeys.IMAGE][i] *= 0 |
| 95 | + batchdata = list_data_collate(batchdata_list) |
| 96 | + |
| 97 | + # first item in batch only |
| 98 | + engine.state.batch = batchdata |
| 99 | + |
| 100 | + return engine._iteration(engine, batchdata) |
0 commit comments