postprocessor.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/vision/detection/contrib/yolov5/postprocessor.h"
  15. #include "ultra_infer/vision/utils/utils.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. namespace detection {
  19. YOLOv5Postprocessor::YOLOv5Postprocessor() {
  20. conf_threshold_ = 0.25;
  21. nms_threshold_ = 0.5;
  22. multi_label_ = true;
  23. max_wh_ = 7680.0;
  24. }
  25. bool YOLOv5Postprocessor::Run(
  26. const std::vector<FDTensor> &tensors, std::vector<DetectionResult> *results,
  27. const std::vector<std::map<std::string, std::array<float, 2>>> &ims_info) {
  28. int batch = tensors[0].shape[0];
  29. results->resize(batch);
  30. for (size_t bs = 0; bs < batch; ++bs) {
  31. (*results)[bs].Clear();
  32. if (multi_label_) {
  33. (*results)[bs].Reserve(tensors[0].shape[1] * (tensors[0].shape[2] - 5));
  34. } else {
  35. (*results)[bs].Reserve(tensors[0].shape[1]);
  36. }
  37. if (tensors[0].dtype != FDDataType::FP32) {
  38. FDERROR << "Only support post process with float32 data." << std::endl;
  39. return false;
  40. }
  41. const float *data = reinterpret_cast<const float *>(tensors[0].Data()) +
  42. bs * tensors[0].shape[1] * tensors[0].shape[2];
  43. for (size_t i = 0; i < tensors[0].shape[1]; ++i) {
  44. int s = i * tensors[0].shape[2];
  45. float confidence = data[s + 4];
  46. if (multi_label_) {
  47. for (size_t j = 5; j < tensors[0].shape[2]; ++j) {
  48. confidence = data[s + 4];
  49. const float *class_score = data + s + j;
  50. confidence *= (*class_score);
  51. // filter boxes by conf_threshold
  52. if (confidence <= conf_threshold_) {
  53. continue;
  54. }
  55. int32_t label_id = std::distance(data + s + 5, class_score);
  56. // convert from [x, y, w, h] to [x1, y1, x2, y2]
  57. (*results)[bs].boxes.emplace_back(std::array<float, 4>{
  58. data[s] - data[s + 2] / 2.0f + label_id * max_wh_,
  59. data[s + 1] - data[s + 3] / 2.0f + label_id * max_wh_,
  60. data[s + 0] + data[s + 2] / 2.0f + label_id * max_wh_,
  61. data[s + 1] + data[s + 3] / 2.0f + label_id * max_wh_});
  62. (*results)[bs].label_ids.push_back(label_id);
  63. (*results)[bs].scores.push_back(confidence);
  64. }
  65. } else {
  66. const float *max_class_score =
  67. std::max_element(data + s + 5, data + s + tensors[0].shape[2]);
  68. confidence *= (*max_class_score);
  69. // filter boxes by conf_threshold
  70. if (confidence <= conf_threshold_) {
  71. continue;
  72. }
  73. int32_t label_id = std::distance(data + s + 5, max_class_score);
  74. // convert from [x, y, w, h] to [x1, y1, x2, y2]
  75. (*results)[bs].boxes.emplace_back(std::array<float, 4>{
  76. data[s] - data[s + 2] / 2.0f + label_id * max_wh_,
  77. data[s + 1] - data[s + 3] / 2.0f + label_id * max_wh_,
  78. data[s + 0] + data[s + 2] / 2.0f + label_id * max_wh_,
  79. data[s + 1] + data[s + 3] / 2.0f + label_id * max_wh_});
  80. (*results)[bs].label_ids.push_back(label_id);
  81. (*results)[bs].scores.push_back(confidence);
  82. }
  83. }
  84. if ((*results)[bs].boxes.size() == 0) {
  85. return true;
  86. }
  87. utils::NMS(&((*results)[bs]), nms_threshold_);
  88. // scale the boxes to the origin image shape
  89. auto iter_out = ims_info[bs].find("output_shape");
  90. auto iter_ipt = ims_info[bs].find("input_shape");
  91. FDASSERT(iter_out != ims_info[bs].end() && iter_ipt != ims_info[bs].end(),
  92. "Cannot find input_shape or output_shape from im_info.");
  93. float out_h = iter_out->second[0];
  94. float out_w = iter_out->second[1];
  95. float ipt_h = iter_ipt->second[0];
  96. float ipt_w = iter_ipt->second[1];
  97. float scale = std::min(out_h / ipt_h, out_w / ipt_w);
  98. float pad_h = (out_h - ipt_h * scale) / 2;
  99. float pad_w = (out_w - ipt_w * scale) / 2;
  100. for (size_t i = 0; i < (*results)[bs].boxes.size(); ++i) {
  101. int32_t label_id = ((*results)[bs].label_ids)[i];
  102. // clip box
  103. (*results)[bs].boxes[i][0] =
  104. (*results)[bs].boxes[i][0] - max_wh_ * label_id;
  105. (*results)[bs].boxes[i][1] =
  106. (*results)[bs].boxes[i][1] - max_wh_ * label_id;
  107. (*results)[bs].boxes[i][2] =
  108. (*results)[bs].boxes[i][2] - max_wh_ * label_id;
  109. (*results)[bs].boxes[i][3] =
  110. (*results)[bs].boxes[i][3] - max_wh_ * label_id;
  111. (*results)[bs].boxes[i][0] =
  112. std::max(((*results)[bs].boxes[i][0] - pad_w) / scale, 0.0f);
  113. (*results)[bs].boxes[i][1] =
  114. std::max(((*results)[bs].boxes[i][1] - pad_h) / scale, 0.0f);
  115. (*results)[bs].boxes[i][2] =
  116. std::max(((*results)[bs].boxes[i][2] - pad_w) / scale, 0.0f);
  117. (*results)[bs].boxes[i][3] =
  118. std::max(((*results)[bs].boxes[i][3] - pad_h) / scale, 0.0f);
  119. (*results)[bs].boxes[i][0] = std::min((*results)[bs].boxes[i][0], ipt_w);
  120. (*results)[bs].boxes[i][1] = std::min((*results)[bs].boxes[i][1], ipt_h);
  121. (*results)[bs].boxes[i][2] = std::min((*results)[bs].boxes[i][2], ipt_w);
  122. (*results)[bs].boxes[i][3] = std::min((*results)[bs].boxes[i][3], ipt_h);
  123. }
  124. }
  125. return true;
  126. }
  127. } // namespace detection
  128. } // namespace vision
  129. } // namespace ultra_infer