(一)MNN框架学习:编译及使用

1、官方对依赖的版本要求及检查命令如下表:

依赖项 建议版本 检查是否安装的命令
cmake 理论依赖3.0以上即可,但建议版本以及测试环境中的版本都为3.10+ cmake -v
protobuf version >= 3.0 is required protoc --version
C++编译器 GCC 或 Clang 皆可,version >= 4.9,对于macOS已经默认安装 Clang clang -v 或者 gcc -v

 依赖安装自行百度!!!

2、下载 MNN源码并解压

git clone https://github.com/alibaba/MNN.git

3、编译MNN模型转化器:MNNConvert,整个编译过程需要几分钟

cd MNN

./schema/generate.sh

mkdir build

cd build

cmake .. -DMNN_BUILD_CONVERTER= true&& make -j4

4、下载demo需要的模型然后转换成MNN支持的模型,主要用于demo工程使用,可以跳过。

下载demo模型时,容易下载失败,可能是被墙或其他原因,如果一直失败的话可以把模型手动下载下来,然后进行转换(大致操作就是修改./tools/script/get_model.sh脚本,注释掉下载逻辑,然后手动下载完成之后,再执行下面的命令

cd MNN
./tools/script/get_model.sh

    下载的模型在/MNN/resource/model/ (应该没记错^_^)

5、使用MNNConvert(build文件夹内)进行模型转换

使用下面的命令可查看帮助信息:

 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

      --prototxt arg    only used for caffe, ex: *.prototxt

      --MNNModel arg    MNN model, ex: *.mnn

      --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.

(1)TensorFlow -> MNN

./MNNConvert -f TF --modelFile path/to/XXX.pb --MNNModel path/to/XXX.mnn --bizCode MNN

(2)TensorFlow Lite -> MNN

./MNNConvert -f TFLITE --modelFile XXX.tflite --MNNModel XXX.mnn --bizCode MNN

(3)Caffe -> MNN

./MNNConvert -f CAFFE --modelFile XXX.caffemodel --prototxt XXX.prototxt --MNNModel XXX.mnn --bizCode MNN

   (4)   ONNX -> MNN

./MNNConvert -f ONNX --modelFile XXX.onnx --MNNModel XXX.mnn --bizCode MNN

  (5)  PyTorch -> MNN

     a:   Convert PyTorch model to ONNX (https://pytorch.org/docs/stable/onnx.html)

import torch

import torchvision

dummy_input = torch.randn(10, 3, 224, 224, device='cuda')

model = torchvision.models.alexnet(pretrained=True).cuda()

# Providing input and output names sets the display names for values

# within the model's graph. Setting these does not change the semantics

# of the graph; it is only for readability.

#

# The inputs to the network consist of the flat list of inputs (i.e.

# the values you would pass to the forward() method) followed by the

# flat list of parameters. You can partially specify names, i.e. provide

# a list here shorter than the number of inputs to the model, and we will

# only set that subset of names, starting from the beginning.

input_names = [ "actual_input_1" ] + [ "learned_%d" % i for i in range(16) ]

output_names = [ "output1" ]

torch.onnx.export(model, dummy_input, "alexnet.onnx", verbose=True, input_names=input_names, output_names=output_names)

  b. Convert ONNX to MNN

./MNNConvert -f ONNX --modelFile XXX.onnx --MNNModel XXX.mnn --bizCode MNN

6、测试MNNDump2Json (build文件夹内)

MNNDump2Json用于将MNN二进制模型文件转储为可读的json格式。 与原始模型参数进行比较有助于检查模型转换是否正确

./MNNDump2Json path/to/xxx.mnn path/to/xxx.json

阅读文档:https://www.yuque.com/mnn/en/model_convert

                 https://github.com/alibaba/MNN/tree/master/tools/converter

                 https://blog.csdn.net/sinat_31425585/category_9390512.html

你可能感兴趣的:((一)MNN框架学习:编译及使用)