Difference between revisions of "ONNX simple sample"

From RidgeRun Developer Connection
Jump to: navigation, search
Line 1: Line 1:
 
__TOC__
 
__TOC__
 
= Introduction =
 
= Introduction =
On this page, you are going to find the steps to install ONXX and ONXXRuntime and run a simple C/C++ example 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.
+
On this page, you are going to find the steps to install ONXX and ONXXRuntime and run a simple C/C++ example on Linux. This wiki page describes the importance of ONNX models and how to use it. The goal is to provide you some examples.
  
 
= Installing ONNX =
 
= Installing ONNX =
You can then install ONNX from PyPi with the following command:
+
You can install ONNX from PyPI with the following command:
  
 
<pre>
 
<pre>
Line 19: Line 19:
  
 
= Installing ONNXRuntime =
 
= Installing ONNXRuntime =
This guide builds the baseline CPU version of ONNXRuntime form source, to build it from the source code using the following commands:
+
This guide builds the baseline CPU version of ONNXRuntime form source, to build it use the following commands:
  
 
<pre>
 
<pre>
Line 32: Line 32:
 
</pre>
 
</pre>
  
After install CMake run the following command for build onnxruntime:
+
After install CMake run the following command to build onnxruntime:
  
 
<pre>
 
<pre>
Line 45: Line 45:
 
</pre>
 
</pre>
  
Because this test is on Linux you need to copy the .so file to general lib path:
+
Finally, copy the .so file to general lib path:
 
<pre>
 
<pre>
 
cp libonnxruntime.so /usr/lib/x86_64-linux-gnu/
 
cp libonnxruntime.so /usr/lib/x86_64-linux-gnu/
Line 51: Line 51:
  
 
= Example =
 
= Example =
This guide is for using an ONNXRuntime C/C++ code on Linux, for that reason only the SqueezeNet examples are build it.
+
This guide is for using an ONNXRuntime C/C++ code on Linux, for that reason only the SqueezeNet examples are built it.
  
 
== Build ==
 
== Build ==
First go to the path with the C/C++ code examples.
+
First, go to the path with the C/C++ code examples.
 
<pre>
 
<pre>
 
cd onnxruntime/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/
 
cd onnxruntime/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/
 
</pre>
 
</pre>
  
After that build the code:
+
After that, build the code:
  
 
<pre>
 
<pre>
Line 67: Line 67:
 
== Run ==
 
== Run ==
  
Finally just run the code:
+
Finally, just run the code:
  
 
<pre>
 
<pre>

Revision as of 11:57, 20 November 2019

Introduction

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

Installing ONNX

You can 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 builds the baseline CPU version of ONNXRuntime form source, to build it 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 to build onnxruntime:

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

Finally, install it:

cd build/Linux/RelWithDebInfo
sudo make install

Finally, copy the .so file to general lib path:

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

Example

This guide is for using an ONNXRuntime C/C++ code on Linux, for that reason only the SqueezeNet examples are built it.

Build

First, go to the path with the C/C++ code examples.

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 example 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!