Coverage for src/bioimageio/core/backends/__init__.py: 61%

69 statements  

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

1from __future__ import annotations 

2 

3from collections.abc import Sequence 

4 

5from exceptiongroup import ExceptionGroup 

6from typing_extensions import assert_never 

7 

8from bioimageio.spec.model import v0_4, v0_5 

9 

10from ..common import SupportedWeightsFormat 

11 

12# Known weight formats in order of priority 

13# First match wins 

14DEFAULT_WEIGHT_FORMAT_PRIORITY_ORDER: tuple[SupportedWeightsFormat, ...] = ( 

15 "pytorch_state_dict", 

16 "tensorflow_saved_model_bundle", 

17 "torchscript", 

18 "onnx", 

19 "keras_v3", 

20 "keras_hdf5", 

21) 

22 

23 

24def create_model_adapter( 

25 model_description: v0_4.ModelDescr | v0_5.ModelDescr, 

26 *, 

27 devices: Sequence[str] | None = None, 

28 weight_format_priority_order: Sequence[SupportedWeightsFormat] | None = None, 

29): 

30 """Creates model adapter for `model_descritption`""" 

31 if not isinstance(model_description, (v0_4.ModelDescr, v0_5.ModelDescr)): 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true

32 raise TypeError( 

33 f"expected v0_4.ModelDescr or v0_5.ModelDescr, but got {type(model_description)}" 

34 ) 

35 

36 weights = model_description.weights 

37 errors: list[Exception] = [] 

38 weight_format_priority_order = ( 

39 DEFAULT_WEIGHT_FORMAT_PRIORITY_ORDER 

40 if weight_format_priority_order is None 

41 else weight_format_priority_order 

42 ) 

43 # limit weight formats to the ones present 

44 weight_format_priority_order_present: Sequence[SupportedWeightsFormat] = [ 

45 w for w in weight_format_priority_order if getattr(weights, w, None) is not None 

46 ] 

47 if not weight_format_priority_order_present: 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true

48 raise ValueError( 

49 f"None of the specified weight formats ({weight_format_priority_order}) is present ({weight_format_priority_order_present})" 

50 ) 

51 

52 for wf in weight_format_priority_order_present: 52 ↛ 113line 52 didn't jump to line 113 because the loop on line 52 didn't complete

53 if wf == "pytorch_state_dict": 

54 assert weights.pytorch_state_dict is not None 

55 try: 

56 from .pytorch_backend import PytorchModelAdapter 

57 

58 return PytorchModelAdapter(model_description, devices=devices) 

59 except Exception as e: 

60 errors.append(e) 

61 elif wf == "tensorflow_saved_model_bundle": 

62 assert weights.tensorflow_saved_model_bundle is not None 

63 try: 

64 from .tensorflow_backend import create_tf_model_adapter 

65 

66 return create_tf_model_adapter(model_description, devices=devices) 

67 except Exception as e: 

68 errors.append(e) 

69 elif wf == "onnx": 

70 assert weights.onnx is not None 

71 try: 

72 from .onnx_backend import ONNXModelAdapter 

73 

74 return ONNXModelAdapter(model_description, devices=devices) 

75 except Exception as e: 

76 errors.append(e) 

77 elif wf == "torchscript": 

78 assert weights.torchscript is not None 

79 try: 

80 from .torchscript_backend import TorchscriptModelAdapter 

81 

82 return TorchscriptModelAdapter(model_description, devices=devices) 

83 except Exception as e: 

84 errors.append(e) 

85 elif wf == "keras_hdf5": 85 ↛ 99line 85 didn't jump to line 99 because the condition on line 85 was always true

86 assert weights.keras_hdf5 is not None 

87 # keras can either be installed as a separate package or used as part of tensorflow 

88 # we try to first import the keras model adapter using the separate package and, 

89 # if it is not available, try to load the one using tf 

90 try: 

91 try: 

92 from .keras_backend import KerasModelAdapter 

93 except Exception: 

94 from .tensorflow_backend import KerasModelAdapter 

95 

96 return KerasModelAdapter(model_description, devices=devices) 

97 except Exception as e: 

98 errors.append(e) 

99 elif wf == "keras_v3": 

100 assert not isinstance(weights, v0_4.WeightsDescr), ( 

101 "keras_v3 weights not supported for v0.4 specs" 

102 ) 

103 assert weights.keras_v3 is not None 

104 try: 

105 from .keras_backend import KerasModelAdapter 

106 

107 return KerasModelAdapter(model_description, devices=devices) 

108 except Exception as e: 

109 errors.append(e) 

110 else: 

111 assert_never(wf) 

112 

113 assert errors 

114 if len(weight_format_priority_order) == 1: 

115 assert len(errors) == 1 

116 raise errors[0] 

117 

118 else: 

119 msg = ( 

120 "None of the weight format specific model adapters could be created" 

121 + " in this environment." 

122 ) 

123 raise ExceptionGroup(msg, errors)