-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
scripts.py
1090 lines (921 loc) · 47.4 KB
/
scripts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 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 ast
import json
import os
import pprint
import re
import time
import warnings
from logging.config import fileConfig
from pathlib import Path
from shutil import copyfile
from textwrap import dedent
from typing import Dict, Mapping, Optional, Sequence, Tuple, Union
import torch
from torch.cuda import is_available
from monai.apps.mmars.mmars import _get_all_ngc_models
from monai.apps.utils import _basename, download_url, extractall, get_logger
from monai.bundle.config_item import ConfigComponent
from monai.bundle.config_parser import ConfigParser
from monai.bundle.utils import DEFAULT_EXP_MGMT_SETTINGS, DEFAULT_INFERENCE, DEFAULT_METADATA
from monai.config import IgniteInfo, PathLike
from monai.data import load_net_with_metadata, save_net_with_metadata
from monai.networks import convert_to_torchscript, copy_model_state, get_state_dict, save_state
from monai.utils import check_parent_dir, get_equivalent_dtype, min_version, optional_import
from monai.utils.misc import ensure_tuple
validate, _ = optional_import("jsonschema", name="validate")
ValidationError, _ = optional_import("jsonschema.exceptions", name="ValidationError")
Checkpoint, has_ignite = optional_import("ignite.handlers", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Checkpoint")
requests_get, has_requests = optional_import("requests", name="get")
logger = get_logger(module_name=__name__)
# set BUNDLE_DOWNLOAD_SRC="ngc" to use NGC source in default for bundle download
download_source = os.environ.get("BUNDLE_DOWNLOAD_SRC", "github")
def _update_args(args: Optional[Union[str, Dict]] = None, ignore_none: bool = True, **kwargs) -> Dict:
"""
Update the `args` with the input `kwargs`.
For dict data, recursively update the content based on the keys.
Args:
args: source args to update.
ignore_none: whether to ignore input args with None value, default to `True`.
kwargs: destination args to update.
"""
args_: Dict = args if isinstance(args, dict) else {}
if isinstance(args, str):
# args are defined in a structured file
args_ = ConfigParser.load_config_file(args)
# recursively update the default args with new args
for k, v in kwargs.items():
if ignore_none and v is None:
continue
if isinstance(v, dict) and isinstance(args_.get(k), dict):
args_[k] = _update_args(args_[k], ignore_none, **v)
else:
args_[k] = v
return args_
def _pop_args(src: Dict, *args, **kwargs):
"""
Pop args from the `src` dictionary based on specified keys in `args` and (key, default value) pairs in `kwargs`.
"""
return tuple([src.pop(i) for i in args] + [src.pop(k, v) for k, v in kwargs.items()])
def _log_input_summary(tag, args: Dict):
logger.info(f"--- input summary of monai.bundle.scripts.{tag} ---")
for name, val in args.items():
logger.info(f"> {name}: {pprint.pformat(val)}")
logger.info("---\n\n")
def _get_var_names(expr: str):
"""
Parse the expression and discover what variables are present in it based on ast module.
Args:
expr: source expression to parse.
"""
tree = ast.parse(expr)
return [m.id for m in ast.walk(tree) if isinstance(m, ast.Name)]
def _get_fake_spatial_shape(shape: Sequence[Union[str, int]], p: int = 1, n: int = 1, any: int = 1) -> Tuple:
"""
Get spatial shape for fake data according to the specified shape pattern.
It supports `int` number and `string` with formats like: "32", "32 * n", "32 ** p", "32 ** p *n".
Args:
shape: specified pattern for the spatial shape.
p: power factor to generate fake data shape if dim of expected shape is "x**p", default to 1.
p: multiply factor to generate fake data shape if dim of expected shape is "x*n", default to 1.
any: specified size to generate fake data shape if dim of expected shape is "*", default to 1.
"""
ret = []
for i in shape:
if isinstance(i, int):
ret.append(i)
elif isinstance(i, str):
if i == "*":
ret.append(any)
else:
for c in _get_var_names(i):
if c not in ["p", "n"]:
raise ValueError(f"only support variables 'p' and 'n' so far, but got: {c}.")
ret.append(eval(i, {"p": p, "n": n}))
else:
raise ValueError(f"spatial shape items must be int or string, but got: {type(i)} {i}.")
return tuple(ret)
def _get_git_release_url(repo_owner: str, repo_name: str, tag_name: str, filename: str):
return f"https://github.com/{repo_owner}/{repo_name}/releases/download/{tag_name}/{filename}"
def _get_ngc_bundle_url(model_name: str, version: str):
return f"https://api.ngc.nvidia.com/v2/models/nvidia/monaitoolkit/{model_name}/versions/{version}/zip"
def _download_from_github(repo: str, download_path: Path, filename: str, progress: bool = True):
repo_owner, repo_name, tag_name = repo.split("/")
if ".zip" not in filename:
filename += ".zip"
url = _get_git_release_url(repo_owner, repo_name, tag_name=tag_name, filename=filename)
filepath = download_path / f"{filename}"
download_url(url=url, filepath=filepath, hash_val=None, progress=progress)
extractall(filepath=filepath, output_dir=download_path, has_base=True)
def _add_ngc_prefix(name: str, prefix: str = "monai_"):
if name.startswith(prefix):
return name
return f"{prefix}{name}"
def _remove_ngc_prefix(name: str, prefix: str = "monai_"):
if name.startswith(prefix):
return name[len(prefix) :]
return name
def _download_from_ngc(download_path: Path, filename: str, version: str, remove_prefix: Optional[str], progress: bool):
# ensure prefix is contained
filename = _add_ngc_prefix(filename)
url = _get_ngc_bundle_url(model_name=filename, version=version)
filepath = download_path / f"{filename}_v{version}.zip"
if remove_prefix:
filename = _remove_ngc_prefix(filename, prefix=remove_prefix)
extract_path = download_path / f"{filename}"
download_url(url=url, filepath=filepath, hash_val=None, progress=progress)
extractall(filepath=filepath, output_dir=extract_path, has_base=True)
def _get_latest_bundle_version(source: str, name: str, repo: str):
if source == "ngc":
name = _add_ngc_prefix(name)
model_dict = _get_all_ngc_models(name)
for v in model_dict.values():
if v["name"] == name:
return v["latest"]
return None
elif source == "github":
repo_owner, repo_name, tag_name = repo.split("/")
return get_bundle_versions(name, repo=os.path.join(repo_owner, repo_name), tag=tag_name)["latest_version"]
else:
raise ValueError(f"To get the latest bundle version, source should be 'github' or 'ngc', got {source}.")
def _process_bundle_dir(bundle_dir: Optional[PathLike] = None):
if bundle_dir is None:
get_dir, has_home = optional_import("torch.hub", name="get_dir")
if has_home:
bundle_dir = Path(get_dir()) / "bundle"
else:
raise ValueError("bundle_dir=None, but no suitable default directory computed. Upgrade Pytorch to 1.6+ ?")
return Path(bundle_dir)
def download(
name: Optional[str] = None,
version: Optional[str] = None,
bundle_dir: Optional[PathLike] = None,
source: str = download_source,
repo: Optional[str] = None,
url: Optional[str] = None,
remove_prefix: Optional[str] = "monai_",
progress: bool = True,
args_file: Optional[str] = None,
):
"""
download bundle from the specified source or url. The bundle should be a zip file and it
will be extracted after downloading.
This function refers to:
https://pytorch.org/docs/stable/_modules/torch/hub.html
Typical usage examples:
.. code-block:: bash
# Execute this module as a CLI entry, and download bundle from the model-zoo repo:
python -m monai.bundle download --name <bundle_name> --version "0.1.0" --bundle_dir "./"
# Execute this module as a CLI entry, and download bundle from specified github repo:
python -m monai.bundle download --name <bundle_name> --source "github" --repo "repo_owner/repo_name/release_tag"
# Execute this module as a CLI entry, and download bundle from ngc with latest version:
python -m monai.bundle download --name <bundle_name> --source "ngc" --bundle_dir "./"
# Execute this module as a CLI entry, and download bundle via URL:
python -m monai.bundle download --name <bundle_name> --url <url>
# Set default args of `run` in a JSON / YAML file, help to record and simplify the command line.
# Other args still can override the default args at runtime.
# The content of the JSON / YAML file is a dictionary. For example:
# {"name": "spleen", "bundle_dir": "download", "source": ""}
# then do the following command for downloading:
python -m monai.bundle download --args_file "args.json" --source "github"
Args:
name: bundle name. If `None` and `url` is `None`, it must be provided in `args_file`.
for example:
"spleen_ct_segmentation", "prostate_mri_anatomy" in model-zoo:
https://github.com/Project-MONAI/model-zoo/releases/tag/hosting_storage_v1.
"monai_brats_mri_segmentation" in ngc:
https://catalog.ngc.nvidia.com/models?filters=&orderBy=scoreDESC&query=monai.
version: version name of the target bundle to download, like: "0.1.0". If `None`, will download
the latest version.
bundle_dir: target directory to store the downloaded data.
Default is `bundle` subfolder under `torch.hub.get_dir()`.
source: storage location name. This argument is used when `url` is `None`.
In default, the value is achieved from the environment variable BUNDLE_DOWNLOAD_SRC, and
it should be "ngc" or "github".
repo: repo name. This argument is used when `url` is `None` and `source` is "github".
If used, it should be in the form of "repo_owner/repo_name/release_tag".
url: url to download the data. If not `None`, data will be downloaded directly
and `source` will not be checked.
If `name` is `None`, filename is determined by `monai.apps.utils._basename(url)`.
remove_prefix: This argument is used when `source` is "ngc". Currently, all ngc bundles
have the ``monai_`` prefix, which is not existing in their model zoo contrasts. In order to
maintain the consistency between these two sources, remove prefix is necessary.
Therefore, if specified, downloaded folder name will remove the prefix.
progress: whether to display a progress bar.
args_file: a JSON or YAML file to provide default values for all the args in this function.
so that the command line inputs can be simplified.
"""
_args = _update_args(
args=args_file,
name=name,
version=version,
bundle_dir=bundle_dir,
source=source,
repo=repo,
url=url,
remove_prefix=remove_prefix,
progress=progress,
)
_log_input_summary(tag="download", args=_args)
source_, progress_, remove_prefix_, repo_, name_, version_, bundle_dir_, url_ = _pop_args(
_args, "source", "progress", remove_prefix=None, repo=None, name=None, version=None, bundle_dir=None, url=None
)
bundle_dir_ = _process_bundle_dir(bundle_dir_)
if repo_ is None:
repo_ = "Project-MONAI/model-zoo/hosting_storage_v1"
if len(repo_.split("/")) != 3:
raise ValueError("repo should be in the form of `repo_owner/repo_name/release_tag`.")
if url_ is not None:
if name_ is not None:
filepath = bundle_dir_ / f"{name_}.zip"
else:
filepath = bundle_dir_ / f"{_basename(url_)}"
download_url(url=url_, filepath=filepath, hash_val=None, progress=progress_)
extractall(filepath=filepath, output_dir=bundle_dir_, has_base=True)
else:
if name_ is None:
raise ValueError(f"To download from source: {source_}, `name` must be provided.")
if version_ is None:
version_ = _get_latest_bundle_version(source=source_, name=name_, repo=repo_)
if source_ == "github":
if version_ is not None:
name_ = "_v".join([name_, version_])
_download_from_github(repo=repo_, download_path=bundle_dir_, filename=name_, progress=progress_)
elif source_ == "ngc":
_download_from_ngc(
download_path=bundle_dir_,
filename=name_,
version=version_,
remove_prefix=remove_prefix_,
progress=progress_,
)
else:
raise NotImplementedError(
f"Currently only download from `url`, source 'github' or 'ngc' are implemented, got source: {source_}."
)
def load(
name: str,
version: Optional[str] = None,
model_file: Optional[str] = None,
load_ts_module: bool = False,
bundle_dir: Optional[PathLike] = None,
source: str = download_source,
repo: Optional[str] = None,
remove_prefix: Optional[str] = "monai_",
progress: bool = True,
device: Optional[str] = None,
key_in_ckpt: Optional[str] = None,
config_files: Sequence[str] = (),
net_name: Optional[str] = None,
**net_kwargs,
):
"""
Load model weights or TorchScript module of a bundle.
Args:
name: bundle name. If `None` and `url` is `None`, it must be provided in `args_file`.
for example:
"spleen_ct_segmentation", "prostate_mri_anatomy" in model-zoo:
https://github.com/Project-MONAI/model-zoo/releases/tag/hosting_storage_v1.
"monai_brats_mri_segmentation" in ngc:
https://catalog.ngc.nvidia.com/models?filters=&orderBy=scoreDESC&query=monai.
version: version name of the target bundle to download, like: "0.1.0". If `None`, will download
the latest version.
model_file: the relative path of the model weights or TorchScript module within bundle.
If `None`, "models/model.pt" or "models/model.ts" will be used.
load_ts_module: a flag to specify if loading the TorchScript module.
bundle_dir: directory the weights/TorchScript module will be loaded from.
Default is `bundle` subfolder under `torch.hub.get_dir()`.
source: storage location name. This argument is used when `model_file` is not existing locally and need to be
downloaded first.
In default, the value is achieved from the environment variable BUNDLE_DOWNLOAD_SRC, and
it should be "ngc" or "github".
repo: repo name. This argument is used when `url` is `None` and `source` is "github".
If used, it should be in the form of "repo_owner/repo_name/release_tag".
remove_prefix: This argument is used when `source` is "ngc". Currently, all ngc bundles
have the ``monai_`` prefix, which is not existing in their model zoo contrasts. In order to
maintain the consistency between these two sources, remove prefix is necessary.
Therefore, if specified, downloaded folder name will remove the prefix.
progress: whether to display a progress bar when downloading.
device: target device of returned weights or module, if `None`, prefer to "cuda" if existing.
key_in_ckpt: for nested checkpoint like `{"model": XXX, "optimizer": XXX, ...}`, specify the key of model
weights. if not nested checkpoint, no need to set.
config_files: extra filenames would be loaded. The argument only works when loading a TorchScript module,
see `_extra_files` in `torch.jit.load` for more details.
net_name: if not `None`, a corresponding network will be instantiated and load the achieved weights.
This argument only works when loading weights.
net_kwargs: other arguments that are used to instantiate the network class defined by `net_name`.
Returns:
1. If `load_ts_module` is `False` and `net_name` is `None`, return model weights.
2. If `load_ts_module` is `False` and `net_name` is not `None`,
return an instantiated network that loaded the weights.
3. If `load_ts_module` is `True`, return a triple that include a TorchScript module,
the corresponding metadata dict, and extra files dict.
please check `monai.data.load_net_with_metadata` for more details.
"""
bundle_dir_ = _process_bundle_dir(bundle_dir)
if model_file is None:
model_file = os.path.join("models", "model.ts" if load_ts_module is True else "model.pt")
if source == "ngc":
name = _add_ngc_prefix(name)
if remove_prefix:
name = _remove_ngc_prefix(name, prefix=remove_prefix)
full_path = os.path.join(bundle_dir_, name, model_file)
if not os.path.exists(full_path):
download(
name=name,
version=version,
bundle_dir=bundle_dir_,
source=source,
repo=repo,
remove_prefix=remove_prefix,
progress=progress,
)
if device is None:
device = "cuda:0" if is_available() else "cpu"
# loading with `torch.jit.load`
if load_ts_module is True:
return load_net_with_metadata(full_path, map_location=torch.device(device), more_extra_files=config_files)
# loading with `torch.load`
model_dict = torch.load(full_path, map_location=torch.device(device))
if not isinstance(model_dict, Mapping):
warnings.warn(f"the state dictionary from {full_path} should be a dictionary but got {type(model_dict)}.")
model_dict = get_state_dict(model_dict)
if net_name is None:
return model_dict
net_kwargs["_target_"] = net_name
configer = ConfigComponent(config=net_kwargs)
model = configer.instantiate()
model.to(device) # type: ignore
copy_model_state(dst=model, src=model_dict if key_in_ckpt is None else model_dict[key_in_ckpt]) # type: ignore
return model
def _get_all_bundles_info(
repo: str = "Project-MONAI/model-zoo", tag: str = "hosting_storage_v1", auth_token: Optional[str] = None
):
if has_requests:
request_url = f"https://api.github.com/repos/{repo}/releases"
if auth_token is not None:
headers = {"Authorization": f"Bearer {auth_token}"}
resp = requests_get(request_url, headers=headers)
else:
resp = requests_get(request_url)
resp.raise_for_status()
else:
raise ValueError("requests package is required, please install it.")
releases_list = json.loads(resp.text)
bundle_name_pattern = re.compile(r"_v\d*.")
bundles_info: Dict = {}
for release in releases_list:
if release["tag_name"] == tag:
for asset in release["assets"]:
asset_name = bundle_name_pattern.split(asset["name"])[0]
if asset_name not in bundles_info:
bundles_info[asset_name] = {}
asset_version = asset["name"].split(f"{asset_name}_v")[-1].replace(".zip", "")
bundles_info[asset_name][asset_version] = {
"id": asset["id"],
"name": asset["name"],
"size": asset["size"],
"download_count": asset["download_count"],
"browser_download_url": asset["browser_download_url"],
"created_at": asset["created_at"],
"updated_at": asset["updated_at"],
}
return bundles_info
return bundles_info
def get_all_bundles_list(
repo: str = "Project-MONAI/model-zoo", tag: str = "hosting_storage_v1", auth_token: Optional[str] = None
):
"""
Get all bundles names (and the latest versions) that are stored in the release of specified repository
with the provided tag. The default values of arguments correspond to the release of MONAI model zoo.
In order to increase the rate limits of calling Github APIs, you can input your personal access token.
Please check the following link for more details about rate limiting:
https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
The following link shows how to create your personal access token:
https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
Args:
repo: it should be in the form of "repo_owner/repo_name/".
tag: the tag name of the release.
auth_token: github personal access token.
Returns:
a list of tuple in the form of (bundle name, latest version).
"""
bundles_info = _get_all_bundles_info(repo=repo, tag=tag, auth_token=auth_token)
bundles_list = []
for bundle_name in bundles_info:
latest_version = sorted(bundles_info[bundle_name].keys())[-1]
bundles_list.append((bundle_name, latest_version))
return bundles_list
def get_bundle_versions(
bundle_name: str,
repo: str = "Project-MONAI/model-zoo",
tag: str = "hosting_storage_v1",
auth_token: Optional[str] = None,
):
"""
Get the latest version, as well as all existing versions of a bundle that is stored in the release of specified
repository with the provided tag.
In order to increase the rate limits of calling Github APIs, you can input your personal access token.
Please check the following link for more details about rate limiting:
https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
The following link shows how to create your personal access token:
https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
Args:
bundle_name: bundle name.
repo: it should be in the form of "repo_owner/repo_name/".
tag: the tag name of the release.
auth_token: github personal access token.
Returns:
a dictionary that contains the latest version and all versions of a bundle.
"""
bundles_info = _get_all_bundles_info(repo=repo, tag=tag, auth_token=auth_token)
if bundle_name not in bundles_info:
raise ValueError(f"bundle: {bundle_name} is not existing in repo: {repo}.")
bundle_info = bundles_info[bundle_name]
all_versions = sorted(bundle_info.keys())
return {"latest_version": all_versions[-1], "all_versions": all_versions}
def get_bundle_info(
bundle_name: str,
version: Optional[str] = None,
repo: str = "Project-MONAI/model-zoo",
tag: str = "hosting_storage_v1",
auth_token: Optional[str] = None,
):
"""
Get all information
(include "id", "name", "size", "download_count", "browser_download_url", "created_at", "updated_at") of a bundle
with the specified bundle name and version.
In order to increase the rate limits of calling Github APIs, you can input your personal access token.
Please check the following link for more details about rate limiting:
https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
The following link shows how to create your personal access token:
https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
Args:
bundle_name: bundle name.
version: version name of the target bundle, if None, the latest version will be used.
repo: it should be in the form of "repo_owner/repo_name/".
tag: the tag name of the release.
auth_token: github personal access token.
Returns:
a dictionary that contains the bundle's information.
"""
bundles_info = _get_all_bundles_info(repo=repo, tag=tag, auth_token=auth_token)
if bundle_name not in bundles_info:
raise ValueError(f"bundle: {bundle_name} is not existing.")
bundle_info = bundles_info[bundle_name]
if version is None:
version = sorted(bundle_info.keys())[-1]
if version not in bundle_info:
raise ValueError(f"version: {version} of bundle: {bundle_name} is not existing.")
return bundle_info[version]
def patch_bundle_tracking(parser: ConfigParser, settings: dict):
"""
Patch the loaded bundle config with a new handler logic to enable experiment tracking features.
Args:
parser: loaded config content to patch the handler.
settings: settings for the experiment tracking, should follow the pattern of default settings.
"""
for k, v in settings["configs"].items():
if k in settings["handlers_id"]:
engine = parser.get(settings["handlers_id"][k]["id"])
if engine is not None:
handlers = parser.get(settings["handlers_id"][k]["handlers"])
if handlers is None:
engine["train_handlers" if k == "trainer" else "val_handlers"] = [v]
else:
handlers.append(v)
elif k not in parser:
parser[k] = v
# save the executed config into file
default_name = f"config_{time.strftime('%Y%m%d_%H%M%S')}.json"
filepath = parser.get("execute_config", None)
if filepath is None:
if "output_dir" not in parser:
# if no "output_dir" in the bundle config, default to "<bundle root>/eval"
parser["output_dir"] = "$@bundle_root + '/eval'"
# experiment management tools can refer to this config item to track the config info
parser["execute_config"] = parser["output_dir"] + f" + '/{default_name}'"
filepath = os.path.join(parser.get_parsed_content("output_dir"), default_name)
Path(filepath).parent.mkdir(parents=True, exist_ok=True)
parser.export_config_file(parser.get(), filepath)
def run(
runner_id: Optional[Union[str, Sequence[str]]] = None,
meta_file: Optional[Union[str, Sequence[str]]] = None,
config_file: Optional[Union[str, Sequence[str]]] = None,
logging_file: Optional[str] = None,
tracking: Optional[Union[str, dict]] = None,
args_file: Optional[str] = None,
**override,
):
"""
Specify `meta_file` and `config_file` to run monai bundle components and workflows.
Typical usage examples:
.. code-block:: bash
# Execute this module as a CLI entry:
python -m monai.bundle run training --meta_file <meta path> --config_file <config path>
# Override config values at runtime by specifying the component id and its new value:
python -m monai.bundle run training --net#input_chns 1 ...
# Override config values with another config file `/path/to/another.json`:
python -m monai.bundle run evaluating --net %/path/to/another.json ...
# Override config values with part content of another config file:
python -m monai.bundle run training --net %/data/other.json#net_arg ...
# Set default args of `run` in a JSON / YAML file, help to record and simplify the command line.
# Other args still can override the default args at runtime:
python -m monai.bundle run --args_file "/workspace/data/args.json" --config_file <config path>
Args:
runner_id: ID name of the expected config expression to run, can also be a list of IDs to run in order.
meta_file: filepath of the metadata file, if it is a list of file paths, the content of them will be merged.
config_file: filepath of the config file, if `None`, must be provided in `args_file`.
if it is a list of file paths, the content of them will be merged.
logging_file: config file for `logging` module in the program, default to `None`. for more details:
https://docs.python.org/3/library/logging.config.html#logging.config.fileConfig.
tracking: enable the experiment tracking feature at runtime with optionally configurable and extensible.
if "mlflow", will add `MLFlowHandler` to the parsed bundle with default logging settings,
if other string, treat it as file path to load the logging settings, if `dict`,
treat it as logging settings, otherwise, use all the default settings.
will patch the target config content with `tracking handlers` and the top-level items of `configs`.
example of customized settings:
.. code-block:: python
tracking = {
"handlers_id": {
"trainer": {"id": "train#trainer", "handlers": "train#handlers"},
"validator": {"id": "evaluate#evaluator", "handlers": "evaluate#handlers"},
"evaluator": {"id": "evaluator", "handlers": "handlers"},
},
"configs": {
"tracking_uri": "<path>",
"experiment_name": "monai_experiment",
"run_name": None,
"is_not_rank0": (
"$torch.distributed.is_available() \
and torch.distributed.is_initialized() and torch.distributed.get_rank() > 0"
),
"trainer": {
"_target_": "MLFlowHandler",
"_disabled_": "@is_not_rank0",
"tracking_uri": "@tracking_uri",
"experiment_name": "@experiment_name",
"run_name": "@run_name",
"iteration_log": True,
"output_transform": "$monai.handlers.from_engine(['loss'], first=True)",
"close_on_complete": True,
},
"validator": {
"_target_": "MLFlowHandler",
"_disabled_": "@is_not_rank0",
"tracking_uri": "@tracking_uri",
"experiment_name": "@experiment_name",
"run_name": "@run_name",
"iteration_log": False,
},
"evaluator": {
"_target_": "MLFlowHandler",
"_disabled_": "@is_not_rank0",
"tracking_uri": "@tracking_uri",
"experiment_name": "@experiment_name",
"run_name": "@run_name",
"iteration_log": False,
"close_on_complete": True,
},
},
},
args_file: a JSON or YAML file to provide default values for `runner_id`, `meta_file`,
`config_file`, `logging`, and override pairs. so that the command line inputs can be simplified.
override: id-value pairs to override or add the corresponding config content.
e.g. ``--net#input_chns 42``.
"""
_args = _update_args(
args=args_file,
runner_id=runner_id,
meta_file=meta_file,
config_file=config_file,
logging_file=logging_file,
tracking=tracking,
**override,
)
if "config_file" not in _args:
warnings.warn("`config_file` not provided for 'monai.bundle run'.")
_log_input_summary(tag="run", args=_args)
config_file_, meta_file_, runner_id_, logging_file_, tracking_ = _pop_args(
_args, config_file=None, meta_file=None, runner_id="", logging_file=None, tracking=None
)
if logging_file_ is not None:
if not os.path.exists(logging_file_):
raise FileNotFoundError(f"can't find the logging config file: {logging_file_}.")
logger.info(f"set logging properties based on config: {logging_file_}.")
fileConfig(logging_file_, disable_existing_loggers=False)
parser = ConfigParser()
parser.read_config(f=config_file_)
if meta_file_ is not None:
parser.read_meta(f=meta_file_)
# the rest key-values in the _args are to override config content
parser.update(pairs=_args)
# set tracking configs for experiment management
if tracking_ is not None:
if isinstance(tracking_, str) and tracking_ in DEFAULT_EXP_MGMT_SETTINGS:
settings_ = DEFAULT_EXP_MGMT_SETTINGS[tracking_]
else:
settings_ = ConfigParser.load_config_files(tracking_)
patch_bundle_tracking(parser=parser, settings=settings_)
# resolve and execute the specified runner expressions in the config, return the results
return [parser.get_parsed_content(i, lazy=True, eval_expr=True, instantiate=True) for i in ensure_tuple(runner_id_)]
def verify_metadata(
meta_file: Optional[Union[str, Sequence[str]]] = None,
filepath: Optional[PathLike] = None,
create_dir: Optional[bool] = None,
hash_val: Optional[str] = None,
hash_type: Optional[str] = None,
args_file: Optional[str] = None,
**kwargs,
):
"""
Verify the provided `metadata` file based on the predefined `schema`.
`metadata` content must contain the `schema` field for the URL of schema file to download.
The schema standard follows: http://json-schema.org/.
Args:
meta_file: filepath of the metadata file to verify, if `None`, must be provided in `args_file`.
if it is a list of file paths, the content of them will be merged.
filepath: file path to store the downloaded schema.
create_dir: whether to create directories if not existing, default to `True`.
hash_val: if not None, define the hash value to verify the downloaded schema file.
hash_type: if not None, define the hash type to verify the downloaded schema file. Defaults to "md5".
args_file: a JSON or YAML file to provide default values for all the args in this function.
so that the command line inputs can be simplified.
kwargs: other arguments for `jsonschema.validate()`. for more details:
https://python-jsonschema.readthedocs.io/en/stable/validate/#jsonschema.validate.
"""
_args = _update_args(
args=args_file,
meta_file=meta_file,
filepath=filepath,
create_dir=create_dir,
hash_val=hash_val,
hash_type=hash_type,
**kwargs,
)
_log_input_summary(tag="verify_metadata", args=_args)
filepath_, meta_file_, create_dir_, hash_val_, hash_type_ = _pop_args(
_args, "filepath", "meta_file", create_dir=True, hash_val=None, hash_type="md5"
)
check_parent_dir(path=filepath_, create_dir=create_dir_)
metadata = ConfigParser.load_config_files(files=meta_file_)
url = metadata.get("schema")
if url is None:
raise ValueError("must provide the `schema` field in the metadata for the URL of schema file.")
download_url(url=url, filepath=filepath_, hash_val=hash_val_, hash_type=hash_type_, progress=True)
schema = ConfigParser.load_config_file(filepath=filepath_)
try:
# the rest key-values in the _args are for `validate` API
validate(instance=metadata, schema=schema, **_args)
except ValidationError as e: # pylint: disable=E0712
# as the error message is very long, only extract the key information
raise ValueError(
re.compile(r".*Failed validating", re.S).findall(str(e))[0] + f" against schema `{url}`."
) from e
logger.info("metadata is verified with no error.")
def verify_net_in_out(
net_id: Optional[str] = None,
meta_file: Optional[Union[str, Sequence[str]]] = None,
config_file: Optional[Union[str, Sequence[str]]] = None,
device: Optional[str] = None,
p: Optional[int] = None,
n: Optional[int] = None,
any: Optional[int] = None,
args_file: Optional[str] = None,
**override,
):
"""
Verify the input and output data shape and data type of network defined in the metadata.
Will test with fake Tensor data according to the required data shape in `metadata`.
Typical usage examples:
.. code-block:: bash
python -m monai.bundle verify_net_in_out network --meta_file <meta path> --config_file <config path>
Args:
net_id: ID name of the network component to verify, it must be `torch.nn.Module`.
meta_file: filepath of the metadata file to get network args, if `None`, must be provided in `args_file`.
if it is a list of file paths, the content of them will be merged.
config_file: filepath of the config file to get network definition, if `None`, must be provided in `args_file`.
if it is a list of file paths, the content of them will be merged.
device: target device to run the network forward computation, if None, prefer to "cuda" if existing.
p: power factor to generate fake data shape if dim of expected shape is "x**p", default to 1.
n: multiply factor to generate fake data shape if dim of expected shape is "x*n", default to 1.
any: specified size to generate fake data shape if dim of expected shape is "*", default to 1.
args_file: a JSON or YAML file to provide default values for `net_id`, `meta_file`, `config_file`,
`device`, `p`, `n`, `any`, and override pairs. so that the command line inputs can be simplified.
override: id-value pairs to override or add the corresponding config content.
e.g. ``--_meta#network_data_format#inputs#image#num_channels 3``.
"""
_args = _update_args(
args=args_file,
net_id=net_id,
meta_file=meta_file,
config_file=config_file,
device=device,
p=p,
n=n,
any=any,
**override,
)
_log_input_summary(tag="verify_net_in_out", args=_args)
config_file_, meta_file_, net_id_, device_, p_, n_, any_ = _pop_args(
_args, "config_file", "meta_file", net_id="", device="cuda:0" if is_available() else "cpu", p=1, n=1, any=1
)
parser = ConfigParser()
parser.read_config(f=config_file_)
parser.read_meta(f=meta_file_)
# the rest key-values in the _args are to override config content
for k, v in _args.items():
parser[k] = v
try:
key: str = net_id_ # mark the full id when KeyError
net = parser.get_parsed_content(key).to(device_)
key = "_meta_#network_data_format#inputs#image#num_channels"
input_channels = parser[key]
key = "_meta_#network_data_format#inputs#image#spatial_shape"
input_spatial_shape = tuple(parser[key])
key = "_meta_#network_data_format#inputs#image#dtype"
input_dtype = get_equivalent_dtype(parser[key], torch.Tensor)
key = "_meta_#network_data_format#outputs#pred#num_channels"
output_channels = parser[key]
key = "_meta_#network_data_format#outputs#pred#dtype"
output_dtype = get_equivalent_dtype(parser[key], torch.Tensor)
except KeyError as e:
raise KeyError(f"Failed to verify due to missing expected key in the config: {key}.") from e
net.eval()
with torch.no_grad():
spatial_shape = _get_fake_spatial_shape(input_spatial_shape, p=p_, n=n_, any=any_)
test_data = torch.rand(*(1, input_channels, *spatial_shape), dtype=input_dtype, device=device_)
if input_dtype == torch.float16:
# fp16 can only be executed in gpu mode
net.to("cuda")
from torch.cuda.amp import autocast
with autocast():
output = net(test_data.cuda())
net.to(device_)
else:
output = net(test_data)
if output.shape[1] != output_channels:
raise ValueError(f"output channel number `{output.shape[1]}` doesn't match: `{output_channels}`.")
if output.dtype != output_dtype:
raise ValueError(f"dtype of output data `{output.dtype}` doesn't match: {output_dtype}.")
logger.info("data shape of network is verified with no error.")
def ckpt_export(
net_id: Optional[str] = None,
filepath: Optional[PathLike] = None,
ckpt_file: Optional[str] = None,
meta_file: Optional[Union[str, Sequence[str]]] = None,
config_file: Optional[Union[str, Sequence[str]]] = None,
key_in_ckpt: Optional[str] = None,
args_file: Optional[str] = None,
**override,
):
"""
Export the model checkpoint to the given filepath with metadata and config included as JSON files.
Typical usage examples:
.. code-block:: bash
python -m monai.bundle ckpt_export network --filepath <export path> --ckpt_file <checkpoint path> ...
Args:
net_id: ID name of the network component in the config, it must be `torch.nn.Module`.
filepath: filepath to export, if filename has no extension it becomes `.ts`.
ckpt_file: filepath of the model checkpoint to load.
meta_file: filepath of the metadata file, if it is a list of file paths, the content of them will be merged.
config_file: filepath of the config file to save in TorchScript model and extract network information,
the saved key in the TorchScript model is the config filename without extension, and the saved config
value is always serialized in JSON format no matter the original file format is JSON or YAML.
it can be a single file or a list of files. if `None`, must be provided in `args_file`.
key_in_ckpt: for nested checkpoint like `{"model": XXX, "optimizer": XXX, ...}`, specify the key of model
weights. if not nested checkpoint, no need to set.
args_file: a JSON or YAML file to provide default values for `meta_file`, `config_file`,
`net_id` and override pairs. so that the command line inputs can be simplified.
override: id-value pairs to override or add the corresponding config content.
e.g. ``--_meta#network_data_format#inputs#image#num_channels 3``.
"""
_args = _update_args(
args=args_file,
net_id=net_id,
filepath=filepath,
meta_file=meta_file,
config_file=config_file,
ckpt_file=ckpt_file,
key_in_ckpt=key_in_ckpt,
**override,
)
_log_input_summary(tag="ckpt_export", args=_args)
filepath_, ckpt_file_, config_file_, net_id_, meta_file_, key_in_ckpt_ = _pop_args(
_args, "filepath", "ckpt_file", "config_file", net_id="", meta_file=None, key_in_ckpt=""
)
parser = ConfigParser()
parser.read_config(f=config_file_)
if meta_file_ is not None:
parser.read_meta(f=meta_file_)
# the rest key-values in the _args are to override config content
for k, v in _args.items():
parser[k] = v
net = parser.get_parsed_content(net_id_)
if has_ignite:
# here we use ignite Checkpoint to support nested weights and be compatible with MONAI CheckpointSaver
Checkpoint.load_objects(to_load={key_in_ckpt_: net}, checkpoint=ckpt_file_)
else:
ckpt = torch.load(ckpt_file_)
copy_model_state(dst=net, src=ckpt if key_in_ckpt_ == "" else ckpt[key_in_ckpt_])
# convert to TorchScript model and save with metadata, config content
net = convert_to_torchscript(model=net)
extra_files: Dict = {}
for i in ensure_tuple(config_file_):
# split the filename and directory
filename = os.path.basename(i)
# remove extension
filename, _ = os.path.splitext(filename)
# because all files are stored as JSON their name parts without extension must be unique
if filename in extra_files:
raise ValueError(f"Filename part '{filename}' is given multiple times in config file list.")
# the file may be JSON or YAML but will get loaded and dumped out again as JSON
extra_files[filename] = json.dumps(ConfigParser.load_config_file(i)).encode()
# add .json extension to all extra files which are always encoded as JSON
extra_files = {k + ".json": v for k, v in extra_files.items()}
save_net_with_metadata(
jit_obj=net,
filename_prefix_or_stream=filepath_,
include_config_vals=False,
append_timestamp=False,
meta_values=parser.get().pop("_meta_", None),
more_extra_files=extra_files,
)
logger.info(f"exported to TorchScript file: {filepath_}.")