classifier.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/classifier.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. Classifier::Classifier() {}
  21. Classifier::Classifier(const std::string &model_file,
  22. const std::string &params_file,
  23. const RuntimeOption &custom_option,
  24. const ModelFormat &model_format) {
  25. if (model_format == ModelFormat::ONNX) {
  26. valid_cpu_backends = {Backend::ORT, Backend::OPENVINO};
  27. valid_gpu_backends = {Backend::ORT, Backend::TRT};
  28. } else {
  29. valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::OPENVINO,
  30. Backend::LITE};
  31. valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
  32. valid_kunlunxin_backends = {Backend::LITE};
  33. valid_ascend_backends = {Backend::LITE};
  34. valid_sophgonpu_backends = {Backend::SOPHGOTPU};
  35. valid_rknpu_backends = {Backend::RKNPU2};
  36. }
  37. runtime_option = custom_option;
  38. runtime_option.model_format = model_format;
  39. runtime_option.model_file = model_file;
  40. runtime_option.params_file = params_file;
  41. initialized = Initialize();
  42. }
  43. bool Classifier::Initialize() {
  44. if (!InitRuntime()) {
  45. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  46. return false;
  47. }
  48. return true;
  49. }
  50. std::unique_ptr<Classifier> Classifier::Clone() const {
  51. std::unique_ptr<Classifier> clone_model =
  52. utils::make_unique<Classifier>(Classifier(*this));
  53. clone_model->SetRuntime(clone_model->CloneRuntime());
  54. return clone_model;
  55. }
  56. bool Classifier::Predict(const cv::Mat &img, int32_t *cls_label,
  57. float *cls_score) {
  58. std::vector<int32_t> cls_labels(1);
  59. std::vector<float> cls_scores(1);
  60. bool success = BatchPredict({img}, &cls_labels, &cls_scores);
  61. if (!success) {
  62. return success;
  63. }
  64. *cls_label = cls_labels[0];
  65. *cls_score = cls_scores[0];
  66. return true;
  67. }
  68. bool Classifier::Predict(const cv::Mat &img, vision::OCRResult *ocr_result) {
  69. ocr_result->cls_labels.resize(1);
  70. ocr_result->cls_scores.resize(1);
  71. if (!Predict(img, &(ocr_result->cls_labels[0]),
  72. &(ocr_result->cls_scores[0]))) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. bool Classifier::BatchPredict(const std::vector<cv::Mat> &images,
  78. vision::OCRResult *ocr_result) {
  79. return BatchPredict(images, &(ocr_result->cls_labels),
  80. &(ocr_result->cls_scores));
  81. }
  82. bool Classifier::BatchPredict(const std::vector<cv::Mat> &images,
  83. std::vector<int32_t> *cls_labels,
  84. std::vector<float> *cls_scores) {
  85. return BatchPredict(images, cls_labels, cls_scores, 0, images.size());
  86. }
  87. bool Classifier::BatchPredict(const std::vector<cv::Mat> &images,
  88. std::vector<int32_t> *cls_labels,
  89. std::vector<float> *cls_scores,
  90. size_t start_index, size_t end_index) {
  91. size_t total_size = images.size();
  92. std::vector<FDMat> fd_images = WrapMat(images);
  93. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_, start_index,
  94. end_index)) {
  95. FDERROR << "Failed to preprocess the input image." << std::endl;
  96. return false;
  97. }
  98. reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
  99. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  100. FDERROR << "Failed to inference by runtime." << std::endl;
  101. return false;
  102. }
  103. if (!postprocessor_.Run(reused_output_tensors_, cls_labels, cls_scores,
  104. start_index, total_size)) {
  105. FDERROR << "Failed to postprocess the inference cls_results by runtime."
  106. << std::endl;
  107. return false;
  108. }
  109. return true;
  110. }
  111. } // namespace ocr
  112. } // namespace vision
  113. } // namespace ultra_infer