From c44e49d3e9b7954151e1e2ad659b779d1c4b80ff Mon Sep 17 00:00:00 2001 From: nihuini Date: Wed, 21 Nov 2018 15:55:28 +0800 Subject: [PATCH] implement roialign layer --- src/CMakeLists.txt | 1 + src/layer/roialign.cpp | 145 +++++++++++++++++++++++++++++++++++++++++ src/layer/roialign.h | 39 +++++++++++ 3 files changed, 185 insertions(+) create mode 100644 src/layer/roialign.cpp create mode 100644 src/layer/roialign.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2a12e9350..21180c046 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -146,6 +146,7 @@ ncnn_add_layer(Quantize) ncnn_add_layer(Dequantize) ncnn_add_layer(Yolov3DetectionOutput) ncnn_add_layer(PSROIPooling) +ncnn_add_layer(ROIAlign OFF) add_library(ncnn STATIC ${ncnn_SRCS}) diff --git a/src/layer/roialign.cpp b/src/layer/roialign.cpp new file mode 100644 index 000000000..18ba3933c --- /dev/null +++ b/src/layer/roialign.cpp @@ -0,0 +1,145 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include "roialign.h" +#include +#include + +namespace ncnn { + +DEFINE_LAYER_CREATOR(ROIAlign) + +ROIAlign::ROIAlign() +{ +} + +int ROIAlign::load_param(const ParamDict& pd) +{ + pooled_width = pd.get(0, 0); + pooled_height = pd.get(1, 0); + spatial_scale = pd.get(2, 1.f); + + return 0; +} + +static inline float bilinear_interpolate(const float* ptr, int w, int h, float x, float y) +{ + int x0 = x; + int x1 = x0 + 1; + int y0 = y; + int y1 = y0 + 1; + + if (x1 >= w) + x1 = w-1; + if (y1 >= h) + y1 = h-1; + + float a0 = x1 - x; + float a1 = x - x0; + float b0 = y1 - y; + float b1 = y - y0; + + float r0 = ptr[ y0 * w + x0 ] * a0 + ptr[ y0 * w + x1 ] * a1; + float r1 = ptr[ y1 * w + x0 ] * a0 + ptr[ y1 * w + x1 ] * a1; + + float v = r0 * b0 + r1 * b1; + + return v; +} + +int ROIAlign::forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const +{ + const Mat& bottom_blob = bottom_blobs[0]; + int w = bottom_blob.w; + int h = bottom_blob.h; + size_t elemsize = bottom_blob.elemsize; + int channels = bottom_blob.c; + + const Mat& roi_blob = bottom_blobs[1]; + + Mat& top_blob = top_blobs[0]; + top_blob.create(pooled_width, pooled_height, channels, elemsize, opt.blob_allocator); + if (top_blob.empty()) + return -100; + + // For each ROI R = [x y w h]: avg pool over R + const float* roi_ptr = roi_blob; + + float roi_x1 = roi_ptr[0] * spatial_scale; + float roi_y1 = roi_ptr[1] * spatial_scale; + float roi_x2 = roi_ptr[2] * spatial_scale; + float roi_y2 = roi_ptr[3] * spatial_scale; + + float roi_w = std::max(roi_x2 - roi_x1, 1.f); + float roi_h = std::max(roi_y2 - roi_y1, 1.f); + + float bin_size_w = roi_w / (float)pooled_width; + float bin_size_h = roi_h / (float)pooled_height; + + #pragma omp parallel for num_threads(opt.num_threads) + for (int q=0; q& bottom_blobs, std::vector& top_blobs, const Option& opt) const; + +public: + int pooled_width; + int pooled_height; + float spatial_scale; +}; + +} // namespace ncnn + +#endif // LAYER_ROIALIGN_H