You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

spp.cpp 5.0 kB

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