| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2018 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 "psroipooling.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | PSROIPooling::PSROIPooling() |
| 20 | { |
| 21 | one_blob_only = false; |
| 22 | support_inplace = false; |
| 23 | } |
| 24 | |
| 25 | int PSROIPooling::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | pooled_width = pd.get(0, 7); |
| 28 | pooled_height = pd.get(1, 7); |
| 29 | spatial_scale = pd.get(2, 0.0625f); |
| 30 | output_dim = pd.get(3, 0); |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | int PSROIPooling::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 36 | { |
| 37 | const Mat& bottom_blob = bottom_blobs[0]; |
| 38 | int w = bottom_blob.w; |
| 39 | int h = bottom_blob.h; |
| 40 | size_t elemsize = bottom_blob.elemsize; |
| 41 | int channels = bottom_blob.c; |
| 42 | |
| 43 | const Mat& roi_blob = bottom_blobs[1]; |
| 44 | |
| 45 | if (channels != output_dim * pooled_width * pooled_height) |
| 46 | { |
| 47 | // input channel number does not match layer parameters |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | Mat& top_blob = top_blobs[0]; |
| 52 | top_blob.create(pooled_width, pooled_height, output_dim, elemsize, opt.blob_allocator); |
| 53 | if (top_blob.empty()) |
| 54 | return -100; |
| 55 | |
| 56 | // For each ROI R = [x y w h]: avg pool over R |
| 57 | const float* roi_ptr = roi_blob; |
| 58 | |
| 59 | float roi_x1 = roundf(roi_ptr[0]) * spatial_scale; |
| 60 | float roi_y1 = roundf(roi_ptr[1]) * spatial_scale; |
| 61 | float roi_x2 = roundf(roi_ptr[2] + 1.f) * spatial_scale; |
| 62 | float roi_y2 = roundf(roi_ptr[3] + 1.f) * spatial_scale; |
| 63 | |
| 64 | float roi_w = std::max(roi_x2 - roi_x1, 0.1f); |
| 65 | float roi_h = std::max(roi_y2 - roi_y1, 0.1f); |
| 66 | |
| 67 | float bin_size_w = roi_w / (float)pooled_width; |
| 68 | float bin_size_h = roi_h / (float)pooled_height; |
| 69 | |
| 70 | #pragma omp parallel for num_threads(opt.num_threads) |
| 71 | for (int q = 0; q < output_dim; q++) |
| 72 | { |
| 73 | float* outptr = top_blob.channel(q); |
| 74 | |
| 75 | for (int ph = 0; ph < pooled_height; ph++) |
| 76 | { |
| 77 | for (int pw = 0; pw < pooled_width; pw++) |
| 78 | { |
| 79 | const float* ptr = bottom_blob.channel((q * pooled_height + ph) * pooled_width + pw); |
| 80 | |
| 81 | int hstart = static_cast<int>(floorf(roi_y1 + ph * bin_size_h)); |
| 82 | int wstart = static_cast<int>(floorf(roi_x1 + pw * bin_size_w)); |
| 83 | int hend = static_cast<int>(ceilf(roi_y1 + (ph + 1) * bin_size_h)); |
| 84 | int wend = static_cast<int>(ceilf(roi_x1 + (pw + 1) * bin_size_w)); |
| 85 | |
| 86 | hstart = std::min(std::max(hstart, 0), h); |
| 87 | wstart = std::min(std::max(wstart, 0), w); |
| 88 | hend = std::min(std::max(hend, 0), h); |
| 89 | wend = std::min(std::max(wend, 0), w); |
| 90 | |
| 91 | bool is_empty = (hend <= hstart) || (wend <= wstart); |
| 92 | int area = (hend - hstart) * (wend - wstart); |
| 93 | |
| 94 | float sum = 0.f; |
| 95 | for (int y = hstart; y < hend; y++) |
| 96 | { |
| 97 | for (int x = wstart; x < wend; x++) |
| 98 | { |
| 99 | int index = y * w + x; |
| 100 | sum += ptr[index]; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | outptr[pw] = is_empty ? 0.f : (sum / (float)area); |
| 105 | } |
| 106 | |
| 107 | outptr += pooled_width; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | } // namespace ncnn |