MNN编译模型转换工具

devtools/2024/10/24 6:37:40/

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

# macOS
brew install protobuf

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

cd MNN
mkdir build
cd build
cmake .. -DMNN_BUILD_CONVERTER=true
make

无法正常编译参考

模型转换的使用

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--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'sgamma,beta,mean and variance etc. Only used to testthe cost of the model--bizCode arg     MNN Model Flag, ex: MNN--debug           Enable debugging mode.

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

tensorflow/ONNX/tflite

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

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

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

caffe

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

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

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

MNN

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

查看版本号

./MNNConvert --version

MNNDump2Json

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

Pytorch 模型转换

  • 用Pytorch的 onnx.export 接口转换 Onnx 模型文件(参考:https://pytorch.org/docs/stable/onnx.html)
import torch
import torchvisiondummy_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, do_constant_folding=True)
  • 将 Onnx 模型文件转成 MNN 模型
./MNNConvert -f ONNX --modelFile alexnet.onnx --MNNModel alexnet.mnn --bizCode MNN

http://www.ppmy.cn/devtools/57662.html

相关文章

Pytorch中方法对象和属性,例如size()和shape

文章目录 方法对象和属性的基本概念方法对象属性示例说明总结 常见的方法对象和属性常见的方法对象常见的属性总结示例 方法对象和属性的基本概念 方法对象(method object)和属性(attribute)是面向对象编程中的两个重要概念。让我…

SpringBoot | 大新闻项目后端(redis优化登录)

该项目的前篇内容的使用jwt令牌实现登录认证,使用Md5加密实现注册,在上一篇:http://t.csdnimg.cn/vn3rB 该篇主要内容:redis优化登录和ThreadLocal提供线程局部变量,以及该大新闻项目的主要代码。 redis优化登录 其实…

【锁】各种锁的概念

解释一下什么是乐观锁、悲观锁、自旋锁、互斥锁、读写锁、排他锁、共享锁、统一锁、分段锁? 定义角度不同,通常把(乐观锁、悲观锁)、(自旋锁、互斥锁)、(读写锁、排他锁、共享锁)、…

7 动态规划

下面的例子不错: 对于动态规划,能学到不少东西; 你要清楚每一步都在做什么,划分细致就能够拆解清楚! xk. - 力扣(LeetCode) labuladong的算法笔记-动态规划-CSDN博客 动态规划是一种强大的算法…

Java:封装

文章目录 一、概念二、实现三、测试四、总结 一、概念 在面向对象编程中, 封装从字面上来理解就是包装的意思,特点就是信息隐藏,防止该类的代码和数据被外部类的代码随机访问。 封装的优点: 良好的封装能够减少耦合。 统一接口…

如何使用缓存提升SpringBoot性能(EhCache和Redis方式)

一、简介 缓存是通过将频繁访问的数据存储在内存中来提高Web应用程序性能的重要策略,从而减少对数据库等较慢的存储系统的重复进行数据检索操作的需求。在这里将介绍如何使用流行的缓存实现(如EhCache和Redis等),专门为Spring Bo…

MySQL使用LIKE索引是否失效的验证

1、简单的示例展示 在MySQL中,LIKE查询可以通过一些方法来使得LIKE查询能够使用索引。以下是一些可以使用的方法: 使用前导通配符(%),但确保它紧跟着一个固定的字符。 避免使用后置通配符(%)&…

Ubuntu安装Docker

一,Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有…