Skip to content

Commit 9b796e4

Browse files
authoredMar 30, 2023
1266 update bundle and model zoo tutorials (#1272)
Fixes #1266 and #1043 . ### Description This PR does the following works: 1. updates tutorials in `bundle` and `model_zoo` via using the new BundleWorkflow interface. 2. To avoid maintaining the spleen bundle in both `tutorials` and `model-zoo`, this PR changes to use download API to download the spleen bundle. 3. remove "MONAI model-zoo" related words in tutorials that can also use NGC bundles. ### Checks <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Avoid including large-size files in the PR. - [x] Clean up long text outputs from code cells in the notebook. - [x] For security purposes, please check the contents and remove any sensitive info such as user names and private key. - [x] Ensure (1) hyperlinks and markdown anchors are working (2) use relative paths for tutorial repo files (3) put figure and graphs in the `./figure` folder - [ ] Notebook runs automatically `./runner.sh -t <path to .ipynb file>` --------- Signed-off-by: Yiheng Wang <[email protected]>
1 parent bb9ccb7 commit 9b796e4

File tree

20 files changed

+72
-700
lines changed

20 files changed

+72
-700
lines changed
 

‎bundle/README.md

-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ This folder contains the `getting started` tutorial and below code examples of t
44
### [introducing_config](./introducing_config)
55
A simple example to introduce the MONAI bundle config and parsing.
66

7-
### [spleen segmentation](./spleen_segmentation)
8-
A bundle example for volumetric (3D) segmentation of the spleen from CT image.
9-
107
### [customize component](./custom_component)
118
Example shows the use cases of bringing customized python components, such as transform, network, and metrics, in a configuration-based workflow.
129

‎bundle/custom_component/README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
# Description
22
This example mainly shows a typical use case that brings customized python components (such as transform, network, metrics) in a configuration-based workflow.
33

4-
Please note that this example depends on the `spleen_segmentation` bundle example and executes via overriding the config file of it.
4+
Please note that this example depends on the `spleen_ct_segmentation` bundle example and executes via overriding the config file of it.
5+
6+
## Download bundle
7+
8+
Download the `spleen_ct_segmentation` bundle for this example.
9+
10+
```shell
11+
python -m monai.bundle download --name spleen_ct_segmentation --bundle_dir "./"
12+
```
513

614
## commands example
715
To run the workflow with customized components, `PYTHONPATH` should be revised to include the path to the customized component:
@@ -12,7 +20,7 @@ And please make sure the folder `custom_component/scripts` is a valid python mod
1220

1321
Override the `train` config with the customized `transform` and execute training:
1422
```bash
15-
python -m monai.bundle run training --meta_file <spleen_configs_path>/metadata.json \
23+
python -m monai.bundle run --meta_file <spleen_configs_path>/metadata.json \
1624
--config_file "['<spleen_configs_path>/train.json','configs/custom_train.json']" \
1725
--logging_file <spleen_configs_path>/logging.conf
1826
```

‎bundle/get_started.md

+23-17
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ You can find the usage examples of MONAI bundle key features and syntax in this
2222
- Override config content at runtime.
2323
- Hybrid programming with config and python code.
2424

25-
## Download dataset
25+
## Download bundle and dataset
2626

27-
Downloads and extracts the dataset for this example.
28-
The dataset comes from http://medicaldecathlon.com/.
27+
Download the `spleen_ct_segmentation` bundle for this example.
28+
```shell
29+
python -m monai.bundle download --name spleen_ct_segmentation --bundle_dir "./"
30+
```
31+
32+
The dataset for this example comes from http://medicaldecathlon.com/.
2933
Here specify a directory with the `MONAI_DATA_DIRECTORY` environment variable to save downloaded dataset and outputs, if no environment, save to the temorary directory.
3034

3135
```python
@@ -46,7 +50,7 @@ if not os.path.exists(data_dir):
4650

4751
## Define train config - Set imports and input / output environments
4852

49-
Now let's start to define the config file for a regular training task. MONAI bundle support both `JSON` and `YAML` format, here we use `JSON` as the example. the [whole config for training](spleen_segmentation/configs/train.json) is available and can be a reference.
53+
Now let's start to define the config file for a regular training task. MONAI bundle support both `JSON` and `YAML` format, here we use `JSON` as the example. After downloading the bundle, the train config file in `spleen_ct_segmentation/configs/train.json` is available for reference.
5054

5155
According to the predefined syntax of MONAI bundle, `$` indicates an expression to evaluate and `@` refers to another object in the config content. For more details about the syntax in bundle config, please check: https://docs.monai.io/en/latest/config_syntax.html.
5256

@@ -305,24 +309,32 @@ Just show an example of `macro text replacement` to simplify the config content
305309

306310
We can define a `metadata` file in the bundle, which contains the metadata information relating to the model, including what the shape and format of inputs and outputs are, what the meaning of the outputs are, what type of model is present, and other information. The structure is a dictionary containing a defined set of keys with additional user-specified keys.
307311

308-
A typical [metadata example](spleen_segmentation/configs/metadata.json) is available.
312+
After downloading the bundle, a typical metadata example in `spleen_ct_segmentation/configs/metadata.json` is available for reference.
309313

310314
## Execute training with bundle script - `run`
311315

312316
There are several predefined scripts in MONAI bundle module, here we leverage the `run` script and specify the ID of trainer in the config.
313317

314-
Just define the entry point expressions in the config to execute in order, and specify the `runner_id` in CLI script.
318+
We can define the following three sections:
319+
320+
"run" determines the section of the expected config expression to run.
321+
"initialize" determines the section of the expected config expression to initialize before running.
322+
"finalize" determines the section of the expected config expression to finalize after running.
323+
324+
In this example, only "initialize" and "run" are utilized:
315325

316326
```json
317-
"training": [
327+
"initialize": [
318328
"$monai.utils.set_determinism(seed=123)",
319-
"$setattr(torch.backends.cudnn, 'benchmark', True)",
329+
"$setattr(torch.backends.cudnn, 'benchmark', True)"
330+
],
331+
"run": [
320332
"$@train#trainer.run()"
321333
]
322334
```
323335

324336
```shell
325-
python -m monai.bundle run training --config_file configs/train.json
337+
python -m monai.bundle run --config_file configs/train.json
326338
```
327339

328340
## Execute training with bundle script - Override config at runtime
@@ -331,11 +343,11 @@ To override some config items at runtime, users can specify the target `id` and
331343

332344
Please note that "#" and "$" may be meaningful syntax for some `shell` and `CLI` tools, so may need to add escape character or quotes for them in the command line, like: `"\$torch.device('cuda:1')"`. For more details: https://github.com/google/python-fire/blob/v0.4.0/fire/parser.py#L60.
333345
```shell
334-
python -m monai.bundle run training --config_file configs/train.json --device "\$torch.device('cuda:1')"
346+
python -m monai.bundle run --config_file configs/train.json --device "\$torch.device('cuda:1')"
335347
```
336348
Override content from another config file.
337349
```shell
338-
python -m monai.bundle run training --config_file configs/train.json --network "%configs/test.json#network"
350+
python -m monai.bundle run --config_file configs/train.json --network "%configs/inference.json#network"
339351
```
340352

341353
## Execute other bundle scripts
@@ -365,12 +377,6 @@ python -m monai.bundle verify_net_in_out network --meta_file <metadata path> --c
365377
```
366378
The acceptable data shape in the metadata can support `"*"` for any size, or use an expression with Python mathematical operators and one character variables to represent dependence on an unknown quantity, for example, `"2**p"` represents a size which must be a power of 2, `"2**p*n"` must be a multiple of a power of 2. `"spatial_shape": [ "32 * n", "2 ** p * n", "*"]`.
367379

368-
369-
5. Download a bundle from Github release or URL.
370-
```shell
371-
python -m monai.bundle download --name <bundle_name> --version "0.1.0" --bundle_dir "./"
372-
```
373-
374380
## Hybrid programming with config and python code
375381

376382
A MONAI bundle supports flexible customized logic, there are several ways to achieve this:

‎bundle/hybrid_programming/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ python -m scripts.inference run \
1010
--config_file "['configs/data_loading.json','configs/net_inferer.yaml','configs/post_processing.json']" \
1111
--ckpt_path <path_to_checkpoint>
1212
```
13+
Before running above command, please check the script first and modify `datalist` and `output_dir` according to the actual paths.
1314

1415
Parse the config file content in Python and overriding the default values, using the entry point defined in `scrips/train_demo.py`.
1516
```bash

‎bundle/introducing_config/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ plt.show()
152152

153153
## Topics not covered but possible in the config
154154

155-
- Running customized Python components (made available on the `PYTHONPATH`, more examples [in the model_zoo](https://github.com/Project-MONAI/model-zoo)).
155+
- Running customized Python components (made available on the `PYTHONPATH`).
156156
- Overriding the component in `example.yaml` using, for example, `--id=new_value` in the command line.
157157
- Multiple configuration files and cross-file references.
158158
- Replacing in terms of plain texts instead of Python objects ([tutorial](../get_started.md)).

‎bundle/spleen_segmentation/configs/evaluate.json

-58
This file was deleted.

‎bundle/spleen_segmentation/configs/inference.json

-132
This file was deleted.

‎bundle/spleen_segmentation/configs/logging.conf

-21
This file was deleted.

‎bundle/spleen_segmentation/configs/metadata.json

-78
This file was deleted.

‎bundle/spleen_segmentation/configs/multi_gpu_train.json

-34
This file was deleted.

‎bundle/spleen_segmentation/configs/train.json

-232
This file was deleted.

‎bundle/spleen_segmentation/docs/README.md

-90
This file was deleted.

‎bundle/spleen_segmentation/docs/license.txt

-6
This file was deleted.

‎model_zoo/README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
This folder contains typical usage examples of the bundles in MONAI model-zoo.
1+
This folder contains tutorials and examples to show how to use an existing bundle of our released models, including:
2+
3+
- Adapt a bundle to another dataset.
4+
- Integrate a bundle into your own application.
5+
- Load the pre-trained weights from a bundle and do transfer-learning.
6+
- Extend the features of workflow in MONAI bundles based on `event-handler` mechanism.
27

38
## Getting Started
49

5-
To download and get started with the models, please see also https://monai.io/model-zoo.
10+
To download and get started with the models, `monai.bundle` API is recommended. The following is an example to download the `spleen_ct_segmentation` bundle:
11+
12+
```shell
13+
python -m monai.bundle download --name spleen_ct_segmentation --bundle_dir "./"
14+
```

‎model_zoo/adapt_bundle_to_another_dataset/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Adapt a bundle to another dataset.
33

44
# Overview
5-
This tutorial shows how to adapt an example monai bundle from MONAI model-zoo to a new dataset.
5+
This tutorial shows how to adapt an example monai bundle to a new dataset.
66

77
## Data
88
The new dataset is BTCV challenge dataset (https://www.synapse.org/#!Synapse:syn3193805/wiki/217752). It has 24 Training + 6 Validation CT abdominal scans.
@@ -19,9 +19,9 @@ In this experiment, we focus on spleen only, which has intensity = 1 in the labe
1919

2020
Step 4: `cp -avr ./data/RawData/Training/img ./data/btcv_spleen/imagesTr`
2121

22-
## Download example monai bundle from model-zoo
22+
## Download example monai bundle
2323
```
24-
python -m monai.bundle download --name spleen_ct_segmentation --version "0.1.1" --bundle_dir "./"
24+
python -m monai.bundle download --name spleen_ct_segmentation --bundle_dir "./"
2525
```
2626

2727
## Training configuration
@@ -33,7 +33,7 @@ Actual Model Input: 96 x 96 x 96
3333
## Modify ./spleen_ct_segmentation/configs/train.json from the downloaded example
3434
| Old json config | Updated json config |
3535
| --- | --- |
36-
| "bundle_root": "/workspace/data/tutorials/bundle/spleen_segmentation", | "bundle_root": "./spleen_ct_segmentation", |
36+
| "bundle_root": ".", | "bundle_root": "./spleen_ct_segmentation", |
3737
| "dataset_dir": "/workspace/data/Task09_Spleen",| "data_file_base_dir": "./data/btcv_spleen", |
3838
| "images": "$list(sorted(glob.glob(@dataset_dir + '/imagesTr/*.nii.gz')))",| "data_list_file_path": "./data/dataset_0.json", |
3939
| "labels": "$list(sorted(glob.glob(@dataset_dir + '/labelsTr/*.nii.gz')))",| "train_datalist": "$monai.data.load_decathlon_datalist(@data_list_file_path, is_segmentation=True, data_list_key='training', base_dir=@data_file_base_dir)", |
@@ -59,13 +59,13 @@ Mean Dice = 0.9294.
5959
Execute training:
6060

6161
```
62-
python -m monai.bundle run training --meta_file ./spleen_ct_segmentation/configs/metadata.json --config_file ./spleen_ct_segmentation/configs/train.json --logging_file ./spleen_ct_segmentation/configs/logging.conf
62+
python -m monai.bundle run --meta_file ./spleen_ct_segmentation/configs/metadata.json --config_file ./spleen_ct_segmentation/configs/train.json --logging_file ./spleen_ct_segmentation/configs/logging.conf
6363
```
6464

6565
Override the `train` config to execute evaluation:
6666

6767
```
68-
python -m monai.bundle run evaluating --meta_file ./spleen_ct_segmentation/configs/metadata.json --config_file "['./spleen_ct_segmentation/configs/train.json','./spleen_ct_segmentation/configs/evaluate.json']" --logging_file ./spleen_ct_segmentation/configs/logging.conf
68+
python -m monai.bundle run --meta_file ./spleen_ct_segmentation/configs/metadata.json --config_file "['./spleen_ct_segmentation/configs/train.json','./spleen_ct_segmentation/configs/evaluate.json']" --logging_file ./spleen_ct_segmentation/configs/logging.conf
6969
```
7070

7171

‎model_zoo/app_integrate_bundle/README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ The script is tested with:
2222
You can conda environments to install the dependencies.
2323

2424
```bash
25-
pip install monai==0.9.1 scikit-learn==0.24.2
25+
pip install scikit-learn==0.24.2
2626
```
2727

28-
or you can just use nvidia docker.
28+
or you can just use MONAI docker.
2929
```bash
30-
docker pull projectmonai/monai:0.9.1
30+
docker pull projectmonai/monai:latest
3131
```
3232

3333
For more information please check out [the installation guide](https://docs.monai.io/en/latest/installation.html).
@@ -42,7 +42,11 @@ python ./ensemble.py -h
4242
### Get started
4343
1. Prepare your bundle.
4444

45-
First download a bundle from [model-zoo](https://github.com/Project-MONAI/model-zoo/releases/tag/hosting_storage_v1) to somewhere as your `bundle_root_path`.
45+
First download a bundle to somewhere as your `bundle_root_path`:
46+
47+
```shell
48+
python -m monai.bundle download --name spleen_ct_segmentation --bundle_dir "./"
49+
```
4650

4751
2. Prepare your data.
4852

‎model_zoo/app_integrate_bundle/ensemble.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class EnsembleTrainTask:
4848
"""
4949
To construct an n-fold training and ensemble infer on any dataset.
5050
Just specify the bundle root path and data root path.
51-
Bundle can be download from https://github.com/Project-MONAI/model-zoo/releases/tag/hosting_storage_v1
5251
Date root path also need a dataset.json which should be like:
5352
{
5453
"training": [
@@ -256,7 +255,7 @@ def run_command(self, cmd, env):
256255
if __name__ == "__main__":
257256
"""
258257
Usage
259-
first download a bundle from model-zoo to somewhere as your bundle_root path
258+
first download a bundle to somewhere as your bundle_root path
260259
split your data into train and test datalist
261260
train datalist: [
262261
{

‎model_zoo/extend_workflow_with_handler.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Overview
2-
This tutorial shows how to extend the features of workflow in the model-zoo bundles based on `event-handler` mechanism.
2+
This tutorial shows how to extend the features of workflow in MONAI bundles based on `event-handler` mechanism.
33
Here we try to add the execution time computation logic in the spleen segmentation bundle.
44

55
## Event-handler mechanism
6-
The bundles in the `model-zoo` are constructed by MONAI workflow, which can enable quick start of training and evaluation experiments.
6+
Bundles that constructed by MONAI workflow enable quick start of training and evaluation experiments.
77
The MONAI workflow is compatible with pytorch-ignite `Engine` and `Event-Handler` mechanism: https://pytorch-ignite.ai/.
88

99
So we can easily extend new features to the workflow by defining a new independent event handler and attaching to the workflow engine.
@@ -91,9 +91,9 @@ like the existing [StatsHandler](https://docs.monai.io/en/stable/handlers.html#m
9191
MONAI contains a convenient utility `monai.handlers.from_engine` to support most of the typical `output_transform` callables.
9292
For more details, please refer to: https://docs.monai.io/en/stable/handlers.html#monai.handlers.utils.from_engine.
9393

94-
## Download example MONAI bundle from model-zoo
94+
## Download example MONAI bundle
9595
```
96-
python -m monai.bundle download --name spleen_ct_segmentation --version "0.1.1" --bundle_dir "./"
96+
python -m monai.bundle download --name spleen_ct_segmentation --bundle_dir "./"
9797
```
9898

9999
## Extend the workflow to print the execution time for every iteration, every epoch and total time
@@ -152,5 +152,5 @@ And please make sure the folder `spleen_ct_segmentation/scripts` is a valid pyth
152152
Execute training:
153153

154154
```
155-
python -m monai.bundle run training --meta_file configs/metadata.json --config_file configs/train.json --logging_file configs/logging.conf
155+
python -m monai.bundle run --meta_file configs/metadata.json --config_file configs/train.json --logging_file configs/logging.conf
156156
```

‎model_zoo/transfer_learning_with_bundle/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# Description
2-
Load the pre-trained weights from a MONAI model-zoo model and execute the transfer-learning in a typical PyTorch training program based on it.
2+
Load the pre-trained weights from a MONAI bundle and execute the transfer-learning in a typical PyTorch training program based on it.
33

44
# Overview
5-
This tutorial shows how to load a pretrained U-net from MONAI model-zoo and train it on BTCV dataset using pytorch. The pretrained U-net was trained on volumetric (3D) segmentation of CT spleen from MSD dataset.
5+
This tutorial shows how to load a pretrained U-net from a MONAI bundle and train it on BTCV dataset using pytorch. The pretrained U-net was trained on volumetric (3D) segmentation of CT spleen from MSD dataset.
66

77
## Data
88
The description of data and data preparation can be found in [adapt_bundle_to_another_dataset](../adapt_bundle_to_another_dataset)
99

1010
## Pretrained model
11-
The pretrained model is from MONAI model-zoo spleen_ct_segmentation.
11+
The pretrained model is from MONAI spleen_ct_segmentation bundle.
1212
It was trained using the runner-up [1] awarded pipeline of the "Medical Segmentation Decathlon Challenge 2018" using the UNet architecture [2] with 32 training images and 9 validation images.
1313

1414
It can automatically download the bundle and load the model with the following code, which can be found in `train.py`.
1515
```
1616
import monai
1717
pretrained_model = monai.bundle.load(
18-
name="spleen_ct_segmentation", bundle_dir="./", version="0.1.1"
18+
name="spleen_ct_segmentation", bundle_dir="./"
1919
)
2020
model.load_state_dict(pretrained_model)
2121
```

‎model_zoo/transfer_learning_with_bundle/train.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def main(tempdir, load_pretrained_ckpt=False):
7272
save_model = "./models/model_from_scratch.pt"
7373
max_epochs = 600
7474

75-
# create UNet and load pretrained weights from monai model-zoo
75+
# create UNet and load pretrained weights
7676
device = torch.device("cuda:0")
7777
model = UNet(
7878
spatial_dims=3,
@@ -85,8 +85,7 @@ def main(tempdir, load_pretrained_ckpt=False):
8585
).to(device)
8686

8787
if load_pretrained_ckpt:
88-
print("Load model from monai model-zoo.")
89-
pretrained_model = monai.bundle.load(name="spleen_ct_segmentation", bundle_dir="./", version="0.1.1")
88+
pretrained_model = monai.bundle.load(name="spleen_ct_segmentation", bundle_dir="./")
9089
model.load_state_dict(pretrained_model)
9190

9291
# define transforms for image and segmentation

0 commit comments

Comments
 (0)
Please sign in to comment.