-
Notifications
You must be signed in to change notification settings - Fork 1.2k
5821 Enhance bundle CLI entry for different bundle workflows #6181
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
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
42a45e0
Merge pull request #19 from Project-MONAI/master
Nic-Ma cd16a13
Merge pull request #32 from Project-MONAI/master
Nic-Ma 6f87afd
Merge pull request #180 from Project-MONAI/dev
Nic-Ma f398298
Merge pull request #214 from Project-MONAI/dev
Nic-Ma ec463d6
Merge pull request #397 from Project-MONAI/dev
Nic-Ma ca62306
Merge pull request #429 from Project-MONAI/dev
Nic-Ma 6b63f3e
Merge branch 'Project-MONAI:main' into main
Nic-Ma 8abbb2d
Merge pull request #461 from Project-MONAI/dev
Nic-Ma a007ac8
[DLMED] enhance CLI entry
Nic-Ma ebc8b39
[DLMED] add unit tests
Nic-Ma d54520e
Merge branch 'dev' into 5821-bundle-entry
Nic-Ma 841523d
Merge branch 'dev' into 5821-bundle-entry
Nic-Ma d249406
[DLMED] change to new API
Nic-Ma 9645194
Merge branch 'dev' into 5821-bundle-entry
Nic-Ma 810b1f8
[DLMED] update according to comments
Nic-Ma ef1b03f
[DLMED] fix mypy
Nic-Ma ff380b4
[DLMED] fix test error
Nic-Ma 12a01ef
Merge branch 'dev' into 5821-bundle-entry
Nic-Ma 1759bb5
[DLMED] update according to comments
Nic-Ma b6bd5df
Merge branch 'dev' into 5821-bundle-entry
Nic-Ma 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
init_bundle, | ||
load, | ||
run, | ||
run_workflow, | ||
trt_export, | ||
verify_metadata, | ||
verify_net_in_out, | ||
|
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
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,127 @@ | ||
# Copyright (c) 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. | ||
|
||
from __future__ import annotations | ||
|
||
import torch | ||
|
||
from monai.bundle import BundleWorkflow | ||
from monai.data import DataLoader, Dataset | ||
from monai.engines import SupervisedEvaluator | ||
from monai.inferers import SlidingWindowInferer | ||
from monai.networks.nets import UNet | ||
from monai.transforms import ( | ||
Activationsd, | ||
AsDiscreted, | ||
Compose, | ||
EnsureChannelFirstd, | ||
LoadImaged, | ||
SaveImaged, | ||
ScaleIntensityd, | ||
) | ||
from monai.utils import BundleProperty, set_determinism | ||
|
||
|
||
class NonConfigWorkflow(BundleWorkflow): | ||
""" | ||
Test class simulates the bundle workflow defined by Python script directly. | ||
|
||
""" | ||
|
||
def __init__(self, filename, output_dir): | ||
super().__init__(workflow="inference") | ||
self.filename = filename | ||
self.output_dir = output_dir | ||
self._bundle_root = "will override" | ||
self._device = torch.device("cpu") | ||
self._network_def = None | ||
self._inferer = None | ||
self._preprocessing = None | ||
self._postprocessing = None | ||
self._evaluator = None | ||
|
||
def initialize(self): | ||
set_determinism(0) | ||
if self._preprocessing is None: | ||
self._preprocessing = Compose( | ||
[LoadImaged(keys="image"), EnsureChannelFirstd(keys="image"), ScaleIntensityd(keys="image")] | ||
) | ||
dataset = Dataset(data=[{"image": self.filename}], transform=self._preprocessing) | ||
dataloader = DataLoader(dataset, batch_size=1, num_workers=4) | ||
|
||
if self._network_def is None: | ||
self._network_def = UNet( | ||
spatial_dims=3, | ||
in_channels=1, | ||
out_channels=2, | ||
channels=[2, 2, 4, 8, 4], | ||
strides=[2, 2, 2, 2], | ||
num_res_units=2, | ||
norm="batch", | ||
) | ||
if self._inferer is None: | ||
self._inferer = SlidingWindowInferer(roi_size=(64, 64, 32), sw_batch_size=4, overlap=0.25) | ||
|
||
if self._postprocessing is None: | ||
self._postprocessing = Compose( | ||
[ | ||
Activationsd(keys="pred", softmax=True), | ||
AsDiscreted(keys="pred", argmax=True), | ||
SaveImaged(keys="pred", output_dir=self.output_dir, output_postfix="seg"), | ||
] | ||
) | ||
|
||
self._evaluator = SupervisedEvaluator( | ||
device=self._device, | ||
val_data_loader=dataloader, | ||
network=self._network_def.to(self._device), | ||
inferer=self._inferer, | ||
postprocessing=self._postprocessing, | ||
amp=False, | ||
) | ||
|
||
def run(self): | ||
self._evaluator.run() | ||
|
||
def finalize(self): | ||
return True | ||
|
||
def _get_property(self, name, property): | ||
if name == "bundle_root": | ||
return self._bundle_root | ||
if name == "device": | ||
return self._device | ||
if name == "network_def": | ||
return self._network_def | ||
if name == "inferer": | ||
return self._inferer | ||
if name == "preprocessing": | ||
return self._preprocessing | ||
if name == "postprocessing": | ||
return self._postprocessing | ||
if property[BundleProperty.REQUIRED]: | ||
raise ValueError(f"unsupported property '{name}' is required in the bundle properties.") | ||
|
||
def _set_property(self, name, property, value): | ||
if name == "bundle_root": | ||
self._bundle_root = value | ||
elif name == "device": | ||
self._device = value | ||
elif name == "network_def": | ||
self._network_def = value | ||
elif name == "inferer": | ||
self._inferer = value | ||
elif name == "preprocessing": | ||
self._preprocessing = value | ||
elif name == "postprocessing": | ||
self._postprocessing = value | ||
elif property[BundleProperty.REQUIRED]: | ||
raise ValueError(f"unsupported property '{name}' is required in the bundle properties.") |
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
Oops, something went wrong.
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.