-
Notifications
You must be signed in to change notification settings - Fork 748
[WIP] 486 Add example model package #487
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
Closed
Closed
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
fefa39a
[DLMED] add folder structure
Nic-Ma 97ebc38
[DLMED] add more components
Nic-Ma 1c56a47
[DLMED] add changelog
Nic-Ma 607c817
[DLMED] add export logic
Nic-Ma e87bc37
[DLMED] add inference logic
Nic-Ma a6e282d
[DLMED] update to pre / post processing according to Wenqi's comments
Nic-Ma 60aa6ab
Merge branch 'master' into 486-add-model-package
Nic-Ma 21e415f
Merge branch 'master' into 486-add-model-package
Nic-Ma 0a88da1
[DLMED] update according to comments
Nic-Ma 88b13ec
Merge branch 'master' into 486-add-model-package
Nic-Ma 1b01dad
[DLMED] add more features
Nic-Ma fab729f
[DLMED] add more materials
Nic-Ma fa5dbf0
[DLMED] update according to discussion
Nic-Ma d7eb155
Merge branch 'master' into 486-add-model-package
Nic-Ma 918a4bd
[DLMED] update according to Eric's suggestion
Nic-Ma c385605
[DLMED] add config schema example
Nic-Ma a6e9280
Merge branch 'master' into 486-add-model-package
Nic-Ma 826192e
[DLMED] network verification example
Nic-Ma 0890d38
[DLMED] change the "override" example to a separate MMAR example
Nic-Ma 1ccb783
[DLMED] update according to comments
Nic-Ma 0171768
[DLMED] refine override example
Nic-Ma c5dc8be
[DLMED] add image data type
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
python ../programs/export.py | ||
--weights ../models/model.pt | ||
--config ../configs/inference.json | ||
--meta ../configs/metadata.json |
3 changes: 3 additions & 0 deletions
3
modules/model_package/spleen_segmentation/commands/inference.sh
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,3 @@ | ||
python ../programs/inference.py | ||
--config ../configs/inference.json | ||
--meta ../configs/metadata.json |
142 changes: 142 additions & 0 deletions
142
modules/model_package/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,142 @@ | ||
{ | ||
"multi_gpu": false, | ||
"amp": true, | ||
"model": "monai.data.load_net_with_metadata('../models/model.ts')[0]", | ||
"network": { | ||
"name": "UNet", | ||
"args": { | ||
"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" | ||
} | ||
}, | ||
"preprocessing": [ | ||
{ | ||
"name": "LoadImaged", | ||
"args": { | ||
"keys": "image" | ||
} | ||
}, | ||
{ | ||
"name": "Spacingd", | ||
"args": { | ||
"keys": "image", | ||
"pixdim": [1.5, 1.5, 2], | ||
"mode": "bilinear" | ||
} | ||
}, | ||
{ | ||
"name": "Orientationd", | ||
"args": { | ||
"keys": "image", | ||
"axcodes": "RAS" | ||
} | ||
}, | ||
{ | ||
"name": "EnsureChannelFirstd", | ||
"args": { | ||
"keys": "image" | ||
} | ||
}, | ||
{ | ||
"name": "ScaleIntensityRanged", | ||
"args": { | ||
"keys": "image", | ||
"a_min": -57, | ||
"a_max": 164, | ||
"b_min": 0, | ||
"b_max": 1, | ||
"clip": true | ||
} | ||
}, | ||
{ | ||
"name": "CropForegroundd", | ||
"args": { | ||
"keys": "image", | ||
"source_key": "image" | ||
} | ||
}, | ||
{ | ||
"name": "ToTensord", | ||
"args": { | ||
"keys": "image" | ||
} | ||
} | ||
], | ||
"datalist": { | ||
"name": "DatasetFunc", | ||
"args": { | ||
"data": "#@dataset_dir + '/dataset.json'", | ||
"func": "monai.data.load_decathlon_datalist", | ||
"is_segmentation": true, | ||
"data_list_key": "test", | ||
"base_dir": "@dataset_dir" | ||
} | ||
}, | ||
"dataset": { | ||
"name": "Dataset", | ||
"args": { | ||
"data": "@datalist", | ||
"transform": "@preprocessing" | ||
} | ||
}, | ||
"dataloader": { | ||
"name": "DataLoader", | ||
"args": { | ||
"dataset": "@dataset", | ||
"batch_size": 1, | ||
"shuffle": false, | ||
"num_workers": 4 | ||
} | ||
}, | ||
"inferer": { | ||
"name": "SlidingWindowInferer", | ||
"args": { | ||
"roi_size": [160, 160, 160], | ||
"sw_batch_size": 4, | ||
"overlap": 0.5 | ||
} | ||
}, | ||
"postprocessing": [ | ||
{ | ||
"name": "Activationsd", | ||
"args": { | ||
"keys": "pred", | ||
"softmax": true | ||
} | ||
}, | ||
{ | ||
"name": "Invertd", | ||
"args": { | ||
"keys": "pred", | ||
"transform": "@preprocessing", | ||
"orig_keys": "image", | ||
"meta_keys": "pred_meta_dict", | ||
"nearest_interp": false, | ||
"to_tensor": true, | ||
"device": "cuda" | ||
} | ||
}, | ||
{ | ||
"name": "AsDiscreted", | ||
"args": { | ||
"keys": "pred", | ||
"argmax": true | ||
} | ||
}, | ||
{ | ||
"name": "SaveImaged", | ||
"args": { | ||
"keys": "pred", | ||
"meta_keys": "pred_meta_dict", | ||
"output_dir": "{MMAR_EVAL_OUTPUT_PATH}", | ||
"resample": false, | ||
"squeeze_end_dims": true | ||
} | ||
} | ||
] | ||
} |
30 changes: 30 additions & 0 deletions
30
modules/model_package/spleen_segmentation/configs/metadata.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,30 @@ | ||
{ | ||
"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", | ||
"authorship": "MONAI team", | ||
"data_source": "Task09_Spleen.tar from http://medicaldecathlon.com/", | ||
"dataset_dir": "/workspace/data/Task09_Spleen", | ||
"image_classes": "single channel data, intensity scaled to [0, 1]", | ||
Nic-Ma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"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", | ||
"image_shape": [1, 160, 160, 160], | ||
"label_shape": [1, 160, 160, 160], | ||
"pred_shape": [2, 160, 160, 160], | ||
"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" | ||
] | ||
} |
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,31 @@ | ||
# Description | ||
A pre-trained model for volumetric (3D) segmentation of the spleen from CT image. | ||
|
||
# Model Overview | ||
This model is trained using the runnerup [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 command train.sh, which required 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 achieve the following Dice score on the validation data (our own split from the training dataset): | ||
|
||
Mean dice = 0.96 | ||
|
||
# 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/ |
Empty file.
Empty file.
11 changes: 11 additions & 0 deletions
11
modules/model_package/spleen_segmentation/programs/__init__.py
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,11 @@ | ||
|
||
Nic-Ma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Copyright 2020 - 2021 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. |
59 changes: 59 additions & 0 deletions
59
modules/model_package/spleen_segmentation/programs/export.py
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,59 @@ | ||
|
||
# Copyright 2020 - 2021 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. | ||
|
||
import argparse | ||
import json | ||
|
||
import torch | ||
from ignite.handlers import Checkpoint | ||
from monai.data import save_net_with_metadata | ||
from monai.networks import convert_to_torchscript | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--weights', '-w', type=str, help='file path of the trained model weights', required=True) | ||
parser.add_argument('--config', '-c', type=str, help='file path of config file that defines network', required=True) | ||
parser.add_argument('--meta', '-e', type=str, help='file path of the meta data') | ||
args = parser.parse_args() | ||
|
||
# load config file | ||
with open(args.config, "r") as f: | ||
cofnig_dict = json.load(f) | ||
# load meta data | ||
with open(args.meta, "r") as f: | ||
meta_dict = json.load(f) | ||
|
||
net: torch.nn.Module = None | ||
# TODO: parse network definiftion from config file and construct network instance | ||
# config_parser = ConfigParser(config_dict, meta_dict) | ||
# net = config_parser.get_component("network") | ||
|
||
checkpoint = torch.load(args.weights) | ||
# here we use ignite Checkpoint to support nested weights and be compatible with MONAI CheckpointSaver | ||
Checkpoint.load_objects(to_load={"model": net}, checkpoint=checkpoint) | ||
|
||
# convert to TorchScript model and save with meta data | ||
net = convert_to_torchscript(model=net) | ||
|
||
save_net_with_metadata( | ||
jit_obj=net, | ||
filename_prefix_or_stream="model.ts", | ||
include_config_vals=False, | ||
append_timestamp=False, | ||
meta_values=meta_dict, | ||
more_extra_files={args.config: json.dumps(cofnig_dict).encode()}, | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
60 changes: 60 additions & 0 deletions
60
modules/model_package/spleen_segmentation/programs/inference.py
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,60 @@ | ||
|
||
# Copyright 2020 - 2021 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. | ||
|
||
import argparse | ||
import json | ||
|
||
import torch | ||
from monai.data import decollate_batch | ||
from monai.inferers import Inferer | ||
from monai.transforms import Transform | ||
from monai.utils.enums import CommonKeys | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--config', '-c', type=str, help='file path of config file that defines network', required=True) | ||
parser.add_argument('--meta', '-e', type=str, help='file path of the meta data') | ||
args = parser.parse_args() | ||
|
||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
|
||
# load config file | ||
with open(args.config, "r") as f: | ||
cofnig_dict = json.load(f) | ||
# load meta data | ||
with open(args.meta, "r") as f: | ||
meta_dict = json.load(f) | ||
|
||
net: torch.nn.Module = None | ||
dataloader: torch.utils.data.DataLoader = None | ||
inferer: Inferer = None | ||
postprocessing: Transform = None | ||
# TODO: parse inference config file and construct instances | ||
# config_parser = ConfigParser(config_dict, meta_dict) | ||
# net = config_parser.get_component("model").to(device) | ||
# dataloader = config_parser.get_component("dataloader") | ||
# inferer = config_parser.get_component("inferer") | ||
# postprocessing = config_parser.get_component("postprocessing") | ||
|
||
net.eval() | ||
with torch.no_grad(): | ||
for d in dataloader: | ||
images = d[CommonKeys.IMAGE].to(device) | ||
# define sliding window size and batch size for windows inference | ||
d[CommonKeys.PRED] = inferer(inputs=images, predictor=net) | ||
# decollate the batch data into a list of dictionaries, then execute postprocessing transforms | ||
d = [postprocessing(i) for i in decollate_batch(d)] | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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.