11
11
from keras .src .utils import io_utils
12
12
13
13
14
- def export_onnx (model , filepath , verbose = None , input_signature = None , ** kwargs ):
14
+ def export_onnx (
15
+ model ,
16
+ filepath ,
17
+ verbose = None ,
18
+ input_signature = None ,
19
+ opset_version = None ,
20
+ ** kwargs ,
21
+ ):
15
22
"""Export the model as a ONNX artifact for inference.
16
23
17
24
This method lets you export a model to a lightweight ONNX artifact
@@ -31,6 +38,9 @@ def export_onnx(model, filepath, verbose=None, input_signature=None, **kwargs):
31
38
inputs. Can be a structure of `keras.InputSpec`, `tf.TensorSpec`,
32
39
`backend.KerasTensor`, or backend tensor. If not provided, it will
33
40
be automatically computed. Defaults to `None`.
41
+ opset_version: Optional. An integer value that specifies the ONNX opset
42
+ version. If not provided, the default version for the backend will
43
+ be used. Defaults to `None`.
34
44
**kwargs: Additional keyword arguments.
35
45
36
46
**Note:** This feature is currently supported only with TensorFlow, JAX and
@@ -82,7 +92,10 @@ def export_onnx(model, filepath, verbose=None, input_signature=None, **kwargs):
82
92
# Use `tf2onnx` to convert the `decorated_fn` to the ONNX format.
83
93
patch_tf2onnx () # TODO: Remove this once `tf2onnx` supports numpy 2.
84
94
tf2onnx .convert .from_function (
85
- decorated_fn , input_signature , output_path = filepath
95
+ decorated_fn ,
96
+ input_signature ,
97
+ opset = opset_version ,
98
+ output_path = filepath ,
86
99
)
87
100
88
101
elif backend .backend () == "torch" :
@@ -126,7 +139,11 @@ def export_onnx(model, filepath, verbose=None, input_signature=None, **kwargs):
126
139
try :
127
140
# Try the TorchDynamo-based ONNX exporter first.
128
141
onnx_program = torch .onnx .export (
129
- model , sample_inputs , verbose = actual_verbose , dynamo = True
142
+ model ,
143
+ sample_inputs ,
144
+ verbose = actual_verbose ,
145
+ opset_version = opset_version ,
146
+ dynamo = True ,
130
147
)
131
148
if hasattr (onnx_program , "optimize" ):
132
149
onnx_program .optimize () # Only supported by torch>=2.6.0.
@@ -139,7 +156,11 @@ def export_onnx(model, filepath, verbose=None, input_signature=None, **kwargs):
139
156
140
157
# Fall back to the TorchScript-based ONNX exporter.
141
158
torch .onnx .export (
142
- model , sample_inputs , filepath , verbose = actual_verbose
159
+ model ,
160
+ sample_inputs ,
161
+ filepath ,
162
+ verbose = actual_verbose ,
163
+ opset_version = opset_version ,
143
164
)
144
165
else :
145
166
raise NotImplementedError (
0 commit comments