foreword
MNN is a lightweight deep neural network reasoning engine that loads a deep neural network model on the device side for reasoning and prediction. At present, MNN has been used in more than 20 apps such as Alibaba's mobile Taobao, mobile Tmall, and Youku, covering live broadcast, short video, search recommendation, product image search, interactive marketing, rights distribution, security risk control and other scenarios. In addition, there are several applications in scenarios such as IoT.
The following describes the process of compiling MNN under Ubuntu20.04 environment.
Compilation environment preparation
- gcc (version 4.9 or above is recommended)
- cmake (version 3.10 or above is recommended)
- protobuf (version 3.0 or above is recommended)
1. gcc installation
sudo apt update
sudo apt install build-essential
2. cmake installation
- Download the linux version of cmake from the official website
- Open the command terminal in the folder where the cmake source code is located, and unzip the file
tar -zxv -f cmake-3.24.1.tar.gz
- Enter the decompressed folder to execute
./bootstrap
May report an error
Need to install libssl-dev
sudo apt-get install libssl-dev
After the installation is complete, execute ./bootstrap again
- Compile and build cmake
make
- install cmake
sudo make install
- After the installation is complete, you can execute cmake --version to verify whether the installation is successful,
3. protobuf installation
- install dependencies
sudo apt-get install autoconf automake libtool curl make g++ unzip libffi-dev -y
- Download the protobuf source code and unzip it
tar -zxv -f protobuf-cpp-3.20.0-rc-1.tar.gz
- Enter the decompressed folder to generate the configuration file
cd protobuf-3.20.0-rc-1/
./autogen.sh
- Configuration Environment
./configure
- Compile the source code
make
- Install
sudo make install
- Refresh dynamic library
sudo ldconfig
- You can check whether the installation is successful through protobuf --version
Compile MNN locally on Linux
- Download the MNN source code and unzip it
unzip MNN-master.zip
- Enter the decompressed folder to execute
cd MNN-master
./schema/generate.sh
- local compilation
mkdir build
cd build
cmake ..
make -j8
- After the compilation is completed, the dynamic library of MNN appears locally
Android compile MNN
1. Linux command line mode
- Download and install NDK at https://developer.android.com/ndk/downloads/ , it is recommended to use the latest version
- Set NDK environment variables in .bashrc or .bash_profile, such as
export ANDROID_NDK=/home/wkx/Downloads/android-ndk-r25
- Enter the MNN folder to execute
cd /path/to/MNN-master
# 可选,更改 schema 文件后需要重新生成
./schema/generate.sh
# 可选,模型仅 demo 工程需要
./tools/script/get_model.sh
- Enter the android folder to compile the dynamic library
cd project/android
# 编译 armv7 动态库
mkdir build_32
cd build_32
../build_32.sh
# 编译armv8动态库
mkdir build_64
cd build_64
../build_64.sh
The picture below shows the armv8 dynamic library
2. Android Studio method
- Open MNN-master/project/android/demo with Android Studio, and compile the apk
- Use zip to decompress the compiled apk, and the lib directory contains the dynamic library of MNN
The picture below shows the compiled apk
Unzip with zip and enter the lib directory
Dynamic library under the arm64-v8a directory
MNN model conversion
- Enter the MNN directory and execute
cd MNN-master
./schema/generate.sh
mkdir build
cd build
cmake .. -DMNN_BUILD_CONVERTER=true && make -j4
The figure below shows the resulting model conversion tool
- Convert other models to MNN models, for example, convert model-mobilenet.pb model to abc.mnn model
./MNNConvert -f TF --modelFile model-mobilenet.pb --MNNModel abc.mnn --bizCode biz
conversion successful
Model before conversion and MNN model after conversion