Set opset to embed in ONNX

What is ONNX opset?

ONNX files have the ability to specify opset as the version when the file was created. The operators that can be used and their functions (operator properties) change depending on the value of opset. Unless otherwise specified, it is treated as the latest opset.

Some Deep Learning tools include a routine to check the opset value in the ONNX file and see if it can handle it. If it is rejected by the confirmation routine there, you need to lower the opset and recreate the ONNX file.

https://github.com/onnx/onnx/blob/master/docs/Operators.md

If you look here, you can check the version supported by each operator. For example, you can see that there are versions 1, 6 and 13 in ABS, and bfloat 16 is added as a type that can be used in ABS in 13.

Create an ONNX file by specifying opset in Keras

How to specify the opset depends on the converter, but in most cases you can specify the opset version as an argument. This is the specification method when using keras2onnx.

import tensorflow
import onnx
import keras2onnx

model_file = 'foo.h5'

#Set opset and save
keras_model = tensorflow.keras.models.load_model(model_file)
onnx_model = keras2onnx.convert_keras(keras_model, 'foo1', target_opset=9)

By specifying target_opset, an ONNX file with opset = 9 will be generated.

Modify ONNX files directly

When you know that the operator you are using is clearly compatible with the old operator (if you are not using the new features) and you want to specify the opset value in the ONNX file just to pass the converter check. There is also. In this case, you can edit the ONNX file directly to change the opset.

As a continuation of the above source

onnx_model.opset_import[0].version = 10
with open("op10.onnx", 'wb') as f:
    f.write(onnx_model.SerializeToString())

A value is specified directly for opset_import [0] .version in the onnx file. 10 is used as the opset when saved.

How to check opset

The quickest way is to print onnx directly as a string with print (onnx_model) and the value will be displayed as opset_import towards the end.

opset_import {
  domain: ""
  version: 10
}

opset is set to 10.

Recommended Posts

Set opset to embed in ONNX
To set default encoding to utf-8 in python
I want to embed Matplotlib in PySimpleGUI
How to embed a variable in a python string
How to set browser location in Headless Chrome
How to set up public key authentication in ssh
I want to embed a variable in a Python string
The easiest way to set up Last-Modified in Flask
Set spinning_friction in pybullet
Set ulimit in supervisor
Convert keras-yolo3 to onnx
Set the form DateField to type = date in Django
Set constructor keyworded arguments to mock attributes in Python
How to embed multiple embeds in one message with Discord.py
How to set the html class attribute in Django's forms.py
Dynamically set env.hosts in fabric
Covector to think in function
To flush stdout in Python
Embed matplotlib graphs in Tkinter
Login to website in Python
Set VS Code to PyCharm.
Speech to speech in python [text to speech]
Set BigIntegerField to auto increment
Methods available in set type
How to develop in Python
Set up Nunjucks in Node.js
Embed wav files in Jupyter
Post to Slack in Python
Set python test in jenkins
How to set the output resolution for each keyframe in Blender
fd_set in select (2) drops when trying to set fd greater than 1024