色哟哟视频在线观看-色哟哟视频在线-色哟哟欧美15最新在线-色哟哟免费在线观看-国产l精品国产亚洲区在线观看-国产l精品国产亚洲区久久

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
电子发烧友
开通电子发烧友VIP会员 尊享10大特权
海量资料免费下载
精品直播免费看
优质内容免费畅学
课程9折专享价
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

自定義算子開發

地瓜機器人 ? 2022-04-07 16:11 ? 次閱讀

地平線工具鏈中已經支持了豐富的算子,在大多數情況下,您的模型應該可以通過使用hb_mapper工具完成轉換并順利部署到地平線芯片上。 少部分算子不支持情況下,我們建議您先嘗試下替換算子的可能性,這樣有利于將地平線芯片能力充分發揮出來。

自定義算子目前只提供CPU算子開發能力,可自定義onnx算子以及caffe算子。一個完整的自定義算子應用過程包括注冊算子、算子實現、含自定義算子模型轉換和運行含自定義op模型四個階段。

1 自定義onnx算子

1.1 將含有自定義算子的pytorch模型導出ONNX

使用torch.onnx.register_custom_op_symbolic注冊自定義算子,再導出onnx模型。有以下幾處配置參數需要注意:

1. register_custom_op_symbolic函數的第一個參數'::adaptive_avg_pool2d'為pytorch對應操作符名稱,若填寫錯誤,則會導致自定義算子注冊失敗

2. 操作域必須設置為horizon.custom,算子類型為PyOp

3. class_name_s需要與算子實現文件中的類名相對應

4. module_s與算子實現文件名相同,若算子實現文件在當前目錄的子目錄(custom_op)中,要將相對路徑包含進去:"custom_op/sample_custom"

5. 必須指定input_types_i、output_types_i、output_shape_s三個參數

6. 注意指定opset_version為10或11

參考代碼:

import torch

from horizon_nn.horizon_onnx.onnx_pb import TensorProto

from torch.onnx.utils import register_custom_op_symbolic

#prepare your model and input_data

def horizon_pool(g, input, output_size):

return g.op(

'horizon.custom::PyOp', #required, ! must be 'horizon.custom' domain !

input,

class_name_s="GlobalAveragePool", #required ! must match the class def name in sample_custom python file !

compute_s="compute", #optional, 'compute' by default

module_s="sample_custom",#required ! must match the file name of the "op_register_files" !

input_types_i=[TensorProto.FLOAT], #required

output_types_i=[TensorProto.FLOAT],#required

output_shape_s=["1, 1024, 1, 1"]) #required

register_custom_op_symbolic('::adaptive_avg_pool2d',

horizon_pool,

opset_version=11)

torch.onnx.export(model, input_data, "custom_op.onnx", opset_version=11)

1.2 算子實現

對應上一節注冊自定義算子時配置的算子實現文件(class_name需要保持一致)。

#sample_custom.py

import numpy as np

from horizon_nn.custom import op_implement_register

@op_implement_register("CustomIdentity")

class CustomIdentity(object):

def __init__(self, kernel_size, threshold):

self._kernel_size = kernel_size

self._default_threshold = threshold

def compute(self, X):

return X

@op_implement_register("GlobalAveragePool")

class GlobalAveragePool(object):

def __init__(self):

pass

def compute(self, X):

return np.nanmean(X, axis=(2, 3)).reshape(-1, 1024, 1, 1)

2 自定義caffe算子

2.1 修改prototxt

在原始模型文件中,將自定義算子對應的類型標記為"Custom" ,并設置custom_param。params 是算子的傳入參數,指定方式為‘param_name’:param_value, 多個參數之間使用 \n 分隔。

layer {

name: "hr_op"

type: "Custom"

bottom: "res3d_in"

top: "res3d"

custom_param {

kind: "CustomIdentity"

shape {

dim: 1

dim: 512

dim: 28

dim: 28

}

params: "'kernel_size': 10 \n'threshold': 0.5"

}

}

