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