官方中文文档:https://www.yuque.com/mnn/cn/about
github源码:https://github.com/alibaba/MNN
参考: windows MNN 的使用流程(Python版)-CSDN博客
本人处于初学阶段,文中有错误希望大佬指正!
这种方法便于模型转换和量化等操作,但对于模型推理似乎还没实现
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 !
mnnconvert -f ONNX --modelFile xx.onnx --MNNModel xx.mnn --bizCode biz
这里是离线量化,即训练后量化,带训练量化见官方文档:训练量化
命令
mnnquant --weightQuantBits 8 [--weightQuantAsymmetric] origin.mnn quan.mnn imageInputConfig.json
–weightQuantBits 8:int8量化
–weightQuantAsymmetric:可选,使用非对称量化方法
origin.mnn:原始模型文件路径,即待量化的浮点模
quan.mnn:为目标模型文件路径,即量化后的模型
imageInputConfig.json:为预处理的配置项,参考imageInputConfig.json
可以通过编译,实现对mnn模型的推理
在github下载源码
进行推理架构的编译
环境
步骤
使用根目录的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,所以得到的分割只有上半部分
这就不需要在原本环境下安装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 -f……
在这里,我将MNNConvert在build2中编译,测试demo在build_demo中编译,整体用法如上
未完待续。。。。