11import os
22import shutil
33from pathlib import Path
4+ from tempfile import TemporaryDirectory
45from typing import Union , no_type_check
56from zipfile import ZipFile
67
78import tensorflow # pyright: ignore[reportMissingTypeStubs]
89
9- from bioimageio .spec ._internal .io import download
1010from bioimageio .spec ._internal .version_type import Version
1111from bioimageio .spec .common import ZipPath
1212from bioimageio .spec .model .v0_5 import (
@@ -70,7 +70,7 @@ def convert(
7070 raise ValueError ("Missing Keras Hdf5 weights to convert from." )
7171
7272 weight_spec = model_descr .weights .keras_hdf5
73- weight_path = download ( weight_spec .source ). path
73+ weight_reader = weight_spec .get_reader ()
7474
7575 if weight_spec .tensorflow_version :
7676 model_tf_major_ver = int (weight_spec .tensorflow_version .major )
@@ -79,30 +79,34 @@ def convert(
7979 f"Tensorflow major versions of model { model_tf_major_ver } is not { tf_major_ver } "
8080 )
8181
82- if tf_major_ver == 1 :
83- if len (model_descr .inputs ) != 1 or len (model_descr .outputs ) != 1 :
84- raise NotImplementedError (
85- "Weight conversion for models with multiple inputs or outputs is not yet implemented."
86- )
87-
88- input_name = str (
89- d .id
90- if isinstance ((d := model_descr .inputs [0 ]), InputTensorDescr )
91- else d .name
92- )
93- output_name = str (
94- d .id
95- if isinstance ((d := model_descr .outputs [0 ]), OutputTensorDescr )
96- else d .name
97- )
98- return _convert_tf1 (
99- ensure_unzipped (weight_path , Path ("bioimageio_unzipped_tf_weights" )),
100- output_path ,
101- input_name ,
102- output_name ,
82+ with TemporaryDirectory (ignore_cleanup_errors = True ) as temp_dir :
83+ local_weights = ensure_unzipped (
84+ weight_reader , Path (temp_dir ) / "bioimageio_unzipped_tf_weights"
10385 )
104- else :
105- return _convert_tf2 (weight_path , output_path )
86+ if tf_major_ver == 1 :
87+ if len (model_descr .inputs ) != 1 or len (model_descr .outputs ) != 1 :
88+ raise NotImplementedError (
89+ "Weight conversion for models with multiple inputs or outputs is not yet implemented."
90+ )
91+
92+ input_name = str (
93+ d .id
94+ if isinstance ((d := model_descr .inputs [0 ]), InputTensorDescr )
95+ else d .name
96+ )
97+ output_name = str (
98+ d .id
99+ if isinstance ((d := model_descr .outputs [0 ]), OutputTensorDescr )
100+ else d .name
101+ )
102+ return _convert_tf1 (
103+ ensure_unzipped (local_weights , Path ("bioimageio_unzipped_tf_weights" )),
104+ output_path ,
105+ input_name ,
106+ output_name ,
107+ )
108+ else :
109+ return _convert_tf2 (local_weights , output_path )
106110
107111
108112def _convert_tf2 (
0 commit comments