// 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; float a0 = x1 - x; float a1 = x - x0; float b0 = y1 - y; float b1 = y - y0; if (x1 >= w) { x1 = w-1; a0 = 1.f; a1 = 0.f; } if (y1 >= h) { y1 = h-1; b0 = 1.f; b1 = 0.f; } 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