2.2 算子實現

相比于onnx模型,caffe模型的自定義算子實現還需要提供該算子的輸出尺寸。

#sample_custom.py

from horizon_nn.custom.op_registration import op_implement_register, op_shape_infer_register

@op_implement_register("CustomIdentity")

class CustomIdentity(object):

def __init__(self, kernel_size, threshold):

self._kernel_size = kernel_size

self._default_threshold = threshold

def compute(self, X):

return X

@op_shape_infer_register("CustomIdentity")

def infer_shape(inputs_shape):

"""Infer the output shapes of the custom operator.

Arguments:

input_shapes: A list of input shapes.

Returns:

Return a list of custom operator's output shapes.

"""

outputs_shape = inputs_shape

return outputs_shape

3 含自定義算子的模型轉換

在模型轉換配置文件中,添加自定義算子相關參數,示例如下:

poYBAGJOnKmALm2DAAI_kfFzMYs348.png

custom_op_method固定使用 register

op_register_files自定義算子計算的實現文件,如果有多份實現,使用 ‘;’ 將各個文件分開即可。

4 含自定義算子的模型推理

想將包含自定算子的.bin模型順利部署到開發板上,還需要提供自定義算子的C++代碼實現。 您可以使用下文提供的模板進行修改:

頭文件:

// custom_identity.h

#ifndef ADVANCED_SAMPLES_CUSTOM_IDENTITY_H_

#define ADVANCED_SAMPLES_CUSTOM_IDENTITY_H_

#include

#include

#include "dnn/hb_dnn.h"

#include "dnn/plugin/hb_dnn_layer.h"

#include "dnn/plugin/hb_dnn_ndarray.h"

namespace hobot {

namespace dnn {

Layer *CustomIdentity_layer_creator();

class CustomIdentity : public Layer {

public:

CustomIdentity() = default;

~CustomIdentity() override = default;

public:

int32_t Init(const Attribute &attributes) override;

int32_t Forward(const std::vector &bottomBlobs,

std::vector &topBlobs,

const hbDNNInferCtrlParam *inferCtrlParam) override;

std::string GetType() const override { return "CustomIdentity"; }

private:

std::string module_;

};

} // namespace dnn

} // namespace hobot

#endif

cpp文件:

// custom_identity.cpp

#include "custom_identity.h"

namespace hobot {

namespace dnn {

Layer *CustomIdentity_layer_creator() { return new CustomIdentity; }

int32_t CustomIdentity::Init(const Attribute &attributes) {

// unused attribute, just demonstrating

attributes.GetAttributeValue(&module_, "module");

return 0;

}

int32_t CustomIdentity::Forward(const std::vector &bottomBlobs,

std::vector &topBlobs,

const hbDNNInferCtrlParam *inferCtrlParam) {

const NDArray *input = bottomBlobs[0];

NDArray *out = topBlobs[0];

const auto *input_data = input->Dptr();

auto *out_data = out->Dptr();

uint32_t size = input->Size();

for (uint32_t i = 0U; i < size; i++) {?

out_data[i] = input_data[i];

}

return 0;

}

} // namespace dnn

} // namespace hobot

將以上兩個文件放在當前工程目錄下之后,編寫infer代碼時僅需要在加載模型之前增加對算子的注冊即可,注冊可參考以下代碼:

//infer.cpp

#include "custom_identity.h"

// register custom layer

hbDNNRegisterLayerCreator("CustomIdentity",

hobot::dnn::CustomIdentity_layer_creator)

本文轉載自地平線開發者社區:https://developer.horizon.ai
原作者:顏值即正義
原文鏈接:https://developer.horizon.ai/forumDetail/71036525692881018

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 算子
    +關注

    關注

    0

    文章

    16

    瀏覽量

    7320
  • 模型轉換
    +關注

    關注

    0

    文章

    4

    瀏覽量

    5263
