base_model.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2020 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. #pragma once
  15. #include <iostream>
  16. #include <memory>
  17. #include <string>
  18. #include <vector>
  19. #include "yaml-cpp/yaml.h"
  20. #include "model_deploy/common/include/deploy_delacre.h"
  21. #include "model_deploy/common/include/base_postprocess.h"
  22. #include "model_deploy/common/include/base_preprocess.h"
  23. #include "model_deploy/common/include/output_struct.h"
  24. #include "model_deploy/engine/include/engine.h"
  25. namespace PaddleDeploy {
  26. class PD_INFER_DECL Model {
  27. private:
  28. const std::string model_type_;
  29. public:
  30. /*store the data after the YAML file has been parsed */
  31. YAML::Node yaml_config_;
  32. /* preprocess */
  33. std::shared_ptr<BasePreprocess> preprocess_;
  34. /* inference */
  35. std::shared_ptr<InferEngine> infer_engine_;
  36. /* postprocess */
  37. std::shared_ptr<BasePostprocess> postprocess_;
  38. Model() {}
  39. // Init model_type.
  40. explicit Model(const std::string model_type) : model_type_(model_type) {}
  41. virtual bool Init(const std::string& cfg_file) {
  42. if (!YamlConfigInit(cfg_file)) return false;
  43. if (!PreprocessInit()) return false;
  44. if (!PostprocessInit()) return false;
  45. return true;
  46. }
  47. virtual bool YamlConfigInit(const std::string& cfg_file) {
  48. // YAML::Node yaml_config_ = YAML::LoadFile(cfg_file);
  49. std::cerr << "Error! The Base Model was incorrectly entered" << std::endl;
  50. return false;
  51. }
  52. virtual bool PreprocessInit() {
  53. preprocess_ = nullptr;
  54. std::cerr << "model no Preprocess!" << std::endl;
  55. return false;
  56. }
  57. bool PaddleEngineInit(const PaddleEngineConfig& engine_config);
  58. bool TritonEngineInit(const TritonEngineConfig& engine_config);
  59. bool TensorRTInit(const TensorRTEngineConfig& engine_config);
  60. virtual bool PostprocessInit() {
  61. postprocess_ = nullptr;
  62. std::cerr << "model no Postprocess!" << std::endl;
  63. return false;
  64. }
  65. virtual bool Predict(const std::vector<cv::Mat>& imgs,
  66. std::vector<Result>* results,
  67. int thread_num = 1) {
  68. if (!preprocess_ || !postprocess_ || !infer_engine_) {
  69. std::cerr << "No init,cann't predict" << std::endl;
  70. return false;
  71. }
  72. results->clear();
  73. std::vector<cv::Mat> imgs_clone;
  74. for (auto i = 0; i < imgs.size(); ++i) {
  75. imgs_clone.push_back(imgs[i].clone());
  76. }
  77. std::vector<ShapeInfo> shape_infos;
  78. std::vector<DataBlob> inputs;
  79. std::vector<DataBlob> outputs;
  80. if (!preprocess_->Run(&imgs_clone, &inputs, &shape_infos, thread_num)) {
  81. return false;
  82. }
  83. if (!infer_engine_->Infer(inputs, &outputs)) {
  84. return false;
  85. }
  86. if (!postprocess_->Run(outputs, shape_infos, results, thread_num)) {
  87. return false;
  88. }
  89. return true;
  90. }
  91. virtual bool PrePrecess(const std::vector<cv::Mat>& imgs,
  92. std::vector<DataBlob>* inputs,
  93. std::vector<ShapeInfo>* shape_infos,
  94. int thread_num = 1) {
  95. if (!preprocess_) {
  96. std::cerr << "No PrePrecess, No pre Init. model_type=" << model_type_
  97. << std::endl;
  98. return false;
  99. }
  100. std::vector<cv::Mat> imgs_clone(imgs.size());
  101. for (auto i = 0; i < imgs.size(); ++i) {
  102. imgs[i].copyTo(imgs_clone[i]);
  103. }
  104. if (!preprocess_->Run(&imgs_clone, inputs, shape_infos, thread_num))
  105. return false;
  106. return true;
  107. }
  108. virtual void Infer(const std::vector<DataBlob>& inputs,
  109. std::vector<DataBlob>* outputs) {
  110. infer_engine_->Infer(inputs, outputs);
  111. }
  112. virtual bool PostPrecess(const std::vector<DataBlob>& outputs,
  113. const std::vector<ShapeInfo>& shape_infos,
  114. std::vector<Result>* results,
  115. int thread_num = 1) {
  116. if (!postprocess_) {
  117. std::cerr << "No PostPrecess, No post Init. model_type=" << model_type_
  118. << std::endl;
  119. return false;
  120. }
  121. if (postprocess_->Run(outputs, shape_infos, results, thread_num))
  122. return false;
  123. return true;
  124. }
  125. };
  126. } // namespace PaddleDeploy