MySQL HeatWave User Guide  /  ...  /  Importing ONNX Models

3.10.2 Importing ONNX Models

As of MySQL 8.0.31, HeatWave AutoML supports the upload of pre-trained models in ONNX (Open Neural Network Exchange) format to the model catalog. You can load them using the ML_MODEL_IMPORT routine. After import, all the HeatWave AutoML routines can be used with the ONNX models.

Models in ONNX format (.onnx) cannot be loaded directly into a MySQL table. They require string serialization and conversion to Base64 encoding before you use the ML_MODEL_IMPORT routine.

Follow these steps to import a model in ONNX format to the model catalog:

  1. Convert the .onnx file containing the model to Base64 encoding and carry out string serialization. You can do this with the Python base64 module. In this example, the file iris.onnx is converted:

    $> python -c "import onnx; import base64; 
    $> open('iris_base64.onnx', 'wb').write(
    $> base64.b64encode(onnx.load('iris.onnx').SerializeToString()))"
  2. Connect to the DB system for the HeatWave Cluster as a client, and create a temporary table to upload the model. For example:

    mysql> CREATE TEMPORARY TABLE onnx_temp (onnx_string LONGTEXT);
  3. Use a LOAD DATA INFILE statement to load the preprocessed .onnx file into the temporary table. For example:

    mysql> LOAD DATA INFILE 'iris_base64.onnx' 
              INTO TABLE onnx_temp 
              CHARACTER SET binary 
              FIELDS TERMINATED BY '\t' 
              LINES TERMINATED BY '\r' (onnx_string);
  4. Select the uploaded model from the temporary table into a session variable. For example:

    mysql> SELECT onnx_string FROM onnx_temp INTO @onnx_encode;
  5. Call the ML_MODEL_IMPORT routine to import the ONNX model into the model catalog. For example:

    mysql> CALL sys.ML_MODEL_IMPORT(@onnx_encode, NULL, 'iris_onnx');

    In this example, the model handle is iris_onnx, and the optional model metadata is omitted (NULL). For details of the metadata you can add for imported ONNX models, see ML_MODEL_IMPORT.

After import, all the HeatWave AutoML routines can be used with the ONNX model. It is in the model catalog and can be managed in the same ways as a model created by HeatWave AutoML.