收藏 0人收藏
  • voidpbq1

評論

相關推薦

LabVIEW運動控制(三):EtherCAT運動控制器的高效加工指令自定義封裝

LabVIEW高效加工指令自定義封裝
的頭像 發表于 04-08 13:49 ?1037次閱讀
LabVIEW運動控制(三):EtherCAT運動控制器的高效加工指令<b class='flag-5'>自定義</b>封裝

如何添加自定義單板

開發過程中,用戶有時需要創建自定義板配置。本節將通過一個實例講解用戶如何創建屬于自己的machine,下面以g2l-test.conf為例進行說明。
的頭像 發表于 03-12 14:43 ?408次閱讀

如何快速創建用戶自定義Board和App工程

可將該文件夾復制到用戶自定義的工作目錄(workspace)中,基于此模板進行開發。本模板主要牽涉到的用戶自定義的文件有:用戶板級文件Board用戶應用程序App用
的頭像 發表于 02-08 13:38 ?315次閱讀
如何快速創建用戶<b class='flag-5'>自定義</b>Board和App工程

Altium Designer 15.0自定義元件設計

電子發燒友網站提供《Altium Designer 15.0自定義元件設計.pdf》資料免費下載
發表于 01-21 15:04 ?0次下載
Altium Designer 15.0<b class='flag-5'>自定義</b>元件設計

think-cell:自定義think-cell(四)

C.5 設置默認議程幻燈片布局 think-cell 議程可以在演示文稿中使用特定的自定義布局來定義議程、位置和議程幻燈片上的其他形狀,例如標題或圖片。通過將此自定義布局添加到模板,您可以為整個組織
的頭像 發表于 01-13 10:37 ?349次閱讀
think-cell:<b class='flag-5'>自定義</b>think-cell(四)

think-cell;自定義think-cell(一)

本章介紹如何自定義 think-cell,即如何更改默認顏色和其他默認屬性;這是通過 think-cell 的樣式文件完成的,這些文件將在前四個部分中進行討論。 第五部分 C.5 設置默認議程幻燈片
的頭像 發表于 01-08 11:31 ?501次閱讀
think-cell;<b class='flag-5'>自定義</b>think-cell(一)

創建自定義的基于閃存的引導加載程序(BSL)

電子發燒友網站提供《創建自定義的基于閃存的引導加載程序(BSL).pdf》資料免費下載
發表于 09-19 10:50 ?0次下載
創建<b class='flag-5'>自定義</b>的基于閃存的引導加載程序(BSL)

智能工業主板:ROC-RK3576-PC

Transformer架構下超大規模參數模型的私有化部署,支持多種深度學習框架、自定義算子開發、Docker容器化管理技術。配置外部看門狗,具備工業級穩定性,廣泛適用于AI本地
的頭像 發表于 09-11 08:00 ?649次閱讀
智能工業主板:ROC-RK3576-PC

NVIDIA NeMo加速并簡化自定義模型開發

如果企業希望充分發揮出 AI 的力量,就需要根據其行業需求量身定制的自定義模型。
的頭像 發表于 07-26 11:17 ?1049次閱讀
NVIDIA NeMo加速并簡化<b class='flag-5'>自定義</b>模型<b class='flag-5'>開發</b>

低功耗大模型主板:ROC-RK3576-PC

Transformer架構下超大規模參數模型的私有化部署,支持多種深度學習框架、自定義算子開發、Docker容器化管理技術。配置外部看門狗,具備工業級穩定性,廣泛適用于AI本地
的頭像 發表于 05-30 08:02 ?1295次閱讀
低功耗大模型主板:ROC-RK3576-PC

HarmonyOS開發案例:【 自定義彈窗】

基于ArkTS的聲明式開發范式實現了三種不同的彈窗,第一種直接使用公共組件,后兩種使用CustomDialogController實現自定義彈窗
的頭像 發表于 05-16 18:18 ?1712次閱讀
HarmonyOS<b class='flag-5'>開發</b>案例:【 <b class='flag-5'>自定義</b>彈窗】

