From cb2fd3b3ba6a90398f785228c576f281419adb7f Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Thu, 12 Oct 2023 17:19:29 +0800 Subject: [PATCH 1/5] fix #7123 Signed-off-by: KumoLiu --- monai/bundle/scripts.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 4607ef65b7..3f85ef387b 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -1255,6 +1255,7 @@ def ckpt_export( e.g. ``--_meta#network_data_format#inputs#image#num_channels 3``. """ + bundle_root = override.pop("bundle_root", os.getcwd()) _args = update_kwargs( args=args_file, net_id=net_id, @@ -1266,6 +1267,7 @@ def ckpt_export( use_trace=use_trace, input_shape=input_shape, converter_kwargs=converter_kwargs, + bundle_root=bundle_root, **override, ) _log_input_summary(tag="ckpt_export", args=_args) @@ -1273,7 +1275,6 @@ def ckpt_export( config_file_, filepath_, ckpt_file_, - bundle_root_, net_id_, meta_file_, key_in_ckpt_, @@ -1285,7 +1286,6 @@ def ckpt_export( "config_file", filepath=None, ckpt_file=None, - bundle_root=os.getcwd(), net_id=None, meta_file=None, key_in_ckpt="", @@ -1297,9 +1297,15 @@ def ckpt_export( parser = ConfigParser() parser.read_config(f=config_file_) - meta_file_ = os.path.join(bundle_root_, "configs", "metadata.json") if meta_file_ is None else meta_file_ - filepath_ = os.path.join(bundle_root_, "models", "model.ts") if filepath_ is None else filepath_ - ckpt_file_ = os.path.join(bundle_root_, "models", "model.pt") if ckpt_file_ is None else ckpt_file_ + # the rest key-values in the _args are to override config content + for k, v in _args.items(): + if k == "bundle_root": + print(k) + parser[k] = v + + meta_file_ = os.path.join(bundle_root, "configs", "metadata.json") if meta_file_ is None else meta_file_ + filepath_ = os.path.join(bundle_root, "models", "model.ts") if filepath_ is None else filepath_ + ckpt_file_ = os.path.join(bundle_root, "models", "model.pt") if ckpt_file_ is None else ckpt_file_ if not os.path.exists(ckpt_file_): raise FileNotFoundError(f'Checkpoint file "{ckpt_file_}" not found, please specify it in argument "ckpt_file".') if os.path.exists(meta_file_): @@ -1313,10 +1319,6 @@ def ckpt_export( f'Network definition "{net_id_}" cannot be found in "{config_file_}", specify name with argument "net_id".' ) from e - # the rest key-values in the _args are to override config content - for k, v in _args.items(): - parser[k] = v - # When export through torch.jit.trace without providing input_shape, will try to parse one from the parser. if (not input_shape_) and use_trace: input_shape_ = _get_fake_input_shape(parser=parser) From e07b4e008a74072d941da8c63e8120e5f03650da Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Thu, 12 Oct 2023 17:27:17 +0800 Subject: [PATCH 2/5] minor fix Signed-off-by: KumoLiu --- monai/bundle/scripts.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 3f85ef387b..4317001ea7 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -1299,8 +1299,6 @@ def ckpt_export( parser.read_config(f=config_file_) # the rest key-values in the _args are to override config content for k, v in _args.items(): - if k == "bundle_root": - print(k) parser[k] = v meta_file_ = os.path.join(bundle_root, "configs", "metadata.json") if meta_file_ is None else meta_file_ From b8a39b2e3468eadc87afe13c487ba1ede4f6285f Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Thu, 12 Oct 2023 17:56:59 +0800 Subject: [PATCH 3/5] address comments Signed-off-by: KumoLiu --- monai/bundle/scripts.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 4317001ea7..6bac1835de 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -1255,7 +1255,6 @@ def ckpt_export( e.g. ``--_meta#network_data_format#inputs#image#num_channels 3``. """ - bundle_root = override.pop("bundle_root", os.getcwd()) _args = update_kwargs( args=args_file, net_id=net_id, @@ -1267,7 +1266,6 @@ def ckpt_export( use_trace=use_trace, input_shape=input_shape, converter_kwargs=converter_kwargs, - bundle_root=bundle_root, **override, ) _log_input_summary(tag="ckpt_export", args=_args) @@ -1294,20 +1292,25 @@ def ckpt_export( converter_kwargs={}, ) - parser = ConfigParser() + if "bundle_root" in _args: + bundle_root = _args["bundle_root"] + else: + bundle_root = os.getcwd() + parser = ConfigParser() parser.read_config(f=config_file_) + meta_file_ = os.path.join(bundle_root, "configs", "metadata.json") if meta_file_ is None else meta_file_ + if os.path.exists(meta_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 - meta_file_ = os.path.join(bundle_root, "configs", "metadata.json") if meta_file_ is None else meta_file_ filepath_ = os.path.join(bundle_root, "models", "model.ts") if filepath_ is None else filepath_ ckpt_file_ = os.path.join(bundle_root, "models", "model.pt") if ckpt_file_ is None else ckpt_file_ if not os.path.exists(ckpt_file_): raise FileNotFoundError(f'Checkpoint file "{ckpt_file_}" not found, please specify it in argument "ckpt_file".') - if os.path.exists(meta_file_): - parser.read_meta(f=meta_file_) net_id_ = "network_def" if net_id_ is None else net_id_ try: From bcd0bdfe503fd8aa7032bcdb39fab8230a016d13 Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Thu, 12 Oct 2023 17:58:39 +0800 Subject: [PATCH 4/5] minor fix Signed-off-by: KumoLiu --- monai/bundle/scripts.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 6bac1835de..4b22a94c21 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -1291,11 +1291,7 @@ def ckpt_export( input_shape=None, converter_kwargs={}, ) - - if "bundle_root" in _args: - bundle_root = _args["bundle_root"] - else: - bundle_root = os.getcwd() + bundle_root = _args["bundle_root"] if "bundle_root" in _args else os.getcwd() parser = ConfigParser() parser.read_config(f=config_file_) From 258af6951f675bf51de347dc2b07118dda7599ac Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:59:58 +0800 Subject: [PATCH 5/5] Update monai/bundle/scripts.py Co-authored-by: Wenqi Li <831580+wyli@users.noreply.github.com> Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> --- monai/bundle/scripts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 4b22a94c21..264d3fbf0e 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -1291,7 +1291,7 @@ def ckpt_export( input_shape=None, converter_kwargs={}, ) - bundle_root = _args["bundle_root"] if "bundle_root" in _args else os.getcwd() + bundle_root = _args.get("bundle_root", os.getcwd()) parser = ConfigParser() parser.read_config(f=config_file_)