Skip to content

Commit ac88fee

Browse files
Nic-Mawyli
andauthored
Add inference example for bundle (#604)
* [DLMED] add inference example Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] update based on latest design Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] update config content Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] update to latest design Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] update according to comments Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] enhance the expression Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] add more transforms Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] adjust config Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] add checkpoint logic Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] add checkpoint test Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] update imports Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] update for _requires_ Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] add logging Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] fix typo Signed-off-by: Nic Ma <nma@nvidia.com> * Update README.md * Update metadata.json * [DLMED] add hugging face download Signed-off-by: Nic Ma <nma@nvidia.com> Co-authored-by: Wenqi Li <831580+wyli@users.noreply.github.com>
1 parent ef59c37 commit ac88fee

File tree

5 files changed

+299
-0
lines changed

5 files changed

+299
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"imports": [
3+
"$import glob",
4+
"$import os"
5+
],
6+
"cudnn_opt": "$setattr(torch.backends.cudnn, 'benchmark', True)",
7+
"dataset_dir": "/workspace/data/Task09_Spleen",
8+
"ckpt_path": "/workspace/data/tutorials/modules/bundles/spleen_segmentation/models/model.pt",
9+
"download_ckpt": "$monai.apps.utils.download_url('https://huggingface.co/MONAI/example_spleen_segmentation/resolve/main/model.pt', @ckpt_path)",
10+
"device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
11+
"datalist": "$list(sorted(glob.glob(@dataset_dir + '/imagesTs/*.nii.gz')))",
12+
"network_def": {
13+
"_target_": "UNet",
14+
"spatial_dims": 3,
15+
"in_channels": 1,
16+
"out_channels": 2,
17+
"channels": [
18+
16,
19+
32,
20+
64,
21+
128,
22+
256
23+
],
24+
"strides": [
25+
2,
26+
2,
27+
2,
28+
2
29+
],
30+
"num_res_units": 2,
31+
"norm": "batch"
32+
},
33+
"network": "$@network_def.to(@device)",
34+
"preprocessing": {
35+
"_target_": "Compose",
36+
"transforms": [
37+
{
38+
"_target_": "LoadImaged",
39+
"keys": "image"
40+
},
41+
{
42+
"_target_": "EnsureChannelFirstd",
43+
"keys": "image"
44+
},
45+
{
46+
"_target_": "Orientationd",
47+
"keys": "image",
48+
"axcodes": "RAS"
49+
},
50+
{
51+
"_target_": "Spacingd",
52+
"keys": "image",
53+
"pixdim": [1.5, 1.5, 2.0],
54+
"mode": "bilinear"
55+
},
56+
{
57+
"_target_": "ScaleIntensityRanged",
58+
"keys": "image",
59+
"a_min": -57,
60+
"a_max": 164,
61+
"b_min": 0,
62+
"b_max": 1,
63+
"clip": true
64+
},
65+
{
66+
"_target_": "EnsureTyped",
67+
"keys": "image"
68+
}
69+
]
70+
},
71+
"dataset": {
72+
"_target_": "Dataset",
73+
"data": "$[{'image': i} for i in @datalist]",
74+
"transform": "@preprocessing"
75+
},
76+
"dataloader": {
77+
"_target_": "DataLoader",
78+
"dataset": "@dataset",
79+
"batch_size": 1,
80+
"shuffle": false,
81+
"num_workers": 4
82+
},
83+
"inferer": {
84+
"_target_": "SlidingWindowInferer",
85+
"roi_size": [
86+
96,
87+
96,
88+
96
89+
],
90+
"sw_batch_size": 4,
91+
"overlap": 0.5
92+
},
93+
"postprocessing": {
94+
"_target_": "Compose",
95+
"transforms": [
96+
{
97+
"_target_": "Activationsd",
98+
"keys": "pred",
99+
"softmax": true
100+
},
101+
{
102+
"_target_": "Invertd",
103+
"keys": "pred",
104+
"transform": "@preprocessing",
105+
"orig_keys": "image",
106+
"meta_key_postfix": "meta_dict",
107+
"nearest_interp": false,
108+
"to_tensor": true
109+
},
110+
{
111+
"_target_": "AsDiscreted",
112+
"keys": "pred",
113+
"argmax": true
114+
},
115+
{
116+
"_target_": "SaveImaged",
117+
"keys": "pred",
118+
"meta_keys": "pred_meta_dict",
119+
"output_dir": "eval"
120+
}
121+
]
122+
},
123+
"handlers": [
124+
{
125+
"_target_": "CheckpointLoader",
126+
"_requires_": "@download_ckpt",
127+
"_disabled_": "$not os.path.exists(@ckpt_path)",
128+
"load_path": "@ckpt_path",
129+
"load_dict": {"model": "@network"}
130+
},
131+
{
132+
"_target_": "StatsHandler",
133+
"iteration_log": false
134+
}
135+
],
136+
"evaluator": {
137+
"_target_": "SupervisedEvaluator",
138+
"_requires_": "@cudnn_opt",
139+
"device": "@device",
140+
"val_data_loader": "@dataloader",
141+
"network": "@network",
142+
"inferer": "@inferer",
143+
"postprocessing": "@postprocessing",
144+
"val_handlers": "@handlers",
145+
"amp": false
146+
}
147+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[loggers]
2+
keys=root
3+
4+
[handlers]
5+
keys=consoleHandler
6+
7+
[formatters]
8+
keys=fullFormatter
9+
10+
[logger_root]
11+
level=INFO
12+
handlers=consoleHandler
13+
14+
[handler_consoleHandler]
15+
class=StreamHandler
16+
level=INFO
17+
formatter=fullFormatter
18+
args=(sys.stdout,)
19+
20+
[formatter_fullFormatter]
21+
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_202203171008.json",
3+
"version": "0.1.0",
4+
"changelog": {
5+
"0.1.0": "complete the model package",
6+
"0.0.1": "initialize the model package structure"
7+
},
8+
"monai_version": "0.8.0",
9+
"pytorch_version": "1.10.0",
10+
"numpy_version": "1.21.2",
11+
"optional_packages_version": {
12+
"nibabel": "3.2.1"
13+
},
14+
"task": "Decathlon spleen segmentation",
15+
"description": "A pre-trained model for volumetric (3D) segmentation of the spleen from CT image",
16+
"authors": "MONAI team",
17+
"copyright": "Copyright (c) MONAI Consortium",
18+
"data_source": "Task09_Spleen.tar from http://medicaldecathlon.com/",
19+
"data_type": "dicom",
20+
"image_classes": "single channel data, intensity scaled to [0, 1]",
21+
"label_classes": "single channel data, 1 is spleen, 0 is everything else",
22+
"pred_classes": "2 channels OneHot data, channel 1 is spleen, channel 0 is background",
23+
"eval_metrics": {
24+
"mean_dice": 0.96
25+
},
26+
"intended_use": "This is an example, not to be used for diagnostic purposes",
27+
"references": [
28+
"Xia, Yingda, et al. '3D Semi-Supervised Learning with Uncertainty-Aware Multi-View Co-Training. arXiv preprint arXiv:1811.12506 (2018). https://arxiv.org/abs/1811.12506.",
29+
"Kerfoot E., Clough J., Oksuz I., Lee J., King A.P., Schnabel J.A. (2019) Left-Ventricle Quantification Using Residual U-Net. In: Pop M. et al. (eds) Statistical Atlases and Computational Models of the Heart. Atrial Segmentation and LV Quantification Challenges. STACOM 2018. Lecture Notes in Computer Science, vol 11395. Springer, Cham. https://doi.org/10.1007/978-3-030-12029-0_40"
30+
],
31+
"network_data_format": {
32+
"inputs": {
33+
"image": {
34+
"type": "image",
35+
"format": "magnitude",
36+
"num_channels": 1,
37+
"spatial_shape": [
38+
160,
39+
160,
40+
160
41+
],
42+
"dtype": "float32",
43+
"value_range": [
44+
0,
45+
1
46+
],
47+
"is_patch_data": false,
48+
"channel_def": {
49+
"0": "image"
50+
}
51+
}
52+
},
53+
"outputs": {
54+
"pred": {
55+
"type": "image",
56+
"format": "segmentation",
57+
"num_channels": 2,
58+
"spatial_shape": [
59+
160,
60+
160,
61+
160
62+
],
63+
"dtype": "float32",
64+
"value_range": [
65+
0,
66+
1
67+
],
68+
"is_patch_data": false,
69+
"channel_def": {
70+
"0": "background",
71+
"1": "spleen"
72+
}
73+
}
74+
}
75+
}
76+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Description
2+
A pre-trained model for volumetric (3D) segmentation of the spleen from CT image.
3+
4+
# Model Overview
5+
This model is 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.
6+
7+
## Data
8+
The training dataset is Task09_Spleen.tar from http://medicaldecathlon.com/.
9+
10+
## Training configuration
11+
The training was performed with at least 12GB-memory GPUs.
12+
13+
Actual Model Input: 96 x 96 x 96
14+
15+
## Input and output formats
16+
Input: 1 channel CT image
17+
18+
Output: 2 channels: Label 1: spleen; Label 0: everything else
19+
20+
## Scores
21+
This model achieves the following Dice score on the validation data (our own split from the training dataset):
22+
23+
Mean Dice = 0.96
24+
25+
## commands example
26+
Execute inference:
27+
```
28+
python -m monai.bundle run evaluator --meta_file configs/metadata.json --config_file configs/inference.json --logging_file configs/logging.conf
29+
```
30+
Verify the metadata format:
31+
```
32+
python -m monai.bundle verify_metadata --meta_file configs/metadata.json --filepath eval/schema.json
33+
```
34+
Verify the data shape of network:
35+
```
36+
python -m monai.bundle verify_net_in_out network_def --meta_file configs/metadata.json --config_file configs/inference.json
37+
```
38+
Export checkpoint to TorchScript file:
39+
```
40+
python -m monai.bundle export network_def --filepath models/model.ts --ckpt_file models/model.pt --meta_file configs/metadata.json --config_file configs/inference.json
41+
```
42+
43+
# Disclaimer
44+
This is an example, not to be used for diagnostic purposes.
45+
46+
# References
47+
[1] Xia, Yingda, et al. "3D Semi-Supervised Learning with Uncertainty-Aware Multi-View Co-Training." arXiv preprint arXiv:1811.12506 (2018). https://arxiv.org/abs/1811.12506.
48+
49+
[2] Kerfoot E., Clough J., Oksuz I., Lee J., King A.P., Schnabel J.A. (2019) Left-Ventricle Quantification Using Residual U-Net. In: Pop M. et al. (eds) Statistical Atlases and Computational Models of the Heart. Atrial Segmentation and LV Quantification Challenges. STACOM 2018. Lecture Notes in Computer Science, vol 11395. Springer, Cham. https://doi.org/10.1007/978-3-030-12029-0_40
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Third Party Licenses
2+
-----------------------------------------------------------------------
3+
4+
/*********************************************************************/
5+
i. Medical Segmentation Decathlon
6+
http://medicaldecathlon.com/

0 commit comments

Comments
 (0)