This post was last edited by CoderX9527 on 2024-11-5 17:02
Introduction
This is the last chapter of the book. It integrates the knowledge of previous modules, library searches, and calling third-party libraries, and writes a command line tool for handwritten digit recognition based on ONNX runtime.
ONNX runtime installation
The official address of ONNX runtime is as follows
ONNX Runtime is a cross-platform machine learning accelerator for inference and training. By using ONNX runtime for inference, you can achieve a faster customer experience and reduce costs, and support deep learning frameworks such as PyTorch, TensorFlow/Keras, and classic machine learning libraries such as sckit-learn, LightGBM, and XGBoost. ONNX Runtime is compatible with different hardware, drivers, and operating systems, and takes advantage of the hardware accelerators used and graphics optimization and conversion to provide the best performance.
Install and verify the library (latest version fails, older versions succeed)
Download the compressed package for the linux-x64 platform and unzip it to the following directory:
Compilation failed in ch009 directory: It should be related to the c++ version and compilation. Don't bother with it for now.
The lower version was compiled successfully
Download onnxruntime-linux-x64-1.10.0.tgz, the same as the author, and unzip it to the corresponding directory.
The running prompt is Load model failed, which is consistent with the description in the book.
Compilation and installation of zlib library
Compile
Create a build-release directory in the zlib source code path, enter it, then configure cmake and start compiling.
mkdir build-release
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release
Install
cmake --install . failed, need to install with administrator privileges
sudo cmake --install . Installation successful
Compilation and installation of libpng library
Compile
Create a build-release directory in the source directory and compile in this directory
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release
Install
cmake --install . Installation failed, administrator privileges are required
sudo cmake --install . Installation successful
Handwriting Library Example
This directory program is an application that calls the handwritten digit recognition library and depends on the above three libraries.
CMake Catalog Program
Setting project properties
The above command sets the project properties for the CMake catalog program.
Find the package onnxruntime
Here, the onnxruntime version is specified as 1.10.0. Users can modify it according to the actual version downloaded.
Line 17 searches for the onnxruntime package and specifies the version number as 1.10. This actually calls the cmake/Findonnxruntime.cmake file. Its contents are as follows:
Finally, set the two variables onnxruntime_INCLUDE_DIR and onnxruntime_LIBRARIES.
Find the package libpng
at line 18 of CMakeLists.txt.
It actually calls the cmake/Findlibpng.cmake file, which has the following content:
Note that it will continue to look for the libpng16.cmake module file in the system, that is, this file is a wrapper of the libpng16.cmake file, exporting the two variables libpng_LIBRARY and libpng_INCLUDE_DIR.
Note that the libpng16.cmake file path is /usr/local/lib/libpng/libpng16.cmake, which is installed by libpng above.
Generate error
An error occurred, saying that the png_shared interface target includes ZLIB: ZLIB was not found.
ZLIB: ZLIB solution
Modify /usr/local/lib/libpng/libpng16.cmake file
Open this file with administrator privileges and shield the two statements containing ZLIB:ZLIB, as shown in the following figure:
cmake configures and compiles successfully
mkdir build-debug
cd build-debug
cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config Debug
Running results - some are right and some are wrong
Command line runs ./recognize ../models/mnist.onnx ../2.png
2.png/3.png/4.png were identified successfully respectively.
2.png/3.png/4.png here are original pictures provided by the author.
The following are the images 8.png/9.png/0.png I provided. The image size is the same as 2.png, 111x131 pixels.
The running results are as follows: 0/8 are recognized correctly, and 9 are recognized incorrectly (recognized as 1).
I provided another bold 9_bold.png image, but the recognition result was still wrong, and it was recognized as 7.