在上节主要是讲述了OpenVINO 整个custom layer流程,官网中给出了基于CPU和GPU整个实现用例过程,用例链接

该用例主要是在custom layer中实现一下公式:

整个流程主要如下:

设置环境变量

source /opt/intel/openvino/bin/setupvars.sh

这个是使用openvino中都需要设置的,最好设置到启动脚本~.barshrc中

安装所需要的python包

1: 在/opt/intel/openvino/deployment_tools/tools/extension_generator/extgen.py脚本中需要使用到Cog,需要安装cogapp:

sudo pip3 install cogapp

2: 安装opencv

sudo pip3 install opencv-python

下载用例

1:home目录下创建cl_tutorial目录:

cd ~
mkdir cl_tutorial
cd cl_tutorial

2:下载用例:

git clone https://github.com/david-drew/OpenVINO-Custom-Layers.git

3:设置环境变量:

export CLWS=~/cl_tutorial
export CLT=$CLWS/OpenVINO-Custom-Layers/2019.r2.0

生成TensorFlow模型

使用用例中的build_cosh_model.py中的脚本生成TensorFlow模型,命令如下:

mkdir $CLWS/tf_model
$CLT/../create_tf_model/build_cosh_model.py $CLWS/tf_model

运行结果:

生成的文件目录如下:

其中model.ckpt.meta文件是后面需要的

生成cosh Custom Layer注册文件

使用Model Extension Generator生成Extension Template中间文件,脚本目录位置/opt/intel/openvino/deployment_tools/tools/extension_generator/extgen.py,该脚本主要功能是向tensorflow 注册kernel功能,生成一系列的中间文件

usage: You can use any combination of the following arguments:Arguments to configure extension generation in the interactive modeoptional arguments:-h, --help            show this help message and exit--mo-caffe-ext        generate a Model Optimizer Caffe* extractor--mo-mxnet-ext        generate a Model Optimizer MXNet* extractor--mo-tf-ext           generate a Model Optimizer TensorFlow* extractor--mo-op               generate a Model Optimizer operation--ie-cpu-ext          generate an Inference Engine CPU extension--ie-gpu-ext          generate an Inference Engine GPU extension--output_dir OUTPUT_DIRset an output directory. If not specified, the currentdirectory is used by default.

生成中间文件时将CPU和GPU都选上:

python3 /opt/intel/openvino/deployment_tools/tools/extension_generator/extgen.py new --mo-tf-ext --mo-op --ie-cpu-ext --ie-gpu-ext --output_dir=$CLWS/cl_cosh

运行过程如下:

Generating:Model Optimizer: Extractor for Caffe Custom Layer: NoExtractor for MxNet Custom Layer: NoExtractor for TensorFlow Custom Layer: YesFramework-agnostic operation extension: YesInference Engine: CPU extension: YesGPU extension: YesEnter layer name:   [cosh]Do you want to automatically parse all parameters from the model file? (y/n)Yes means layer parameters will be automatically parsed during Model Optimizer work as is.No means you will be prompted for layer parameters in the following section    nEnter all parameters in the following format:<param1> <new name1> <type1><param2> <new name2> <type2>...
Where type is one of the following types:b - Bool,                               padding - Padding type,                 list.b - List of bools,                 f - Float,                              batch - Get batch from dataFormat,      list.f - List of floats,                i - Int,                                channel - Get channel from dataFormat,  list.i - List of ints,                  s - String,                             spatial - Get spatial from dataFormat,  list.s - List of strings,               shape - TensorShapeProto,               list.shape - List of TensorShapeProto,  type - DataType,                        list.type - List of DataType,
Example: length attr_length iIf your attribute type is not shown in the list above, or you want to implement your own attribute parsing,
omit the <type> parameter.
Enter 'q' when finished:    q**********************************************************************************************
Check your answers for TensorFlow* extractor generation:1.  Layer name:                                                            [cosh]
2.  Automatically parse all parameters from model file:                    No
3.  Parameters entered:  <param1> <new name1> <type1>                      []**********************************************************************************************Do you want to change any answer (y/n) ? Default 'no'
nDo you want to use the layer name as the operation name? (y/n)    yDoes your operation change shape? (y/n)    n**********************************************************************************************
Check your answers for the Model Optimizer operation generation:4.  Use layer name as operation name? (y/n)                                Yes
5.  Operation changes shape? (y/n)                                         No**********************************************************************************************Do you want to change any answer (y/n) ? Default 'no'

