Query Version

  1. ./MNNConvert --version

Model Convert

Parameters

  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:
  7. *.pb,*caffemodel
  8. --prototxt arg only used for caffe, ex: *.prototxt
  9. --MNNModel arg MNN model, ex: *.mnn
  10. --fp16 save Conv's weight/bias in half_float data
  11. type
  12. --benchmarkModel Do NOT save big size data, such as Conv's
  13. weight,BN's gamma,beta,mean and variance etc.
  14. Only used to test the cost of the model
  15. --bizCode arg MNN Model Flag, ex: MNN
  16. --debug Enable debugging mode.
  17. --forTraining whether or not to save training ops BN and
  18. Dropout, default: false
  19. --weightQuantBits arg quantize conv/matmul/LSTM float weights to int
  20. type, only optimize for model size, optional 2-8 bits,
  21. default: 0, which means no weight quant
  22. --compressionParamsFile arg
  23. The path of the compression parameters that
  24. stores activation, weight scales and zero
  25. points for quantization or information for
  26. sparsity.
  27. --saveStaticModel save static model with fix shape, default:
  28. false
  29. --inputConfigFile arg set input config file for static model, ex:
  30. ~/config.txt

Note1: The benchmarkModel option removes some parameters from the model to reduce the size of it, such as weight of convolution, mean, var of BN. removed parameters will be initialized randomly in runtime. This will be helpful in performance testing.

Note2: The weightQuantBits option should be used as “—weightQuantBits numBits”, where numBits=2~8. this function is to quantize conv/matmul/LSTM float weights to int type, only optimize for model size, optional 2-8 bits. the weight-quantized model will be decoded to float32 when it is loaded. inference speed is the same with the float32 model, but 4X smaller(8bits), the accuracy is almost the same with its original float32 version.

TensorFlow -> MNN

  1. ./MNNConvert -f TF --modelFile XXX.pb --MNNModel XXX.mnn --bizCode biz

TensorFlow Lite -> MNN

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

Caffe -> MNN

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

ONNX -> MNN

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

PyTorch -> MNN

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

    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)
  2. Convert ONNX to MNN

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

Python Tools for Model Conversion

We provided the model conversion tools in our Python toolchain. See here: https://www.yuque.com/mnn/en/usage_in_python