Coverage for src/bioimageio/core/weight_converters/_add_weights.py: 64%
80 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
1from __future__ import annotations
3import traceback
5from loguru import logger
6from pydantic import DirectoryPath
8from bioimageio.spec import (
9 InvalidDescr,
10 load_model_description,
11 save_bioimageio_package_as_folder,
12)
13from bioimageio.spec.model.v0_5 import ModelDescr, WeightsFormat
15from .._resource_tests import load_description_and_test
18def add_weights(
19 model_descr: ModelDescr,
20 *,
21 output_path: DirectoryPath,
22 source_format: WeightsFormat | None = None,
23 target_format: WeightsFormat | None = None,
24 verbose: bool = False,
25 allow_tracing: bool = True,
26) -> ModelDescr | InvalidDescr:
27 """Convert model weights to other formats and add them to the model description
29 Args:
30 output_path: Path to save updated model package to.
31 source_format: convert from a specific weights format.
32 Default: choose automatically from any available.
33 target_format: convert to a specific weights format.
34 Default: attempt to convert to any missing format.
35 verbose: log more (error) output
36 allow_tracing: allow conversion to torchscript by tracing if scripting fails.
38 Returns:
39 A (potentially invalid) model copy stored at `output_path` with added weights if any conversion was possible.
41 """
42 if not isinstance(model_descr, ModelDescr): 42 ↛ 43line 42 didn't jump to line 43 because the condition on line 42 was never true
43 if model_descr.type == "model" and not isinstance(model_descr, InvalidDescr):
44 raise TypeError(
45 f"Model format {model_descr.format} is not supported, please update"
46 + f" model to format {ModelDescr.implemented_format_version} first."
47 )
49 raise TypeError(type(model_descr))
51 # save model to local folder
52 output_path = save_bioimageio_package_as_folder(
53 model_descr, output_path=output_path
54 )
55 # reload from local folder to make sure we do not edit the given model
56 model_descr = load_model_description(
57 output_path, perform_io_checks=False, format_version="latest"
58 )
60 if source_format is None:
61 available = set(model_descr.weights.available_formats)
62 else:
63 available = {source_format}
65 if target_format is None:
66 missing = set(model_descr.weights.missing_formats)
67 else:
68 missing = {target_format}
70 originally_missing = set(missing)
72 if "pytorch_state_dict" in available and "torchscript" in missing:
73 logger.info(
74 "Attempting to convert 'pytorch_state_dict' weights to 'torchscript'."
75 )
76 from .pytorch_to_torchscript import convert
78 try:
79 torchscript_weights_path = output_path / "weights_torchscript.pt"
80 model_descr.weights.torchscript = convert(
81 model_descr,
82 output_path=torchscript_weights_path,
83 use_tracing=False,
84 )
85 except Exception as e:
86 if verbose:
87 traceback.print_exception(type(e), e, e.__traceback__)
89 logger.error(e)
90 else:
91 available.add("torchscript")
92 missing.discard("torchscript")
94 if allow_tracing and "pytorch_state_dict" in available and "torchscript" in missing: 94 ↛ 95line 94 didn't jump to line 95 because the condition on line 94 was never true
95 logger.info(
96 "Attempting to convert 'pytorch_state_dict' weights to 'torchscript' by tracing."
97 )
98 from .pytorch_to_torchscript import convert
100 try:
101 torchscript_weights_path = output_path / "weights_torchscript_traced.pt"
103 model_descr.weights.torchscript = convert(
104 model_descr,
105 output_path=torchscript_weights_path,
106 use_tracing=True,
107 )
108 except Exception as e:
109 if verbose:
110 traceback.print_exception(type(e), e, e.__traceback__)
112 logger.error(e)
113 else:
114 available.add("torchscript")
115 missing.discard("torchscript")
117 if "pytorch_state_dict" in available and "onnx" in missing:
118 logger.info("Attempting to convert 'pytorch_state_dict' weights to 'onnx'.")
119 from .pytorch_to_onnx import convert
121 try:
122 onnx_weights_path = output_path / "weights.onnx"
124 model_descr.weights.onnx = convert(
125 model_descr,
126 output_path=onnx_weights_path,
127 verbose=verbose,
128 )
129 except Exception as e:
130 if verbose:
131 traceback.print_exception(type(e), e, e.__traceback__)
133 logger.error(e)
134 else:
135 available.add("onnx")
136 missing.discard("onnx")
138 if "torchscript" in available and "onnx" in missing:
139 logger.info("Attempting to convert 'torchscript' weights to 'onnx'.")
140 from .torchscript_to_onnx import convert
142 try:
143 onnx_weights_path = output_path / "weights.onnx"
144 model_descr.weights.onnx = convert(
145 model_descr,
146 output_path=onnx_weights_path,
147 verbose=verbose,
148 )
149 except Exception as e:
150 if verbose:
151 traceback.print_exception(type(e), e, e.__traceback__)
153 logger.error(e)
154 else:
155 available.add("onnx")
156 missing.discard("onnx")
158 if missing:
159 logger.warning(
160 f"Converting from any of the available weights formats {available} to any"
161 + f" of {missing} failed or is not yet implemented. Please create an issue"
162 + " at https://github.com/bioimage-io/core-bioimage-io-python/issues/new/choose"
163 + " if you would like bioimageio.core to support a particular conversion."
164 )
166 if originally_missing == missing:
167 logger.warning("failed to add any converted weights")
168 return model_descr
169 else:
170 logger.info("added weights formats {}", originally_missing - missing)
171 # resave model with updated rdf.yaml
172 _ = save_bioimageio_package_as_folder(model_descr, output_path=output_path)
173 tested_model_descr = load_description_and_test(
174 model_descr, format_version="latest", expected_type="model"
175 )
176 if not isinstance(tested_model_descr, ModelDescr): 176 ↛ 177line 176 didn't jump to line 177 because the condition on line 176 was never true
177 logger.error(
178 f"The updated model description at {output_path} did not pass testing."
179 )
181 return tested_model_descr