From 697767b062cecc9d5c5f7de816e594aaebe4eaba Mon Sep 17 00:00:00 2001 From: ncnnnnn <67086033+ncnnnnn@users.noreply.github.com> Date: Fri, 9 Oct 2020 17:03:08 +0800 Subject: [PATCH] Update add-custom-layer.zh.md (#2164) --- docs/developer-guide/add-custom-layer.zh.md | 181 ++++++++++++++------ 1 file changed, 124 insertions(+), 57 deletions(-) diff --git a/docs/developer-guide/add-custom-layer.zh.md b/docs/developer-guide/add-custom-layer.zh.md index ad8c5ddcf..7e2cf0c26 100644 --- a/docs/developer-guide/add-custom-layer.zh.md +++ b/docs/developer-guide/add-custom-layer.zh.md @@ -1,4 +1,8 @@ -这里举个例子添加 Relu6,即 std::min(6, std::max(0, val)) +# NCNN增加自定义层 + +## 举例 + +这里举个例子添加自定义层次 如Relu6,即 std::min(6, std::max(0, val)) ``` Input input 0 1 input @@ -7,102 +11,165 @@ Relu6 relu6 1 1 conv2d relu6 Pooling maxpool 1 1 relu6 maxpool 0=0 1=3 2=2 3=-233 4=0 ``` -## method 1 -- 注册自定义层 -```cpp + + +## 定义源码h文件:src/layer/relu6.h + +```CPP +#ifndef LAYER_RELU6_H +#define LAYER_RELU6_H + #include "layer.h" -class Relu6 : public ncnn::Layer +namespace ncnn { + +class Relu6 : public Layer { public: - Relu6() - { - one_blob_only = true; - support_inplace = true; - } + Relu6(); - virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const - { + virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const; +}; + +} // namespace ncnn + +#endif // LAYER_RELU6_H +``` + + + +## 定义源码CPP文件:src/layer/relu6.cpp + +```CPP +#include "relu6.h" + +#include + +namespace ncnn { + +Relu6::Relu6() +{ + one_blob_only = true; + support_inplace = true; +} + +int Relu6::forward_inplace(Mat& bottom_top_blob, const Option& opt) const +{ int w = bottom_top_blob.w; int h = bottom_top_blob.h; int channels = bottom_top_blob.c; int size = w * h; #pragma omp parallel for num_threads(opt.num_threads) - for (int q=0; q weights(0); - #pragma omp parallel for - for (int q=0; q("Relu6", pd, weights, a); + if (ret != 0) { - float* outptr = relu6mat.channel(q); - for (int i=0; i