| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "roipooling.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | ROIPooling::ROIPooling() |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | int ROIPooling::load_param(const ParamDict& pd) |
| 24 | { |
| 25 | pooled_width = pd.get(0, 0); |
| 26 | pooled_height = pd.get(1, 0); |
| 27 | spatial_scale = pd.get(2, 1.f); |
| 28 | |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | int ROIPooling::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 33 | { |
| 34 | const Mat& bottom_blob = bottom_blobs[0]; |
| 35 | int w = bottom_blob.w; |
| 36 | int h = bottom_blob.h; |
| 37 | size_t elemsize = bottom_blob.elemsize; |
| 38 | int channels = bottom_blob.c; |
| 39 | |
| 40 | const Mat& roi_blob = bottom_blobs[1]; |
| 41 | |
| 42 | Mat& top_blob = top_blobs[0]; |
| 43 | top_blob.create(pooled_width, pooled_height, channels, elemsize, opt.blob_allocator); |
| 44 | if (top_blob.empty()) |
| 45 | return -100; |
| 46 | |
| 47 | // For each ROI R = [x y w h]: max pool over R |
| 48 | const float* roi_ptr = roi_blob; |
| 49 | |
| 50 | int roi_x1 = static_cast<int>(round(roi_ptr[0] * spatial_scale)); |
| 51 | int roi_y1 = static_cast<int>(round(roi_ptr[1] * spatial_scale)); |
| 52 | int roi_x2 = static_cast<int>(round(roi_ptr[2] * spatial_scale)); |
| 53 | int roi_y2 = static_cast<int>(round(roi_ptr[3] * spatial_scale)); |
| 54 | |
| 55 | int roi_w = std::max(roi_x2 - roi_x1 + 1, 1); |
| 56 | int roi_h = std::max(roi_y2 - roi_y1 + 1, 1); |
| 57 | |
| 58 | float bin_size_w = (float)roi_w / (float)pooled_width; |
| 59 | float bin_size_h = (float)roi_h / (float)pooled_height; |
| 60 | |
| 61 | #pragma omp parallel for num_threads(opt.num_threads) |
| 62 | for (int q = 0; q < channels; q++) |
| 63 | { |
| 64 | const float* ptr = bottom_blob.channel(q); |
| 65 | float* outptr = top_blob.channel(q); |
| 66 | |
| 67 | for (int ph = 0; ph < pooled_height; ph++) |
| 68 | { |
| 69 | for (int pw = 0; pw < pooled_width; pw++) |
| 70 | { |
| 71 | // Compute pooling region for this output unit: |
| 72 | // start (included) = floor(ph * roi_height / pooled_height) |
| 73 | // end (excluded) = ceil((ph + 1) * roi_height / pooled_height) |
| 74 | int hstart = static_cast<int>(roi_y1 + floorf(ph * bin_size_h)); |
| 75 | int wstart = static_cast<int>(roi_x1 + floorf(pw * bin_size_w)); |
| 76 | int hend = static_cast<int>(roi_y1 + ceilf((ph + 1) * bin_size_h)); |
| 77 | int wend = static_cast<int>(roi_x1 + ceilf((pw + 1) * bin_size_w)); |
| 78 | |
| 79 | hstart = std::min(std::max(hstart, 0), h); |
| 80 | wstart = std::min(std::max(wstart, 0), w); |
| 81 | hend = std::min(std::max(hend, 0), h); |
| 82 | wend = std::min(std::max(wend, 0), w); |
| 83 | |
| 84 | bool is_empty = (hend <= hstart) || (wend <= wstart); |
| 85 | |
| 86 | float max = is_empty ? 0.f : ptr[hstart * w + wstart]; |
| 87 | |
| 88 | for (int y = hstart; y < hend; y++) |
| 89 | { |
| 90 | for (int x = wstart; x < wend; x++) |
| 91 | { |
| 92 | int index = y * w + x; |
| 93 | max = std::max(max, ptr[index]); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | outptr[pw] = max; |
| 98 | } |
| 99 | |
| 100 | outptr += pooled_width; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | } // namespace ncnn |