Skip to content

1.ONNX Model

Rafael CF Sousa edited this page Mar 30, 2022 · 4 revisions

1. Open Neural Network eXchange

In the first project, you receive a template in python of LeNet Model. You will complement the model, generate the ONNX graph and use Netron tool to display and print the model to pdf.

In order to reproduce the experiments of this class, the following packages have to be installed in your system:

  • keras
  • tensorflow
  • tf2onnx
  • onnx
  • onnxruntime
  • opencv-python
  • tflite2onnx
  • notebook
  • netron

To install them, you have to use the pip command as the example shown below:

pip install --user keras tensorflow [...]

The first notebook (train_lenet.ipynb) refers to a Python code that trains LeNet-5 model on Keras using the MNIST dataset and then converts the trained model from Keras (h5) to ONNX using the package tf2onnx. Notice that during this process the ONNX model is printed out in your jupyter-notebook; thus, you can verify the structure of it besides checking the nodes/weights that compose the ONNX model.

For a better understanding of each one of the parameters (e.g., input, output) and also the attributes that compose the operator, refer to the following page to get more information: https://github.com/onnx/onnx/blob/main/docs/Operators.md

With the model already trained and converted to ONNX, you can now execute it using, e.g., onnxruntime (see inference.ipynb). The steps taken in this notebook are the following: 1) load an image from MNIST dataset; 2) pre-process it using OpenCV; 3) create an InferenceSession instance from onnxruntime; and 4) execute the inference.

You can also convert an already trained model from a framework (e.g., Tensorflow Lite) to an ONNX model. Refer to the example below to see how it works:

wget $ https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/upload_20180427/inception_v3_2018_04_27.tgz
tar xvf inception_v3_2018_04_27.tgz 
tflite2onnx inception_v3.tflite inception_v3.onnx

You can visualize the models using netron. You just have to type the following commands:

netron lenet.onnx

Easy, isn't it? :-)

Homework

The goal of this homework is to reproduce exactly the steps described at the following link: https://github.com/MO436-MC934/notebooks/wiki/1.ONNX-Model At the end, take a screenshot of the LeNet model generated (onnx) using Netron and then send this screenshoot to us through Google Classroom.

EXTRA: Try to execute ResNet thru onnxruntime. You can download ResNet from ONNX Model Zoo (https://github.com/onnx/models/tree/main/vision/classification/resnet). Notice that what differentiate this step from the actual homework is the way the input is preprocessed.

Clone this wiki locally