女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取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

    瀏覽量

    7335
  • 模型轉換
    +關注

    關注

    0

    文章

    4

    瀏覽量

    5277
收藏 人收藏

    評論

    相關推薦
    熱點推薦

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

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

    如何添加自定義單板

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

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

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

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

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

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

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

    TPS659xx應用程序自定義工具

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

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

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

    如何創建TestStand自定義步驟

    在之前的課程中簡單地介紹過TestStand自帶的一些步驟類型,如測試、消息彈窗、賦值、標簽等等,這些簡單的步驟從TestStand的插入選版中就可以添加到序列中。那么在使用中如果碰到需要實現更加靈活、復雜的功能,使用自帶的一些步驟類型可能難以滿足,這時就需要使用到自定義步驟。
    的頭像 發表于 09-11 14:46 ?2129次閱讀
    如何創建TestStand<b class='flag-5'>自定義</b>步驟

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

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

    開發用于將四個ADC通道連接到MCU/MPU的自定義多通道SPI

    電子發燒友網站提供《開發用于將四個ADC通道連接到MCU/MPU的自定義多通道SPI.pdf》資料免費下載
    發表于 09-02 10:12 ?0次下載
    <b class='flag-5'>開發</b>用于將四個ADC通道連接到MCU/MPU的<b class='flag-5'>自定義</b>多通道SPI

    EtherCAT運動控制器PT/PVT實現用戶自定義軌跡規劃

    EtherCAT運動控制器PT/PVT實現用戶自定義軌跡規劃。
    的頭像 發表于 08-15 11:49 ?1083次閱讀
    EtherCAT運動控制器PT/PVT實現用戶<b class='flag-5'>自定義</b>軌跡規劃

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

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

    如何在IDF框架中使用自定義的靜態庫和動態庫?

    基于商業需要,我們需要在 ESP-IDF v4.0-rc 這個版本的IDF中開發與使用自定義庫,有如下問題請協助: 1如何利用IDF框架編寫自定義靜態庫和動態庫? 2如何在IDF框架中使用自定
    發表于 06-25 07:57