| 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 "pooling.h" |
| 16 | |
| 17 | #include "layer_type.h" |
| 18 | |
| 19 | #include <float.h> |
| 20 | |
| 21 | namespace ncnn { |
| 22 | |
| 23 | Pooling::Pooling() |
| 24 | { |
| 25 | one_blob_only = true; |
| 26 | support_inplace = false; |
| 27 | } |
| 28 | |
| 29 | int Pooling::load_param(const ParamDict& pd) |
| 30 | { |
| 31 | pooling_type = pd.get(0, 0); |
| 32 | kernel_w = pd.get(1, 0); |
| 33 | kernel_h = pd.get(11, kernel_w); |
| 34 | stride_w = pd.get(2, 1); |
| 35 | stride_h = pd.get(12, stride_w); |
| 36 | pad_left = pd.get(3, 0); |
| 37 | pad_right = pd.get(14, pad_left); |
| 38 | pad_top = pd.get(13, pad_left); |
| 39 | pad_bottom = pd.get(15, pad_top); |
| 40 | global_pooling = pd.get(4, 0); |
| 41 | pad_mode = pd.get(5, 0); |
| 42 | avgpool_count_include_pad = pd.get(6, 0); |
| 43 | adaptive_pooling = pd.get(7, 0); |
| 44 | out_w = pd.get(8, 0); |
| 45 | out_h = pd.get(18, out_w); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int Pooling::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 51 | { |
| 52 | // max value in NxN window |
| 53 | // avg value in NxN window |
| 54 | |
| 55 | int w = bottom_blob.w; |
| 56 | int h = bottom_blob.h; |
| 57 | int channels = bottom_blob.c; |
| 58 | size_t elemsize = bottom_blob.elemsize; |
| 59 | |
| 60 | // NCNN_LOGE("Pooling input %d x %d pad = %d %d %d %d ksize=%d %d stride=%d %d", w, h, pad_left, pad_right, pad_top, pad_bottom, kernel_w, kernel_h, stride_w, stride_h); |
| 61 | if (global_pooling) |
| 62 | { |
| 63 | top_blob.create(channels, elemsize, opt.blob_allocator); |
| 64 | if (top_blob.empty()) |
| 65 | return -100; |
| 66 | |
| 67 | int size = w * h; |
| 68 | |
| 69 | if (pooling_type == PoolMethod_MAX) |
| 70 | { |
| 71 | #pragma omp parallel for num_threads(opt.num_threads) |
| 72 | for (int q = 0; q < channels; q++) |
| 73 | { |
| 74 | const float* ptr = bottom_blob.channel(q); |
| 75 | |
| 76 | float max = ptr[0]; |
| 77 | for (int i = 0; i < size; i++) |
| 78 | { |
| 79 | max = std::max(max, ptr[i]); |
| 80 | } |
| 81 | |
| 82 | top_blob[q] = max; |
| 83 | } |
| 84 | } |
| 85 | else if (pooling_type == PoolMethod_AVE) |
| 86 | { |
| 87 | #pragma omp parallel for num_threads(opt.num_threads) |
| 88 | for (int q = 0; q < channels; q++) |
| 89 | { |
| 90 | const float* ptr = bottom_blob.channel(q); |
| 91 | |
| 92 | float sum = 0.f; |
| 93 | for (int i = 0; i < size; i++) |
| 94 | { |
| 95 | sum += ptr[i]; |
| 96 | } |
| 97 | |
| 98 | top_blob[q] = sum / size; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | if (adaptive_pooling) |
| 106 | { |
| 107 | int _out_w = out_w == -233 ? w : out_w; |
| 108 | int _out_h = out_h == -233 ? h : out_h; |
| 109 | |
| 110 | if (_out_w == w && _out_h == h) |
| 111 | { |
| 112 | top_blob = bottom_blob; |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | top_blob.create(_out_w, _out_h, channels, elemsize, opt.blob_allocator); |
| 117 | if (top_blob.empty()) |
| 118 | return -100; |
| 119 | |
| 120 | if (pooling_type == PoolMethod_MAX) |
| 121 | { |
| 122 | #pragma omp parallel for num_threads(opt.num_threads) |
| 123 | for (int q = 0; q < channels; q++) |
| 124 | { |
| 125 | const float* inptr = bottom_blob.channel(q); |
| 126 | float* outptr = top_blob.channel(q); |
| 127 | |
| 128 | for (int i = 0; i < _out_h; i++) |
| 129 | { |
| 130 | // floor div |
| 131 | const int ih0 = h * i / _out_h; |
| 132 | // ceil div |
| 133 | const int ih1 = (h * (i + 1) + _out_h - 1) / _out_h; |
| 134 | for (int j = 0; j < _out_w; j++) |
| 135 | { |
| 136 | // floor div |
| 137 | const int iw0 = w * j / _out_w; |
| 138 | // ceil div |
| 139 | const int iw1 = (w * (j + 1) + _out_w - 1) / _out_w; |
| 140 | |
| 141 | float max = inptr[ih0 * w + iw0]; |
| 142 | for (int ih = ih0; ih < ih1; ih++) |
| 143 | { |
| 144 | for (int iw = iw0; iw < iw1; iw++) |
| 145 | { |
| 146 | max = std::max(max, inptr[ih * w + iw]); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | outptr[j] = max; |
| 151 | } |
| 152 | outptr += _out_w; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | else if (pooling_type == PoolMethod_AVE) |
| 157 | { |
| 158 | #pragma omp parallel for num_threads(opt.num_threads) |
| 159 | for (int q = 0; q < channels; q++) |
| 160 | { |
| 161 | const float* inptr = bottom_blob.channel(q); |
| 162 | float* outptr = top_blob.channel(q); |
| 163 | |
| 164 | for (int i = 0; i < _out_h; i++) |
| 165 | { |
| 166 | // floor div |
| 167 | const int ih0 = h * i / _out_h; |
| 168 | // ceil div |
| 169 | const int ih1 = (h * (i + 1) + _out_h - 1) / _out_h; |
| 170 | const int hk = ih1 - ih0; |
| 171 | for (int j = 0; j < _out_w; j++) |
| 172 | { |
| 173 | // floor div |
| 174 | const int iw0 = w * j / _out_w; |
| 175 | // ceil div |
| 176 | const int iw1 = (w * (j + 1) + _out_w - 1) / _out_w; |
| 177 | const int wk = iw1 - iw0; |
| 178 | |
| 179 | float sum = 0; |
| 180 | for (int ih = ih0; ih < ih1; ih++) |
| 181 | { |
| 182 | for (int iw = iw0; iw < iw1; iw++) |
| 183 | { |
| 184 | sum += inptr[ih * w + iw]; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | outptr[j] = sum / hk / wk; |
| 189 | } |
| 190 | |
| 191 | outptr += _out_w; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | Mat bottom_blob_bordered; |
| 200 | make_padding(bottom_blob, bottom_blob_bordered, opt); |
| 201 | if (bottom_blob_bordered.empty()) |
| 202 | return -100; |
| 203 | |
| 204 | w = bottom_blob_bordered.w; |
| 205 | h = bottom_blob_bordered.h; |
| 206 | |
| 207 | int outw = (w - kernel_w) / stride_w + 1; |
| 208 | int outh = (h - kernel_h) / stride_h + 1; |
| 209 | |
| 210 | top_blob.create(outw, outh, channels, elemsize, opt.blob_allocator); |
| 211 | if (top_blob.empty()) |
| 212 | return -100; |
| 213 | |
| 214 | const int maxk = kernel_w * kernel_h; |
| 215 | |
| 216 | // kernel offsets |
| 217 | std::vector<int> _space_ofs(maxk); |
| 218 | int* space_ofs = &_space_ofs[0]; |
| 219 | { |
| 220 | int p1 = 0; |
| 221 | int p2 = 0; |
| 222 | int gap = w - kernel_w; |
| 223 | for (int i = 0; i < kernel_h; i++) |
| 224 | { |
| 225 | for (int j = 0; j < kernel_w; j++) |
| 226 | { |
| 227 | space_ofs[p1] = p2; |
| 228 | p1++; |
| 229 | p2++; |
| 230 | } |
| 231 | p2 += gap; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (pooling_type == PoolMethod_MAX) |
| 236 | { |
| 237 | #pragma omp parallel for num_threads(opt.num_threads) |
| 238 | for (int q = 0; q < channels; q++) |
| 239 | { |
| 240 | const Mat m = bottom_blob_bordered.channel(q); |
| 241 | float* outptr = top_blob.channel(q); |
| 242 | |
| 243 | for (int i = 0; i < outh; i++) |
| 244 | { |
| 245 | for (int j = 0; j < outw; j++) |
| 246 | { |
| 247 | const float* sptr = m.row(i * stride_h) + j * stride_w; |
| 248 | |
| 249 | float max = sptr[0]; |
| 250 | |
| 251 | for (int k = 0; k < maxk; k++) |
| 252 | { |
| 253 | float val = sptr[space_ofs[k]]; |
| 254 | max = std::max(max, val); |
| 255 | } |
| 256 | |
| 257 | outptr[j] = max; |
| 258 | } |
| 259 | |
| 260 | outptr += outw; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | else if (pooling_type == PoolMethod_AVE) |
| 265 | { |
| 266 | if (avgpool_count_include_pad == 0) |
| 267 | { |
| 268 | int wtailpad = 0; |
| 269 | int htailpad = 0; |
| 270 | |
| 271 | if (pad_mode == 0) // full padding |
| 272 | { |
| 273 | wtailpad = bottom_blob_bordered.w - bottom_blob.w - pad_left - pad_right; |
| 274 | htailpad = bottom_blob_bordered.h - bottom_blob.h - pad_top - pad_bottom; |
| 275 | } |
| 276 | |
| 277 | #pragma omp parallel for num_threads(opt.num_threads) |
| 278 | for (int q = 0; q < channels; q++) |
| 279 | { |
| 280 | const Mat m = bottom_blob_bordered.channel(q); |
| 281 | float* outptr = top_blob.channel(q); |
| 282 | |
| 283 | for (int i = 0; i < outh; i++) |
| 284 | { |
| 285 | int sy0 = i * stride_h; |
| 286 | |
| 287 | for (int j = 0; j < outw; j++) |
| 288 | { |
| 289 | int sx0 = j * stride_w; |
| 290 | |
| 291 | float sum = 0; |
| 292 | int area = 0; |
| 293 | |
| 294 | for (int ki = 0; ki < kernel_h; ki++) |
| 295 | { |
| 296 | int sy = sy0 + ki; |
| 297 | |
| 298 | if (sy < pad_top) |
| 299 | continue; |
| 300 | |
| 301 | if (sy >= h - pad_bottom - htailpad) |
| 302 | break; |
| 303 | |
| 304 | for (int kj = 0; kj < kernel_w; kj++) |
| 305 | { |
| 306 | int sx = sx0 + kj; |
| 307 | |
| 308 | if (sx < pad_left) |
| 309 | continue; |
| 310 | |
| 311 | if (sx >= w - pad_right - wtailpad) |
| 312 | break; |
| 313 | |
| 314 | float val = m.row(sy)[sx]; |
| 315 | sum += val; |
| 316 | area += 1; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | outptr[j] = sum / area; |
| 321 | } |
| 322 | |
| 323 | outptr += outw; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | else // if (avgpool_count_include_pad == 1) |
| 328 | { |
| 329 | #pragma omp parallel for num_threads(opt.num_threads) |
| 330 | for (int q = 0; q < channels; q++) |
| 331 | { |
| 332 | const Mat m = bottom_blob_bordered.channel(q); |
| 333 | float* outptr = top_blob.channel(q); |
| 334 | |
| 335 | for (int i = 0; i < outh; i++) |
| 336 | { |
| 337 | for (int j = 0; j < outw; j++) |
| 338 | { |
| 339 | const float* sptr = m.row(i * stride_h) + j * stride_w; |
| 340 | |
| 341 | float sum = 0; |
| 342 | |
| 343 | for (int k = 0; k < maxk; k++) |
| 344 | { |
| 345 | float val = sptr[space_ofs[k]]; |
| 346 | sum += val; |
| 347 | } |
| 348 | |
| 349 | outptr[j] = sum / maxk; |
| 350 | } |
| 351 | |
| 352 | outptr += outw; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | void Pooling::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, const Option& opt) const |
| 362 | { |
| 363 | int w = bottom_blob.w; |
| 364 | int h = bottom_blob.h; |
| 365 | |
| 366 | bottom_blob_bordered = bottom_blob; |
| 367 | |
| 368 | float pad_value = 0.f; |
| 369 | if (pooling_type == PoolMethod_MAX) |
| 370 | { |
| 371 | pad_value = bottom_blob.elemsize == 1 ? -128.f : -FLT_MAX; |
| 372 | } |
| 373 | else if (pooling_type == PoolMethod_AVE) |
| 374 | { |
| 375 | pad_value = 0.f; |
| 376 | } |
| 377 | |
| 378 | int wtailpad = 0; |
| 379 | int htailpad = 0; |
| 380 | |
| 381 | if (pad_mode == 0) // full padding |
| 382 | { |
| 383 | int wtail = (w + pad_left + pad_right - kernel_w) % stride_w; |
| 384 | int htail = (h + pad_top + pad_bottom - kernel_h) % stride_h; |
| 385 | |
| 386 | if (wtail != 0) |
| 387 | wtailpad = stride_w - wtail; |
| 388 | if (htail != 0) |
| 389 | htailpad = stride_h - htail; |
| 390 | |
| 391 | Option opt_b = opt; |
| 392 | opt_b.blob_allocator = opt.workspace_allocator; |
| 393 | copy_make_border(bottom_blob, bottom_blob_bordered, pad_top, pad_bottom + htailpad, pad_left, pad_right + wtailpad, BORDER_CONSTANT, pad_value, opt_b); |
| 394 | } |
| 395 | else if (pad_mode == 1) // valid padding |
| 396 | { |
| 397 | Option opt_b = opt; |
| 398 | opt_b.blob_allocator = opt.workspace_allocator; |
| 399 | copy_make_border(bottom_blob, bottom_blob_bordered, pad_top, pad_bottom, pad_left, pad_right, BORDER_CONSTANT, pad_value, opt_b); |
| 400 | } |
| 401 | else if (pad_mode == 2) // tensorflow padding=SAME or onnx padding=SAME_UPPER |
| 402 | { |
| 403 | int wpad = kernel_w + (w - 1) / stride_w * stride_w - w; |
| 404 | int hpad = kernel_h + (h - 1) / stride_h * stride_h - h; |
| 405 | if (wpad > 0 || hpad > 0) |
| 406 | { |
| 407 | Option opt_b = opt; |
| 408 | opt_b.blob_allocator = opt.workspace_allocator; |
| 409 | copy_make_border(bottom_blob, bottom_blob_bordered, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, pad_value, opt_b); |
| 410 | } |
| 411 | } |
| 412 | else if (pad_mode == 3) // onnx padding=SAME_LOWER |
| 413 | { |
| 414 | int wpad = kernel_w + (w - 1) / stride_w * stride_w - w; |
| 415 | int hpad = kernel_h + (h - 1) / stride_h * stride_h - h; |
| 416 | if (wpad > 0 || hpad > 0) |
| 417 | { |
| 418 | Option opt_b = opt; |
| 419 | opt_b.blob_allocator = opt.workspace_allocator; |
| 420 | copy_make_border(bottom_blob, bottom_blob_bordered, hpad - hpad / 2, hpad / 2, wpad - wpad / 2, wpad / 2, BORDER_CONSTANT, pad_value, opt_b); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | } // namespace ncnn |