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.

roipooling.cpp 3.7 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "roipooling.h"
  15. #include <math.h>
  16. #include <algorithm>
  17. namespace ncnn {
  18. DEFINE_LAYER_CREATOR(ROIPooling)
  19. ROIPooling::ROIPooling()
  20. {
  21. }
  22. int ROIPooling::load_param(const ParamDict& pd)
  23. {
  24. pooled_width = pd.get(0, 0);
  25. pooled_height = pd.get(1, 0);
  26. spatial_scale = pd.get(2, 1.f);
  27. return 0;
  28. }
  29. int ROIPooling::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  30. {
  31. const Mat& bottom_blob = bottom_blobs[0];
  32. int w = bottom_blob.w;
  33. int h = bottom_blob.h;
  34. size_t elemsize = bottom_blob.elemsize;
  35. int channels = bottom_blob.c;
  36. const Mat& roi_blob = bottom_blobs[1];
  37. Mat& top_blob = top_blobs[0];
  38. top_blob.create(pooled_width, pooled_height, channels, elemsize, opt.blob_allocator);
  39. if (top_blob.empty())
  40. return -100;
  41. // For each ROI R = [x y w h]: max pool over R
  42. const float* roi_ptr = roi_blob;
  43. int roi_x1 = round(roi_ptr[0] * spatial_scale);
  44. int roi_y1 = round(roi_ptr[1] * spatial_scale);
  45. int roi_x2 = round(roi_ptr[2] * spatial_scale);
  46. int roi_y2 = round(roi_ptr[3] * spatial_scale);
  47. int roi_w = std::max(roi_x2 - roi_x1 + 1, 1);
  48. int roi_h = std::max(roi_y2 - roi_y1 + 1, 1);
  49. float bin_size_w = (float)roi_w / (float)pooled_width;
  50. float bin_size_h = (float)roi_h / (float)pooled_height;
  51. #pragma omp parallel for num_threads(opt.num_threads)
  52. for (int q=0; q<channels; q++)
  53. {
  54. const float* ptr = bottom_blob.channel(q);
  55. float* outptr = top_blob.channel(q);
  56. for (int ph = 0; ph < pooled_height; ph++)
  57. {
  58. for (int pw = 0; pw < pooled_width; pw++)
  59. {
  60. // Compute pooling region for this output unit:
  61. // start (included) = floor(ph * roi_height / pooled_height)
  62. // end (excluded) = ceil((ph + 1) * roi_height / pooled_height)
  63. int hstart = roi_y1 + floor((float)(ph) * bin_size_h);
  64. int wstart = roi_x1 + floor((float)(pw) * bin_size_w);
  65. int hend = roi_y1 + ceil((float)(ph + 1) * bin_size_h);
  66. int wend = roi_x1 + ceil((float)(pw + 1) * bin_size_w);
  67. hstart = std::min(std::max(hstart, 0), h);
  68. wstart = std::min(std::max(wstart, 0), w);
  69. hend = std::min(std::max(hend, 0), h);
  70. wend = std::min(std::max(wend, 0), w);
  71. bool is_empty = (hend <= hstart) || (wend <= wstart);
  72. float max = is_empty ? 0.f : ptr[hstart * w + wstart];
  73. for (int y = hstart; y < hend; y++)
  74. {
  75. for (int x = wstart; x < wend; x++)
  76. {
  77. int index = y * w + x;
  78. max = std::max(max, ptr[index]);
  79. }
  80. }
  81. outptr[pw] = max;
  82. }
  83. outptr += pooled_width;
  84. }
  85. }
  86. return 0;
  87. }
  88. } // namespace ncnn