MNNConvert

编译模型转换工具(gcc>=4.9)

首先需要安装protobuf(3.0以上)

  1. # MacOS
  2. brew install protobuf

其它平台请参考官方安装步骤

  1. cd MNN/tools/converter
  2. ./generate_schema.sh
  3. mkdir build
  4. cd build && cmake .. && make -j4
  5. # 或者直接执行脚本
  6. ./build_tool.sh

模型转换的使用

  1. Usage:
  2. MNNConvert [OPTION...]
  3. -h, --help Convert Other Model Format To MNN Model
  4. -v, --version show current version
  5. -f, --framework arg model type, ex: [TF,CAFFE,ONNX,TFLITE,MNN]
  6. --modelFile arg tensorflow Pb or caffeModel, ex: *.pb,*caffemodel
  7. --prototxt arg only used for caffe, ex: *.prototxt
  8. --MNNModel arg MNN model, ex: *.mnn
  9. --benchmarkModel Do NOT save big size data, such as Conv's weight,BN's
  10. gamma,beta,mean and variance etc. Only used to test
  11. the cost of the model
  12. --bizCode arg MNN Model Flag, ex: MNN
  13. --debug Enable debugging mode.

说明: 选项benchmarkModel将模型中例如卷积的weight,BN的mean,var等参数移除,减小转换后模型文件大小,在运行时随机初始化参数,以方便测试模型的性能。

tensorflow/ONNX/tflite

  1. ./MNNConvert -f TF/ONNX/TFLITE --modelFile XXX.pb/XXX.onnx/XXX.tflite --MNNModel XXX.XX --bizCode XXX

三个选项是必须的! 例如:

  1. ./MNNConvert -f TF --modelFile path/to/mobilenetv1.pb --MNNModel model.mnn --bizCode MNN

caffe

  1. ./MNNConvert -f CAFFE --modelFile XXX.caffemodel --prototxt XXX.prototxt --MNNModel XXX.XX --bizCode XXX

四个选项是必须的! 例如:

  1. ./MNNConvert -f CAFFE --modelFile path/to/mobilenetv1.caffemodel --prototxt path/to/mobilenetv1.prototxt --MNNModel model.mnn --bizCode MNN

MNN

  1. ./MNNConvert -f MNN --modelFile XXX.mnn --MNNModel XXX.XX --bizCode XXX

查看版本号

  1. ./MNNConvert --version

MNNDump2Json

将MNN模型bin文件 dump 成可读的类json格式文件,以方便对比原始模型参数

Pytorch 模型转换

  1. import torch
  2. import torchvision
  3. dummy_input = torch.randn(10, 3, 224, 224, device='cuda')
  4. model = torchvision.models.alexnet(pretrained=True).cuda()
  5. # Providing input and output names sets the display names for values
  6. # within the model's graph. Setting these does not change the semantics
  7. # of the graph; it is only for readability.
  8. #
  9. # The inputs to the network consist of the flat list of inputs (i.e.
  10. # the values you would pass to the forward() method) followed by the
  11. # flat list of parameters. You can partially specify names, i.e. provide
  12. # a list here shorter than the number of inputs to the model, and we will
  13. # only set that subset of names, starting from the beginning.
  14. input_names = [ "actual_input_1" ] + [ "learned_%d" % i for i in range(16) ]
  15. output_names = [ "output1" ]
  16. torch.onnx.export(model, dummy_input, "alexnet.onnx", verbose=True, input_names=input_names, output_names=output_names)
  • 将 Onnx 模型文件转成 MNN 模型
  1. ./MNNConvert -f ONNX --modelFile alexnet.onnx --MNNModel alexnet.mnn --bizCode MNN