MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / ckpt_export

Function ckpt_export

monai/bundle/scripts.py:1437–1568  ·  view source on GitHub ↗

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 --ckpt_file ... Args: net_id: ID

(
    net_id: str | None = None,
    filepath: PathLike | None = None,
    ckpt_file: str | None = None,
    meta_file: str | Sequence[str] | None = None,
    config_file: str | Sequence[str] | None = None,
    key_in_ckpt: str | None = None,
    use_trace: bool | None = None,
    input_shape: Sequence[int] | None = None,
    args_file: str | None = None,
    converter_kwargs: Mapping | None = None,
    **override: Any,
)

Source from the content-addressed store, hash-verified

1435
1436
1437def ckpt_export(
1438 net_id: str | None = None,
1439 filepath: PathLike | None = None,
1440 ckpt_file: str | None = None,
1441 meta_file: str | Sequence[str] | None = None,
1442 config_file: str | Sequence[str] | None = None,
1443 key_in_ckpt: str | None = None,
1444 use_trace: bool | None = None,
1445 input_shape: Sequence[int] | None = None,
1446 args_file: str | None = None,
1447 converter_kwargs: Mapping | None = None,
1448 **override: Any,
1449) -> None:
1450 """
1451 Export the model checkpoint to the given filepath with metadata and config included as JSON files.
1452
1453 Typical usage examples:
1454
1455 .. code-block:: bash
1456
1457 python -m monai.bundle ckpt_export network --filepath <export path> --ckpt_file <checkpoint path> ...
1458
1459 Args:
1460 net_id: ID name of the network component in the config, it must be `torch.nn.Module`.
1461 Default to "network_def".
1462 filepath: filepath to export, if filename has no extension it becomes `.ts`.
1463 Default to "models/model.ts" under "os.getcwd()" if `bundle_root` is not specified.
1464 ckpt_file: filepath of the model checkpoint to load.
1465 Default to "models/model.pt" under "os.getcwd()" if `bundle_root` is not specified.
1466 meta_file: filepath of the metadata file, if it is a list of file paths, the content of them will be merged.
1467 Default to "configs/metadata.json" under "os.getcwd()" if `bundle_root` is not specified.
1468 config_file: filepath of the config file to save in TorchScript model and extract network information,
1469 the saved key in the TorchScript model is the config filename without extension, and the saved config
1470 value is always serialized in JSON format no matter the original file format is JSON or YAML.
1471 it can be a single file or a list of files. if `None`, must be provided in `args_file`.
1472 key_in_ckpt: for nested checkpoint like `{"model": XXX, "optimizer": XXX, ...}`, specify the key of model
1473 weights. if not nested checkpoint, no need to set.
1474 use_trace: whether using `torch.jit.trace` to convert the PyTorch model to TorchScript model.
1475 input_shape: a shape used to generate the random input of the network, when converting the model to a
1476 TorchScript model. Should be a list like [N, C, H, W] or [N, C, H, W, D]. If not given, will try to
1477 parse from the `metadata` config.
1478 args_file: a JSON or YAML file to provide default values for all the parameters of this function, so that
1479 the command line inputs can be simplified.
1480 converter_kwargs: extra arguments that are needed by `convert_to_torchscript`, except ones that already exist
1481 in the input parameters.
1482 override: id-value pairs to override or add the corresponding config content.
1483 e.g. ``--_meta#network_data_format#inputs#image#num_channels 3``.
1484
1485 """
1486 _args = update_kwargs(
1487 args=args_file,
1488 net_id=net_id,
1489 filepath=filepath,
1490 meta_file=meta_file,
1491 config_file=config_file,
1492 ckpt_file=ckpt_file,
1493 key_in_ckpt=key_in_ckpt,
1494 use_trace=use_trace,

Callers

nothing calls this directly

Calls 11

read_configMethod · 0.95
read_metaMethod · 0.95
get_parsed_contentMethod · 0.95
ConfigParserClass · 0.90
update_kwargsFunction · 0.85
_log_input_summaryFunction · 0.85
_pop_argsFunction · 0.85
_get_fake_input_shapeFunction · 0.85
_exportFunction · 0.85
getMethod · 0.80
updateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…