katielink commited on
Commit
76338b6
1 Parent(s): 86522f2

update dataset processing

Browse files
README.md ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - monai
4
+ - medical
5
+ library_name: monai
6
+ license: apache-2.0
7
+ ---
8
+ # Description
9
+ A pre-trained model for the endoscopic tool segmentation task.
10
+
11
+ # Model Overview
12
+ This model is trained using a flexible unet structure with an efficient-b0 [1] as the backbone and a UNet architecture [2] as the decoder. Datasets use private samples from [Activ Surgical](https://www.activsurgical.com/).
13
+ The [pytorch model](https://drive.google.com/file/d/14r6WmzaZrgaWLGu0O9vSAzdeIGVFQ3cs/view?usp=sharing) and [torchscript model](https://drive.google.com/file/d/1i-e5xXHtmvmqitwUP8Q3JqvnmN3mlrEm/view?usp=sharing) are shared in google drive. Details can be found in large_files.yml file. Modify the "bundle_root" parameter specified in configs/train.json and configs/inference.json to reflect where models are downloaded. Expected directory path to place downloaded models is "models/" under "bundle_root".
14
+
15
+ ## Data
16
+ Datasets used in this work were provided by [Activ Surgical](https://www.activsurgical.com/).
17
+
18
+ Since datasets are private, existing public datasets like [EndoVis 2017](https://endovissub2017-roboticinstrumentsegmentation.grand-challenge.org/Data/) can be used to train a similar model.
19
+
20
+ When using EndoVis or any other dataset, it should be divided into "train", "valid" and "test" folders. Samples in each folder would better be images and converted to jpg format. Otherwise, "images", "labels", "val_images" and "val_labels" parameters in "configs/train.json" and "datalist" in "configs/inference.json" should be modified to fit given dataset. After that, "dataset_dir" parameter in "configs/train.json" and "configs/inference.json" should be changed to root folder which contains previous "train", "valid" and "test" folders.
21
+
22
+ Please notice that loading data operation in this bundle is adaptive. If images and labels are not in the same format, it may lead to a mismatching problem. For example, if images are in jpg format and labels are in npy format, PIL and Numpy readers will be used separately to load images and labels. Since these two readers have their own way to parse file's shape, loaded labels will be transpose of the correct ones and incur a missmatching problem.
23
+
24
+ ## Training configuration
25
+ The training was performed with an at least 12GB-memory GPU.
26
+
27
+ Actual Model Input: 736 x 480 x 3
28
+
29
+ ## Input and output formats
30
+ Input: 3 channel video frames
31
+
32
+ Output: 2 channels: Label 1: tools; Label 0: everything else
33
+
34
+ ## Scores
35
+ This model achieves the following IoU score on the test dataset (our own split from the first batch data):
36
+
37
+ Mean IoU = 0.87
38
+
39
+ ## commands example
40
+ Execute training:
41
+
42
+ ```
43
+ python -m monai.bundle run training --meta_file configs/metadata.json --config_file configs/train.json --logging_file configs/logging.conf
44
+ ```
45
+
46
+ Override the `train` config to execute evaluation with the trained model:
47
+
48
+ ```
49
+ python -m monai.bundle run evaluating --meta_file configs/metadata.json --config_file "['configs/train.json','configs/evaluate.json']" --logging_file configs/logging.conf
50
+ ```
51
+
52
+ Execute inference:
53
+
54
+ ```
55
+ python -m monai.bundle run evaluating --meta_file configs/metadata.json --config_file configs/inference.json --logging_file configs/logging.conf
56
+ ```
57
+
58
+ Export checkpoint to TorchScript file:
59
+
60
+ ```
61
+ python -m monai.bundle ckpt_export network_def --filepath models/model.ts --ckpt_file models/model.pt --meta_file configs/metadata.json --config_file configs/inference.json
62
+ ```
63
+
64
+ Export checkpoint to onnx file, which has been tested on pytorch 1.12.0:
65
+
66
+ ```
67
+ python scripts/export_to_onnx.py --model models/model.pt --outpath models/model.onnx
68
+ ```
69
+
70
+ Export TorchScript file to a torchscript module targeting a TensorRT engine with float16 precision.
71
+
72
+ ```
73
+ torchtrtc -p f16 models/model.ts models/model_trt.ts "[(1,3,736,480);(4,3,736,480);(8,3,736,480)]"
74
+ ```
75
+ The last parameter is the dynamic input shape in which each parameter means "[(MIN_BATCH, MIN_CHANNEL, MIN_WIDTH, MIN_HEIGHT), (OPT_BATCH, .., ..., OPT_HEIGHT), (MAX_BATCH, .., ..., MAX_HEIGHT)]". Please notice if using docker, the TensorRT CUDA must match the environment CUDA and the Torch-TensorRT c++&python version must be installed. For more examples on how to use the Torch-TensorRT, you can go to this [link](https://pytorch.org/TensorRT/). The [github source code link](https://github.com/pytorch/TensorRT) here shows the detail about how to install it on your own environment.
76
+
77
+ Export TensorRT float16 model from the onnx model:
78
+
79
+ ```
80
+ polygraphy surgeon sanitize --fold-constants models/model.onnx -o models/new_model.onnx
81
+ ```
82
+
83
+ ```
84
+ trtexec --onnx=models/new_model.onnx --saveEngine=models/model.trt --fp16 --minShapes=INPUT__0:1x3x736x480 --optShapes=INPUT__0:4x3x736x480 --maxShapes=INPUT__0:8x3x736x480 --shapes=INPUT__0:4x3x736x480
85
+ ```
86
+ This command need TensorRT with correct CUDA installed in the environment. For the detail of installing TensorRT, please refer to [this link](https://docs.nvidia.com/deeplearning/tensorrt/install-guide/index.html). In addition, there are padding operations in this FlexibleUNet structure that not support by TensorRT. Therefore, when tried to convert the onnx model to a TensorRT engine, an extra polygraphy command is needed to execute.
87
+
88
+
89
+ # References
90
+ [1] Tan, M. and Le, Q. V. Efficientnet: Rethinking model scaling for convolutional neural networks. ICML, 2019a. https://arxiv.org/pdf/1905.11946.pdf
91
+
92
+ [2] O. Ronneberger, P. Fischer, and T. Brox. U-net: Convolutional networks for biomedical image segmentation. In International Conference on Medical image computing and computer-assisted intervention, pages 234–241. Springer, 2015. https://arxiv.org/pdf/1505.04597.pdf
93
+
94
+ # License
95
+ Copyright (c) MONAI Consortium
96
+
97
+ Licensed under the Apache License, Version 2.0 (the "License");
98
+ you may not use this file except in compliance with the License.
99
+ You may obtain a copy of the License at
100
+
101
+ http://www.apache.org/licenses/LICENSE-2.0
102
+
103
+ Unless required by applicable law or agreed to in writing, software
104
+ distributed under the License is distributed on an "AS IS" BASIS,
105
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
106
+ See the License for the specific language governing permissions and
107
+ limitations under the License.
configs/inference.json CHANGED
@@ -6,7 +6,7 @@
6
  "bundle_root": "/workspace/bundle/endoscopic_tool_segmentation",
7
  "output_dir": "$@bundle_root + '/eval'",
8
  "dataset_dir": "/workspace/data/endoscopic_tool_dataset",
9
- "datalist": "$list(sorted(glob.glob(os.path.join(@dataset_dir,'test', '*[!seg].jpg'))))",
10
  "device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
11
  "network_def": {
12
  "_target_": "FlexibleUNet",
 
6
  "bundle_root": "/workspace/bundle/endoscopic_tool_segmentation",
7
  "output_dir": "$@bundle_root + '/eval'",
8
  "dataset_dir": "/workspace/data/endoscopic_tool_dataset",
9
+ "datalist": "$list(sorted(glob.glob(os.path.join(@dataset_dir,'test', '*', '*[!seg].jpg'))))",
10
  "device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
11
  "network_def": {
12
  "_target_": "FlexibleUNet",
configs/metadata.json CHANGED
@@ -1,14 +1,15 @@
1
  {
2
  "schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20220324.json",
3
- "version": "0.2.1",
4
  "changelog": {
 
5
  "0.2.1": "update to use monai 1.0.1",
6
  "0.2.0": "update license files",
7
  "0.1.0": "complete the first version model package",
8
  "0.0.1": "initialize the model package structure"
9
  },
10
- "monai_version": "1.0.0",
11
- "pytorch_version": "1.12.0",
12
  "numpy_version": "1.22.4",
13
  "optional_packages_version": {
14
  "nibabel": "4.0.1",
 
1
  {
2
  "schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20220324.json",
3
+ "version": "0.3.0",
4
  "changelog": {
5
+ "0.3.0": "update dataset processing",
6
  "0.2.1": "update to use monai 1.0.1",
7
  "0.2.0": "update license files",
8
  "0.1.0": "complete the first version model package",
9
  "0.0.1": "initialize the model package structure"
10
  },
11
+ "monai_version": "1.0.1",
12
+ "pytorch_version": "1.13.0",
13
  "numpy_version": "1.22.4",
14
  "optional_packages_version": {
15
  "nibabel": "4.0.1",
configs/train.json CHANGED
@@ -9,9 +9,9 @@
9
  "ckpt_dir": "$@bundle_root + '/models'",
10
  "output_dir": "$@bundle_root + '/eval'",
11
  "dataset_dir": "/workspace/data/endoscopic_tool_dataset",
12
- "images": "$list(sorted(glob.glob(os.path.join(@dataset_dir,'train', '*[!seg].jpg'))))",
13
  "labels": "$[x.replace('.jpg', '_seg.jpg') for x in @images]",
14
- "val_images": "$list(sorted(glob.glob(os.path.join(@dataset_dir,'valid', '*[!seg].jpg'))))",
15
  "val_labels": "$[x.replace('.jpg', '_seg.jpg') for x in @val_images]",
16
  "val_interval": 1,
17
  "device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
 
9
  "ckpt_dir": "$@bundle_root + '/models'",
10
  "output_dir": "$@bundle_root + '/eval'",
11
  "dataset_dir": "/workspace/data/endoscopic_tool_dataset",
12
+ "images": "$list(sorted(glob.glob(os.path.join(@dataset_dir,'train', '*', '*[!seg].jpg'))))",
13
  "labels": "$[x.replace('.jpg', '_seg.jpg') for x in @images]",
14
+ "val_images": "$list(sorted(glob.glob(os.path.join(@dataset_dir,'valid', '*' '*[!seg].jpg'))))",
15
  "val_labels": "$[x.replace('.jpg', '_seg.jpg') for x in @val_images]",
16
  "val_interval": 1,
17
  "device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
docs/README.md ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Description
2
+ A pre-trained model for the endoscopic tool segmentation task.
3
+
4
+ # Model Overview
5
+ This model is trained using a flexible unet structure with an efficient-b0 [1] as the backbone and a UNet architecture [2] as the decoder. Datasets use private samples from [Activ Surgical](https://www.activsurgical.com/).
6
+ The [pytorch model](https://drive.google.com/file/d/14r6WmzaZrgaWLGu0O9vSAzdeIGVFQ3cs/view?usp=sharing) and [torchscript model](https://drive.google.com/file/d/1i-e5xXHtmvmqitwUP8Q3JqvnmN3mlrEm/view?usp=sharing) are shared in google drive. Details can be found in large_files.yml file. Modify the "bundle_root" parameter specified in configs/train.json and configs/inference.json to reflect where models are downloaded. Expected directory path to place downloaded models is "models/" under "bundle_root".
7
+
8
+ ## Data
9
+ Datasets used in this work were provided by [Activ Surgical](https://www.activsurgical.com/).
10
+
11
+ Since datasets are private, existing public datasets like [EndoVis 2017](https://endovissub2017-roboticinstrumentsegmentation.grand-challenge.org/Data/) can be used to train a similar model.
12
+
13
+ When using EndoVis or any other dataset, it should be divided into "train", "valid" and "test" folders. Samples in each folder would better be images and converted to jpg format. Otherwise, "images", "labels", "val_images" and "val_labels" parameters in "configs/train.json" and "datalist" in "configs/inference.json" should be modified to fit given dataset. After that, "dataset_dir" parameter in "configs/train.json" and "configs/inference.json" should be changed to root folder which contains previous "train", "valid" and "test" folders.
14
+
15
+ Please notice that loading data operation in this bundle is adaptive. If images and labels are not in the same format, it may lead to a mismatching problem. For example, if images are in jpg format and labels are in npy format, PIL and Numpy readers will be used separately to load images and labels. Since these two readers have their own way to parse file's shape, loaded labels will be transpose of the correct ones and incur a missmatching problem.
16
+
17
+ ## Training configuration
18
+ The training was performed with an at least 12GB-memory GPU.
19
+
20
+ Actual Model Input: 736 x 480 x 3
21
+
22
+ ## Input and output formats
23
+ Input: 3 channel video frames
24
+
25
+ Output: 2 channels: Label 1: tools; Label 0: everything else
26
+
27
+ ## Scores
28
+ This model achieves the following IoU score on the test dataset (our own split from the first batch data):
29
+
30
+ Mean IoU = 0.87
31
+
32
+ ## commands example
33
+ Execute training:
34
+
35
+ ```
36
+ python -m monai.bundle run training --meta_file configs/metadata.json --config_file configs/train.json --logging_file configs/logging.conf
37
+ ```
38
+
39
+ Override the `train` config to execute evaluation with the trained model:
40
+
41
+ ```
42
+ python -m monai.bundle run evaluating --meta_file configs/metadata.json --config_file "['configs/train.json','configs/evaluate.json']" --logging_file configs/logging.conf
43
+ ```
44
+
45
+ Execute inference:
46
+
47
+ ```
48
+ python -m monai.bundle run evaluating --meta_file configs/metadata.json --config_file configs/inference.json --logging_file configs/logging.conf
49
+ ```
50
+
51
+ Export checkpoint to TorchScript file:
52
+
53
+ ```
54
+ python -m monai.bundle ckpt_export network_def --filepath models/model.ts --ckpt_file models/model.pt --meta_file configs/metadata.json --config_file configs/inference.json
55
+ ```
56
+
57
+ Export checkpoint to onnx file, which has been tested on pytorch 1.12.0:
58
+
59
+ ```
60
+ python scripts/export_to_onnx.py --model models/model.pt --outpath models/model.onnx
61
+ ```
62
+
63
+ Export TorchScript file to a torchscript module targeting a TensorRT engine with float16 precision.
64
+
65
+ ```
66
+ torchtrtc -p f16 models/model.ts models/model_trt.ts "[(1,3,736,480);(4,3,736,480);(8,3,736,480)]"
67
+ ```
68
+ The last parameter is the dynamic input shape in which each parameter means "[(MIN_BATCH, MIN_CHANNEL, MIN_WIDTH, MIN_HEIGHT), (OPT_BATCH, .., ..., OPT_HEIGHT), (MAX_BATCH, .., ..., MAX_HEIGHT)]". Please notice if using docker, the TensorRT CUDA must match the environment CUDA and the Torch-TensorRT c++&python version must be installed. For more examples on how to use the Torch-TensorRT, you can go to this [link](https://pytorch.org/TensorRT/). The [github source code link](https://github.com/pytorch/TensorRT) here shows the detail about how to install it on your own environment.
69
+
70
+ Export TensorRT float16 model from the onnx model:
71
+
72
+ ```
73
+ polygraphy surgeon sanitize --fold-constants models/model.onnx -o models/new_model.onnx
74
+ ```
75
+
76
+ ```
77
+ trtexec --onnx=models/new_model.onnx --saveEngine=models/model.trt --fp16 --minShapes=INPUT__0:1x3x736x480 --optShapes=INPUT__0:4x3x736x480 --maxShapes=INPUT__0:8x3x736x480 --shapes=INPUT__0:4x3x736x480
78
+ ```
79
+ This command need TensorRT with correct CUDA installed in the environment. For the detail of installing TensorRT, please refer to [this link](https://docs.nvidia.com/deeplearning/tensorrt/install-guide/index.html). In addition, there are padding operations in this FlexibleUNet structure that not support by TensorRT. Therefore, when tried to convert the onnx model to a TensorRT engine, an extra polygraphy command is needed to execute.
80
+
81
+
82
+ # References
83
+ [1] Tan, M. and Le, Q. V. Efficientnet: Rethinking model scaling for convolutional neural networks. ICML, 2019a. https://arxiv.org/pdf/1905.11946.pdf
84
+
85
+ [2] O. Ronneberger, P. Fischer, and T. Brox. U-net: Convolutional networks for biomedical image segmentation. In International Conference on Medical image computing and computer-assisted intervention, pages 234–241. Springer, 2015. https://arxiv.org/pdf/1505.04597.pdf
86
+
87
+ # License
88
+ Copyright (c) MONAI Consortium
89
+
90
+ Licensed under the Apache License, Version 2.0 (the "License");
91
+ you may not use this file except in compliance with the License.
92
+ You may obtain a copy of the License at
93
+
94
+ http://www.apache.org/licenses/LICENSE-2.0
95
+
96
+ Unless required by applicable law or agreed to in writing, software
97
+ distributed under the License is distributed on an "AS IS" BASIS,
98
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
99
+ See the License for the specific language governing permissions and
100
+ limitations under the License.