cosh为生成的kernel函数,生成如下文件目录:

The following folders and files were created:Stub file for TensorFlow Model Optimizer extractor is in /home/magicdepth/cl_tutorial/cl_cosh/user_mo_extensions/front/tf folder
Stub file for the Model Optimizer operation is in /home/magicdepth/cl_tutorial/cl_cosh/user_mo_extensions/ops folder
Stub files for the Inference Engine CPU extension are in /home/magicdepth/cl_tutorial/cl_cosh/user_ie_extensions/cpu folder
Stub files for the Inference Engine GPU extension are in /home/magicdepth/cl_tutorial/cl_cosh/user_ie_extensions/gpu folder

生成的目录如下:

  • TensorFlow Model Optimizer extractor extension:

    • $CLWS/cl_cosh/user_mo_extensions/front/tf/

      • cosh_ext.py
  • Model Optimizer operation extension:
    • $CLWS/cl_cosh/user_mo_extensions/ops

      • cosh.py
  • Inference Engine CPU extension:
    • $CLWS/cl_cosh/user_ie_extensions/cpu

      • ext_cosh.cpp
      • CMakeLists.txt
  • Inference Engine GPU extension:
    • $CLWS/cl_cosh/user_ie_extensions/gpu

      • cosh_kernel.cl
      • cosh_kernel.xml

使用Model Optimizer生成IR

在用例中解释了生成的文件中比较关键的点,自己写的内核函数是如何挂载到拓扑网络中,以及参数如何传递的.将不再解释

$CLWS/tf_model/mo_tf.py文件生成IR

cd $CLWS/tf_model
mo_tf.py --input_meta_graph model.ckpt.meta --batch 1 --output "ModCosh/Activation_8/softmax_output" --extensions $CLWS/cl_cosh/user_mo_extensions --output_dir $CLWS/cl_ext_cosh

生成的xml和bin文件如下:

至此custom layer在model option上步骤以及结束,主要是生成xml和bin文件,其中模型文件里面具有向tensorflow注册kernel,用于告诉网络,需要调用的是哪个自定义kernel

编写kernel

上述流程只是在生成的模型告诉了openvino在调用时需要调用哪个kernel,到目前为止还没有编写kernel,在kernel编写时由基于CPU,GPU和VPU版本,CPU版本直接使用c/C++编写即可, GPU和VPU编写是使用opencl,opencl kernel的编写在前面有介绍,先介绍下CPU版本

在使用exgen.py脚本中,生成了整个框架目录,其中kernel部分是在/cl_tutorial/cl_cosh/user_ie_extensions目录下,可以看到由CPU和gpu的目录,但是缺少VPU的目录,官方脚本中没有提供VPU方面,需要花时间摸索,目录还没搞通

CPU目录下的文件如下:

算法主要实现是在ext_cosh.cpp文件中,将addConfig打开,默认是注释掉

kernel实现是在exectute中

修改cmake, ~/cl_tutorial/cl_cosh/user_ie_extensions/cpu/CMakeLists.txt.

修改TARGET_NAME:

添加openvino头文件

添加link

将tbb lib添加到编译中

编译kerel:

编译OK

运行:

~/inference_engine_samples_build/intel64/Release/classification_sample_async -i $CLT/../pics/dog.bmp -m $CLWS/tf_model/model.ckpt.xml -d CPU -l $CLWS/cl_cosh/user_ie_extensions/cpu/build/libcosh_cpu_extension.so

运行结果:

