base_model.h 4.9 KB

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