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