Skip to content

victorbadenas/SimpleOnnxInference

Repository files navigation

SimpleOnnxInference

CI debian macos

c++ onnxruntime opencv

install requirements

For this project we need opencv and onnx. OpenCV needs to be built from source. For this project we will be using the v3.4.1 tag in their repository.

Ubuntu

For ubuntu installation, follow or run the installRequirements.sh bash script.

./scripts/installRequirements.sh

MacOS

For macos installation, follow or run the installRequirementsOsx.sh bash script.

./scripts/installRequirementsOsx.sh

Loading a sample model from torchvision for imagenet classification

This repository provides a getTorchvisionModel.py python script to load and export a model present in the torchvision package to onnx format. For running the python script create an environment and install the requirements in requirements.txt. Then simply run the file as follows:

python3 scripts/getTorchvisionModel.py -m ${modelArch} -O 13 -o ${outFolder} -i 3 224 224

where -i 3 224 224 corresponds to the size of the input to the network.

Build

To build, install the conan packages (cxxopts):

conan install -if build/ .

Then configure the cmake project:

cmake -DONNXRUNTIME_ROOTDIR="~/.local/include/onnxruntime" -DOpenCV_DIR="/usr/local/share/OpenCV" -S . -B build/

And build:

cmake --build build/

Run

The binary is stored in the ./build/bin/ folder. It requires the modelPath (onnx format), the imagepath (jpeg format). the label file is optional but highly encouraged as it provides the output label in human-readable format.

./build/bin/main -m ${modelPath} -i ${imagePath} -l ${labelPath} -d

The label file must be a plain text file where each line corresponds to the label of the line index where is located. for instance, for a 3 class classification, the label file would be as follows:

class1
class2
class3