ONNX simple sample

From RidgeRun Developer Connection
Revision as of 10:06, 20 November 2019 by Maumontero (talk | contribs)
Jump to: navigation, search

Introduction

On this page, you are going to find the steps to install ONXX and ONXXRuntime and run a simple C/C++ sample on Linux. This wiki page tries to describe the importance of ONNX models and how to use it. The goal is to provide you some examples.

Installing ONNX

You can then install ONNX from PyPi with the following command:

sudo pip install onnx

You can also build and install ONNX locally from source code:

git clone https://github.com/onnx/onnx.git
cd onnx
git submodule update --init --recursive
python setup.py install

Installing ONNXRuntime

This guide build the baseline CPU version of ONNXRuntime form source, for build it from the source code use the following commands:

git clone --recursive https://github.com/Microsoft/onnxruntime -b v1.0.0
cd onnxruntime

Before install onnxruntime you need to install CMake 3.13 or higher.

sudo -H pip3 install cmake

After install CMake run the following command for build onnxruntime:

./build.sh --config RelWithDebInfo --build_shared_lib --parallel

Finally, install it:

cd build/Linux/RelWithDebInfo
sudo make install

Because this test is on Linux you need to copy the .so file to general lib path:

cp libonnxruntime.so /usr/lib/x86_64-linux-gnu/

Sample

This guide is for using an ONNXRuntime C/C++ code on linux, for that reason only the SqueezeNet samples are be build it.

Build

First go to the path with the C/C++ code samples.

cd onnxruntime/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/

After that build the code:

g++ -o Capi_sample C_Api_Sample.cpp -I $PATHTOONNXRUNTIMESESSION (#CHOOSE THE APPROPRIATE PATH TO onnxruntime/include/onnxruntime/core/session) -lonnxruntime -std=c++14

Run

Finally just run the code:

./Capi_sample

Running this sample you will get the following output:

Using Onnxruntime C API
Number of inputs = 1
Input 0 : name=data_0
Input 0 : type=1
Input 0 : num_dims=4
Input 0 : dim 0=1
Input 0 : dim 1=3
Input 0 : dim 2=224
Input 0 : dim 3=224
Score for class [0] =  0.000045
Score for class [1] =  0.003846
Score for class [2] =  0.000125
Score for class [3] =  0.001180
Score for class [4] =  0.001317
Done!