Coverage for src/bioimageio/core/weight_converters/keras_to_tensorflow.py: 0%

73 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 09:46 +0000

1from __future__ import annotations 

2 

3import os 

4import shutil 

5import sys 

6from pathlib import Path 

7from tempfile import TemporaryDirectory 

8from typing import Any, no_type_check 

9from zipfile import ZipFile 

10 

11import tensorflow 

12 

13from bioimageio.spec._internal.version_type import Version 

14from bioimageio.spec.common import ZipPath 

15from bioimageio.spec.model.v0_5 import ( 

16 InputTensorDescr, 

17 ModelDescr, 

18 OutputTensorDescr, 

19 TensorflowSavedModelBundleWeightsDescr, 

20) 

21 

22from .. import __version__ 

23from ..io import ensure_unzipped 

24 

25try: 

26 # try to build the tf model with the keras import from tensorflow 

27 from tensorflow import keras # type: ignore 

28except Exception: 

29 # if the above fails try to export with the standalone keras 

30 import keras # pyright: ignore[reportMissingTypeStubs] 

31 

32 

33def convert( 

34 model_descr: ModelDescr, output_path: Path 

35) -> TensorflowSavedModelBundleWeightsDescr: 

36 """ 

37 Convert model weights from the 'keras_hdf5' format to the 'tensorflow_saved_model_bundle' format. 

38 

39 This method handles the conversion of Keras HDF5 model weights into a TensorFlow SavedModel bundle, 

40 which is the recommended format for deploying TensorFlow models. The method supports both TensorFlow 1.x 

41 and 2.x versions, with appropriate checks to ensure compatibility. 

42 

43 Adapted from: 

44 https://github.com/deepimagej/pydeepimagej/blob/5aaf0e71f9b04df591d5ca596f0af633a7e024f5/pydeepimagej/yaml/create_config.py 

45 

46 Args: 

47 model_descr: 

48 The bioimage.io model description containing the model's metadata and weights. 

49 output_path: 

50 Path with .zip suffix (.zip is appended otherwise) to which a zip archive 

51 with the TensorFlow SavedModel bundle will be saved. 

52 Raises: 

53 ValueError: 

54 - If the specified `output_path` already exists. 

55 - If the Keras HDF5 weights are missing in the model description. 

56 RuntimeError: 

57 If there is a mismatch between the TensorFlow version used by the model and the version installed. 

58 NotImplementedError: 

59 If the model has multiple inputs or outputs and TensorFlow 1.x is being used. 

60 

61 Returns: 

62 A descriptor object containing information about the converted TensorFlow SavedModel bundle. 

63 """ 

64 tf_major_ver = int(tensorflow.__version__.split(".")[0]) # pyright: ignore[reportUnknownArgumentType] 

65 

66 if output_path.suffix != ".zip": 

67 output_path = output_path.with_suffix("") 

68 

69 if output_path.exists(): 

70 raise ValueError(f"The ouptut directory at {output_path} must not exist.") 

71 

72 if model_descr.weights.keras_hdf5 is None: 

73 raise ValueError("Missing Keras Hdf5 weights to convert from.") 

74 

75 weight_spec = model_descr.weights.keras_hdf5 

76 weight_reader = weight_spec.get_reader() 

77 

78 if weight_spec.tensorflow_version: 

79 model_tf_major_ver = int(weight_spec.tensorflow_version.major) 

80 if model_tf_major_ver != tf_major_ver: 

81 raise RuntimeError( 

82 f"Tensorflow major versions of model {model_tf_major_ver} is not {tf_major_ver}" 

83 ) 

84 

85 td_kwargs: dict[str, Any] = ( 

86 {"ignore_cleanup_errors": True} if sys.version_info >= (3, 10) else {} 

87 ) 

88 with TemporaryDirectory(**td_kwargs) as temp_dir: 

89 local_weights = ensure_unzipped( 

90 weight_reader, Path(temp_dir) / "bioimageio_unzipped_tf_weights" 

91 ) 

