ONNXConversion#

class ONNXConversion(model, model_name=None)[source]#

Bases: object

Transform ONNX model. The transformation returns a CasADi expression of the model and can be used e.g. in the do_mpc.model.Model class.

Warning

The feature is experimental and currently only has a limited number of supported operations. All supported operations can be found in the ONNXOperations class.

Other known limitations are listed at the end of this page.

How to use:

  1. Create an ONNX model in your favorite framework (e.g. TensorFlow, PyTorch, Keras, ONNX).

  2. Initiate the ONNXConversion class with the ONNX model as input.

  3. Obtain information about model inputs and ouputs by printing the class instance.

  4. Call the ONNXConversion.convert() method, passing with keyword arguments the external inputs of the model. The inputs are propagated through the model and all node expressions are created.

  5. Query the class instance with the respective layer or node name to obtain the CasADi expression of the respective layer or node.

Example:

We start with a simple Tensorflow (with Keras) model:

model_input = keras.Input(shape=(3), name='input')
hidden_layer = keras.layers.Dense(5, activation='relu', name='hidden')(model_input)
output_layer = keras.layers.Dense(1, activation='linear', name='output')(hidden_layer)

keras_model = keras.Model(inputs=model_input, outputs=output_layer)

We then proceed to export the model in the ONNX format, using the tf2onnx package:

model_input_signature = [
    tf.TensorSpec(np.array((1, 3)), name='input'),
]
output_path = os.path.join('models', 'model.onnx')

onnx_model, _ = tf2onnx.convert.from_keras(keras_model,
    output_path=output_path,
    input_signature=model_input_signature
)

We can now use the ONNX model (either directly or loaded from disc) to initialize the ONNXConversion class:

casadi_converter = do_mpc.sysid.ONNXConversion(onnx_model)

Obtain information about the model inputs and outputs by calling print(casadi_converter), yielding, in this example:

ONNX2Casadi model 'casadi_model'
----------------------------------
Call 'convert' by supplying the inputs with respective name and shape below.
Input shape of 'input' is (1, 3)
----------------------------------
Query the instance with the following keywords to obtain the CasADi expression of the respective layer or graph operation node:
- 'input'
- 'model_4/hidden/MatMul:0'
- 'model_4/hidden/Relu:0'
- 'output'

Call the ONNXConversion.convert() method, considering the name and shape of the inputs:

# Inputs can be numpy arrays
casadi_converter.convert(input=np.ones((1,3)))

# or CasADi expressions
x = casadi.SX.sym('x',1,3)
casadi_converter.convert(input=x)

Query the instance with the respective layer or node name to obtain the CasADi expression of the respective layer or node:

print(casadi_converter['output'])
Parameters:
  • model (ModelProto) – An ONNX model.

  • model_name (Optional[str]) – Name of the model

__getitem__(key)[source]#

Enables the output of the CasADi expression of a specific layer or graph operation node.

To learn about possible keywords, it is recommended to print the instance of the class:

print(converter)
Parameters:

key (str) – Name of the layer of the ONNX graph.

Methods#

convert#

convert(self, verbose=False, **kwargs)#

Evaluate ONNX model with inputs of type casadi.SX, casadi.MX, casadi.DM or numpy.ndarray.

The keyword arguments of this method refer to the names of the inputs of the model. If these names are unknown, print the instance of the class to obtain the names.

Convert does not return anything. The converted model is stored in the instance of the class. To obtain the results of the conversion at an arbitrary internal layer, query the instance with the respective layer name. Layer names can be obtained by printing the instance of the class.

Parameters:
  • verbose – If True, prints the conversion progress.

  • **kwargs – Keyword arguments of the method refer to the names of the inputs of the model. The values of the keyword arguments are the inputs of the model and can be of type casadi.SX, casadi.MX, casadi.DM or numpy.ndarray.

Return type:

None