chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

自定義算子開發(fā)

地瓜機(jī)器人 ? 2022-04-07 16:11 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

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

自定義算子目前只提供CPU算子開發(fā)能力,可自定義onnx算子以及caffe算子。一個(gè)完整的自定義算子應(yīng)用過程包括注冊算子、算子實(shí)現(xiàn)、含自定義算子模型轉(zhuǎn)換和運(yùn)行含自定義op模型四個(gè)階段。

1 自定義onnx算子

1.1 將含有自定義算子的pytorch模型導(dǎo)出ONNX

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

1. register_custom_op_symbolic函數(shù)的第一個(gè)參數(shù)'::adaptive_avg_pool2d'為pytorch對應(yīng)操作符名稱,若填寫錯(cuò)誤,則會導(dǎo)致自定義算子注冊失敗

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

3. class_name_s需要與算子實(shí)現(xiàn)文件中的類名相對應(yīng)

4. module_s與算子實(shí)現(xiàn)文件名相同,若算子實(shí)現(xiàn)文件在當(dāng)前目錄的子目錄(custom_op)中,要將相對路徑包含進(jìn)去:"custom_op/sample_custom"

5. 必須指定input_types_i、output_types_i、output_shape_s三個(gè)參數(shù)

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 算子實(shí)現(xiàn)

對應(yīng)上一節(jié)注冊自定義算子時(shí)配置的算子實(shí)現(xiàn)文件(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

在原始模型文件中,將自定義算子對應(yīng)的類型標(biāo)記為"Custom" ,并設(shè)置custom_param。params 是算子的傳入?yún)?shù),指定方式為‘param_name’:param_value, 多個(gè)參數(shù)之間使用 \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 算子實(shí)現(xiàn)

相比于onnx模型,caffe模型的自定義算子實(shí)現(xiàn)還需要提供該算子的輸出尺寸。

#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 含自定義算子的模型轉(zhuǎn)換

在模型轉(zhuǎn)換配置文件中,添加自定義算子相關(guān)參數(shù),示例如下:

poYBAGJOnKmALm2DAAI_kfFzMYs348.png

custom_op_method固定使用 register;

op_register_files自定義算子計(jì)算的實(shí)現(xiàn)文件,如果有多份實(shí)現(xiàn),使用 ‘;’ 將各個(gè)文件分開即可。

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

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

頭文件:

// 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

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

//infer.cpp

#include "custom_identity.h"

// register custom layer

hbDNNRegisterLayerCreator("CustomIdentity",

hobot::dnn::CustomIdentity_layer_creator)