OpenVINO 2019 R2.0 Custom Layer Implementation for linux(2)相关推荐

  1. OpenVINO 2019 R2.0 Custom Layer Implementation for linux(1)

    OpenVINO除了支持比较流行的Caffe等深度学习框架,还支持用户自定义网络Custom Layer层. Custom Layer OpenVINO的Custom Layer的实施包括Model ...

  2. OpenVINO Inference Engine之custom Layer自定义算法(VPU版本)

    OpenVINO不仅支持广泛的已知深度学习架构(Caff,TensorFlow等),还支持用户自定义CNN 算法,拥有良好的可扩展性.要使用用户自定义功能就要使用到Inference Engine K ...

  3. TensorRT Samples: MNIST(Plugin, add a custom layer)

    关于TensorRT的介绍可以参考:http://blog.csdn.net/fengbingchun/article/details/78469551 以下是参考TensorRT 2.1.2中的sa ...

  4. mysql+server+80_Windows Server 2019 IIS10.0+PHP(FastCGI)+MySQL环境搭建教程

    准备篇 一.环境说明: 操作系统:Windows Server 2019 PHP版本:php 7.3.11 MySQL版本:MySQL 8.0.18.0 二.相关软件下载: 1.PHP下载地址: ht ...

  5. caffe 添加自定义层(custom layer)

    在<剖析Caffe源码之Layer>可以知道layer是所有层的基类,由此类派生出各种不同的不同的层,其如下图所示: 由此扩展出了各种不同的层,基本能满足要求,但是有时候在搭建拓扑网络时, ...

  6. php7 iis10 mysql_Windows Server 2019 IIS10.0+PHP(FastCGI)+MySQL环境搭建教程

    准备篇 一.环境说明: 操作系统:Windows Server 2019 PHP版本:php 7.3.11 MySQL版本:MySQL 8.0.18.0 二.相关软件下载: 1.PHP下载地址: ht ...

  7. iis10 php,Windows Server 2019 IIS10.0+PHP(FastCGI)+MySQL环境搭建教程

    准备篇 一.环境说明: 操作系统:Windows Server 2019 PHP版本:php 7.3.11 MySQL版本:MySQL 8.0.18.0 二.相关软件下载: 1.PHP下载地址: ht ...

  8. 【keras】Input 0 of layer conv2d is incompatible with the layer. expected ndim=4, found ndim=3

    在构建卷积神经网络时,遇到了这个错误 ValueError: Input 0 of layer conv2d is incompatible with the layer: expected ndim ...

  9. Adobe Photoshop CC 2019 20.0.6软件免费下载及安装教程

    Adobe Photoshop CC 2019 20.0.6免费下载最新版本的Windows.在上传之前,程序和所有文件都会被手动检查和安装,程序运行正常,没有任何问题.它是完全脱机安装程序独立设置的 ...

最新文章

  1. python能绘制统计图吗-特征锦囊:常用的统计图在Python里怎么画?
  2. 笔记本安装ubuntu后触控板失灵
  3. Hive集成HBase详解
  4. fedora yum mysql_Fedora14使用yum安装mysql
  5. Netty in action—Netty传输服务
  6. 【代码笔记】iOS-竖状图
  7. Struts action-mapping 元素讲解
  8. Atitit file cms api jcr1 jcr2 目录 1.1. 么是JSR-170幸运的是,被称为Java内容仓库(Java Content Repository,JCR)的JSR-
  9. 推荐算法之协同过滤算法详解(原理,流程,步骤,适用场景)
  10. 腾讯信鸽Java服务端推送IOS静默消息的IOS客户端走两个回调的问题
  11. Python3爬虫-selenium爬取百度文库
  12. 集中隔离第3天(其实说的都是前2天的事) 2022.2.27
  13. 项目管理中的进度控制与目标计划
  14. 外星人大战----------------------游戏开发(四)
  15. 几何光学学习笔记(7)- 3.1 理想光学系统
  16. Lambda使用指北(上)
  17. 安卓设备门禁识别开发_基于android手机的视频通话门禁控制系统
  18. WinCC智能报表(代替热风炉岗位工手抄日志)
  19. kafka.common.InconsistentClusterIdException: The Cluster ID 8ytUwdxNRXqINczxiKozcA doesn‘t match sto
  20. 机器学习算法工程师到底应该学哪个编程语言?

热门文章

  1. IntelliJ IDEA下的使用git
  2. 算法的力量万变不离其宗 -- 李开复
  3. haskell vscode下的环境搭配(包含各种坑的解决办法)
  4. 解决ajax重复提交问题?
  5. Android源码解析:UI绘制流程之测量.md
  6. 《DirectX 9.0 3D游戏开发编程基础》 第一章 初始化Direct3D 读书笔记
  7. linux学习之路之使用脚本来复制二进制程序和所需的库文件
  8. 真实的金陵十三钗:15名传教士的爱与泪
  9. 程 序 测 试 规 范
  10. 源码编译mysql5.5_源码编译安装MySQL5.5