postprocessor.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/ppdet/blazeface/postprocessor.h"
  15. #include "ultra_infer/vision/detection/ppdet/multiclass_nms.h"
  16. #include "ultra_infer/vision/utils/utils.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace facedet {
  20. BlazeFacePostprocessor::BlazeFacePostprocessor() {
  21. conf_threshold_ = 0.5;
  22. nms_threshold_ = 0.3;
  23. }
  24. bool BlazeFacePostprocessor::Run(
  25. const std::vector<FDTensor> &tensors,
  26. std::vector<FaceDetectionResult> *results,
  27. const std::vector<std::map<std::string, std::array<float, 2>>> &ims_info) {
  28. // Get number of boxes for each input image
  29. std::vector<int> num_boxes(tensors[1].shape[0]);
  30. int total_num_boxes = 0;
  31. if (tensors[1].dtype == FDDataType::INT32) {
  32. const auto *data = static_cast<const int32_t *>(tensors[1].CpuData());
  33. for (size_t i = 0; i < tensors[1].shape[0]; ++i) {
  34. num_boxes[i] = static_cast<int>(data[i]);
  35. total_num_boxes += num_boxes[i];
  36. }
  37. } else if (tensors[1].dtype == FDDataType::INT64) {
  38. const auto *data = static_cast<const int64_t *>(tensors[1].CpuData());
  39. for (size_t i = 0; i < tensors[1].shape[0]; ++i) {
  40. num_boxes[i] = static_cast<int>(data[i]);
  41. }
  42. }
  43. // Special case for TensorRT, it has fixed output shape of NMS
  44. // So there's invalid boxes in its' output boxes
  45. int num_output_boxes = static_cast<int>(tensors[0].Shape()[0]);
  46. bool contain_invalid_boxes = false;
  47. if (total_num_boxes != num_output_boxes) {
  48. if (num_output_boxes % num_boxes.size() == 0) {
  49. contain_invalid_boxes = true;
  50. } else {
  51. FDERROR << "Cannot handle the output data for this model, unexpected "
  52. "situation."
  53. << std::endl;
  54. return false;
  55. }
  56. }
  57. // Get boxes for each input image
  58. results->resize(num_boxes.size());
  59. if (tensors[0].shape[0] == 0) {
  60. // No detected boxes
  61. return true;
  62. }
  63. const auto *box_data = static_cast<const float *>(tensors[0].CpuData());
  64. int offset = 0;
  65. for (size_t i = 0; i < num_boxes.size(); ++i) {
  66. const float *ptr = box_data + offset;
  67. (*results)[i].Reserve(num_boxes[i]);
  68. for (size_t j = 0; j < num_boxes[i]; ++j) {
  69. if (ptr[j * 6 + 1] > conf_threshold_) {
  70. (*results)[i].scores.push_back(ptr[j * 6 + 1]);
  71. (*results)[i].boxes.emplace_back(std::array<float, 4>(
  72. {ptr[j * 6 + 2], ptr[j * 6 + 3], ptr[j * 6 + 4], ptr[j * 6 + 5]}));
  73. }
  74. }
  75. if (contain_invalid_boxes) {
  76. offset += static_cast<int>(num_output_boxes * 6 / num_boxes.size());
  77. } else {
  78. offset += static_cast<int>(num_boxes[i] * 6);
  79. }
  80. }
  81. return true;
  82. }
  83. } // namespace facedet
  84. } // namespace vision
  85. } // namespace ultra_infer