update license files
Browse files- .gitattributes +1 -0
- LICENSE +201 -0
- configs/evaluate.json +48 -0
- configs/inference.json +113 -0
- configs/logging.conf +21 -0
- configs/metadata.json +77 -0
- configs/multi_gpu_train.json +36 -0
- configs/train.json +259 -0
- docs/data_license.txt +1 -0
- docs/readme.md +129 -0
- models/model.pt +3 -0
- models/model.ts +3 -0
- scripts/export_to_onnx.py +118 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
models/model.ts filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Apache License
|
2 |
+
Version 2.0, January 2004
|
3 |
+
http://www.apache.org/licenses/
|
4 |
+
|
5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6 |
+
|
7 |
+
1. Definitions.
|
8 |
+
|
9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
11 |
+
|
12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13 |
+
the copyright owner that is granting the License.
|
14 |
+
|
15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
16 |
+
other entities that control, are controlled by, or are under common
|
17 |
+
control with that entity. For the purposes of this definition,
|
18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
19 |
+
direction or management of such entity, whether by contract or
|
20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22 |
+
|
23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24 |
+
exercising permissions granted by this License.
|
25 |
+
|
26 |
+
"Source" form shall mean the preferred form for making modifications,
|
27 |
+
including but not limited to software source code, documentation
|
28 |
+
source, and configuration files.
|
29 |
+
|
30 |
+
"Object" form shall mean any form resulting from mechanical
|
31 |
+
transformation or translation of a Source form, including but
|
32 |
+
not limited to compiled object code, generated documentation,
|
33 |
+
and conversions to other media types.
|
34 |
+
|
35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
36 |
+
Object form, made available under the License, as indicated by a
|
37 |
+
copyright notice that is included in or attached to the work
|
38 |
+
(an example is provided in the Appendix below).
|
39 |
+
|
40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41 |
+
form, that is based on (or derived from) the Work and for which the
|
42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
44 |
+
of this License, Derivative Works shall not include works that remain
|
45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46 |
+
the Work and Derivative Works thereof.
|
47 |
+
|
48 |
+
"Contribution" shall mean any work of authorship, including
|
49 |
+
the original version of the Work and any modifications or additions
|
50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
54 |
+
means any form of electronic, verbal, or written communication sent
|
55 |
+
to the Licensor or its representatives, including but not limited to
|
56 |
+
communication on electronic mailing lists, source code control systems,
|
57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
59 |
+
excluding communication that is conspicuously marked or otherwise
|
60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
61 |
+
|
62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
64 |
+
subsequently incorporated within the Work.
|
65 |
+
|
66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
71 |
+
Work and such Derivative Works in Source or Object form.
|
72 |
+
|
73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76 |
+
(except as stated in this section) patent license to make, have made,
|
77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78 |
+
where such license applies only to those patent claims licensable
|
79 |
+
by such Contributor that are necessarily infringed by their
|
80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
82 |
+
institute patent litigation against any entity (including a
|
83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84 |
+
or a Contribution incorporated within the Work constitutes direct
|
85 |
+
or contributory patent infringement, then any patent licenses
|
86 |
+
granted to You under this License for that Work shall terminate
|
87 |
+
as of the date such litigation is filed.
|
88 |
+
|
89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
90 |
+
Work or Derivative Works thereof in any medium, with or without
|
91 |
+
modifications, and in Source or Object form, provided that You
|
92 |
+
meet the following conditions:
|
93 |
+
|
94 |
+
(a) You must give any other recipients of the Work or
|
95 |
+
Derivative Works a copy of this License; and
|
96 |
+
|
97 |
+
(b) You must cause any modified files to carry prominent notices
|
98 |
+
stating that You changed the files; and
|
99 |
+
|
100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
101 |
+
that You distribute, all copyright, patent, trademark, and
|
102 |
+
attribution notices from the Source form of the Work,
|
103 |
+
excluding those notices that do not pertain to any part of
|
104 |
+
the Derivative Works; and
|
105 |
+
|
106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107 |
+
distribution, then any Derivative Works that You distribute must
|
108 |
+
include a readable copy of the attribution notices contained
|
109 |
+
within such NOTICE file, excluding those notices that do not
|
110 |
+
pertain to any part of the Derivative Works, in at least one
|
111 |
+
of the following places: within a NOTICE text file distributed
|
112 |
+
as part of the Derivative Works; within the Source form or
|
113 |
+
documentation, if provided along with the Derivative Works; or,
|
114 |
+
within a display generated by the Derivative Works, if and
|
115 |
+
wherever such third-party notices normally appear. The contents
|
116 |
+
of the NOTICE file are for informational purposes only and
|
117 |
+
do not modify the License. You may add Your own attribution
|
118 |
+
notices within Derivative Works that You distribute, alongside
|
119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
120 |
+
that such additional attribution notices cannot be construed
|
121 |
+
as modifying the License.
|
122 |
+
|
123 |
+
You may add Your own copyright statement to Your modifications and
|
124 |
+
may provide additional or different license terms and conditions
|
125 |
+
for use, reproduction, or distribution of Your modifications, or
|
126 |
+
for any such Derivative Works as a whole, provided Your use,
|
127 |
+
reproduction, and distribution of the Work otherwise complies with
|
128 |
+
the conditions stated in this License.
|
129 |
+
|
130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
132 |
+
by You to the Licensor shall be under the terms and conditions of
|
133 |
+
this License, without any additional terms or conditions.
|
134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135 |
+
the terms of any separate license agreement you may have executed
|
136 |
+
with Licensor regarding such Contributions.
|
137 |
+
|
138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
140 |
+
except as required for reasonable and customary use in describing the
|
141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
142 |
+
|
143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144 |
+
agreed to in writing, Licensor provides the Work (and each
|
145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147 |
+
implied, including, without limitation, any warranties or conditions
|
148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150 |
+
appropriateness of using or redistributing the Work and assume any
|
151 |
+
risks associated with Your exercise of permissions under this License.
|
152 |
+
|
153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
154 |
+
whether in tort (including negligence), contract, or otherwise,
|
155 |
+
unless required by applicable law (such as deliberate and grossly
|
156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157 |
+
liable to You for damages, including any direct, indirect, special,
|
158 |
+
incidental, or consequential damages of any character arising as a
|
159 |
+
result of this License or out of the use or inability to use the
|
160 |
+
Work (including but not limited to damages for loss of goodwill,
|
161 |
+
work stoppage, computer failure or malfunction, or any and all
|
162 |
+
other commercial damages or losses), even if such Contributor
|
163 |
+
has been advised of the possibility of such damages.
|
164 |
+
|
165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168 |
+
or other liability obligations and/or rights consistent with this
|
169 |
+
License. However, in accepting such obligations, You may act only
|
170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171 |
+
of any other Contributor, and only if You agree to indemnify,
|
172 |
+
defend, and hold each Contributor harmless for any liability
|
173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
174 |
+
of your accepting any such warranty or additional liability.
|
175 |
+
|
176 |
+
END OF TERMS AND CONDITIONS
|
177 |
+
|
178 |
+
APPENDIX: How to apply the Apache License to your work.
|
179 |
+
|
180 |
+
To apply the Apache License to your work, attach the following
|
181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182 |
+
replaced with your own identifying information. (Don't include
|
183 |
+
the brackets!) The text should be enclosed in the appropriate
|
184 |
+
comment syntax for the file format. We also recommend that a
|
185 |
+
file or class name and description of purpose be included on the
|
186 |
+
same "printed page" as the copyright notice for easier
|
187 |
+
identification within third-party archives.
|
188 |
+
|
189 |
+
Copyright [yyyy] [name of copyright owner]
|
190 |
+
|
191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192 |
+
you may not use this file except in compliance with the License.
|
193 |
+
You may obtain a copy of the License at
|
194 |
+
|
195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
196 |
+
|
197 |
+
Unless required by applicable law or agreed to in writing, software
|
198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200 |
+
See the License for the specific language governing permissions and
|
201 |
+
limitations under the License.
|
configs/evaluate.json
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"validate#postprocessing": {
|
3 |
+
"_target_": "Compose",
|
4 |
+
"transforms": [
|
5 |
+
{
|
6 |
+
"_target_": "AsDiscreted",
|
7 |
+
"keys": [
|
8 |
+
"pred",
|
9 |
+
"label"
|
10 |
+
],
|
11 |
+
"argmax": [
|
12 |
+
true,
|
13 |
+
false
|
14 |
+
],
|
15 |
+
"to_onehot": 2
|
16 |
+
}
|
17 |
+
]
|
18 |
+
},
|
19 |
+
"validate#handlers": [
|
20 |
+
{
|
21 |
+
"_target_": "CheckpointLoader",
|
22 |
+
"load_path": "$@ckpt_dir + '/model.pt'",
|
23 |
+
"load_dict": {
|
24 |
+
"model": "@network"
|
25 |
+
}
|
26 |
+
},
|
27 |
+
{
|
28 |
+
"_target_": "StatsHandler",
|
29 |
+
"iteration_log": false
|
30 |
+
},
|
31 |
+
{
|
32 |
+
"_target_": "MetricsSaver",
|
33 |
+
"save_dir": "@output_dir",
|
34 |
+
"metrics": [
|
35 |
+
"val_accu"
|
36 |
+
],
|
37 |
+
"metric_details": [
|
38 |
+
"val_accu"
|
39 |
+
],
|
40 |
+
"batch_transform": "$monai.handlers.from_engine(['image_meta_dict'])",
|
41 |
+
"summary_ops": "*"
|
42 |
+
}
|
43 |
+
],
|
44 |
+
"evaluating": [
|
45 |
+
"$setattr(torch.backends.cudnn, 'benchmark', True)",
|
46 |
+
"$@validate#evaluator.run()"
|
47 |
+
]
|
48 |
+
}
|
configs/inference.json
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"imports": [
|
3 |
+
"$import json",
|
4 |
+
"$import os",
|
5 |
+
"$import torch"
|
6 |
+
],
|
7 |
+
"bundle_root": "/workspace/bundle/endoscopic_inbody_classification",
|
8 |
+
"output_dir": "$@bundle_root + '/eval'",
|
9 |
+
"dataset_dir": "/workspace/data/endoscopic_inbody_classification",
|
10 |
+
"test_json": "$@dataset_dir+'/test.json'",
|
11 |
+
"test_fp": "$open(@test_json,'r', encoding='utf8')",
|
12 |
+
"test_dict": "$json.load(@test_fp)",
|
13 |
+
"test_close": "$@test_fp.close()",
|
14 |
+
"device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
|
15 |
+
"network_def": {
|
16 |
+
"_target_": "SEResNet50",
|
17 |
+
"spatial_dims": 2,
|
18 |
+
"in_channels": 3,
|
19 |
+
"num_classes": 2
|
20 |
+
},
|
21 |
+
"network": "$@network_def.to(@device)",
|
22 |
+
"preprocessing": {
|
23 |
+
"_target_": "Compose",
|
24 |
+
"transforms": [
|
25 |
+
{
|
26 |
+
"_target_": "LoadImaged",
|
27 |
+
"keys": "image"
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"_target_": "AsChannelFirstd",
|
31 |
+
"keys": "image"
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"_target_": "Resized",
|
35 |
+
"keys": "image",
|
36 |
+
"spatial_size": [
|
37 |
+
256,
|
38 |
+
256
|
39 |
+
],
|
40 |
+
"mode": "bilinear"
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"_target_": "CastToTyped",
|
44 |
+
"dtype": "$torch.float32",
|
45 |
+
"keys": "image"
|
46 |
+
},
|
47 |
+
{
|
48 |
+
"_target_": "NormalizeIntensityd",
|
49 |
+
"nonzero": true,
|
50 |
+
"channel_wise": true,
|
51 |
+
"keys": "image"
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"_target_": "EnsureTyped",
|
55 |
+
"keys": "image"
|
56 |
+
}
|
57 |
+
]
|
58 |
+
},
|
59 |
+
"dataset": {
|
60 |
+
"_target_": "Dataset",
|
61 |
+
"data": "@test_dict",
|
62 |
+
"transform": "@preprocessing"
|
63 |
+
},
|
64 |
+
"dataloader": {
|
65 |
+
"_target_": "DataLoader",
|
66 |
+
"dataset": "@dataset",
|
67 |
+
"batch_size": 1,
|
68 |
+
"shuffle": false,
|
69 |
+
"num_workers": 4
|
70 |
+
},
|
71 |
+
"inferer": {
|
72 |
+
"_target_": "SimpleInferer"
|
73 |
+
},
|
74 |
+
"postprocessing": {
|
75 |
+
"_target_": "Compose",
|
76 |
+
"transforms": [
|
77 |
+
{
|
78 |
+
"_target_": "AsDiscreted",
|
79 |
+
"argmax": true,
|
80 |
+
"to_onehot": 2,
|
81 |
+
"keys": [
|
82 |
+
"pred"
|
83 |
+
]
|
84 |
+
}
|
85 |
+
]
|
86 |
+
},
|
87 |
+
"handlers": [
|
88 |
+
{
|
89 |
+
"_target_": "CheckpointLoader",
|
90 |
+
"load_path": "$@bundle_root + '/models/model.pt'",
|
91 |
+
"load_dict": {
|
92 |
+
"model": "@network"
|
93 |
+
}
|
94 |
+
},
|
95 |
+
{
|
96 |
+
"_target_": "StatsHandler",
|
97 |
+
"iteration_log": true
|
98 |
+
}
|
99 |
+
],
|
100 |
+
"evaluator": {
|
101 |
+
"_target_": "SupervisedEvaluator",
|
102 |
+
"device": "@device",
|
103 |
+
"val_data_loader": "@dataloader",
|
104 |
+
"network": "@network",
|
105 |
+
"inferer": "@inferer",
|
106 |
+
"postprocessing": "@postprocessing",
|
107 |
+
"val_handlers": "@handlers"
|
108 |
+
},
|
109 |
+
"evaluating": [
|
110 |
+
"$setattr(torch.backends.cudnn, 'benchmark', True)",
|
111 |
+
"$@evaluator.run()"
|
112 |
+
]
|
113 |
+
}
|
configs/logging.conf
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[loggers]
|
2 |
+
keys=root
|
3 |
+
|
4 |
+
[handlers]
|
5 |
+
keys=consoleHandler
|
6 |
+
|
7 |
+
[formatters]
|
8 |
+
keys=fullFormatter
|
9 |
+
|
10 |
+
[logger_root]
|
11 |
+
level=INFO
|
12 |
+
handlers=consoleHandler
|
13 |
+
|
14 |
+
[handler_consoleHandler]
|
15 |
+
class=StreamHandler
|
16 |
+
level=INFO
|
17 |
+
formatter=fullFormatter
|
18 |
+
args=(sys.stdout,)
|
19 |
+
|
20 |
+
[formatter_fullFormatter]
|
21 |
+
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
|
configs/metadata.json
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.0",
|
4 |
+
"changelog": {
|
5 |
+
"0.2.0": "update license files",
|
6 |
+
"0.1.0": "complete the first version model package",
|
7 |
+
"0.0.1": "initialize the model package structure"
|
8 |
+
},
|
9 |
+
"monai_version": "1.0.0",
|
10 |
+
"pytorch_version": "1.12.0",
|
11 |
+
"numpy_version": "1.22.4",
|
12 |
+
"optional_packages_version": {
|
13 |
+
"nibabel": "4.0.1",
|
14 |
+
"pytorch-ignite": "0.4.9"
|
15 |
+
},
|
16 |
+
"task": "Endoscopic inbody classification classification",
|
17 |
+
"description": "A pre-trained binary classification model for endoscopic inbody classification task",
|
18 |
+
"authors": "NVIDIA DLMED team",
|
19 |
+
"copyright": "Copyright (c) 2021-2022, NVIDIA CORPORATION",
|
20 |
+
"data_source": "private dataset",
|
21 |
+
"data_type": "RGB",
|
22 |
+
"image_classes": "three channel data, intensity [0-255]",
|
23 |
+
"label_classes": "0: inbody, 1: outbody",
|
24 |
+
"pred_classes": "vector whose length equals to 2, [1,0] means in body, [0,1] means out body",
|
25 |
+
"eval_metrics": {
|
26 |
+
"accuracy": 0.98
|
27 |
+
},
|
28 |
+
"references": [
|
29 |
+
"J. Hu, L. Shen and G. Sun, Squeeze-and-Excitation Networks, 2018 IEEE/CVF Conference on Computer Vision and Pattern Recognition, 2018, pp. 7132-7141. https://arxiv.org/pdf/1709.01507.pdf"
|
30 |
+
],
|
31 |
+
"network_data_format": {
|
32 |
+
"inputs": {
|
33 |
+
"image": {
|
34 |
+
"type": "magnitude",
|
35 |
+
"format": "RGB",
|
36 |
+
"modality": "regular",
|
37 |
+
"num_channels": 3,
|
38 |
+
"spatial_shape": [
|
39 |
+
256,
|
40 |
+
256
|
41 |
+
],
|
42 |
+
"dtype": "float32",
|
43 |
+
"value_range": [
|
44 |
+
0,
|
45 |
+
1
|
46 |
+
],
|
47 |
+
"is_patch_data": false,
|
48 |
+
"channel_def": {
|
49 |
+
"0": "R",
|
50 |
+
"1": "G",
|
51 |
+
"2": "B"
|
52 |
+
}
|
53 |
+
}
|
54 |
+
},
|
55 |
+
"outputs": {
|
56 |
+
"pred": {
|
57 |
+
"type": "probabilities",
|
58 |
+
"format": "classes",
|
59 |
+
"num_channels": 2,
|
60 |
+
"spatial_shape": [
|
61 |
+
1,
|
62 |
+
2
|
63 |
+
],
|
64 |
+
"dtype": "float32",
|
65 |
+
"value_range": [
|
66 |
+
0,
|
67 |
+
1
|
68 |
+
],
|
69 |
+
"is_patch_data": false,
|
70 |
+
"channel_def": {
|
71 |
+
"0": "in body",
|
72 |
+
"1": "out body"
|
73 |
+
}
|
74 |
+
}
|
75 |
+
}
|
76 |
+
}
|
77 |
+
}
|
configs/multi_gpu_train.json
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"device": "$torch.device(f'cuda:{dist.get_rank()}')",
|
3 |
+
"network": {
|
4 |
+
"_target_": "torch.nn.parallel.DistributedDataParallel",
|
5 |
+
"module": "$@network_def.to(@device)",
|
6 |
+
"device_ids": [
|
7 |
+
"@device"
|
8 |
+
]
|
9 |
+
},
|
10 |
+
"train#sampler": {
|
11 |
+
"_target_": "DistributedSampler",
|
12 |
+
"dataset": "@train#dataset",
|
13 |
+
"even_divisible": true,
|
14 |
+
"shuffle": true
|
15 |
+
},
|
16 |
+
"train#dataloader#sampler": "@train#sampler",
|
17 |
+
"train#dataloader#shuffle": false,
|
18 |
+
"train#trainer#train_handlers": "$@train#handlers[: -2 if dist.get_rank() > 0 else None]",
|
19 |
+
"validate#sampler": {
|
20 |
+
"_target_": "DistributedSampler",
|
21 |
+
"dataset": "@validate#dataset",
|
22 |
+
"even_divisible": false,
|
23 |
+
"shuffle": false
|
24 |
+
},
|
25 |
+
"validate#dataloader#sampler": "@validate#sampler",
|
26 |
+
"validate#evaluator#val_handlers": "$None if dist.get_rank() > 0 else @validate#handlers",
|
27 |
+
"training": [
|
28 |
+
"$import torch.distributed as dist",
|
29 |
+
"$dist.init_process_group(backend='nccl')",
|
30 |
+
"$torch.cuda.set_device(@device)",
|
31 |
+
"$monai.utils.set_determinism(seed=123)",
|
32 |
+
"$setattr(torch.backends.cudnn, 'benchmark', True)",
|
33 |
+
"$@train#trainer.run()",
|
34 |
+
"$dist.destroy_process_group()"
|
35 |
+
]
|
36 |
+
}
|
configs/train.json
ADDED
@@ -0,0 +1,259 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"imports": [
|
3 |
+
"$import torch",
|
4 |
+
"$import json",
|
5 |
+
"$import ignite"
|
6 |
+
],
|
7 |
+
"bundle_root": "/workspace/bundle/endoscopic_inbody_classification",
|
8 |
+
"ckpt_dir": "$@bundle_root + '/models'",
|
9 |
+
"output_dir": "$@bundle_root + '/eval'",
|
10 |
+
"dataset_dir": "/workspace/data/endoscopic_inbody_classification",
|
11 |
+
"train_json": "$@dataset_dir+'/train.json'",
|
12 |
+
"val_json": "$@dataset_dir+'/val.json'",
|
13 |
+
"train_fp": "$open(@train_json,'r', encoding='utf8')",
|
14 |
+
"train_dict": "$json.load(@train_fp)",
|
15 |
+
"train_close": "$@train_fp.close()",
|
16 |
+
"val_fp": "$open(@val_json,'r', encoding='utf8')",
|
17 |
+
"val_dict": "$json.load(@val_fp)",
|
18 |
+
"val_interval": 1,
|
19 |
+
"val_close": "$@val_fp.close()",
|
20 |
+
"device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
|
21 |
+
"network_def": {
|
22 |
+
"_target_": "SEResNet50",
|
23 |
+
"spatial_dims": 2,
|
24 |
+
"in_channels": 3,
|
25 |
+
"num_classes": 2
|
26 |
+
},
|
27 |
+
"network": "$@network_def.to(@device)",
|
28 |
+
"loss": {
|
29 |
+
"_target_": "torch.nn.CrossEntropyLoss",
|
30 |
+
"reduction": "sum"
|
31 |
+
},
|
32 |
+
"optimizer": {
|
33 |
+
"_target_": "torch.optim.Adam",
|
34 |
+
"params": "$@network.parameters()",
|
35 |
+
"lr": 0.001
|
36 |
+
},
|
37 |
+
"train": {
|
38 |
+
"deterministic_transforms": [
|
39 |
+
{
|
40 |
+
"_target_": "LoadImaged",
|
41 |
+
"keys": "image"
|
42 |
+
},
|
43 |
+
{
|
44 |
+
"_target_": "ToTensord",
|
45 |
+
"keys": "label"
|
46 |
+
},
|
47 |
+
{
|
48 |
+
"_target_": "AsChannelFirstd",
|
49 |
+
"keys": "image"
|
50 |
+
},
|
51 |
+
{
|
52 |
+
"_target_": "Resized",
|
53 |
+
"keys": "image",
|
54 |
+
"spatial_size": [
|
55 |
+
256,
|
56 |
+
256
|
57 |
+
],
|
58 |
+
"mode": "bilinear"
|
59 |
+
},
|
60 |
+
{
|
61 |
+
"_target_": "CastToTyped",
|
62 |
+
"dtype": "$torch.float32",
|
63 |
+
"keys": "image"
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"_target_": "NormalizeIntensityd",
|
67 |
+
"nonzero": true,
|
68 |
+
"channel_wise": true,
|
69 |
+
"keys": "image"
|
70 |
+
},
|
71 |
+
{
|
72 |
+
"_target_": "EnsureTyped",
|
73 |
+
"keys": "image"
|
74 |
+
}
|
75 |
+
],
|
76 |
+
"random_transforms": [
|
77 |
+
{
|
78 |
+
"_target_": "RandRotated",
|
79 |
+
"range_x": 0.3,
|
80 |
+
"prob": 0.2,
|
81 |
+
"mode": "bilinear",
|
82 |
+
"keys": "image"
|
83 |
+
},
|
84 |
+
{
|
85 |
+
"_target_": "RandScaleIntensityd",
|
86 |
+
"factors": 0.3,
|
87 |
+
"prob": 0.5,
|
88 |
+
"keys": "image"
|
89 |
+
},
|
90 |
+
{
|
91 |
+
"_target_": "RandShiftIntensityd",
|
92 |
+
"offsets": 0.1,
|
93 |
+
"prob": 0.5,
|
94 |
+
"keys": "image"
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"_target_": "RandGaussianNoised",
|
98 |
+
"std": 0.01,
|
99 |
+
"prob": 0.15,
|
100 |
+
"keys": "image"
|
101 |
+
},
|
102 |
+
{
|
103 |
+
"_target_": "RandFlipd",
|
104 |
+
"spatial_axis": 0,
|
105 |
+
"prob": 0.5,
|
106 |
+
"keys": "image"
|
107 |
+
},
|
108 |
+
{
|
109 |
+
"_target_": "RandFlipd",
|
110 |
+
"spatial_axis": 1,
|
111 |
+
"prob": 0.5,
|
112 |
+
"keys": "image"
|
113 |
+
}
|
114 |
+
],
|
115 |
+
"preprocessing": {
|
116 |
+
"_target_": "Compose",
|
117 |
+
"transforms": "$@train#deterministic_transforms + @train#random_transforms"
|
118 |
+
},
|
119 |
+
"dataset": {
|
120 |
+
"_target_": "Dataset",
|
121 |
+
"data": "@train_dict",
|
122 |
+
"transform": "@train#preprocessing"
|
123 |
+
},
|
124 |
+
"dataloader": {
|
125 |
+
"_target_": "DataLoader",
|
126 |
+
"dataset": "@train#dataset",
|
127 |
+
"batch_size": 64,
|
128 |
+
"shuffle": true,
|
129 |
+
"num_workers": 4
|
130 |
+
},
|
131 |
+
"inferer": {
|
132 |
+
"_target_": "SimpleInferer"
|
133 |
+
},
|
134 |
+
"handlers": [
|
135 |
+
{
|
136 |
+
"_target_": "ValidationHandler",
|
137 |
+
"validator": "@validate#evaluator",
|
138 |
+
"epoch_level": true,
|
139 |
+
"interval": "@val_interval"
|
140 |
+
},
|
141 |
+
{
|
142 |
+
"_target_": "StatsHandler",
|
143 |
+
"tag_name": "train_loss",
|
144 |
+
"output_transform": "$monai.handlers.from_engine(['loss'], first=True)"
|
145 |
+
},
|
146 |
+
{
|
147 |
+
"_target_": "TensorBoardStatsHandler",
|
148 |
+
"log_dir": "@output_dir",
|
149 |
+
"tag_name": "train_loss",
|
150 |
+
"output_transform": "$monai.handlers.from_engine(['loss'], first=True)"
|
151 |
+
}
|
152 |
+
],
|
153 |
+
"key_metric": {
|
154 |
+
"train_accu": {
|
155 |
+
"_target_": "ignite.metrics.Accuracy",
|
156 |
+
"output_transform": "$monai.handlers.from_engine(['pred', 'label'])"
|
157 |
+
}
|
158 |
+
},
|
159 |
+
"postprocessing": {
|
160 |
+
"_target_": "Compose",
|
161 |
+
"transforms": [
|
162 |
+
{
|
163 |
+
"_target_": "AsDiscreted",
|
164 |
+
"argmax": [
|
165 |
+
true,
|
166 |
+
false
|
167 |
+
],
|
168 |
+
"to_onehot": [
|
169 |
+
2,
|
170 |
+
2
|
171 |
+
],
|
172 |
+
"keys": [
|
173 |
+
"pred",
|
174 |
+
"label"
|
175 |
+
]
|
176 |
+
}
|
177 |
+
]
|
178 |
+
},
|
179 |
+
"trainer": {
|
180 |
+
"_target_": "SupervisedTrainer",
|
181 |
+
"max_epochs": 25,
|
182 |
+
"device": "@device",
|
183 |
+
"train_data_loader": "@train#dataloader",
|
184 |
+
"network": "@network",
|
185 |
+
"loss_function": "@loss",
|
186 |
+
"optimizer": "@optimizer",
|
187 |
+
"inferer": "@train#inferer",
|
188 |
+
"postprocessing": "@train#postprocessing",
|
189 |
+
"key_train_metric": "@train#key_metric",
|
190 |
+
"train_handlers": "@train#handlers"
|
191 |
+
}
|
192 |
+
},
|
193 |
+
"validate": {
|
194 |
+
"preprocessing": {
|
195 |
+
"_target_": "Compose",
|
196 |
+
"transforms": "%train#deterministic_transforms"
|
197 |
+
},
|
198 |
+
"dataset": {
|
199 |
+
"_target_": "Dataset",
|
200 |
+
"data": "@val_dict",
|
201 |
+
"transform": "@validate#preprocessing"
|
202 |
+
},
|
203 |
+
"dataloader": {
|
204 |
+
"_target_": "DataLoader",
|
205 |
+
"dataset": "@validate#dataset",
|
206 |
+
"batch_size": 64,
|
207 |
+
"shuffle": false,
|
208 |
+
"num_workers": 4
|
209 |
+
},
|
210 |
+
"inferer": {
|
211 |
+
"_target_": "SimpleInferer"
|
212 |
+
},
|
213 |
+
"postprocessing": {
|
214 |
+
"_target_": "Compose",
|
215 |
+
"transforms": "%train#postprocessing"
|
216 |
+
},
|
217 |
+
"handlers": [
|
218 |
+
{
|
219 |
+
"_target_": "StatsHandler",
|
220 |
+
"iteration_log": false
|
221 |
+
},
|
222 |
+
{
|
223 |
+
"_target_": "TensorBoardStatsHandler",
|
224 |
+
"log_dir": "@output_dir",
|
225 |
+
"iteration_log": false
|
226 |
+
},
|
227 |
+
{
|
228 |
+
"_target_": "CheckpointSaver",
|
229 |
+
"save_dir": "@ckpt_dir",
|
230 |
+
"save_dict": {
|
231 |
+
"model": "@network"
|
232 |
+
},
|
233 |
+
"save_key_metric": true,
|
234 |
+
"key_metric_filename": "model.pt"
|
235 |
+
}
|
236 |
+
],
|
237 |
+
"key_metric": {
|
238 |
+
"val_accu": {
|
239 |
+
"_target_": "ignite.metrics.Accuracy",
|
240 |
+
"output_transform": "$monai.handlers.from_engine(['pred', 'label'])"
|
241 |
+
}
|
242 |
+
},
|
243 |
+
"evaluator": {
|
244 |
+
"_target_": "SupervisedEvaluator",
|
245 |
+
"device": "@device",
|
246 |
+
"val_data_loader": "@validate#dataloader",
|
247 |
+
"network": "@network",
|
248 |
+
"inferer": "@validate#inferer",
|
249 |
+
"postprocessing": "@validate#postprocessing",
|
250 |
+
"key_val_metric": "@validate#key_metric",
|
251 |
+
"val_handlers": "@validate#handlers"
|
252 |
+
}
|
253 |
+
},
|
254 |
+
"training": [
|
255 |
+
"$monai.utils.set_determinism(seed=0)",
|
256 |
+
"$setattr(torch.backends.cudnn, 'benchmark', True)",
|
257 |
+
"$@train#trainer.run()"
|
258 |
+
]
|
259 |
+
}
|
docs/data_license.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
Datasets used in this work were provided by Activ Surgical.
|
docs/readme.md
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Description
|
2 |
+
A pre-trained model for the endoscopic inbody classification task.
|
3 |
+
|
4 |
+
# Model Overview
|
5 |
+
This model is trained using the SEResNet50 structure, whose details can be found in [1]. All datasets are from private samples of [Activ Surgical](https://www.activsurgical.com/). Samples in training and validation dataset are from the same 4 videos, while test samples are from different two videos.
|
6 |
+
The [pytorch model](https://drive.google.com/file/d/14CS-s1uv2q6WedYQGeFbZeEWIkoyNa-x/view?usp=sharing) and [torchscript model](https://drive.google.com/file/d/1fOoJ4n5DWKHrt9QXTZ2sXwr9C-YvVGCM/view?usp=sharing) are shared in google drive. 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/). Here is a [link](https://drive.google.com/uc?id=1rQQfHcZFs74OG0uJsL4vg7YqMWXV-s4k) of 20 samples (10 in-body and 10 out-body) to show what this dataset looks like. Modify the "dataset_dir" parameter specified in configs/train.json and configs/inference.json to reflect where the data is.
|
10 |
+
|
11 |
+
The input label json should be a list made up by dicts which includes "image" and "label" keys. An example format is shown below.
|
12 |
+
|
13 |
+
```
|
14 |
+
[
|
15 |
+
{
|
16 |
+
"image":"/path/to/image/image_name0.jpg",
|
17 |
+
"label": 0
|
18 |
+
},
|
19 |
+
{
|
20 |
+
"image":"/path/to/image/image_name1.jpg",
|
21 |
+
"label": 0
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"image":"/path/to/image/image_name2.jpg",
|
25 |
+
"label": 1
|
26 |
+
},
|
27 |
+
....
|
28 |
+
{
|
29 |
+
"image":"/path/to/image/image_namek.jpg",
|
30 |
+
"label": 0
|
31 |
+
},
|
32 |
+
]
|
33 |
+
```
|
34 |
+
|
35 |
+
## Training configuration
|
36 |
+
The training was performed with an at least 12GB-memory GPU.
|
37 |
+
|
38 |
+
Actual Model Input: 256 x 256 x 3
|
39 |
+
|
40 |
+
## Input and output formats
|
41 |
+
Input: 3 channel video frames
|
42 |
+
|
43 |
+
Output: probability vector whose length equals to 2: Label 0: in body; Label 1: out body
|
44 |
+
|
45 |
+
## Scores
|
46 |
+
This model achieves the following accuracy score on the test dataset:
|
47 |
+
|
48 |
+
Accuracy = 0.98
|
49 |
+
|
50 |
+
## commands example
|
51 |
+
Execute training:
|
52 |
+
|
53 |
+
```
|
54 |
+
python -m monai.bundle run training \
|
55 |
+
--meta_file configs/metadata.json \
|
56 |
+
--config_file configs/train.json \
|
57 |
+
--logging_file configs/logging.conf
|
58 |
+
```
|
59 |
+
|
60 |
+
Override the `train` config to execute multi-GPU training:
|
61 |
+
```
|
62 |
+
torchrun --standalone --nnodes=1 --nproc_per_node=2 -m monai.bundle run training \
|
63 |
+
--meta_file configs/metadata.json \
|
64 |
+
--config_file "['configs/train.json','configs/multi_gpu_train.json']" \
|
65 |
+
--logging_file configs/logging.conf
|
66 |
+
```
|
67 |
+
|
68 |
+
Override the `train` config to execute evaluation with the trained model:
|
69 |
+
|
70 |
+
```
|
71 |
+
python -m monai.bundle run evaluating \
|
72 |
+
--meta_file configs/metadata.json \
|
73 |
+
--config_file "['configs/train.json','configs/evaluate.json']" \
|
74 |
+
--logging_file configs/logging.conf
|
75 |
+
```
|
76 |
+
|
77 |
+
Execute inference:
|
78 |
+
|
79 |
+
```
|
80 |
+
python -m monai.bundle run evaluating \
|
81 |
+
--meta_file configs/metadata.json \
|
82 |
+
--config_file configs/inference.json \
|
83 |
+
--logging_file configs/logging.conf
|
84 |
+
```
|
85 |
+
|
86 |
+
Export checkpoint to TorchScript file:
|
87 |
+
|
88 |
+
```
|
89 |
+
python -m monai.bundle ckpt_export network_def \
|
90 |
+
--filepath models/model.ts \
|
91 |
+
--ckpt_file models/model.pt \
|
92 |
+
--meta_file configs/metadata.json \
|
93 |
+
--config_file configs/inference.json
|
94 |
+
```
|
95 |
+
|
96 |
+
Export checkpoint to onnx file, which has been tested on pytorch 1.12.0:
|
97 |
+
|
98 |
+
```
|
99 |
+
python scripts/export_to_onnx.py --model models/model.pt --outpath models/model.onnx
|
100 |
+
```
|
101 |
+
|
102 |
+
Export TensorRT float16 model from the onnx model:
|
103 |
+
|
104 |
+
```
|
105 |
+
trtexec --onnx=models/model.onnx --saveEngine=models/model.trt --fp16 \
|
106 |
+
--minShapes=INPUT__0:1x3x256x256 \
|
107 |
+
--optShapes=INPUT__0:16x3x256x256 \
|
108 |
+
--maxShapes=INPUT__0:32x3x256x256 \
|
109 |
+
--shapes=INPUT__0:8x3x256x256
|
110 |
+
```
|
111 |
+
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).
|
112 |
+
|
113 |
+
# References
|
114 |
+
[1] J. Hu, L. Shen and G. Sun, Squeeze-and-Excitation Networks, 2018 IEEE/CVF Conference on Computer Vision and Pattern Recognition, 2018, pp. 7132-7141. https://arxiv.org/pdf/1709.01507.pdf
|
115 |
+
|
116 |
+
# License
|
117 |
+
Copyright (c) MONAI Consortium
|
118 |
+
|
119 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
120 |
+
you may not use this file except in compliance with the License.
|
121 |
+
You may obtain a copy of the License at
|
122 |
+
|
123 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
124 |
+
|
125 |
+
Unless required by applicable law or agreed to in writing, software
|
126 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
127 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
128 |
+
See the License for the specific language governing permissions and
|
129 |
+
limitations under the License.
|
models/model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c99b0f2f60b7ceb93fa0ee4e5225aeb1376a109a9993a6327353a370e1c96ca9
|
3 |
+
size 104506369
|
models/model.ts
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:65f38a4ad17d48414c6a194a6cda11189535ec6263241d3b21f4c56cac579044
|
3 |
+
size 104596723
|
scripts/export_to_onnx.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import os
|
3 |
+
|
4 |
+
import numpy as np
|
5 |
+
import onnx
|
6 |
+
import onnxruntime
|
7 |
+
import torch
|
8 |
+
from monai.networks.nets import SEResNet50
|
9 |
+
|
10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
|
12 |
+
|
13 |
+
def load_model_and_export(modelname, outname, out_channels, height, width, multigpu=False, in_channels=3):
|
14 |
+
"""
|
15 |
+
Loading a model by name.
|
16 |
+
|
17 |
+
Args:
|
18 |
+
modelname: a whole path name of the model that need to be loaded.
|
19 |
+
outname: a name for output onnx model.
|
20 |
+
out_channels: output channels, which usually equals to 1 + class_number.
|
21 |
+
height: input images' height.
|
22 |
+
width: input images' width.
|
23 |
+
multigpu: if the pre-trained model trained on a multigpu environment.
|
24 |
+
in_channels: input images' channel number.
|
25 |
+
"""
|
26 |
+
isopen = os.path.exists(modelname)
|
27 |
+
if not isopen:
|
28 |
+
raise Exception("The specified model to load does not exist!")
|
29 |
+
|
30 |
+
model = SEResNet50(spatial_dims=2, in_channels=in_channels, num_classes=out_channels)
|
31 |
+
|
32 |
+
if multigpu:
|
33 |
+
model = torch.nn.DataParallel(model)
|
34 |
+
model = model.cuda()
|
35 |
+
model.load_state_dict(torch.load(modelname, map_location=device)) # if the model is trained on multi gpu
|
36 |
+
model = model.eval()
|
37 |
+
|
38 |
+
np.random.seed(0)
|
39 |
+
x = np.random.random((1, 3, width, height))
|
40 |
+
x = torch.tensor(x, dtype=torch.float32)
|
41 |
+
x = x.cuda()
|
42 |
+
torch_out = model(x)
|
43 |
+
input_names = ["INPUT__0"]
|
44 |
+
output_names = ["OUTPUT__0"]
|
45 |
+
# Export the model
|
46 |
+
if multigpu:
|
47 |
+
model_trans = model.module
|
48 |
+
else:
|
49 |
+
model_trans = model
|
50 |
+
torch.onnx.export(
|
51 |
+
model_trans, # model to save
|
52 |
+
x, # model input
|
53 |
+
outname, # model save path
|
54 |
+
export_params=True,
|
55 |
+
verbose=True,
|
56 |
+
do_constant_folding=True,
|
57 |
+
input_names=input_names,
|
58 |
+
output_names=output_names,
|
59 |
+
opset_version=15,
|
60 |
+
dynamic_axes={"INPUT__0": {0: "batch_size"}, "OUTPUT__0": {0: "batch_size"}},
|
61 |
+
)
|
62 |
+
onnx_model = onnx.load(outname)
|
63 |
+
onnx.checker.check_model(onnx_model, full_check=True)
|
64 |
+
ort_session = onnxruntime.InferenceSession(outname)
|
65 |
+
|
66 |
+
def to_numpy(tensor):
|
67 |
+
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
|
68 |
+
|
69 |
+
# compute ONNX Runtime output prediction
|
70 |
+
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
|
71 |
+
ort_outs = ort_session.run(["OUTPUT__0"], ort_inputs)
|
72 |
+
numpy_torch_out = to_numpy(torch_out)
|
73 |
+
# compare ONNX Runtime and PyTorch results
|
74 |
+
np.testing.assert_allclose(numpy_torch_out, ort_outs[0], rtol=1e-03, atol=1e-05)
|
75 |
+
print("Exported model has been tested with ONNXRuntime, and the result looks good!")
|
76 |
+
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
parser = argparse.ArgumentParser()
|
80 |
+
# the original model for converting.
|
81 |
+
parser.add_argument(
|
82 |
+
"--model",
|
83 |
+
type=str,
|
84 |
+
default=r"/workspace/bundle/endoscopic_inbody_classification/models/model.pt",
|
85 |
+
help="Input an existing model weight",
|
86 |
+
)
|
87 |
+
|
88 |
+
# path to save the onnx model.
|
89 |
+
parser.add_argument(
|
90 |
+
"--outpath",
|
91 |
+
type=str,
|
92 |
+
default=r"/workspace/bundle/endoscopic_inbody_classification/models/model.onnx",
|
93 |
+
help="A path to save the onnx model.",
|
94 |
+
)
|
95 |
+
|
96 |
+
parser.add_argument("--width", type=int, default=256, help="Width for exporting onnx model.")
|
97 |
+
|
98 |
+
parser.add_argument("--height", type=int, default=256, help="Height for exporting onnx model.")
|
99 |
+
|
100 |
+
parser.add_argument(
|
101 |
+
"--out_channels", type=int, default=2, help="Number of expected out_channels in model for exporting to onnx."
|
102 |
+
)
|
103 |
+
|
104 |
+
parser.add_argument("--multigpu", type=bool, default=False, help="If loading model trained with multi gpu.")
|
105 |
+
|
106 |
+
args = parser.parse_args()
|
107 |
+
modelname = args.model
|
108 |
+
outname = args.outpath
|
109 |
+
out_channels = args.out_channels
|
110 |
+
height = args.height
|
111 |
+
width = args.width
|
112 |
+
multigpu = args.multigpu
|
113 |
+
|
114 |
+
if os.path.exists(outname):
|
115 |
+
raise Exception(
|
116 |
+
"The specified outpath already exists! Change the outpath to avoid overwriting your saved model. "
|
117 |
+
)
|
118 |
+
model = load_model_and_export(modelname, outname, out_channels, height, width, multigpu)
|