postprocessor.cc 5.0 KB

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