nms.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/utils/perf.h"
  15. #include "ultra_infer/vision/utils/utils.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. namespace utils {
  19. // The implementation refers to
  20. // https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.4/deploy/cpp/src/utils.cc
  21. void NMS(DetectionResult *result, float iou_threshold,
  22. std::vector<int> *index) {
  23. // get sorted score indices
  24. std::vector<int> sorted_indices;
  25. if (index != nullptr) {
  26. std::map<float, int, std::greater<float>> score_map;
  27. for (size_t i = 0; i < result->scores.size(); ++i) {
  28. score_map.insert(std::pair<float, int>(result->scores[i], i));
  29. }
  30. for (auto iter : score_map) {
  31. sorted_indices.push_back(iter.second);
  32. }
  33. }
  34. utils::SortDetectionResult(result);
  35. std::vector<float> area_of_boxes(result->boxes.size());
  36. std::vector<int> suppressed(result->boxes.size(), 0);
  37. for (size_t i = 0; i < result->boxes.size(); ++i) {
  38. area_of_boxes[i] = (result->boxes[i][2] - result->boxes[i][0]) *
  39. (result->boxes[i][3] - result->boxes[i][1]);
  40. }
  41. for (size_t i = 0; i < result->boxes.size(); ++i) {
  42. if (suppressed[i] == 1) {
  43. continue;
  44. }
  45. for (size_t j = i + 1; j < result->boxes.size(); ++j) {
  46. if (suppressed[j] == 1) {
  47. continue;
  48. }
  49. float xmin = std::max(result->boxes[i][0], result->boxes[j][0]);
  50. float ymin = std::max(result->boxes[i][1], result->boxes[j][1]);
  51. float xmax = std::min(result->boxes[i][2], result->boxes[j][2]);
  52. float ymax = std::min(result->boxes[i][3], result->boxes[j][3]);
  53. float overlap_w = std::max(0.0f, xmax - xmin);
  54. float overlap_h = std::max(0.0f, ymax - ymin);
  55. float overlap_area = overlap_w * overlap_h;
  56. float overlap_ratio =
  57. overlap_area / (area_of_boxes[i] + area_of_boxes[j] - overlap_area);
  58. if (overlap_ratio > iou_threshold) {
  59. suppressed[j] = 1;
  60. }
  61. }
  62. }
  63. DetectionResult backup(*result);
  64. result->Clear();
  65. result->Reserve(suppressed.size());
  66. for (size_t i = 0; i < suppressed.size(); ++i) {
  67. if (suppressed[i] == 1) {
  68. continue;
  69. }
  70. result->boxes.emplace_back(backup.boxes[i]);
  71. result->scores.push_back(backup.scores[i]);
  72. result->label_ids.push_back(backup.label_ids[i]);
  73. if (index != nullptr) {
  74. index->push_back(sorted_indices[i]);
  75. }
  76. }
  77. }
  78. void NMS(FaceDetectionResult *result, float iou_threshold) {
  79. utils::SortDetectionResult(result);
  80. std::vector<float> area_of_boxes(result->boxes.size());
  81. std::vector<int> suppressed(result->boxes.size(), 0);
  82. for (size_t i = 0; i < result->boxes.size(); ++i) {
  83. area_of_boxes[i] = (result->boxes[i][2] - result->boxes[i][0]) *
  84. (result->boxes[i][3] - result->boxes[i][1]);
  85. }
  86. for (size_t i = 0; i < result->boxes.size(); ++i) {
  87. if (suppressed[i] == 1) {
  88. continue;
  89. }
  90. for (size_t j = i + 1; j < result->boxes.size(); ++j) {
  91. if (suppressed[j] == 1) {
  92. continue;
  93. }
  94. float xmin = std::max(result->boxes[i][0], result->boxes[j][0]);
  95. float ymin = std::max(result->boxes[i][1], result->boxes[j][1]);
  96. float xmax = std::min(result->boxes[i][2], result->boxes[j][2]);
  97. float ymax = std::min(result->boxes[i][3], result->boxes[j][3]);
  98. float overlap_w = std::max(0.0f, xmax - xmin);
  99. float overlap_h = std::max(0.0f, ymax - ymin);
  100. float overlap_area = overlap_w * overlap_h;
  101. float overlap_ratio =
  102. overlap_area / (area_of_boxes[i] + area_of_boxes[j] - overlap_area);
  103. if (overlap_ratio > iou_threshold) {
  104. suppressed[j] = 1;
  105. }
  106. }
  107. }
  108. FaceDetectionResult backup(*result);
  109. int landmarks_per_face = result->landmarks_per_face;
  110. result->Clear();
  111. // don't forget to reset the landmarks_per_face
  112. // before apply Reserve method.
  113. result->landmarks_per_face = landmarks_per_face;
  114. result->Reserve(suppressed.size());
  115. for (size_t i = 0; i < suppressed.size(); ++i) {
  116. if (suppressed[i] == 1) {
  117. continue;
  118. }
  119. result->boxes.emplace_back(backup.boxes[i]);
  120. result->scores.push_back(backup.scores[i]);
  121. // landmarks (if have)
  122. if (result->landmarks_per_face > 0) {
  123. for (size_t j = 0; j < result->landmarks_per_face; ++j) {
  124. result->landmarks.emplace_back(
  125. backup.landmarks[i * result->landmarks_per_face + j]);
  126. }
  127. }
  128. }
  129. }
  130. } // namespace utils
  131. } // namespace vision
  132. } // namespace ultra_infer