AWTK 開源串口屏開發(18) - 用 C 語言自定義命令

編寫代碼即可實現常見的應用。但是,有時候我們需要自定義一些命令,以實現一些特殊的功能。本文檔介紹如何使用C語言自定義命令。1.實現hmi_model_cmd_t接口
的頭像 發表于 05-11 08:24 ?615次閱讀
AWTK 開源串口屏<b class='flag-5'>開發</b>(18) - 用 C 語言<b class='flag-5'>自定義</b>命令

TSMaster 自定義 LIN 調度表編程指導

LIN(LocalInterconnectNetwork)協議調度表是用于LIN總線通信中的消息調度的一種機制,我們收到越來越多來自不同用戶希望能夠通過接口實現自定義LIN調度表的需求。所以在
的頭像 發表于 05-11 08:21 ?962次閱讀
TSMaster <b class='flag-5'>自定義</b> LIN 調度表編程指導

HarmonyOS開發案例:【UIAbility和自定義組件生命周期】

本文檔主要描述了應用運行過程中UIAbility和自定義組件的生命周期。對于UIAbility,描述了Create、Foreground、Background、Destroy四種生命周期。對于頁面
的頭像 發表于 05-10 15:31 ?1707次閱讀
HarmonyOS<b class='flag-5'>開發</b>案例:【UIAbility和<b class='flag-5'>自定義</b>組件生命周期】

HarmonyOS實戰開發-深度探索與打造個性化自定義組件

今天分享一下 什么是自定義組件?及其自定義組件的實戰。 做過前端或者android開發的都知道自定義組件,鴻蒙中顯示在界面上的UI都稱為組件,小打一個按鈕,再到一個列表。 鴻蒙提供的組
發表于 05-08 16:30

電子發燒友

中國電子工程師最喜歡的網站

  • 2931785位工程師會員交流學習
  • 獲取您個性化的科技前沿技術信息
  • 參加活動獲取豐厚的禮品
主站蜘蛛池模板: 好紧好湿太硬了我太爽了文字 | 色婷婷99综合久久久精品 | 久久9精品区-无套内射无码 | 神马影院午夜伦理限级 | 9位美女厕所撒尿11分 | 国产欧美一区二区精品仙草咪 | 麻豆乱码一卡二卡三卡视频 | 果冻传媒妈妈要儿子 | 精品午夜久久福利大片免费 | 亚洲AV无码乱码国产精品品麻豆 | 亚洲精品无码不卡在线播HE | 男生脱美女内裤内衣动态图 | 亚洲精品无夜久久久久久久久 | 久久99re7在线视频精品 | 91精品婷婷国产综合久久8 | 麻豆AV无码精品一区二区 | 999av视频 | 最近的2019中文字幕国语完整版 | 一本色道久久88加勒比—综合 | 强开少妇嫩苞又嫩又紧九色 | 巨乳中文无码亚洲 | 欧美午夜特黄AAAAAA片 | 国产AV天堂亚洲AV麻豆 | 国产精品久久久久久久久免费下载 | 亚洲精品国产一区二区贰佰信息网 | 欧美日韩久久久精品A片 | 97精品国偷拍自产在线 | 国产亚洲欧洲日韩在线观看 | 韩国伦理片2018在线播放免费观看 | 午夜欧洲亚洲AV永久无码精品 | 毛片在线播放网址 | 忘忧草在线影院WWW日本动漫 | 男的插曲女的下面免费APP | caoporn超碰| 亚洲一区在线观看视频 | 国产精品无码AV天天爽人妻蜜桃 | 亚洲国产AV一区二区三区四区 | 男女车车的车车网站W98免费 | 欧美高跟镣铐bdsm视频 | 国产精品自产拍在线观看网站 | 国产高清在线露脸一区 |