multiclass_nms.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #pragma once
  15. #include <map>
  16. #include <string>
  17. #include <vector>
  18. namespace ultra_infer {
  19. namespace vision {
  20. namespace detection {
  21. /** \brief Config for PaddleMultiClassNMS
  22. * \param[in] background_label the value of background label
  23. * \param[in] keep_top_k the value of keep_top_k
  24. * \param[in] nms_eta the value of nms_eta
  25. * \param[in] nms_threshold a dict that contains the arguments of nms operations
  26. * \param[in] nms_top_k if there are more than max_num bboxes after NMS, only
  27. * top max_num will be kept. \param[in] normalized Determine whether normalized
  28. * is required \param[in] score_threshold bbox threshold, bboxes with scores
  29. * lower than it will not be considered.
  30. */
  31. struct NMSOption {
  32. NMSOption() = default;
  33. int64_t background_label = -1;
  34. int64_t keep_top_k = 100;
  35. float nms_eta = 1.0;
  36. float nms_threshold = 0.5;
  37. int64_t nms_top_k = 1000;
  38. bool normalized = true;
  39. float score_threshold = 0.3;
  40. };
  41. struct PaddleMultiClassNMS {
  42. int64_t background_label = -1;
  43. int64_t keep_top_k = -1;
  44. float nms_eta;
  45. float nms_threshold = 0.7;
  46. int64_t nms_top_k;
  47. bool normalized;
  48. float score_threshold;
  49. std::vector<int32_t> out_num_rois_data;
  50. std::vector<int32_t> out_index_data;
  51. std::vector<float> out_box_data;
  52. void FastNMS(const float *boxes, const float *scores, const int &num_boxes,
  53. std::vector<int> *keep_indices);
  54. int NMSForEachSample(const float *boxes, const float *scores, int num_boxes,
  55. int num_classes,
  56. std::map<int, std::vector<int>> *keep_indices);
  57. void Compute(const float *boxes, const float *scores,
  58. const std::vector<int64_t> &boxes_dim,
  59. const std::vector<int64_t> &scores_dim);
  60. void SetNMSOption(const struct NMSOption &nms_option) {
  61. background_label = nms_option.background_label;
  62. keep_top_k = nms_option.keep_top_k;
  63. nms_eta = nms_option.nms_eta;
  64. nms_threshold = nms_option.nms_threshold;
  65. nms_top_k = nms_option.nms_top_k;
  66. normalized = nms_option.normalized;
  67. score_threshold = nms_option.score_threshold;
  68. }
  69. };
  70. } // namespace detection
  71. } // namespace vision
  72. } // namespace ultra_infer