-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Pathology Probability Map Generator #1893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bhashemian
merged 15 commits into
Project-MONAI:master
from
bhashemian:prob_map_generator
Mar 31, 2021
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9379b13
Implement ProbMapGenerator handler
bhashemian e15cf72
Update doc and init
bhashemian adfff4e
Sort init imports
bhashemian 16cb357
Merge branch 'master' into prob_map_generator
bhashemian a77d876
Merge branch 'master' into prob_map_generator
bhashemian d75c657
Add unittest for ProbMapGenerator
bhashemian 55c1c86
Ignore if ignite is not available
bhashemian eee4f60
Update Engine import
bhashemian ad31381
Exclude from min-test
bhashemian bbfc0c8
Address all the comments
bhashemian 193cf5c
Fix file path and dtype
bhashemian c8853e1
Merge branch 'master' into prob_map_generator
bhashemian 2cf8a4a
Merge branch 'master' into prob_map_generator
bhashemian c478a84
Merge branch 'master' into prob_map_generator
bhashemian b9d3670
Merge branch 'master' into prob_map_generator
bhashemian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import logging | ||
| import os | ||
| from typing import TYPE_CHECKING, Dict, Optional | ||
|
|
||
| import numpy as np | ||
|
|
||
| from monai.config import DtypeLike | ||
| from monai.utils import exact_version, optional_import | ||
|
|
||
| Events, _ = optional_import("ignite.engine", "0.4.4", exact_version, "Events") | ||
| if TYPE_CHECKING: | ||
| from ignite.engine import Engine | ||
| else: | ||
| Engine, _ = optional_import("ignite.engine", "0.4.4", exact_version, "Engine") | ||
|
|
||
|
|
||
| class ProbMapProducer: | ||
| """ | ||
| Event handler triggered on completing every iteration to save the probability map | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| output_dir: str = "./", | ||
| output_postfix: str = "", | ||
| dtype: DtypeLike = np.float64, | ||
| name: Optional[str] = None, | ||
| ) -> None: | ||
| """ | ||
| Args: | ||
| output_dir: output directory to save probability maps. | ||
| output_postfix: a string appended to all output file names. | ||
| dtype: the data type in which the probability map is stored. Default np.float64. | ||
| name: identifier of logging.logger to use, defaulting to `engine.logger`. | ||
|
|
||
| """ | ||
| self.logger = logging.getLogger(name) | ||
| self._name = name | ||
| self.output_dir = output_dir | ||
| self.output_postfix = output_postfix | ||
| self.dtype = dtype | ||
| self.prob_map: Dict[str, np.ndarray] = {} | ||
| self.level: Dict[str, int] = {} | ||
| self.counter: Dict[str, int] = {} | ||
| self.num_done_images: int = 0 | ||
| self.num_images: int = 0 | ||
|
|
||
| def attach(self, engine: Engine) -> None: | ||
| """ | ||
| Args: | ||
| engine: Ignite Engine, it can be a trainer, validator or evaluator. | ||
| """ | ||
|
|
||
| self.num_images = len(engine.data_loader.dataset.data) | ||
|
|
||
| for sample in engine.data_loader.dataset.data: | ||
| name = sample["name"] | ||
| self.prob_map[name] = np.zeros(sample["mask_shape"], dtype=self.dtype) | ||
| self.counter[name] = len(sample["mask_locations"]) | ||
| self.level[name] = sample["level"] | ||
|
|
||
| if self._name is None: | ||
| self.logger = engine.logger | ||
| if not engine.has_event_handler(self, Events.ITERATION_COMPLETED): | ||
| engine.add_event_handler(Events.ITERATION_COMPLETED, self) | ||
| if not engine.has_event_handler(self.finalize, Events.COMPLETED): | ||
| engine.add_event_handler(Events.COMPLETED, self.finalize) | ||
|
|
||
| def __call__(self, engine: Engine) -> None: | ||
| """ | ||
| This method assumes self.batch_transform will extract metadata from the input batch. | ||
|
|
||
| Args: | ||
| engine: Ignite Engine, it can be a trainer, validator or evaluator. | ||
| """ | ||
| names = engine.state.batch["name"] | ||
| locs = engine.state.batch["mask_location"] | ||
| pred = engine.state.output["pred"] | ||
| for i, name in enumerate(names): | ||
| self.prob_map[name][locs[0][i], locs[1][i]] = pred[i] | ||
| self.counter[name] -= 1 | ||
| if self.counter[name] == 0: | ||
| self.save_prob_map(name) | ||
|
|
||
| def save_prob_map(self, name: str) -> None: | ||
| """ | ||
| This method save the probability map for an image, when its inference is finished, | ||
| and delete that probability map from memory. | ||
|
|
||
| Args: | ||
| name: the name of image to be saved. | ||
| """ | ||
| file_path = os.path.join(self.output_dir, name) | ||
| np.save(file_path + self.output_postfix + ".npy", self.prob_map[name]) | ||
|
|
||
| self.num_done_images += 1 | ||
| self.logger.info(f"Inference of '{name}' is done [{self.num_done_images}/{self.num_images}]!") | ||
| del self.prob_map[name] | ||
| del self.counter[name] | ||
| del self.level[name] | ||
|
|
||
| def finalize(self, engine: Engine): | ||
| self.logger.info(f"Probability map is created for {self.num_done_images}/{self.num_images} images!") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Copyright 2020 - 2021 MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
| import torch | ||
| from ignite.engine import Engine | ||
| from parameterized import parameterized | ||
| from torch.utils.data import DataLoader | ||
|
|
||
| from monai.apps.pathology.handlers import ProbMapProducer | ||
| from monai.data.dataset import Dataset | ||
| from monai.engines import Evaluator | ||
| from monai.handlers import ValidationHandler | ||
|
|
||
| TEST_CASE_0 = ["image_inference_output_1", 2] | ||
| TEST_CASE_1 = ["image_inference_output_2", 9] | ||
| TEST_CASE_2 = ["image_inference_output_3", 1000] | ||
|
|
||
|
|
||
| class TestDataset(Dataset): | ||
| def __init__(self, name, size): | ||
| self.data = [ | ||
| { | ||
| "name": name, | ||
| "mask_shape": (size, size), | ||
| "mask_locations": [[i, i] for i in range(size)], | ||
| "level": 0, | ||
| } | ||
| ] | ||
| self.len = size | ||
|
|
||
| def __len__(self): | ||
| return self.len | ||
|
|
||
| def __getitem__(self, index): | ||
| return { | ||
| "name": self.data[0]["name"], | ||
| "mask_location": self.data[0]["mask_locations"][index], | ||
| "pred": index + 1, | ||
| } | ||
|
|
||
|
|
||
| class TestEvaluator(Evaluator): | ||
| def _iteration(self, engine, batchdata): | ||
| return batchdata | ||
|
|
||
|
|
||
| class TestHandlerProbMapGenerator(unittest.TestCase): | ||
| @parameterized.expand( | ||
| [ | ||
| TEST_CASE_0, | ||
| TEST_CASE_1, | ||
| TEST_CASE_2, | ||
| ] | ||
| ) | ||
| def test_prob_map_generator(self, name, size): | ||
| # set up dataset | ||
| dataset = TestDataset(name, size) | ||
| data_loader = DataLoader(dataset, batch_size=1) | ||
|
|
||
| # set up engine | ||
| def inference(enging, batch): | ||
| pass | ||
|
|
||
| engine = Engine(inference) | ||
|
|
||
| # add ProbMapGenerator() to evaluator | ||
| output_dir = os.path.join(os.path.dirname(__file__), "testing_data") | ||
| prob_map_gen = ProbMapProducer(output_dir=output_dir) | ||
|
|
||
| evaluator = TestEvaluator(torch.device("cpu:0"), data_loader, size, val_handlers=[prob_map_gen]) | ||
|
|
||
| # set up validation handler | ||
| validation = ValidationHandler(evaluator, interval=1) | ||
| validation.attach(engine) | ||
|
|
||
| engine.run(data_loader) | ||
|
|
||
| prob_map = np.load(os.path.join(output_dir, name + ".npy")) | ||
| self.assertListEqual(np.diag(prob_map).astype(int).tolist(), list(range(1, size + 1))) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.