model.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/segmentation/ppseg/model.h"
  15. #include "ultra_infer/utils/unique_ptr.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. namespace segmentation {
  19. PaddleSegModel::PaddleSegModel(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), postprocessor_(config_file) {
  25. if (model_format == ModelFormat::SOPHGO) {
  26. valid_sophgonpu_backends = {Backend::SOPHGOTPU};
  27. } else {
  28. valid_cpu_backends = {Backend::OPENVINO, Backend::PDINFER, Backend::ORT,
  29. Backend::LITE};
  30. valid_gpu_backends = {Backend::PDINFER, Backend::ORT, Backend::TRT};
  31. }
  32. valid_rknpu_backends = {Backend::RKNPU2};
  33. valid_timvx_backends = {Backend::LITE};
  34. valid_kunlunxin_backends = {Backend::LITE};
  35. valid_ascend_backends = {Backend::LITE};
  36. valid_directml_backends = {Backend::ORT};
  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. std::unique_ptr<PaddleSegModel> PaddleSegModel::Clone() const {
  44. std::unique_ptr<PaddleSegModel> clone_model =
  45. ultra_infer::utils::make_unique<PaddleSegModel>(PaddleSegModel(*this));
  46. clone_model->SetRuntime(clone_model->CloneRuntime());
  47. return clone_model;
  48. }
  49. bool PaddleSegModel::Initialize() {
  50. if (!InitRuntime()) {
  51. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  52. return false;
  53. }
  54. return true;
  55. }
  56. bool PaddleSegModel::Predict(cv::Mat *im, SegmentationResult *result) {
  57. return Predict(*im, result);
  58. }
  59. bool PaddleSegModel::Predict(const cv::Mat &im, SegmentationResult *result) {
  60. std::vector<SegmentationResult> results;
  61. if (!BatchPredict({im}, &results)) {
  62. return false;
  63. }
  64. *result = std::move(results[0]);
  65. return true;
  66. }
  67. bool PaddleSegModel::BatchPredict(const std::vector<cv::Mat> &imgs,
  68. std::vector<SegmentationResult> *results) {
  69. std::vector<FDMat> fd_images = WrapMat(imgs);
  70. // Record the shape of input images
  71. std::map<std::string, std::vector<std::array<int, 2>>> imgs_info;
  72. preprocessor_.SetImgsInfo(&imgs_info);
  73. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_)) {
  74. FDERROR << "Failed to preprocess input data while using model:"
  75. << ModelName() << "." << std::endl;
  76. return false;
  77. }
  78. reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
  79. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  80. FDERROR << "Failed to inference while using model:" << ModelName() << "."
  81. << std::endl;
  82. return false;
  83. }
  84. if (!postprocessor_.Run(reused_output_tensors_, results, imgs_info)) {
  85. FDERROR << "Failed to postprocess while using model:" << ModelName() << "."
  86. << std::endl;
  87. return false;
  88. }
  89. return true;
  90. }
  91. } // namespace segmentation
  92. } // namespace vision
  93. } // namespace ultra_infer