postprocessor.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "ultra_infer/vision/common/processors/transform.h"
  16. #include "ultra_infer/vision/common/result.h"
  17. #include "ultra_infer/vision/detection/contrib/rknpu2/utils.h"
  18. #include <array>
  19. namespace ultra_infer {
  20. namespace vision {
  21. namespace detection {
  22. /*! @brief Postprocessor object for YOLOv5 serials model.
  23. */
  24. class ULTRAINFER_DECL RKYOLOPostprocessor {
  25. public:
  26. /** \brief Create a postprocessor instance for YOLOv5 serials model
  27. */
  28. RKYOLOPostprocessor();
  29. /** \brief Process the result of runtime and fill to DetectionResult structure
  30. *
  31. * \param[in] tensors The inference result from runtime
  32. * \param[in] result The output result of detection
  33. * \param[in] ims_info The shape info list, record input_shape and
  34. * output_shape \return true if the postprocess succeeded, otherwise false
  35. */
  36. bool Run(const std::vector<FDTensor> &tensors,
  37. std::vector<DetectionResult> *results);
  38. /// Set nms_threshold, default 0.45
  39. void SetNMSThreshold(float nms_threshold) { nms_threshold_ = nms_threshold; }
  40. /// Set conf_threshold, default 0.25
  41. void SetConfThreshold(float conf_threshold) {
  42. conf_threshold_ = conf_threshold;
  43. }
  44. /// Get conf_threshold, default 0.25
  45. const float GetConfThreshold() { return conf_threshold_; }
  46. /// Get nms_threshold, default 0.45
  47. const float GetNMSThreshold() { return nms_threshold_; }
  48. /// Set height and weight
  49. void SetHeightAndWeight(int height, int width) {
  50. height_ = height;
  51. width_ = width;
  52. }
  53. /// Set pad_hw_values
  54. void SetPadHWValues(const std::vector<std::vector<int>> &pad_hw_values) {
  55. pad_hw_values_ = pad_hw_values;
  56. }
  57. /// Set scale
  58. void SetScale(const std::vector<float> &scale) { scale_ = scale; }
  59. /// Get Anchor
  60. const std::vector<int> &GetAnchor() { return anchors_; }
  61. /// Set Anchor
  62. void SetAnchor(const std::vector<int> &anchors) { anchors_ = anchors; }
  63. void SetAnchorPerBranch(int anchor_per_branch) {
  64. anchor_per_branch_ = anchor_per_branch;
  65. }
  66. /// Set the number of class
  67. void SetClassNum(int num) {
  68. obj_class_num_ = num;
  69. prob_box_size_ = obj_class_num_ + 5;
  70. }
  71. /// Get the number of class
  72. int GetClassNum() { return obj_class_num_; }
  73. private:
  74. std::vector<int> anchors_ = {10, 13, 16, 30, 33, 23, 30, 61, 62,
  75. 45, 59, 119, 116, 90, 156, 198, 373, 326};
  76. int strides_[3] = {8, 16, 32};
  77. int height_ = 0;
  78. int width_ = 0;
  79. int anchor_per_branch_ = 0;
  80. int ProcessFP16(float *input, int *anchor, int grid_h, int grid_w, int stride,
  81. std::vector<float> &boxes, std::vector<float> &boxScores,
  82. std::vector<int> &classId, float threshold);
  83. // Model
  84. int QuickSortIndiceInverse(std::vector<float> &input, int left, int right,
  85. std::vector<int> &indices);
  86. // post_process values
  87. std::vector<std::vector<int>> pad_hw_values_;
  88. std::vector<float> scale_;
  89. float nms_threshold_ = 0.45;
  90. float conf_threshold_ = 0.25;
  91. int prob_box_size_ = 85;
  92. int obj_class_num_ = 80;
  93. int obj_num_bbox_max_size = 200;
  94. };
  95. } // namespace detection
  96. } // namespace vision
  97. } // namespace ultra_infer