cls_postprocessor.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/ocr/ppocr/cls_postprocessor.h"
  15. #include "ultra_infer/utils/perf.h"
  16. #include "ultra_infer/vision/ocr/ppocr/utils/ocr_utils.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace ocr {
  20. bool SingleBatchPostprocessor(const float *out_data, const size_t &length,
  21. int *cls_label, float *cls_score) {
  22. *cls_label = std::distance(&out_data[0],
  23. std::max_element(&out_data[0], &out_data[length]));
  24. *cls_score = float(*std::max_element(&out_data[0], &out_data[length]));
  25. return true;
  26. }
  27. bool ClassifierPostprocessor::Run(const std::vector<FDTensor> &tensors,
  28. std::vector<int32_t> *cls_labels,
  29. std::vector<float> *cls_scores) {
  30. size_t total_size = tensors[0].shape[0];
  31. return Run(tensors, cls_labels, cls_scores, 0, total_size);
  32. }
  33. bool ClassifierPostprocessor::Run(const std::vector<FDTensor> &tensors,
  34. std::vector<int32_t> *cls_labels,
  35. std::vector<float> *cls_scores,
  36. size_t start_index, size_t total_size) {
  37. // Classifier have only 1 output tensor.
  38. const FDTensor &tensor = tensors[0];
  39. // For Classifier, the output tensor shape = [batch,2]
  40. size_t batch = tensor.shape[0];
  41. size_t length = accumulate(tensor.shape.begin() + 1, tensor.shape.end(), 1,
  42. std::multiplies<int>());
  43. if (batch <= 0) {
  44. FDERROR << "The infer outputTensor.shape[0] <=0, wrong infer result."
  45. << std::endl;
  46. return false;
  47. }
  48. if (start_index < 0 || total_size <= 0) {
  49. FDERROR << "start_index or total_size error. Correct is: 0 <= start_index "
  50. "< total_size"
  51. << std::endl;
  52. return false;
  53. }
  54. if ((start_index + batch) > total_size) {
  55. FDERROR << "start_index or total_size error. Correct is: start_index + "
  56. "batch(outputTensor.shape[0]) <= total_size"
  57. << std::endl;
  58. return false;
  59. }
  60. cls_labels->resize(total_size);
  61. cls_scores->resize(total_size);
  62. const float *tensor_data = reinterpret_cast<const float *>(tensor.Data());
  63. for (int i_batch = 0; i_batch < batch; ++i_batch) {
  64. SingleBatchPostprocessor(tensor_data + i_batch * length, length,
  65. &cls_labels->at(i_batch + start_index),
  66. &cls_scores->at(i_batch + start_index));
  67. }
  68. return true;
  69. }
  70. } // namespace ocr
  71. } // namespace vision
  72. } // namespace ultra_infer