本文轉(zhuǎn)載自地平線開發(fā)者社區(qū):https://developer.horizon.ai
原作者:顏值即正義
原文鏈接:https://developer.horizon.ai/forumDetail/71036525692881018

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • 算子
    +關(guān)注

    關(guān)注

    0

    文章

    16

    瀏覽量

    7388
  • 模型轉(zhuǎn)換
    +關(guān)注

    關(guān)注

    0

    文章

    4

    瀏覽量

    5339
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點(diǎn)推薦

    如何使用SDK進(jìn)行自定義音頻播放功能

    在上一篇文章安信可離線語音模組 VC-01、VC-02 系列教程 【二次開發(fā)篇】自定義音頻替換失敗過程中,簡要概述了res_build_tool.py 文件, 其主要的作用就是將音頻文件進(jìn)行轉(zhuǎn)換,從而使編譯固件的時(shí)候能夠?qū)⒁纛l文件編譯到BIN中,然后在各項(xiàng)事件觸發(fā)的時(shí)候?qū)?/div>
    的頭像 發(fā)表于 09-25 15:52 ?2804次閱讀
    如何使用SDK進(jìn)行<b class='flag-5'>自定義</b>音頻播放功能

    LOTO示波器自定義解碼功能—CANFD解碼

    LOTO示波器軟件更新了自定義解碼功能,并在bilibili上傳了演示視頻,視頻鏈接: https://www.bilibili.com/video/BV1wq3ezjEjQ
    的頭像 發(fā)表于 07-11 10:34 ?503次閱讀
    LOTO示波器<b class='flag-5'>自定義</b>解碼功能—CANFD解碼

    大彩講堂:VisualTFT軟件如何自定義圓形進(jìn)度條

    VisualTFT軟件如何自定義圓形進(jìn)度條
    的頭像 發(fā)表于 07-07 17:10 ?946次閱讀
    大彩講堂:VisualTFT軟件如何<b class='flag-5'>自定義</b>圓形進(jìn)度條

    KiCad 中的自定義規(guī)則(KiCon 演講)

    “ ?Seth Hillbrand 在 KiCon US 2025 上為大家介紹了 KiCad 的規(guī)則系統(tǒng),并詳細(xì)講解了自定義規(guī)則的設(shè)計(jì)與實(shí)例。? ” ? 演講主要圍繞 加強(qiáng) KiCad 中的自定義
    的頭像 發(fā)表于 06-16 11:17 ?1087次閱讀
    KiCad 中的<b class='flag-5'>自定義</b>規(guī)則(KiCon 演講)

    HarmonyOS應(yīng)用自定義鍵盤解決方案

    自定義鍵盤是一種替換系統(tǒng)默認(rèn)鍵盤的解決方案,可實(shí)現(xiàn)鍵盤個(gè)性化交互。允許用戶結(jié)合業(yè)務(wù)需求與操作習(xí)慣,對按鍵布局進(jìn)行可視化重構(gòu)、設(shè)置多功能組合鍵位,使輸入更加便捷和舒適。在安全防護(hù)層面,自定義鍵盤可以
    的頭像 發(fā)表于 06-05 14:19 ?1232次閱讀

    如何使用自定義設(shè)置回調(diào)函數(shù)?

    你好,我正在嘗試編寫自己的自定義設(shè)置回調(diào)函數(shù),并使用 fastEnum=false。 是否有任何代碼示例或資料可供我參考? void CyU3PUsbRegisterSetupCallback
    發(fā)表于 05-21 06:11

    LabVIEW運(yùn)動(dòng)控制(三):EtherCAT運(yùn)動(dòng)控制器的高效加工指令自定義封裝

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

    如何添加自定義單板

    開發(fā)過程中,用戶有時(shí)需要?jiǎng)?chuàng)建自定義板配置。本節(jié)將通過一個(gè)實(shí)例講解用戶如何創(chuàng)建屬于自己的machine,下面以g2l-test.conf為例進(jìn)行說明。
    的頭像 發(fā)表于 03-12 14:43 ?866次閱讀

    如何快速創(chuàng)建用戶自定義Board和App工程

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

    CAN總線十萬個(gè)為什么 | CAN自定義波特率有什么用?

    導(dǎo)讀CAN總線通信中,波特率一致并不總能保證通信順暢。本文將揭秘自定義波特率的原理和應(yīng)用,探討如何通過優(yōu)化采樣點(diǎn)和提高容忍度解決通信問題,助力工程師提升通信穩(wěn)定性。通常情況下,CAN總線通信只需確保
    的頭像 發(fā)表于 02-07 11:36 ?986次閱讀
    CAN總線十萬個(gè)為什么 | CAN<b class='flag-5'>自定義</b>波特率有什么用?

    Altium Designer 15.0自定義元件設(shè)計(jì)

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

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

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

    智能語音識別照明解決方案,平臺自定義,中英切換

    智能語音識別照明方案引入NRK3502芯片,支持平臺自定義,離線控制,中英雙語切換。NRK3502具備高性能和靈活自定義能力,可推動(dòng)智能照明革新,控制其他智能設(shè)備,為國際用戶提供全方位智能生活體驗(yàn)。
    的頭像 發(fā)表于 01-10 13:23 ?661次閱讀
    智能語音識別照明解決方案,平臺<b class='flag-5'>自定義</b>,中英切換

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

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

    TPS659xx應(yīng)用程序自定義工具

    電子發(fā)燒友網(wǎng)站提供《TPS659xx應(yīng)用程序自定義工具.pdf》資料免費(fèi)下載
    發(fā)表于 11-06 10:02 ?0次下載
    TPS659xx應(yīng)用程序<b class='flag-5'>自定義</b>工具