Skip to content

Commit c696773

Browse files
Nic-Mapre-commit-ci[bot]root
authored
5821 Add interface for bundle workflows (#5822)
Fixes #5821 . ### Description The main idea of this PR is to solve 3 pain points of the current bundle training or inference workflow: 1. Training or inference is defined in a "rigid" `training` or `evaluation` list, so 3rd party package can't flexibly initialize and run the workflows separately, see: Project-MONAI/model-zoo#294. This PR splits it into `initialize`, `run` and `finalize`, and still compatible with current existing bundles. 2. 3rd party packages need to access the bundle config directly with specifed keys based on `ConfigParser`, but some developers of bundles don't want to write JSON or YAML configs. This PR implemented the interface to support both config-based and python-based workflows. 3. MONAI Label, MONAI deploy, MONAI-FL require many fixed properties of a training or inference workflow, but we don't have definition for them in the code, just described in the developer guide doc of model-zoo. This PR implemented the `TrainProperties` and `inferenceProperties` interfaces and check tool. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Nic Ma <nma@nvidia.com> Signed-off-by: root <root@apt-sh-ai.nvidia.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: root <root@apt-sh-ai.nvidia.com>
1 parent 9fd6d4c commit c696773

File tree

13 files changed

+1000
-179
lines changed

13 files changed

+1000
-179
lines changed

monai/bundle/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from .config_item import ComponentLocator, ConfigComponent, ConfigExpression, ConfigItem, Instantiable
1515
from .config_parser import ConfigParser
16+
from .properties import InferProperties, TrainProperties
1617
from .reference_resolver import ReferenceResolver
1718
from .scripts import (
1819
ckpt_export,
@@ -22,7 +23,6 @@
2223
get_bundle_versions,
2324
init_bundle,
2425
load,
25-
patch_bundle_tracking,
2626
run,
2727
verify_metadata,
2828
verify_net_in_out,
@@ -36,3 +36,4 @@
3636
MACRO_KEY,
3737
load_bundle_config,
3838
)
39+
from .workflows import BundleWorkflow, ConfigWorkflow

monai/bundle/properties.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
"""
13+
The predefined properties for a bundle workflow, other applications can leverage the properties
14+
to interact with the bundle workflow.
15+
Some properties are required and some are optional, optional properties mean: if some component of the
16+
bundle workflow refer to the property, the property must be defined, otherwise, the property can be None.
17+
Every item in this `TrainProperties` or `InferProperties` dictionary is a property,
18+
the key is the property name and the values include:
19+
1. description.
20+
2. whether it's a required property.
21+
3. config item ID name (only applicable when the bundle workflow is defined in config).
22+
4. reference config item ID name (only applicable when the bundle workflow is defined in config).
23+
24+
"""
25+
26+
from __future__ import annotations
27+
28+
from monai.bundle.utils import ID_SEP_KEY
29+
from monai.utils import BundleProperty, BundlePropertyConfig
30+
31+
TrainProperties = {
32+
"bundle_root": {
33+
BundleProperty.DESC: "root path of the bundle.",
34+
BundleProperty.REQUIRED: True,
35+
BundlePropertyConfig.ID: "bundle_root",
36+
},
37+
"device": {
38+
BundleProperty.DESC: "target device to execute the bundle workflow.",
39+
BundleProperty.REQUIRED: True,
40+
BundlePropertyConfig.ID: "device",
41+
},
42+
"dataset_dir": {
43+
BundleProperty.DESC: "directory path of the dataset.",
44+
BundleProperty.REQUIRED: True,
45+
BundlePropertyConfig.ID: "dataset_dir",
46+
},
47+
"trainer": {
48+
BundleProperty.DESC: "training workflow engine.",
49+
BundleProperty.REQUIRED: True,
50+
BundlePropertyConfig.ID: f"train{ID_SEP_KEY}trainer",
51+
},
52+
"max_epochs": {
53+
BundleProperty.DESC: "max number of epochs to execute the training.",
54+
BundleProperty.REQUIRED: True,
55+
BundlePropertyConfig.ID: f"train{ID_SEP_KEY}trainer{ID_SEP_KEY}max_epochs",
56+
},
57+
"train_dataset": {
58+
BundleProperty.DESC: "PyTorch dataset object for the training logic.",
59+
BundleProperty.REQUIRED: True,
60+
BundlePropertyConfig.ID: f"train{ID_SEP_KEY}dataset",
61+
},
62+
"train_dataset_data": {
63+
BundleProperty.DESC: "data source for the training dataset.",
64+
BundleProperty.REQUIRED: True,
65+
BundlePropertyConfig.ID: f"train{ID_SEP_KEY}dataset{ID_SEP_KEY}data",
66+
},
67+
"train_inferer": {
68+
BundleProperty.DESC: "MONAI Inferer object to execute the model computation in training.",
69+
BundleProperty.REQUIRED: True,
70+
BundlePropertyConfig.ID: f"train{ID_SEP_KEY}inferer",
71+
},
72+
"train_handlers": {
73+
BundleProperty.DESC: "event-handlers for the training logic.",
74+
BundleProperty.REQUIRED: False,
75+
BundlePropertyConfig.ID: f"train{ID_SEP_KEY}handlers",
76+
BundlePropertyConfig.REF_ID: f"train{ID_SEP_KEY}trainer{ID_SEP_KEY}train_handlers",
77+
},
78+
"train_preprocessing": {
79+
BundleProperty.DESC: "preprocessing for the training input data.",
80+
BundleProperty.REQUIRED: False,
81+
BundlePropertyConfig.ID: f"train{ID_SEP_KEY}preprocessing",
82+
BundlePropertyConfig.REF_ID: f"train{ID_SEP_KEY}dataset{ID_SEP_KEY}transform",
83+
},
84+
"train_postprocessing": {
85+
BundleProperty.DESC: "postprocessing for the training model output data.",
86+
BundleProperty.REQUIRED: False,
87+
BundlePropertyConfig.ID: f"train{ID_SEP_KEY}postprocessing",
88+
BundlePropertyConfig.REF_ID: f"train{ID_SEP_KEY}trainer{ID_SEP_KEY}postprocessing",
89+
},
90+
"train_key_metric": {
91+
BundleProperty.DESC: "key metric to compute on the training data.",
92+
BundleProperty.REQUIRED: False,
93+
BundlePropertyConfig.ID: f"train{ID_SEP_KEY}key_metric",
94+
BundlePropertyConfig.REF_ID: f"train{ID_SEP_KEY}trainer{ID_SEP_KEY}key_train_metric",
95+
},
96+
"evaluator": {
97+
BundleProperty.DESC: "validation workflow engine.",
98+
BundleProperty.REQUIRED: False,
99+
BundlePropertyConfig.ID: f"validate{ID_SEP_KEY}evaluator",
100+
BundlePropertyConfig.REF_ID: "validator", # this REF_ID is the arg name of `ValidationHandler`
101+
},
102+
"val_interval": {
103+
BundleProperty.DESC: "validation interval during the training.",
104+
BundleProperty.REQUIRED: False,
105+
BundlePropertyConfig.ID: "val_interval",
106+
BundlePropertyConfig.REF_ID: "interval", # this REF_ID is the arg name of `ValidationHandler`
107+
},
108+
"val_handlers": {
109+
BundleProperty.DESC: "event-handlers for the validation logic.",
110+
BundleProperty.REQUIRED: False,
111+
BundlePropertyConfig.ID: f"validate{ID_SEP_KEY}handlers",
112+
BundlePropertyConfig.REF_ID: f"validate{ID_SEP_KEY}evaluator{ID_SEP_KEY}val_handlers",
113+
},
114+
"val_dataset": {
115+
BundleProperty.DESC: "PyTorch dataset object for the validation logic.",
116+
BundleProperty.REQUIRED: False,
117+
BundlePropertyConfig.ID: f"validate{ID_SEP_KEY}dataset",
118+
BundlePropertyConfig.REF_ID: f"validate{ID_SEP_KEY}dataloader{ID_SEP_KEY}dataset",
119+
},
120+
"val_dataset_data": {
121+
BundleProperty.DESC: "data source for the validation dataset.",
122+
BundleProperty.REQUIRED: False,
123+
BundlePropertyConfig.ID: f"validate{ID_SEP_KEY}dataset{ID_SEP_KEY}data",
124+
BundlePropertyConfig.REF_ID: None, # no reference to this ID
125+
},
126+
"val_inferer": {
127+
BundleProperty.DESC: "MONAI Inferer object to execute the model computation in validation.",
128+
BundleProperty.REQUIRED: False,
129+
BundlePropertyConfig.ID: f"validate{ID_SEP_KEY}inferer",
130+
BundlePropertyConfig.REF_ID: f"validate{ID_SEP_KEY}evaluator{ID_SEP_KEY}inferer",
131+
},
132+
"val_preprocessing": {
133+
BundleProperty.DESC: "preprocessing for the validation input data.",
134+
BundleProperty.REQUIRED: False,
135+
BundlePropertyConfig.ID: f"validate{ID_SEP_KEY}preprocessing",
136+
BundlePropertyConfig.REF_ID: f"validate{ID_SEP_KEY}dataset{ID_SEP_KEY}transform",
137+
},
138+
"val_postprocessing": {
139+
BundleProperty.DESC: "postprocessing for the validation model output data.",
140+
BundleProperty.REQUIRED: False,
141+
BundlePropertyConfig.ID: f"validate{ID_SEP_KEY}postprocessing",
142+
BundlePropertyConfig.REF_ID: f"validate{ID_SEP_KEY}evaluator{ID_SEP_KEY}postprocessing",
143+
},
144+
"val_key_metric": {
145+
BundleProperty.DESC: "key metric to compute on the validation data.",
146+
BundleProperty.REQUIRED: False,
147+
BundlePropertyConfig.ID: f"validate{ID_SEP_KEY}key_metric",
148+
BundlePropertyConfig.REF_ID: f"validate{ID_SEP_KEY}evaluator{ID_SEP_KEY}key_val_metric",
149+
},
150+
}
151+
152+
153+
InferProperties = {
154+
"bundle_root": {
155+
BundleProperty.DESC: "root path of the bundle.",
156+
BundleProperty.REQUIRED: True,
157+
BundlePropertyConfig.ID: "bundle_root",
158+
},
159+
"device": {
160+
BundleProperty.DESC: "target device to execute the bundle workflow.",
161+
BundleProperty.REQUIRED: True,
162+
BundlePropertyConfig.ID: "device",
163+
},
164+
"network_def": {
165+
BundleProperty.DESC: "network module for the inference.",
166+
BundleProperty.REQUIRED: True,
167+
BundlePropertyConfig.ID: "network_def",
168+
},
169+
"inferer": {
170+
BundleProperty.DESC: "MONAI Inferer object to execute the model computation in inference.",
171+
BundleProperty.REQUIRED: True,
172+
BundlePropertyConfig.ID: "inferer",
173+
},
174+
"preprocessing": {
175+
BundleProperty.DESC: "preprocessing for the input data.",
176+
BundleProperty.REQUIRED: False,
177+
BundlePropertyConfig.ID: "preprocessing",
178+
BundlePropertyConfig.REF_ID: f"dataset{ID_SEP_KEY}transform",
179+
},
180+
"postprocessing": {
181+
BundleProperty.DESC: "postprocessing for the model output data.",
182+
BundleProperty.REQUIRED: False,
183+
BundlePropertyConfig.ID: "postprocessing",
184+
BundlePropertyConfig.REF_ID: f"evaluator{ID_SEP_KEY}postprocessing",
185+
},
186+
"key_metric": {
187+
BundleProperty.DESC: "the key metric during evaluation.",
188+
BundleProperty.REQUIRED: False,
189+
BundlePropertyConfig.ID: "key_metric",
190+
BundlePropertyConfig.REF_ID: f"evaluator{ID_SEP_KEY}key_val_metric",
191+
},
192+
}

0 commit comments

Comments
 (0)