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 4.8 kB

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