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

Function save_net_with_metadata

monai/data/torchscript_utils.py:28–100  ·  view source on GitHub ↗

Save the JIT object (script or trace produced object) `jit_obj` to the given file or stream with metadata included as a JSON file. The Torchscript format is a zip file which can contain extra file data which is used here as a mechanism for storing metadata about the network being saved.

(
    jit_obj: torch.nn.Module,
    filename_prefix_or_stream: str | IO[Any],
    include_config_vals: bool = True,
    append_timestamp: bool = False,
    meta_values: Mapping[str, Any] | None = None,
    more_extra_files: Mapping[str, bytes] | None = None,
)

Source from the content-addressed store, hash-verified

26
27
28def save_net_with_metadata(
29 jit_obj: torch.nn.Module,
30 filename_prefix_or_stream: str | IO[Any],
31 include_config_vals: bool = True,
32 append_timestamp: bool = False,
33 meta_values: Mapping[str, Any] | None = None,
34 more_extra_files: Mapping[str, bytes] | None = None,
35) -> None:
36 """
37 Save the JIT object (script or trace produced object) `jit_obj` to the given file or stream with metadata
38 included as a JSON file. The Torchscript format is a zip file which can contain extra file data which is used
39 here as a mechanism for storing metadata about the network being saved. The data in `meta_values` should be
40 compatible with conversion to JSON using the standard library function `dumps`. The intent is this metadata will
41 include information about the network applicable to some use case, such as describing the input and output format,
42 a network name and version, a plain language description of what the network does, and other relevant scientific
43 information. Clients can use this information to determine automatically how to use the network, and users can
44 read what the network does and keep track of versions.
45
46 Examples::
47
48 net = torch.jit.script(monai.networks.nets.UNet(2, 1, 1, [8, 16], [2]))
49
50 meta = {
51 "name": "Test UNet",
52 "used_for": "demonstration purposes",
53 "input_dims": 2,
54 "output_dims": 2
55 }
56
57 # save the Torchscript bundle with the above dictionary stored as an extra file
58 save_net_with_metadata(m, "test", meta_values=meta)
59
60 # load the network back, `loaded_meta` has same data as `meta` plus version information
61 loaded_net, loaded_meta, _ = load_net_with_metadata("test.ts")
62
63
64 Args:
65 jit_obj: object to save, should be generated by `script` or `trace`.
66 filename_prefix_or_stream: filename or file-like stream object, if filename has no extension it becomes `.ts`.
67 include_config_vals: if True, MONAI, Pytorch, and Numpy versions are included in metadata.
68 append_timestamp: if True, a timestamp for "now" is appended to the file's name before the extension.
69 meta_values: metadata values to store with the object, not limited just to keys in `JITMetadataKeys`.
70 more_extra_files: other extra file data items to include in bundle, see `_extra_files` of `torch.jit.save`.
71 """
72
73 now = datetime.datetime.now()
74 metadict = {}
75
76 if include_config_vals:
77 metadict.update(get_config_values())
78 metadict[JITMetadataKeys.TIMESTAMP.value] = now.astimezone().isoformat()
79
80 if meta_values is not None:
81 metadict.update(meta_values)
82
83 json_data = json.dumps(metadict)
84
85 extra_files = {METADATA_FILENAME: json_data.encode()}

Calls 4

get_config_valuesFunction · 0.90
saveMethod · 0.80
updateMethod · 0.45
encodeMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…