MACE(2)-----模型编译
作者:十岁的小男孩
QQ:929994365
无用
本文仅用于学习研究,非商业用途,欢迎大家指出错误一起学习,文章内容翻译自 MACE 官方手册,记录本人阅读与开发过程,力求不失原意,但推荐阅读原文。
本文是mace学习的第二步,如何撰写Yaml文件,将pb模型部署到该文件中进行编译。若环境尚未搭建完毕的同学请看第一篇环境搭建,编译出的库在安卓中如何使用请浏览第三步即mace工程化。
MACE(1)-----环境搭建:https://www.cnblogs.com/missidiot/p/9480033.html
MACE(3)-----工程化:https://www.cnblogs.com/missidiot/p/9717633.html
小米官方文档编写部署文件:https://mace.readthedocs.io/en/latest/user_guide/basic_usage.html
目录
准备模型文件
创建部署文件
高级用法
1.准备模型文件
说明:本文只关注TensorFlow平台。
准备好预先训练的TensorFlow model.pb文件。使用图形转换工具 优化模型以进行推理。此工具将通过进行多个优化来提高推理效率,例如操作员折叠,冗余节点删除等。我们强烈建议MACE用户在构建之前使用它。
# CPU/GPU: ./transform_graph \--in_graph=/path/to/your/tf_model.pb \--out_graph=/path/to/your/output/tf_model_opt.pb \--inputs='input node name' \--outputs='output node name' \--transforms='strip_unused_nodes(type=float, shape="1,64,64,3")strip_unused_nodes(type=float, shape="1,64,64,3")remove_nodes(op=Identity, op=CheckNumerics)fold_constants(ignore_errors=true)flatten_atrous_convfold_batch_normsfold_old_batch_normsremove_control_dependenciesstrip_unused_nodessort_by_execution_order'
注:(尚未搞清楚,以下文件的如何用)
2. 创建部署文件(model.yaml)
转换模型或构建库时,MACE需要读取YAML文件,此文件称为模型部署文件。模型部署文件包含模型和构建选项的所有信息。构建过程包括解析模型部署文件,转换模型,构建MACE核心库以及打包生成的模型库。
一个部署文件将正常生成一个库,但如果指定了多个ABI,则将为每个ABI生成一个库。部署文件还可以包含多个模型。例如,AI相机应用程序可以包含面部识别,对象识别和语音识别模型,所有这些都可以在一个部署文件中定义。
下例为mace官方提供的例子:
# The name of library library_name: mobilenet target_abis: [arm64-v8a] model_graph_format: file model_data_format: file models:mobilenet_v1: # model tag, which will be used in model loading and must be specific. platform: tensorflow# path to your tensorflow model's pb file. Support local path, http:// and https://model_file_path: https://cnbj1.fds.api.xiaomi.com/mace/miai-models/mobilenet-v1/mobilenet-v1-1.0.pb# sha256_checksum of your model's pb file.# use this command to get the sha256_checksum: sha256sum path/to/your/pb/file model_sha256_checksum: 71b10f540ece33c49a7b51f5d4095fc9bd78ce46ebf0300487b2ee23d71294e6# define your model's interface# if there multiple inputs or outputs, write like blow:# subgraphs:# - input_tensors:# - input0# - input1# input_shapes:# - 1,224,224,3# - 1,224,224,3# output_tensors:# - output0# - output1# output_shapes:# - 1,1001# - 1,1001 subgraphs:- input_tensors:- inputinput_shapes:- 1,224,224,3output_tensors:- MobilenetV1/Predictions/Reshape_1output_shapes:- 1,1001
# cpu, gpu or cpu+gpuruntime: cpu+gpuwinograd: 0
Command:
# Get device's soc info.(尚未搞清楚在哪用) adb shell getprop | grep platform# command for generating sha256_sum,获取检测码,路径指向pb文件目录(直接在终端下使用) sha256sum /path/to/your/file
配置:
注:[option]为可选的,在部署文件中可选。
3. 高级用法
3.1 将模型转换成C++代码
1. 更改部署文件
如果要保护模型,可以将模型转换为C ++代码。还有两种情况:
1.1 将模型图转换为代码并使用以下模型配置将权重建模到文件。
model_graph_format: code model_data_format: file
将模型图和模型权重转换为具有以下模型配置的代码。
model_graph_format: code model_data_format: code
以上两步当model_graph_format: code时候后期会生成mace_engine_factory.h文件,后期遇到再解释(未完成,尚未知其用途)
1.2 另一种模型保护方法obfuscate
用于混淆模型运算符的名称。(未知)
2. 将模型转换为代码
python tools/converter.py convert --config=/path/to/model_deployment_file.yml
该命令将在builds / $ {library_name} / model目录中生成$ {library_name} .a,在builds / $ {library_name} / include中生成 ** .h *,如下面的dir-tree。
# model_graph_format: code # model_data_format: file builds├── include│ └── mace│ └── public│ ├── mace_engine_factory.h│ └── mobilenet_v1.h└── model├── mobilenet-v1.a└── mobilenet_v1.data# model_graph_format: code # model_data_format: code builds├── include│ └── mace│ └── public│ ├── mace_engine_factory.h│ └── mobilenet_v1.h└── model└── mobilenet-v1.a
3. 部署
libmace.a和$ {library_name} .a链接到您的目标。
参阅mace/examples/example.cc
完整用法。以下列出了关键步骤。
// Include the headers #include "mace/public/mace.h" #include "mace/public/mace_runtime.h" // If the model_graph_format is code #include "mace/public/${model_name}.h" #include "mace/public/mace_engine_factory.h"// ... Same with the code in basic usage// 4. Create MaceEngine instance std::shared_ptr<mace::MaceEngine> engine; MaceStatus create_engine_status; // Create Engine from compiled code create_engine_status =CreateMaceEngineFromCode(model_name.c_str(),model_data_file, // empty string if model_data_format is codeinput_names,output_names,device_type,&engine); if (create_engine_status != MaceStatus::MACE_SUCCESS) {// Report error }// ... Same with the code in basic usage
3.2 调整特点的SoC的GPU内核(尚未搞清楚干啥)
如果您想使用特定设备的GPU,您只需target_socs
在YAML文件中指定,然后为其调整MACE lib(OpenCL内核),这可能会提高1~10%的性能。
1.更改模型部署文件(.yml)
指定target_socs
模型中的部署文件(.yml):
target_socs: [sdm845]注意:获取设备的soc信息:adb shell getprop | grep平台
2.转换模型
python tools/converter.py convert --config=/path/to/model_deployment_file.yml
3.调整
tools / converter.py将启用GPU内核的自动调整。这通常需要一些时间才能完成,具体取决于模型的复杂程度。
python tools/converter.py run --config=/path/to/model_deployment_file.yml --validate
该命令将在builds / $ {library_name} / opencl中生成两个文件,如下面的dir-tree。
builds └── mobilenet-v2├── model│ ├── mobilenet_v2.data│ └── mobilenet_v2.pb└── opencl└── arm64-v8a├── moblinet-v2_compiled_opencl_kernel.MiNote3.sdm660.bin└── moblinet-v2_tuned_opencl_parameter.MiNote3.sdm660.bin
mobilenet-v2-gpu_compiled_opencl_kernel.MI6.msm8998.bin代表用于模型的OpenCL二进制文件,可以加速初始化阶段。详情请参阅OpenCL规范。
mobilenet-v2-tuned_opencl_parameter.MI6.msm8998.bin代表SoC的调整OpenCL参数。
4.部署
更改上面生成的文件名称以防止发生冲突,并将它们推送到您自己设备的目录中。
使用与前面的过程类似,下面列出了不同的关键步骤。
// Include the headers #include "mace/public/mace.h" #include "mace/public/mace_runtime.h"// 0. Set pre-compiled OpenCL binary program file paths and OpenCL parameters file path when available if (device_type == DeviceType::GPU) {mace::SetOpenCLBinaryPaths(path/to/opencl_binary_paths);mace::SetOpenCLParameterPath(path/to/opencl_parameter_file); }// ... Same with the code in basic usage.
转载于:https://www.cnblogs.com/missidiot/p/9509831.html
MACE(2)-----模型编译相关推荐
- TVM将深度学习模型编译为WebGL
TVM将深度学习模型编译为WebGL TVM带有全新的OpenGL / WebGL后端! OpenGL / WebGL后端 TVM已经瞄准了涵盖各种平台的大量后端:CPU,GPU,移动设备等.这次,添 ...
- Unet项目解析(7): 模型编译-优化函数、损失函数、指标列表
项目GitHub主页:https://github.com/orobix/retina-unet 参考论文:Retina blood vessel segmentation with a convol ...
- TensorFlow 2.9的零零碎碎(五)-模型编译
目录 compile函数的定义在哪里? compile函数的参数 optimizer loss 为什么我用的是sparse_categorical_crossentropy? metrics 有的地方 ...
- model.fit()模型编译时报错:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type
model.fit()模型编译时报错:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type ...
- fossid安装教程_Win10环境下6sV2.1模型编译
这是转载的!!!周知 最新版本6sV2.1模型是通过FORTRAN95编写的,2017年11月代码编写完成,2018年11月发布在模型官网上.通常我们在使用过程中都是调用模型的.exe可执行文件,而下 ...
- 小米开源框架MACE 创建模型部署文件
转载自https://www.jianshu.com/p/823f7d7eaf58 文章内容翻译自 MACE 官方手册,记录本人阅读与开发过程,力求不失原意,但推荐阅读原文. https://medi ...
- sequential模型编译时的指标设置:sklearn.metrics:指标
sklearn.metrics: Metrics 官网是最好的学习区. See the Model evaluation: quantifying the quality of predictions ...
- 算法开发:将合并后的模型编译成动态库(so文件)提供给qt调用
一 .说明: 模型:分类+分割模型,4个模型合并成一个大模型,参考我之前的博客介绍:算法开发:多模型合并,加快推理速度_喜欢天晴的博客-CSDN博客 环境:tensorrt8.2.3.0,cuda版o ...
- 小米开源AI框架mace编译构建
目录 简介 环境要求 1 安装 Bazel 2 安装Android NDK 3 在Ubuntu16.04下安装Docker(17.09) 构建并运行示例模型 1 拉取MACE项目 2 拉取MACE M ...
最新文章
- RESTful之排序
- mac 部署python环境
- WIFI 基本理论-2017
- mysql数据每日更新_[每日更新-MySQL]4.记录操作(数据操作)
- HALCON示例程序vessel.hdev血管的分割与测量
- 文本前后空格去除工具
- java 合并对象中属性_Java2个对象形集合按某一个属性合并
- 2019牛客多校第二场E MAZE(线段树 + 矩阵)题解
- 浅谈微信公众平台和微信开放平台的区别
- 图集压缩格式设置ASTC不生效的原因
- 【转】UEFI引导修复教程和工具
- Sketch快捷键大全 Sketch如何自定义快捷键?
- 【ESP 保姆级教程】疯狂传感器篇 —— 案例:ESP8266 + MQ3酒精传感器 + 串口输出
- 百度智能云 API鉴权总结
- 学会自我欣赏,将缺点变为有点
- 【HTML + CSS】模仿腾讯云页面——初步实现
- 大数据入门之学习视频资料分享
- h5+css3基础面试题
- 问题解决:LaTeX biblatex 参考文献出现 [S.l.]: [s.n.]的问题
- Anchor-free的目标检测文章
热门文章
- 用java怎么实现数据库_用Java实现数据库应用系统
- js输出一个菱形_Threejs使用菱形正方形算法,中点替换算法生成随机地形
- 网络营销er每天必做四件事
- 网站推广——网站推广专员如何提升新站权重?
- 网站开发建设过程中所涉及到的技术问题应当如何面对?
- JAVA窗帘_HomeControl 智能家具系统,包括灯光,窗帘的控制,设备,房间,情景模式的添加 Java Develop 240万源代码下载- www.pudn.com...
- list和map用法java,java 集合list和map的用法
- android uinput 按键_linux 虚拟输入设备(uinput)模拟鼠标和键盘的使用方法
- python工作技巧_Python常用小技巧汇总
- Idea terminal:不是内部或外部命令,也不是可行的程序或批处理文件