// Tencent is pleased to support the open source community by making ncnn available. // // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. // // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except // in compliance with the License. You may obtain a copy of the License at // // https://opensource.org/licenses/BSD-3-Clause // // Unless required by applicable law or agreed to in writing, software distributed // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. #include "pooling.h" #include #include #include "layer_type.h" namespace ncnn { DEFINE_LAYER_CREATOR(Pooling) Pooling::Pooling() { one_blob_only = true; support_inplace = false; } int Pooling::load_param(const ParamDict& pd) { pooling_type = pd.get(0, 0); kernel_w = pd.get(1, 0); kernel_h = pd.get(11, kernel_w); stride_w = pd.get(2, 1); stride_h = pd.get(12, stride_w); pad_left = pd.get(3, 0); pad_right = pd.get(14, pad_left); pad_top = pd.get(13, pad_left); pad_bottom = pd.get(15, pad_top); global_pooling = pd.get(4, 0); pad_mode = pd.get(5, 0); avgpool_count_include_pad = pd.get(6, 0); return 0; } int Pooling::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const { // max value in NxN window // avg value in NxN window int w = bottom_blob.w; int h = bottom_blob.h; int channels = bottom_blob.c; size_t elemsize = bottom_blob.elemsize; // fprintf(stderr, "Pooling input %d x %d pad = %d %d %d %d ksize=%d %d stride=%d %d\n", w, h, pad_left, pad_right, pad_top, pad_bottom, kernel_w, kernel_h, stride_w, stride_h); if (global_pooling) { top_blob.create(channels, elemsize, opt.blob_allocator); if (top_blob.empty()) return -100; int size = w * h; if (pooling_type == PoolMethod_MAX) { #pragma omp parallel for num_threads(opt.num_threads) for (int q=0; q 0 || hpad > 0) { Option opt_b = opt; opt_b.blob_allocator = opt.workspace_allocator; copy_make_border(bottom_blob, bottom_blob_bordered, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, pad_value, opt_b); if (bottom_blob_bordered.empty()) return -100; } w = bottom_blob_bordered.w; h = bottom_blob_bordered.h; } else if (pad_mode == 3) // onnx padding=SAME_LOWER { int wpad = kernel_w + (w - 1) / stride_w * stride_w - w; int hpad = kernel_h + (h - 1) / stride_h * stride_h - h; if (wpad > 0 || hpad > 0) { Option opt_b = opt; opt_b.blob_allocator = opt.workspace_allocator; copy_make_border(bottom_blob, bottom_blob_bordered, hpad - hpad / 2, hpad / 2, wpad - wpad / 2, wpad / 2, BORDER_CONSTANT, pad_value, opt_b); if (bottom_blob_bordered.empty()) return -100; } w = bottom_blob_bordered.w; h = bottom_blob_bordered.h; } int outw = (w - kernel_w) / stride_w + 1; int outh = (h - kernel_h) / stride_h + 1; top_blob.create(outw, outh, channels, elemsize, opt.blob_allocator); if (top_blob.empty()) return -100; const int maxk = kernel_w * kernel_h; // kernel offsets std::vector _space_ofs(maxk); int* space_ofs = &_space_ofs[0]; { int p1 = 0; int p2 = 0; int gap = w - kernel_w; for (int i = 0; i < kernel_h; i++) { for (int j = 0; j < kernel_w; j++) { space_ofs[p1] = p2; p1++; p2++; } p2 += gap; } } if (pooling_type == PoolMethod_MAX) { #pragma omp parallel for num_threads(opt.num_threads) for (int q=0; q