ppshituv2_rec.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/classification/ppshitu/ppshituv2_rec.h"
  15. #include "ultra_infer/utils/unique_ptr.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. namespace classification {
  19. PPShiTuV2Recognizer::PPShiTuV2Recognizer(const std::string &model_file,
  20. const std::string &params_file,
  21. const std::string &config_file,
  22. const RuntimeOption &custom_option,
  23. const ModelFormat &model_format)
  24. : preprocessor_(config_file) {
  25. if (model_format == ModelFormat::PADDLE) {
  26. valid_cpu_backends = {Backend::OPENVINO, Backend::PDINFER, Backend::ORT,
  27. Backend::LITE};
  28. valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT};
  29. valid_timvx_backends = {Backend::LITE};
  30. valid_ascend_backends = {Backend::LITE};
  31. valid_kunlunxin_backends = {Backend::LITE};
  32. valid_ipu_backends = {Backend::PDINFER};
  33. valid_directml_backends = {Backend::ORT};
  34. } else if (model_format == ModelFormat::SOPHGO) {
  35. valid_sophgonpu_backends = {Backend::SOPHGOTPU};
  36. } else {
  37. valid_cpu_backends = {Backend::ORT, Backend::OPENVINO};
  38. valid_gpu_backends = {Backend::ORT, Backend::TRT};
  39. valid_rknpu_backends = {Backend::RKNPU2};
  40. valid_directml_backends = {Backend::ORT};
  41. }
  42. runtime_option = custom_option;
  43. runtime_option.model_format = model_format;
  44. runtime_option.model_file = model_file;
  45. runtime_option.params_file = params_file;
  46. initialized = Initialize();
  47. }
  48. std::unique_ptr<PPShiTuV2Recognizer> PPShiTuV2Recognizer::Clone() const {
  49. std::unique_ptr<PPShiTuV2Recognizer> clone_model =
  50. utils::make_unique<PPShiTuV2Recognizer>(PPShiTuV2Recognizer(*this));
  51. clone_model->SetRuntime(clone_model->CloneRuntime());
  52. return clone_model;
  53. }
  54. bool PPShiTuV2Recognizer::Initialize() {
  55. if (!InitRuntime()) {
  56. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  57. return false;
  58. }
  59. return true;
  60. }
  61. bool PPShiTuV2Recognizer::Predict(cv::Mat *im, ClassifyResult *result) {
  62. if (!Predict(*im, result)) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. bool PPShiTuV2Recognizer::Predict(const cv::Mat &im, ClassifyResult *result) {
  68. FDMat mat = WrapMat(im);
  69. return Predict(mat, result);
  70. }
  71. bool PPShiTuV2Recognizer::BatchPredict(const std::vector<cv::Mat> &images,
  72. std::vector<ClassifyResult> *results) {
  73. std::vector<FDMat> mats = WrapMat(images);
  74. return BatchPredict(mats, results);
  75. }
  76. bool PPShiTuV2Recognizer::Predict(const FDMat &mat, ClassifyResult *result) {
  77. std::vector<ClassifyResult> results;
  78. std::vector<FDMat> mats = {mat};
  79. if (!BatchPredict(mats, &results)) {
  80. return false;
  81. }
  82. *result = std::move(results[0]);
  83. return true;
  84. }
  85. bool PPShiTuV2Recognizer::BatchPredict(const std::vector<FDMat> &mats,
  86. std::vector<ClassifyResult> *results) {
  87. std::vector<FDMat> fd_mats = mats;
  88. if (!preprocessor_.Run(&fd_mats, &reused_input_tensors_)) {
  89. FDERROR << "Failed to preprocess the input image." << std::endl;
  90. return false;
  91. }
  92. reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
  93. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  94. FDERROR << "Failed to inference by runtime." << std::endl;
  95. return false;
  96. }
  97. if (!postprocessor_.Run(reused_output_tensors_, results)) {
  98. FDERROR << "Failed to postprocess the inference results by runtime."
  99. << std::endl;
  100. return false;
  101. }
  102. return true;
  103. }
  104. } // namespace classification
  105. } // namespace vision
  106. } // namespace ultra_infer