MCPcopy Index your code
hub / github.com/Project-MONAI/MONAI / _export

Function _export

monai/bundle/scripts.py:1259–1320  ·  view source on GitHub ↗

Export a model defined in the parser to a new one specified by the converter. Args: converter: a callable object that takes a torch.nn.module and kwargs as input and converts the module to another type. saver: a callable object that accepts the converted model t

(
    converter: Callable,
    saver: Callable,
    parser: ConfigParser,
    net_id: str,
    filepath: str,
    ckpt_file: str,
    config_file: str,
    key_in_ckpt: str,
    **kwargs: Any,
)

Source from the content-addressed store, hash-verified

1257
1258
1259def _export(
1260 converter: Callable,
1261 saver: Callable,
1262 parser: ConfigParser,
1263 net_id: str,
1264 filepath: str,
1265 ckpt_file: str,
1266 config_file: str,
1267 key_in_ckpt: str,
1268 **kwargs: Any,
1269) -> None:
1270 """
1271 Export a model defined in the parser to a new one specified by the converter.
1272
1273 Args:
1274 converter: a callable object that takes a torch.nn.module and kwargs as input and
1275 converts the module to another type.
1276 saver: a callable object that accepts the converted model to save, a filepath to save to, meta values
1277 (extracted from the parser), and a dictionary of extra JSON files (name -> contents) as input.
1278 parser: a ConfigParser of the bundle to be converted.
1279 net_id: ID name of the network component in the parser, it must be `torch.nn.Module`.
1280 filepath: filepath to export, if filename has no extension, it becomes `.ts`.
1281 ckpt_file: filepath of the model checkpoint to load.
1282 config_file: filepath of the config file to save in the converted model,the saved key in the converted
1283 model is the config filename without extension, and the saved config value is always serialized in
1284 JSON format no matter the original file format is JSON or YAML. it can be a single file or a list
1285 of files.
1286 key_in_ckpt: for nested checkpoint like `{"model": XXX, "optimizer": XXX, ...}`, specify the key of model
1287 weights. if not nested checkpoint, no need to set.
1288 kwargs: key arguments for the converter.
1289
1290 """
1291 net = parser.get_parsed_content(net_id)
1292 if has_ignite:
1293 # here we use ignite Checkpoint to support nested weights and be compatible with MONAI CheckpointSaver
1294 Checkpoint.load_objects(to_load={key_in_ckpt: net}, checkpoint=ckpt_file)
1295 else:
1296 ckpt = torch.load(ckpt_file, weights_only=True)
1297 copy_model_state(dst=net, src=ckpt if key_in_ckpt == "" else ckpt[key_in_ckpt])
1298
1299 # Use the given converter to convert a model and save with metadata, config content
1300 net = converter(model=net, **kwargs)
1301
1302 extra_files: dict = {}
1303 for i in ensure_tuple(config_file):
1304 # split the filename and directory
1305 filename = os.path.basename(i)
1306 # remove extension
1307 filename, _ = os.path.splitext(filename)
1308 # because all files are stored as JSON their name parts without extension must be unique
1309 if filename in extra_files:
1310 raise ValueError(f"Filename part '{filename}' is given multiple times in config file list.")
1311 # the file may be JSON or YAML but will get loaded and dumped out again as JSON
1312 extra_files[filename] = json.dumps(ConfigParser.load_config_file(i)).encode()
1313
1314 # add .json extension to all extra files which are always encoded as JSON
1315 extra_files = {k + ".json": v for k, v in extra_files.items()}
1316

Callers 3

onnx_exportFunction · 0.85
ckpt_exportFunction · 0.85
trt_exportFunction · 0.85

Calls 9

copy_model_stateFunction · 0.90
ensure_tupleFunction · 0.90
get_parsed_contentMethod · 0.80
loadMethod · 0.80
load_config_fileMethod · 0.80
popMethod · 0.80
getMethod · 0.80
infoMethod · 0.80
encodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…