paddlex.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <functional>
  16. #include <iostream>
  17. #include <map>
  18. #include <memory>
  19. #include <numeric>
  20. #include <string>
  21. #include <vector>
  22. #include "yaml-cpp/yaml.h"
  23. #ifdef _WIN32
  24. #define OS_PATH_SEP "\\"
  25. #else
  26. #define OS_PATH_SEP "/"
  27. #endif
  28. #include "paddle_inference_api.h" // NOLINT
  29. #include "config_parser.h" // NOLINT
  30. #include "results.h" // NOLINT
  31. #include "transforms.h" // NOLINT
  32. #ifdef WITH_ENCRYPTION
  33. #include "paddle_model_decrypt.h" // NOLINT
  34. #include "model_code.h" // NOLINT
  35. #endif
  36. namespace PaddleX {
  37. /*
  38. * @brief
  39. * This class encapsulates all necessary proccess steps of model infering, which
  40. * include image matrix preprocessing, model predicting and results postprocessing.
  41. * The entire process of model infering can be simplified as below:
  42. * 1. preprocess image matrix (resize, padding, ......)
  43. * 2. model infer
  44. * 3. postprocess the results which generated from model infering
  45. *
  46. * @example
  47. * PaddleX::Model cls_model;
  48. * // initialize model configuration
  49. * cls_model.Init(cls_model_dir, use_gpu, use_trt, gpu_id, encryption_key);
  50. * // define a Classification result object
  51. * PaddleX::ClsResult cls_result;
  52. * // get image matrix from image file
  53. * cv::Mat im = cv::imread(image_file_path, 1);
  54. * cls_model.predict(im, &cls_result);
  55. * */
  56. class Model {
  57. public:
  58. /*
  59. * @brief
  60. * This method aims to initialize the model configuration
  61. *
  62. * @param model_dir: the directory which contains model.yml
  63. * @param use_gpu: use gpu or not when infering
  64. * @param use_trt: use Tensor RT or not when infering
  65. * @param gpu_id: the id of gpu when infering with using gpu
  66. * @param key: the key of encryption when using encrypted model
  67. * */
  68. void Init(const std::string& model_dir,
  69. bool use_gpu = false,
  70. bool use_trt = false,
  71. int gpu_id = 0,
  72. std::string key = "") {
  73. create_predictor(model_dir, use_gpu, use_trt, gpu_id, key);
  74. }
  75. void create_predictor(const std::string& model_dir,
  76. bool use_gpu = false,
  77. bool use_trt = false,
  78. int gpu_id = 0,
  79. std::string key = "");
  80. /*
  81. * @brief
  82. * This method aims to load model configurations which include
  83. * transform steps and label list
  84. *
  85. * @param yaml_input: model configuration string
  86. * @return true if load configuration successfully
  87. * */
  88. bool load_config(const std::string& yaml_input);
  89. /*
  90. * @brief
  91. * This method aims to transform single image matrix, the result will be
  92. * returned at second parameter.
  93. *
  94. * @param input_im: single image matrix to be transformed
  95. * @param blob: the raw data of single image matrix after transformed
  96. * @return true if preprocess image matrix successfully
  97. * */
  98. bool preprocess(const cv::Mat& input_im, ImageBlob* blob);
  99. /*
  100. * @brief
  101. * This method aims to transform mutiple image matrixs, the result will be
  102. * returned at second parameter.
  103. *
  104. * @param input_im_batch: a batch of image matrixs to be transformed
  105. * @param blob_blob: raw data of a batch of image matrixs after transformed
  106. * @param thread_num: the number of preprocessing threads,
  107. * each thread run preprocess on single image matrix
  108. * @return true if preprocess a batch of image matrixs successfully
  109. * */
  110. bool preprocess(const std::vector<cv::Mat> &input_im_batch,
  111. std::vector<ImageBlob> *blob_batch,
  112. int thread_num = 1);
  113. /*
  114. * @brief
  115. * This method aims to execute classification model prediction on single image matrix,
  116. * the result will be returned at second parameter.
  117. *
  118. * @param im: single image matrix to be predicted
  119. * @param result: classification prediction result data after postprocessed
  120. * @return true if predict successfully
  121. * */
  122. bool predict(const cv::Mat& im, ClsResult* result);
  123. /*
  124. * @brief
  125. * This method aims to execute classification model prediction on a batch of image matrixs,
  126. * the result will be returned at second parameter.
  127. *
  128. * @param im: a batch of image matrixs to be predicted
  129. * @param results: a batch of classification prediction result data after postprocessed
  130. * @param thread_num: the number of predicting threads, each thread run prediction
  131. * on single image matrix
  132. * @return true if predict successfully
  133. * */
  134. bool predict(const std::vector<cv::Mat> &im_batch,
  135. std::vector<ClsResult> *results,
  136. int thread_num = 1);
  137. /*
  138. * @brief
  139. * This method aims to execute detection or instance segmentation model prediction
  140. * on single image matrix, the result will be returned at second parameter.
  141. *
  142. * @param im: single image matrix to be predicted
  143. * @param result: detection or instance segmentation prediction result data after postprocessed
  144. * @return true if predict successfully
  145. * */
  146. bool predict(const cv::Mat& im, DetResult* result);
  147. /*
  148. * @brief
  149. * This method aims to execute detection or instance segmentation model prediction
  150. * on a batch of image matrixs, the result will be returned at second parameter.
  151. *
  152. * @param im: a batch of image matrix to be predicted
  153. * @param result: detection or instance segmentation prediction result data after postprocessed
  154. * @param thread_num: the number of predicting threads, each thread run prediction
  155. * on single image matrix
  156. * @return true if predict successfully
  157. * */
  158. bool predict(const std::vector<cv::Mat> &im_batch,
  159. std::vector<DetResult> *result,
  160. int thread_num = 1);
  161. /*
  162. * @brief
  163. * This method aims to execute segmentation model prediction on single image matrix,
  164. * the result will be returned at second parameter.
  165. *
  166. * @param im: single image matrix to be predicted
  167. * @param result: segmentation prediction result data after postprocessed
  168. * @return true if predict successfully
  169. * */
  170. bool predict(const cv::Mat& im, SegResult* result);
  171. /*
  172. * @brief
  173. * This method aims to execute segmentation model prediction on a batch of image matrix,
  174. * the result will be returned at second parameter.
  175. *
  176. * @param im: a batch of image matrix to be predicted
  177. * @param result: segmentation prediction result data after postprocessed
  178. * @param thread_num: the number of predicting threads, each thread run prediction
  179. * on single image matrix
  180. * @return true if predict successfully
  181. * */
  182. bool predict(const std::vector<cv::Mat> &im_batch,
  183. std::vector<SegResult> *result,
  184. int thread_num = 1);
  185. // model type, include 3 type: classifier, detector, segmenter
  186. std::string type;
  187. // model name, such as FasterRCNN, YOLOV3 and so on.
  188. std::string name;
  189. std::map<int, std::string> labels;
  190. // transform(preprocessing) pipeline manager
  191. Transforms transforms_;
  192. // single input preprocessed data
  193. ImageBlob inputs_;
  194. // batch input preprocessed data
  195. std::vector<ImageBlob> inputs_batch_;
  196. // raw data of predicting results
  197. std::vector<float> outputs_;
  198. // a predictor which run the model predicting
  199. std::unique_ptr<paddle::PaddlePredictor> predictor_;
  200. };
  201. } // namespace PaddleX