Predict Responses Using ONNX Model Predict Block
This example shows how to use the ONNX Model Predict block for prediction in Simulink®. The block accepts input data and returns the predicted responses using a trained machine learning model that is executed in Python®. MATLAB® supports the reference implementation of Python, often called CPython. If you use a Mac or Linux® platform, you already have Python installed. If you use Windows®, you need to install a distribution, such as those found at https://www.python.org/downloads/. For more information, see Configure Your System to Use Python. Your MATLAB Python environment must have the onnxruntime
module installed.
The ONNX Model Predict block requires a pretrained ONNX™ model that you saved in Python. This example provides the saved model onnxmodel.onnx
, which is a neural network binary classification model trained on half of the ionosphere
radar signal data set from the UCI Machine Learning Repository [1]. The example also provides the Python files onnxScaler.pkl
, onnxmodel.py
, and onnxpreprocessor.py
. All of the supporting Python files were saved using onnxruntime
version 1.15.0.
Open Provided Simulink Model
This example provides the Simulink model slexONNXPredictExample.slx
, which includes the ONNX Model Predict block. You can open the Simulink model or create a new model as described in the next section.
Open the Simulink model slexONNXPredictExample.slx
.
open_system("slexONNXPredictExample");
When you open the Simulink model, the software runs the code in the PreLoadFcn
callback function before loading the Simulink model. The PreLoadFcn
callback function of slexONNXPredictExample
includes code to check if your workspace contains the modelInput
variable for the trained model. If the workspace does not contain the variable, PreLoadFcn
loads the sample data and creates an input signal for the Simulink model. To view the callback function, in the Setup section on the Modeling tab, click Model Settings and select Model Properties. Then, on the Callbacks tab, select the PreLoadFcn
callback function in the Model callbacks pane.
Create Simulink Model
To create a new Simulink model, open the Blank Model template and add the ONNX Model Predict block from the Statistics and Machine Learning Toolbox™ library.
Add an Inport block and an Outport block, and connect them to the ONNX Model Predict block.
Double-click the ONNX Model Predict block to open the Block Parameters dialog box. Enter onnxmodel.onnx
in the Path to ONNX model file text box. Enter CPUExecutionProvider
in the Execution Providers text box.
On the Pre/Post-processing tab, enter onnxpreprocessor.py
in the Path to Python file defining preprocess() text box. Click OK.
The ONNX Model Predict block expects observations containing 34 predictor values, because the Python model was trained using a data set with 34 predictor variables. Double-click the Inport block, and set Port dimensions to 34 on the Signal Attributes tab.
To specify that the output signals have the same length as the input signal, set Sample Time to 1 on the Execution tab.
Clear the Interpolate data check box and click OK.
Load the ionosphere
data set. This data set has 34 predictors (X
) and 351 binary responses (Y
) for radar returns, either bad ("b
") or good ("g
").
load ionosphere
Create an appropriate structure array for the input data. For more information, see Control How Models Load Input Data (Simulink).
modelInput.time = (1:size(X,1))' - 1; modelInput.signals.values = X; modelInput.signals.dimensions = size(X,2);
Import the signal data from the workspace:
On the Modeling tab, click Model Settings to open the Configuration Parameters dialog box.
On the left of the dialog box, click Data Import/Export. Then select the Input check box and enter
modelInput
in the adjacent text box.On the left, click Solver. Under Simulation time, set Stop time to
size(X,1)-1
. Under Solver selection, set Type toFixed-step
, and set Solver todiscrete (no continuous states)
. Click OK.
For more details, see Load Signal Data for Simulation (Simulink).
Save the model as slexONNXPredictExample.slx
in Simulink.
Simulate Simulink Model
Simulate the Simulink model to predict responses for the input observations. You might receive a warning message if your Python installation uses an onnxruntime
version prior to 1.15.0.
simOut=sim("slexONNXPredictExample");
When the Inport block detects input data, it places the data in the ONNX Model Predict block. The ONNX Model Predict block converts the input data to the Python or NumPy datatype specified in the Python Datatype column on the Input tab of the Block Parameters dialog box. The block passes the data to Python, where the software preprocesses the data using the function defined in onnxpreprocessor.py
and then sends the data to the Python model. The Python model returns the predicted response for the observation. You can use the Simulation Data Inspector (Simulink) to view the logged data of the Outport block.
Visualize Model Predictions
Plot the predicted response for each observation.
Resp = round(squeeze(simOut.yout.getElement(1).Values.Data)); plot(Resp,LineStyle="none",Marker="*") xlabel("Observation") ylabel("Predicted Response")
The bad ("b
") and good ("g
") predicted response values are indicated as 0 and 1, respectively.
References
[1] Lichman, M. UCI Machine Learning Repository, Irvine, CA: University of California, School of Information and Computer Science, 2013. https://archive.ics.uci.edu.
See Also
TensorFlow Model Predict | PyTorch Model Predict | ONNX Model Predict | Custom Python Model Predict