92 if tf_major_ver == 1: 

93 if len(model_descr.inputs) != 1 or len(model_descr.outputs) != 1: 

94 raise NotImplementedError( 

95 "Weight conversion for models with multiple inputs or outputs is not yet implemented." 

96 ) 

97 

98 input_name = str( 

99 d.id 

100 if isinstance((d := model_descr.inputs[0]), InputTensorDescr) 

101 else d.name 

102 ) 

103 output_name = str( 

104 d.id 

105 if isinstance((d := model_descr.outputs[0]), OutputTensorDescr) 

106 else d.name 

107 ) 

108 return _convert_tf1( 

109 ensure_unzipped(local_weights, Path("bioimageio_unzipped_tf_weights")), 

110 output_path, 

111 input_name, 

112 output_name, 

113 ) 

114 else: 

115 return _convert_tf2(local_weights, output_path) 

116 

117 

118def _convert_tf2( 

119 keras_weight_path: Path | ZipPath, output_path: Path 

120) -> TensorflowSavedModelBundleWeightsDescr: 

121 model = keras.models.load_model(keras_weight_path) # type: ignore 

122 model.export(output_path) # type: ignore 

123 

124 output_path = _zip_model_bundle(output_path) 

125 print("TensorFlow model exported to", output_path) 

126 

127 return TensorflowSavedModelBundleWeightsDescr( 

128 source=output_path.absolute(), 

129 parent="keras_hdf5", 

130 tensorflow_version=Version(tensorflow.__version__), # pyright: ignore[reportUnknownArgumentType] 

131 comment=f"Converted with bioimageio.core {__version__}.", 

132 ) 

133 

134 

135# adapted from 

136# https://github.com/deepimagej/pydeepimagej/blob/master/pydeepimagej/yaml/create_config.py#L236 

137def _convert_tf1( 

138 keras_weight_path: Path, 

139 output_path: Path, 

140 input_name: str, 

141 output_name: str, 

142) -> TensorflowSavedModelBundleWeightsDescr: 

143 @no_type_check 

144 def build_tf_model(): 

145 keras_model = keras.models.load_model(keras_weight_path) 

146 builder = tensorflow.saved_model.builder.SavedModelBuilder(output_path) 

147 signature = tensorflow.saved_model.signature_def_utils.predict_signature_def( 

148 inputs={input_name: keras_model.input}, 

149 outputs={output_name: keras_model.output}, 

150 ) 

151 

152 signature_def_map = { 

153 tensorflow.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: ( 

154 signature 

155 ) 

156 } 

157 

158 builder.add_meta_graph_and_variables( 

159 keras.backend.get_session(), 

160 [tensorflow.saved_model.tag_constants.SERVING], 

161 signature_def_map=signature_def_map, 

162 ) 

163 builder.save() 

164 

165 build_tf_model() 

166 

167 output_path = _zip_model_bundle(output_path) 

168 print("TensorFlow model exported to", output_path) 

169 

170 return TensorflowSavedModelBundleWeightsDescr( 

171 source=output_path.absolute(), 

172 parent="keras_hdf5", 

173 tensorflow_version=Version(tensorflow.__version__), # pyright: ignore[reportUnknownArgumentType] 

174 comment=f"Converted with bioimageio.core {__version__}.", 

175 ) 

176 

177 

178def _zip_model_bundle(model_bundle_folder: Path): 

179 zipped_model_bundle = model_bundle_folder.with_suffix(".zip") 

180 

181 with ZipFile(zipped_model_bundle, "w") as zip_obj: 

182 for root, _, files in os.walk(model_bundle_folder): 

183 for filename in files: 

184 src = os.path.join(root, filename) 

185 zip_obj.write(src, os.path.relpath(src, model_bundle_folder)) 

186 

187 try: 

188 shutil.rmtree(model_bundle_folder) 

189 except Exception: 

190 print("TensorFlow bundled model was not removed after compression") 

191 

192 return zipped_model_bundle