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 "spp.h"
16
17 #include <math.h>
18
19 namespace ncnn {
20
SPP()21 SPP::SPP()
22 {
23 one_blob_only = true;
24 support_inplace = false;
25 }
26
load_param(const ParamDict & pd)27 int SPP::load_param(const ParamDict& pd)
28 {
29 pooling_type = pd.get(0, 0);
30 pyramid_height = pd.get(1, 1);
31
32 return 0;
33 }
34
forward(const Mat & bottom_blob,Mat & top_blob,const Option & opt) const35 int SPP::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
36 {
37 size_t elemsize = bottom_blob.elemsize;
38
39 // 1 + 4 + 16 + 64 + ... + (2*pyramid_height)^2
40 int pyramid_num_bins = ((1 << (pyramid_height * 2)) - 1) / 3;
41 top_blob.create(pyramid_num_bins, 1, 2, elemsize, opt.blob_allocator);
42 if (top_blob.empty())
43 return -100;
44
45 float* pyramid_ptr = top_blob;
46
47 // all spatial pyramids
48 for (int p = 0; p < pyramid_height; p++)
49 {
50 int w = bottom_blob.w;
51 int h = bottom_blob.h;
52 int channels = bottom_blob.c;
53
54 int num_bins = 1 << p;
55
56 int kernel_h = ceil(h / (float)num_bins);
57 int stride_h = kernel_h;
58 int remainder_h = stride_h * num_bins - h;
59 int pad_h = (remainder_h + 1) / 2;
60
61 int kernel_w = ceil(w / (float)num_bins);
62 int stride_w = kernel_w;
63 int remainder_w = stride_w * num_bins - w;
64 int pad_w = (remainder_w + 1) / 2;
65
66 // max value in NxN window
67 // avg value in NxN window
68
69 int outw = num_bins;
70 int outh = num_bins;
71
72 Mat bottom_blob_bordered = bottom_blob;
73 if (pad_h > 0 || pad_w > 0)
74 {
75 copy_make_border(bottom_blob, bottom_blob_bordered, pad_h, pad_h, pad_w, pad_w, BORDER_CONSTANT, 0.f, opt);
76 if (bottom_blob_bordered.empty())
77 return -100;
78
79 w = bottom_blob_bordered.w;
80 h = bottom_blob_bordered.h;
81 }
82
83 const int maxk = kernel_h * kernel_w;
84
85 // kernel offsets
86 std::vector<int> _space_ofs(maxk);
87 int* space_ofs = &_space_ofs[0];
88 {
89 int p1 = 0;
90 int p2 = 0;
91 int gap = w - kernel_w;
92 for (int i = 0; i < kernel_h; i++)
93 {
94 for (int j = 0; j < kernel_w; j++)
95 {
96 space_ofs[p1] = p2;
97 p1++;
98 p2++;
99 }
100 p2 += gap;
101 }
102 }
103
104 if (pooling_type == PoolMethod_MAX)
105 {
106 #pragma omp parallel for num_threads(opt.num_threads)
107 for (int q = 0; q < channels; q++)
108 {
109 const Mat m(w, h, bottom_blob_bordered.channel(q));
110 float* outptr = pyramid_ptr + outh * outw * q;
111
112 for (int i = 0; i < outh; i++)
113 {
114 for (int j = 0; j < outw; j++)
115 {
116 const float* sptr = m.row(i * stride_h) + j * stride_w;
117
118 float max = sptr[0];
119
120 for (int k = 0; k < maxk; k++)
121 {
122 float val = sptr[space_ofs[k]];
123 max = std::max(max, val);
124 }
125
126 outptr[j] = max;
127 }
128
129 outptr += outw;
130 }
131 }
132 }
133 else if (pooling_type == PoolMethod_AVE)
134 {
135 #pragma omp parallel for num_threads(opt.num_threads)
136 for (int q = 0; q < channels; q++)
137 {
138 const Mat m(w, h, bottom_blob_bordered.channel(q));
139 float* outptr = pyramid_ptr + outh * outw * q;
140
141 for (int i = 0; i < outh; i++)
142 {
143 for (int j = 0; j < outw; j++)
144 {
145 const float* sptr = m.row(i * stride_h) + j * stride_w;
146
147 float sum = 0;
148
149 for (int k = 0; k < maxk; k++)
150 {
151 float val = sptr[space_ofs[k]];
152 sum += val;
153 }
154
155 outptr[j] = sum / maxk;
156 }
157
158 outptr += outw;
159 }
160 }
161 }
162
163 pyramid_ptr += channels * outh * outw;
164 }
165
166 return 0;
167 }
168
169 } // namespace ncnn
170