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.

psroipooling.cpp 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2018 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 "psroipooling.h"
  15. #include <math.h>
  16. namespace ncnn {
  17. PSROIPooling::PSROIPooling()
  18. {
  19. one_blob_only = false;
  20. support_inplace = false;
  21. }
  22. int PSROIPooling::load_param(const ParamDict& pd)
  23. {
  24. pooled_width = pd.get(0, 7);
  25. pooled_height = pd.get(1, 7);
  26. spatial_scale = pd.get(2, 0.0625f);
  27. output_dim = pd.get(3, 0);
  28. return 0;
  29. }
  30. int PSROIPooling::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  31. {
  32. const Mat& bottom_blob = bottom_blobs[0];
  33. int w = bottom_blob.w;
  34. int h = bottom_blob.h;
  35. size_t elemsize = bottom_blob.elemsize;
  36. int channels = bottom_blob.c;
  37. const Mat& roi_blob = bottom_blobs[1];
  38. if (channels != output_dim * pooled_width * pooled_height)
  39. {
  40. // input channel number does not match layer parameters
  41. return -1;
  42. }
  43. Mat& top_blob = top_blobs[0];
  44. top_blob.create(pooled_width, pooled_height, output_dim, elemsize, opt.blob_allocator);
  45. if (top_blob.empty())
  46. return -100;
  47. // For each ROI R = [x y w h]: avg pool over R
  48. const float* roi_ptr = roi_blob;
  49. float roi_x1 = static_cast<float>(round(roi_ptr[0]) * spatial_scale);
  50. float roi_y1 = static_cast<float>(round(roi_ptr[1]) * spatial_scale);
  51. float roi_x2 = static_cast<float>(round(roi_ptr[2] + 1.f) * spatial_scale);
  52. float roi_y2 = static_cast<float>(round(roi_ptr[3] + 1.f) * spatial_scale);
  53. float roi_w = std::max(roi_x2 - roi_x1, 0.1f);
  54. float roi_h = std::max(roi_y2 - roi_y1, 0.1f);
  55. float bin_size_w = roi_w / (float)pooled_width;
  56. float bin_size_h = roi_h / (float)pooled_height;
  57. #pragma omp parallel for num_threads(opt.num_threads)
  58. for (int q = 0; q < output_dim; q++)
  59. {
  60. float* outptr = top_blob.channel(q);
  61. for (int ph = 0; ph < pooled_height; ph++)
  62. {
  63. for (int pw = 0; pw < pooled_width; pw++)
  64. {
  65. const float* ptr = bottom_blob.channel((q * pooled_height + ph) * pooled_width + pw);
  66. int hstart = static_cast<int>(floor(roi_y1 + (float)(ph)*bin_size_h));
  67. int wstart = static_cast<int>(floor(roi_x1 + (float)(pw)*bin_size_w));
  68. int hend = static_cast<int>(ceil(roi_y1 + (float)(ph + 1) * bin_size_h));
  69. int wend = static_cast<int>(ceil(roi_x1 + (float)(pw + 1) * bin_size_w));
  70. hstart = std::min(std::max(hstart, 0), h);
  71. wstart = std::min(std::max(wstart, 0), w);
  72. hend = std::min(std::max(hend, 0), h);
  73. wend = std::min(std::max(wend, 0), w);
  74. bool is_empty = (hend <= hstart) || (wend <= wstart);
  75. int area = (hend - hstart) * (wend - wstart);
  76. float sum = 0.f;
  77. for (int y = hstart; y < hend; y++)
  78. {
  79. for (int x = wstart; x < wend; x++)
  80. {
  81. int index = y * w + x;
  82. sum += ptr[index];
  83. }
  84. }
  85. outptr[pw] = is_empty ? 0.f : (sum / (float)area);
  86. }
  87. outptr += pooled_width;
  88. }
  89. }
  90. return 0;
  91. }
  92. } // namespace ncnn