MNN
官方中文文档:https://www.yuque.com/mnn/cn/about
github源码:https://github.com/alibaba/MNN
参考: windows MNN 的使用流程(Python版)-CSDN博客
本人处于初学阶段,文中有错误希望大佬指正!
1 使用MNN库的方法
这种方法便于模型转换和量化等操作,但对于模型推理似乎还没实现
1.1 准备
- MNN库python安装
pip install -U MNN -i https://mirror.baidu.com/pypi/simple
安装成功后,在命令行输入mnn
>mnn mnn toolsets has following command line tools $mnn list out mnn commands $mnnconvert convert other model to mnn model $mnnquant quantize mnn model
从上面可以看出,主要有两个工具:
mnnconvert
(模型转换)和mnnquant
(模型量化)- 命令行输入这两个会显示对应使用方法及参数列表
>mnnconvert Usage: MNNConvert [OPTION...] -h, --help Convert Other Model Format To MNN Model -v, --version show current version -f, --framework arg model type, ex: [TF,CAFFE,ONNX,TFLITE,MNN] --modelFile arg tensorflow Pb or caffeModel, ex: *.pb,*caffemodel --batch arg if model input's batch is not set, set as the batch size you set --keepInputFormat keep input dimension format or not, default: false --optimizeLevel arg graph optimize option, 0: don't run optimize(only support for MNN source), 1: use graph optimize only for every input case is right, 2: normally right but some case may be wrong, default 1 --optimizePrefer arg graph optimize option, 0 for normal, 1 for smalleset, 2 for fastest --prototxt arg only used for caffe, ex: *.prototxt --MNNModel arg MNN model, ex: *.mnn --fp16 save Conv's weight/bias in half_float data type --benchmarkModel Do NOT save big size data, such as Conv's weight,BN's gamma,beta,mean and variance etc. Only used to test the cost of the model --bizCode arg MNN Model Flag, ex: MNN --debug Enable debugging mode. --forTraining whether or not to save training ops BN and Dropout, default: false --weightQuantBits arg save conv/matmul/LSTM float weights to int8 type, only optimize for model size, 2-8 bits, default: 0, which means no weight quant --weightQuantAsymmetric the default weight-quant uses SYMMETRIC quant method, which is compatible with old MNN versions. you can try set --weightQuantAsymmetric to use asymmetric quant method to improve accuracy of the weight-quant model in some cases, but asymmetric quant model cannot run on old MNN versions. You will need to upgrade MNN to new version to solve this problem. default: false --compressionParamsFile arg The path of the compression parameters that stores activation, weight scales and zero points for quantization or information for sparsity. --OP print framework supported op --saveStaticModel save static model with fix shape, default: false --targetVersion arg compability for old mnn engine, default: 1.2f --customOpLibs arg custom op libs ex: libmy_add.so;libmy_sub.so --authCode arg code for model authentication. --inputConfigFile arg set input config file for static model, ex: ~/config.txt --alignDenormalizedValue arg if 1, converter would align denormalized float(|x| < 1.18e-38) as zero, because of in ubuntu/protobuf or android/flatbuf, system behaviors are different. default: 1, range: {0, 1} [10:23:53] @ 192: framework Invalid, use -f CAFFE/MNN/ONNX/TFLITE/TORCH !
1.2 模型转换
- 命令输入(以onnx模型转化mnn为例)
mnnconvert -f ONNX --modelFile xx.onnx --MNNModel xx.mnn --bizCode biz
- 初始模型:
- 转换后模型:
1.3 模型量化
这里是离线量化,即训练后量化,带训练量化见官方文档:训练量化
命令
mnnquant --weightQuantBits 8 [--weightQuantAsymmetric] origin.mnn quan.mnn imageInputConfig.json
–weightQuantBits 8:int8量化
–weightQuantAsymmetric:可选,使用非对称量化方法
origin.mnn:原始模型文件路径,即待量化的浮点模
quan.mnn:为目标模型文件路径,即量化后的模型
imageInputConfig.json:为预处理的配置项,参考imageInputConfig.json
2 使用MNN代码编译的方法
可以通过编译,实现对mnn模型的推理
2.1 准备
在github下载源码
进行推理架构的编译
2.2 demo测试
环境
- Microsoft Visual Studio (>=2017)到[微软官网](Visual Studio 较旧的下载 - 2019、2017、2015 和以前的版本 (microsoft.com))下载即可,然后安装时选择C++桌面操作相关即可
- cmake(>=3.13)[下载链接](Download | CMake)
- powershell
- Ninja (构建工具,比nmake更快)
步骤
- 下载完vs后,打开以下命令行
使用根目录的CMakeLists.txt ,打开 MNN_BUILD_DEMO 开关(逐行进行下述操作)
cd path/to/MNN (进到你安装的MNN代码目录下) powershell ./schema/generate.ps1 mkdir build cd build cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DMNN_BUILD_DEMO=ON .. nmake
将你需要进行推理的模型转换为mnn模型(前面有介绍,不在赘述)
将模型及需要测试的图片放入项目下(这里我在built下新建一个文件夹存放)
使用demo中编译好的segment.out进行模型的推理
> ./segment.out wzy/segment.mnn wzy/input.jpg wzy/output.jpg input: w:257 , h:257, bpp: 3 origin size: 700, 940 output w = 257, h=257
这里对图片进行了resize,所以得到的分割只有上半部分
3 编译 MNNConvert.exe(🔺)
这就不需要在原本环境下安装mnn库。目前实际使用中的好处还不太清楚,处于初学期间
- 具体操作:
cd MNN
mkdir build
cd build
cmake -G "Ninja" -DMNN_BUILD_SHARED_LIBS=OFF -DMNN_BUILD_CONVERTER=ON -DCMAKE_BUILD_TYPE=Release -DMNN_WIN_RUNTIME_MT=ON ..
ninja
- 会生成MNNConvert.exe
- 以后使用就可以
./MNNConvert -f……
- 额外测试
在这里,我将MNNConvert在build2中编译,测试demo在build_demo中编译,整体用法如上
未完待续。。。。