-
Notifications
You must be signed in to change notification settings - Fork 739
Add inference example for bundle #604
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
22 commits
Select commit
Hold shift + click to select a range
0984c3d
[DLMED] add inference example
Nic-Ma ad20609
[DLMED] update based on latest design
Nic-Ma c0a9138
Merge branch 'master' into 486-add-inference-example
Nic-Ma 48222d2
[DLMED] update config content
Nic-Ma d8dcad3
[DLMED] update to latest design
Nic-Ma e0d6485
Merge branch 'master' into 486-add-inference-example
Nic-Ma 6f8a007
[DLMED] update according to comments
Nic-Ma 5d26a1b
Merge branch '486-add-inference-example' of https://github.com/Projec…
Nic-Ma eddbec7
[DLMED] enhance the expression
Nic-Ma 6cebebe
[DLMED] add more transforms
Nic-Ma 16f1e5b
[DLMED] adjust config
Nic-Ma 685b42b
[DLMED] add checkpoint logic
Nic-Ma 92d2e0b
[DLMED] add checkpoint test
Nic-Ma 8a89c18
[DLMED] update imports
Nic-Ma e655fb6
Merge branch 'master' into 486-add-inference-example
Nic-Ma 8f1aeaa
[DLMED] update for _requires_
Nic-Ma 8a51fa2
[DLMED] add logging
Nic-Ma 36f5ffd
[DLMED] fix typo
Nic-Ma d76a470
Merge branch 'master' into 486-add-inference-example
Nic-Ma ed7f31c
Update README.md
wyli 00f82e4
Update metadata.json
wyli 07d8204
[DLMED] add hugging face download
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
147 changes: 147 additions & 0 deletions
147
modules/bundles/spleen_segmentation/configs/inference.json
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,147 @@ | ||
{ | ||
"imports": [ | ||
"$import glob", | ||
"$import os" | ||
], | ||
"cudnn_opt": "$setattr(torch.backends.cudnn, 'benchmark', True)", | ||
"dataset_dir": "/workspace/data/Task09_Spleen", | ||
"ckpt_path": "/workspace/data/tutorials/modules/bundles/spleen_segmentation/models/model.pt", | ||
"download_ckpt": "$monai.apps.utils.download_url('https://huggingface.co/MONAI/example_spleen_segmentation/resolve/main/model.pt', @ckpt_path)", | ||
"device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')", | ||
"datalist": "$list(sorted(glob.glob(@dataset_dir + '/imagesTs/*.nii.gz')))", | ||
"network_def": { | ||
"_target_": "UNet", | ||
"spatial_dims": 3, | ||
"in_channels": 1, | ||
"out_channels": 2, | ||
"channels": [ | ||
16, | ||
32, | ||
64, | ||
128, | ||
256 | ||
], | ||
"strides": [ | ||
2, | ||
2, | ||
2, | ||
2 | ||
], | ||
"num_res_units": 2, | ||
"norm": "batch" | ||
}, | ||
"network": "$@network_def.to(@device)", | ||
"preprocessing": { | ||
"_target_": "Compose", | ||
"transforms": [ | ||
{ | ||
"_target_": "LoadImaged", | ||
"keys": "image" | ||
}, | ||
{ | ||
"_target_": "EnsureChannelFirstd", | ||
"keys": "image" | ||
}, | ||
{ | ||
"_target_": "Orientationd", | ||
"keys": "image", | ||
"axcodes": "RAS" | ||
}, | ||
{ | ||
"_target_": "Spacingd", | ||
"keys": "image", | ||
"pixdim": [1.5, 1.5, 2.0], | ||
"mode": "bilinear" | ||
}, | ||
{ | ||
"_target_": "ScaleIntensityRanged", | ||
"keys": "image", | ||
"a_min": -57, | ||
"a_max": 164, | ||
"b_min": 0, | ||
"b_max": 1, | ||
"clip": true | ||
}, | ||
{ | ||
"_target_": "EnsureTyped", | ||
"keys": "image" | ||
} | ||
] | ||
}, | ||
"dataset": { | ||
"_target_": "Dataset", | ||
"data": "$[{'image': i} for i in @datalist]", | ||
"transform": "@preprocessing" | ||
}, | ||
"dataloader": { | ||
"_target_": "DataLoader", | ||
"dataset": "@dataset", | ||
"batch_size": 1, | ||
"shuffle": false, | ||
"num_workers": 4 | ||
}, | ||
"inferer": { | ||
"_target_": "SlidingWindowInferer", | ||
"roi_size": [ | ||
96, | ||
96, | ||
96 | ||
], | ||
"sw_batch_size": 4, | ||
"overlap": 0.5 | ||
}, | ||
"postprocessing": { | ||
"_target_": "Compose", | ||
"transforms": [ | ||
{ | ||
"_target_": "Activationsd", | ||
"keys": "pred", | ||
"softmax": true | ||
}, | ||
{ | ||
"_target_": "Invertd", | ||
"keys": "pred", | ||
"transform": "@preprocessing", | ||
"orig_keys": "image", | ||
"meta_key_postfix": "meta_dict", | ||
"nearest_interp": false, | ||
"to_tensor": true | ||
}, | ||
{ | ||
"_target_": "AsDiscreted", | ||
"keys": "pred", | ||
"argmax": true | ||
}, | ||
{ | ||
"_target_": "SaveImaged", | ||
"keys": "pred", | ||
"meta_keys": "pred_meta_dict", | ||
"output_dir": "eval" | ||
} | ||
] | ||
}, | ||
"handlers": [ | ||
{ | ||
"_target_": "CheckpointLoader", | ||
"_requires_": "@download_ckpt", | ||
"_disabled_": "$not os.path.exists(@ckpt_path)", | ||
"load_path": "@ckpt_path", | ||
"load_dict": {"model": "@network"} | ||
}, | ||
{ | ||
"_target_": "StatsHandler", | ||
"iteration_log": false | ||
} | ||
], | ||
"evaluator": { | ||
"_target_": "SupervisedEvaluator", | ||
"_requires_": "@cudnn_opt", | ||
"device": "@device", | ||
"val_data_loader": "@dataloader", | ||
"network": "@network", | ||
"inferer": "@inferer", | ||
"postprocessing": "@postprocessing", | ||
"val_handlers": "@handlers", | ||
"amp": false | ||
} | ||
} |
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,21 @@ | ||
[loggers] | ||
keys=root | ||
|
||
[handlers] | ||
keys=consoleHandler | ||
|
||
[formatters] | ||
keys=fullFormatter | ||
|
||
[logger_root] | ||
level=INFO | ||
handlers=consoleHandler | ||
|
||
[handler_consoleHandler] | ||
class=StreamHandler | ||
level=INFO | ||
formatter=fullFormatter | ||
args=(sys.stdout,) | ||
|
||
[formatter_fullFormatter] | ||
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s |
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,76 @@ | ||
{ | ||
"schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_202203171008.json", | ||
"version": "0.1.0", | ||
"changelog": { | ||
"0.1.0": "complete the model package", | ||
"0.0.1": "initialize the model package structure" | ||
}, | ||
"monai_version": "0.8.0", | ||
"pytorch_version": "1.10.0", | ||
"numpy_version": "1.21.2", | ||
"optional_packages_version": { | ||
"nibabel": "3.2.1" | ||
}, | ||
"task": "Decathlon spleen segmentation", | ||
"description": "A pre-trained model for volumetric (3D) segmentation of the spleen from CT image", | ||
"authors": "MONAI team", | ||
"copyright": "Copyright (c) MONAI Consortium", | ||
"data_source": "Task09_Spleen.tar from http://medicaldecathlon.com/", | ||
"data_type": "dicom", | ||
"image_classes": "single channel data, intensity scaled to [0, 1]", | ||
"label_classes": "single channel data, 1 is spleen, 0 is everything else", | ||
"pred_classes": "2 channels OneHot data, channel 1 is spleen, channel 0 is background", | ||
"eval_metrics": { | ||
"mean_dice": 0.96 | ||
}, | ||
"intended_use": "This is an example, not to be used for diagnostic purposes", | ||
"references": [ | ||
"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.", | ||
"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" | ||
], | ||
"network_data_format": { | ||
"inputs": { | ||
"image": { | ||
"type": "image", | ||
"format": "magnitude", | ||
"num_channels": 1, | ||
"spatial_shape": [ | ||
160, | ||
160, | ||
160 | ||
], | ||
"dtype": "float32", | ||
"value_range": [ | ||
0, | ||
1 | ||
], | ||
"is_patch_data": false, | ||
"channel_def": { | ||
"0": "image" | ||
} | ||
} | ||
}, | ||
"outputs": { | ||
"pred": { | ||
"type": "image", | ||
"format": "segmentation", | ||
"num_channels": 2, | ||
"spatial_shape": [ | ||
160, | ||
160, | ||
160 | ||
], | ||
"dtype": "float32", | ||
"value_range": [ | ||
0, | ||
1 | ||
], | ||
"is_patch_data": false, | ||
"channel_def": { | ||
"0": "background", | ||
"1": "spleen" | ||
} | ||
} | ||
} | ||
} | ||
} |
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,49 @@ | ||
# Description | ||
A pre-trained model for volumetric (3D) segmentation of the spleen from CT image. | ||
|
||
# Model Overview | ||
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. | ||
|
||
## Data | ||
The training dataset is Task09_Spleen.tar from http://medicaldecathlon.com/. | ||
|
||
## Training configuration | ||
The training was performed with at least 12GB-memory GPUs. | ||
|
||
Actual Model Input: 96 x 96 x 96 | ||
|
||
## Input and output formats | ||
Input: 1 channel CT image | ||
|
||
Output: 2 channels: Label 1: spleen; Label 0: everything else | ||
|
||
## Scores | ||
This model achieves the following Dice score on the validation data (our own split from the training dataset): | ||
|
||
Mean Dice = 0.96 | ||
|
||
## commands example | ||
Execute inference: | ||
``` | ||
python -m monai.bundle run evaluator --meta_file configs/metadata.json --config_file configs/inference.json --logging_file configs/logging.conf | ||
``` | ||
Verify the metadata format: | ||
``` | ||
python -m monai.bundle verify_metadata --meta_file configs/metadata.json --filepath eval/schema.json | ||
``` | ||
Verify the data shape of network: | ||
``` | ||
python -m monai.bundle verify_net_in_out network_def --meta_file configs/metadata.json --config_file configs/inference.json | ||
``` | ||
Export checkpoint to TorchScript file: | ||
``` | ||
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 | ||
``` | ||
|
||
# Disclaimer | ||
This is an example, not to be used for diagnostic purposes. | ||
|
||
# References | ||
[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. | ||
|
||
[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 |
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,6 @@ | ||
Third Party Licenses | ||
----------------------------------------------------------------------- | ||
|
||
/*********************************************************************/ | ||
i. Medical Segmentation Decathlon | ||
http://medicaldecathlon.com/ |
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.