postprocessor.cc 4.6 KB

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