Skip to content
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

[WIP] 486 Add example model package #487

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/model_package/spleen_segmentation/commands/export.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python -m monai.apps.mmar.export
--weights ../models/model.pt
--config ../configs/inference.json
--meta ../configs/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
python -m monai.apps.mmar.inference.py
--config ../configs/inference.json
--meta ../configs/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
python -m monai.apps.mmar.verify_json.py
--schema_id metadata
--config ../configs/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
python -m monai.apps.mmar.verify_network.py
--config ../configs/inference.json
--meta ../configs/metadata.json
162 changes: 162 additions & 0 deletions modules/model_package/spleen_segmentation/configs/inference.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"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": "@network_data_format#inputs#image#num_channels",
"out_channels": "@network_data_format#outputs#pred#num_channels",
"channels": [16, 32, 64, 128, 256],
"strides": [2, 2, 2, 2],
"num_res_units": 2,
"norm": "batch"
}
},
"preprocessing": [
Nic-Ma marked this conversation as resolved.
Show resolved Hide resolved
{
"<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": "@network_data_format#inputs#image#spatial_shape",
"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
}
}
],
"handlers": [
{
"<name>": "StatsHandler",
"<args>": {
"output_transform": "lambda x: None"
}
}
],
"evaluator": {
"<name>": "SupervisedEvaluator",
"<args>": {
"device": "cuda",
"val_data_loader": "@dataloader",
"network": "@model",
"inferer": "@inferer",
"postprocessing": "@postprocessing",
"val_handlers": "@handlers",
"amp": "@amp"
}
}
}
60 changes: 60 additions & 0 deletions modules/model_package/spleen_segmentation/configs/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"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",
"copyright": "Copyright (c) MONAI Consortium",
"data_source": "Task09_Spleen.tar from http://medicaldecathlon.com/",
"data_type": "dicom",
"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",
"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]
},
"label": {
"type": "label",
"format": "segmentation",
"num_channels": 1,
"spatial_shape": [160, 160, 160],
"dtype": "float32",
"value_range": [0, 1]
}
},
"outputs":{
"pred": {
"type": "image",
"format": "segmentation",
"num_channels": 2,
"spatial_shape": [160, 160, 160],
"dtype": "float32",
"value_range": [0, 1]
}
},
"patch_inference": false
}
}
31 changes: 31 additions & 0 deletions modules/model_package/spleen_segmentation/docs/README.md
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
6 changes: 6 additions & 0 deletions modules/model_package/spleen_segmentation/docs/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Third Party Licenses
-----------------------------------------------------------------------

/*********************************************************************/
i. Medical Segmentation Decathlon
http://medicaldecathlon.com/
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python -m monai.apps.mmar.inference.py
--config ../../spleen_segmentation/configs/inference.json
--meta ../configs/metadata.json
--override ../configs/inference.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"preprocessing#1#<args>#pixdim": [1.0, 1.0, 1.0],
"inferer": {
"<path>": "custom_inferer.TensorRTInferer",
"<args>": {
"roi_size": [200, 200, 200],
"sw_batch_size": 4,
"overlap": 0.6
}
}
}
11 changes: 11 additions & 0 deletions modules/model_package/spleen_segmentation_a100/scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# 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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

# 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.

from monai.inferers import SlidingWindowInferer


class TensorRTInferer(SlidingWindowInferer):
def __init__(self, **kwargs) -> None:
print("fake code to show the customize logic.")
super().__init__(**kwargs)