base_model.h 4.7 KB

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