Coverage for src/bioimageio/core/backends/tensorflow_backend.py: 55%
101 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
3from collections.abc import Sequence
4from pathlib import Path
5from typing import Any
7import numpy as np
8import tensorflow as tf
9from loguru import logger
10from numpy.typing import NDArray
12from bioimageio.spec.model import AnyModelDescr, v0_4, v0_5
14from .._model_adapter import LocalModelAdapter
15from ..io import ensure_unzipped
18class TensorflowModelAdapter(LocalModelAdapter[None, Any]):
19 """Adapter for TensorFlow 1 models"""
21 weight_format = "tensorflow_saved_model_bundle"
23 def __init__(
24 self,
25 model_description: v0_4.ModelDescr | v0_5.ModelDescr,
26 devices: Sequence[str] | None = None,
27 ):
29 if model_description.weights.tensorflow_saved_model_bundle is None:
30 raise ValueError("No `tensorflow_saved_model_bundle` weights found")
32 if isinstance(model_description, v0_4.ModelDescr):
33 self._weight_src = (
34 model_description.weights.tensorflow_saved_model_bundle.source
35 )
36 else:
37 self._weight_src = model_description.weights.tensorflow_saved_model_bundle
39 self._graph = None
40 self._io_names: tuple[list[str], list[str]] | None = None
41 super().__init__(model_description=model_description, devices=devices)
43 def _parse_devices(self, devices: Sequence[str] | None) -> tuple[None]:
44 if devices is not None:
45 logger.warning(
46 f"Device management is not implemented for tensorflow yet, ignoring the devices {devices}"
47 )
48 return (None,)
50 def _init_model_on_device(self, device: str | None) -> Any:
52 # TODO: check how to load tf weights without unzipping
53 weight_file = ensure_unzipped(
54 self._weight_src, Path("bioimageio_unzipped_tf_weights")
55 )
57 # TODO read from spec
58 tag = ( # pyright: ignore[reportUnknownVariableType]
59 tf.saved_model.tag_constants.SERVING
60 )
61 signature_key = ( # pyright: ignore[reportUnknownVariableType]
62 tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
63 )
65 self._graph = tf.Graph()
66 with self._graph.as_default():
67 sess = tf.Session(graph=self._graph) # pyright: ignore[reportUnknownVariableType]
68 # load the model and the signature
69 graph_def = tf.saved_model.loader.load( # pyright: ignore[reportUnknownVariableType]
70 sess, [tag], str(weight_file)
71 )
72 signature = ( # pyright: ignore[reportUnknownVariableType]
73 graph_def.signature_def
74 )
76 # get the tensors into the graph
77 in_names = [ # pyright: ignore[reportUnknownVariableType]
78 signature[signature_key].inputs[key].name for key in self._input_ids
79 ]
80 out_names = [ # pyright: ignore[reportUnknownVariableType]
81 signature[signature_key].outputs[key].name for key in self._output_ids
82 ]
83 self._io_names = (in_names, out_names)
85 return sess # pyright: ignore[reportUnknownVariableType]
87 def _forward_impl(
88 self, device: None, model: Any, input_arrays: Sequence[NDArray[Any] | None]
89 ):
90 assert self._io_names is not None
91 assert self._graph is not None
93 in_names, out_names = self._io_names
94 in_tf_tensors = [self._graph.get_tensor_by_name(name) for name in in_names]
95 out_tf_tensors = [self._graph.get_tensor_by_name(name) for name in out_names]
97 # run prediction
98 res = model.run(
99 dict(zip(out_names, out_tf_tensors)),
100 dict(zip(in_tf_tensors, input_arrays)),
101 )
102 # from dict to list of tensors
103 res = [res[out] for out in out_names]
105 return res
107 def _cleanup_pre_model_deletion(self, device: str | None, model: Any) -> None:
108 return
110 def _cleanup_post_model_deletion(self, device: str | None) -> None:
111 return
114class KerasModelAdapter(LocalModelAdapter[None, Any]):
115 def __init__(
116 self,
117 model_description: v0_4.ModelDescr | v0_5.ModelDescr,
118 devices: Sequence[str] | None = None,
119 ):
120 if model_description.weights.tensorflow_saved_model_bundle is None: 120 ↛ 121line 120 didn't jump to line 121 because the condition on line 120 was never true
121 raise ValueError("No `tensorflow_saved_model_bundle` weights found")
123 if isinstance(model_description, v0_4.ModelDescr): 123 ↛ 128line 123 didn't jump to line 128 because the condition on line 123 was always true
124 self._weight_src = (
125 model_description.weights.tensorflow_saved_model_bundle.source
126 )
127 else:
128 self._weight_src = model_description.weights.tensorflow_saved_model_bundle
130 super().__init__(model_description=model_description, devices=devices)
132 def _parse_devices(self, devices: Sequence[str] | None) -> tuple[None]:
133 if devices is not None:
134 logger.warning(
135 f"Device management is not implemented for tensorflow yet, ignoring the devices {devices}"
136 )
137 return (None,)
139 def _init_model_on_device(self, device: None) -> Any:
140 # TODO: check how to load tf weights without unzipping
141 weight_file = str(
142 ensure_unzipped(self._weight_src, Path("bioimageio_unzipped_tf_weights"))
143 )
145 try:
146 tfsm_layer = tf.keras.layers.TFSMLayer( # pyright: ignore[reportUnknownVariableType]
147 weight_file,
148 call_endpoint="serve",
149 )
150 except Exception as e:
151 try:
152 tfsm_layer = tf.keras.layers.TFSMLayer( # pyright: ignore[reportUnknownVariableType]
153 weight_file, call_endpoint="serving_default"
154 )
155 except Exception as ee:
156 logger.opt(exception=ee).info(
157 "keras.layers.TFSMLayer error for alternative call_endpoint='serving_default'"
158 )
159 raise e
161 return tfsm_layer # pyright: ignore[reportUnknownVariableType]
163 def _forward_impl( # pyright: ignore[reportUnknownParameterType]
164 self, device: None, model: Any, input_arrays: Sequence[NDArray[Any] | None]
165 ):
166 assert tf is not None
167 tf_tensor = [
168 None if ipt is None else tf.convert_to_tensor(ipt) for ipt in input_arrays
169 ]
170 result = model(*tf_tensor)
171 assert isinstance(result, dict)
173 # TODO: Use RDF's `outputs[i].id` here
174 result = list( # pyright: ignore[reportUnknownVariableType]
175 result.values() # pyright: ignore[reportUnknownArgumentType]
176 )
178 return [ # pyright: ignore[reportUnknownVariableType]
179 (None if r is None else r if isinstance(r, np.ndarray) else r.numpy())
180 for r in result # pyright: ignore[reportUnknownVariableType]
181 ]
183 def _cleanup_pre_model_deletion(self, device: str | None, model: Any) -> None:
184 return
186 def _cleanup_post_model_deletion(self, device: str | None) -> None:
187 return
190def create_tf_model_adapter(
191 model_description: AnyModelDescr, devices: Sequence[str] | None = None
192):
193 tf_version = v0_5.Version(tf.__version__) # type: ignore[reportUnknownVariableType]
194 weights = model_description.weights.tensorflow_saved_model_bundle
195 if weights is None: 195 ↛ 196line 195 didn't jump to line 196 because the condition on line 195 was never true
196 raise ValueError("No `tensorflow_saved_model_bundle` weights found")
198 model_tf_version = weights.tensorflow_version
199 if model_tf_version is None: 199 ↛ 200line 199 didn't jump to line 200 because the condition on line 199 was never true
200 logger.warning(
201 "The model does not specify the tensorflow version."
202 + f"Cannot check if it is compatible with intalled tensorflow {tf_version}."
203 )
204 elif model_tf_version > tf_version: 204 ↛ 205line 204 didn't jump to line 205 because the condition on line 204 was never true
205 logger.warning(
206 f"The model specifies a newer tensorflow version than installed: {model_tf_version} > {tf_version}."
207 )
208 elif (model_tf_version.major, model_tf_version.minor) != ( 208 ↛ 217line 208 didn't jump to line 217 because the condition on line 208 was always true
209 tf_version.major,
210 tf_version.minor,
211 ):
212 logger.warning(
213 "The tensorflow version specified by the model does not match the installed: "
214 + f"{model_tf_version} != {tf_version}."
215 )
217 if tf_version.major <= 1: 217 ↛ 218line 217 didn't jump to line 218 because the condition on line 217 was never true
218 return TensorflowModelAdapter(model_description, devices=devices)
219 else:
220 return KerasModelAdapter(model_description, devices=devices)