roi_align_common.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #pragma once
  2. #include <ATen/ATen.h>
  3. namespace vision {
  4. namespace ops {
  5. namespace detail {
  6. template <typename T>
  7. struct PreCalc {
  8. int pos1;
  9. int pos2;
  10. int pos3;
  11. int pos4;
  12. T w1;
  13. T w2;
  14. T w3;
  15. T w4;
  16. };
  17. // This helper computes the interpolation weights (w1, w2...) for every sampling
  18. // point of a given box. There are pool_height * pool_width * roi_bin_grid_h *
  19. // roi_bin_grid_w such sampling points.
  20. //
  21. // The weights (w1, w2...) are computed as the areas in this figure:
  22. // https://en.wikipedia.org/wiki/Bilinear_interpolation#/media/File:Bilinear_interpolation_visualisation.svg
  23. // and pos1, pos2 etc correspond to the indices of their respective pixels.
  24. //
  25. // Note: the weights and indices are shared across all channels, which is why
  26. // they are pre-calculated prior to the main loop in the RoIAlign kernel.
  27. // implementation taken from Caffe2
  28. template <typename T>
  29. void pre_calc_for_bilinear_interpolate(
  30. int height,
  31. int width,
  32. int pooled_height,
  33. int pooled_width,
  34. T roi_start_h,
  35. T roi_start_w,
  36. T bin_size_h,
  37. T bin_size_w,
  38. int roi_bin_grid_h,
  39. int roi_bin_grid_w,
  40. std::vector<PreCalc<T>>& pre_calc) {
  41. int pre_calc_index = 0;
  42. for (int ph = 0; ph < pooled_height; ph++) {
  43. for (int pw = 0; pw < pooled_width; pw++) {
  44. for (int iy = 0; iy < roi_bin_grid_h; iy++) {
  45. const T yy = roi_start_h + ph * bin_size_h +
  46. static_cast<T>(iy + .5f) * bin_size_h /
  47. static_cast<T>(roi_bin_grid_h); // e.g., 0.5, 1.5
  48. for (int ix = 0; ix < roi_bin_grid_w; ix++) {
  49. const T xx = roi_start_w + pw * bin_size_w +
  50. static_cast<T>(ix + .5f) * bin_size_w /
  51. static_cast<T>(roi_bin_grid_w);
  52. T x = xx;
  53. T y = yy;
  54. // deal with: inverse elements are out of feature map boundary
  55. if (y < -1.0 || y > height || x < -1.0 || x > width) {
  56. // empty
  57. PreCalc<T> pc;
  58. pc.pos1 = 0;
  59. pc.pos2 = 0;
  60. pc.pos3 = 0;
  61. pc.pos4 = 0;
  62. pc.w1 = 0;
  63. pc.w2 = 0;
  64. pc.w3 = 0;
  65. pc.w4 = 0;
  66. pre_calc[pre_calc_index] = pc;
  67. pre_calc_index += 1;
  68. continue;
  69. }
  70. if (y <= 0) {
  71. y = 0;
  72. }
  73. if (x <= 0) {
  74. x = 0;
  75. }
  76. int y_low = (int)y;
  77. int x_low = (int)x;
  78. int y_high;
  79. int x_high;
  80. if (y_low >= height - 1) {
  81. y_high = y_low = height - 1;
  82. y = (T)y_low;
  83. } else {
  84. y_high = y_low + 1;
  85. }
  86. if (x_low >= width - 1) {
  87. x_high = x_low = width - 1;
  88. x = (T)x_low;
  89. } else {
  90. x_high = x_low + 1;
  91. }
  92. T ly = y - y_low;
  93. T lx = x - x_low;
  94. T hy = 1. - ly, hx = 1. - lx;
  95. T w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx;
  96. // save weights and indices
  97. PreCalc<T> pc;
  98. pc.pos1 = y_low * width + x_low;
  99. pc.pos2 = y_low * width + x_high;
  100. pc.pos3 = y_high * width + x_low;
  101. pc.pos4 = y_high * width + x_high;
  102. pc.w1 = w1;
  103. pc.w2 = w2;
  104. pc.w3 = w3;
  105. pc.w4 = w4;
  106. pre_calc[pre_calc_index] = pc;
  107. pre_calc_index += 1;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. } // namespace detail
  114. } // namespace ops
  115. } // namespace vision