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 13 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 ../scripts/export.py
--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 ../scripts/inference.py
--config ../configs/inference.json
--meta ../configs/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python ../scripts/inference.py
--base_config ../configs/inference.json
--config ../configs/inference_v2.json
--meta ../configs/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
python ../scripts/inference.py
--config ../configs/trtinfer.json
--meta ../configs/metadata.json
142 changes: 142 additions & 0 deletions modules/model_package/spleen_segmentation/configs/inference.json
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": [
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": [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
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"preprocessing": {
"name": "Spacingd",
"args": {
"keys": "image",
"pixdim": [1.0, 1.0, 1.0],
"mode": "bilinear"
}
},
"inferer": {
"name": "SlidingWindowInferer",
"args": {
"roi_size": [96, 96, 96],
"sw_batch_size": 4,
"overlap": 0.6
}
}
}
30 changes: 30 additions & 0 deletions modules/model_package/spleen_segmentation/configs/metadata.json
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"
]
}
19 changes: 19 additions & 0 deletions modules/model_package/spleen_segmentation/configs/trtinfer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"preprocessing": {
"ref": {
"path": "../inference.json/preprocessing"
}
},
"dataset": {
"ref": {
"path": "../inference.json/dataset"
}
},
"model": "#load_trt_model(...)",
"dataloader": {
"name": "DALIpipeline"
},
"inferer": {
"name": "TensorRTInferer"
}
}
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.
11 changes: 11 additions & 0 deletions modules/model_package/spleen_segmentation/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.
60 changes: 60 additions & 0 deletions modules/model_package/spleen_segmentation/scripts/export.py
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.apps import ConfigParser
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:
config_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)
net = config_parser.get_instance("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(config_dict).encode()},
)


if __name__ == '__main__':
main()
Loading