base.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #pragma once
  15. #include "ultra_infer/ultra_infer_model.h"
  16. #include "ultra_infer/vision/common/processors/transform.h"
  17. #include "ultra_infer/vision/common/result.h"
  18. #include "ultra_infer/vision/detection/ppdet/postprocessor.h"
  19. #include "ultra_infer/vision/detection/ppdet/preprocessor.h"
  20. #include "ultra_infer/vision/utils/utils.h"
  21. namespace ultra_infer {
  22. namespace vision {
  23. /** \brief All object detection model APIs are defined inside this namespace
  24. *
  25. */
  26. namespace detection {
  27. /*! @brief Base model object used when to load a model exported by
  28. * PaddleDetection
  29. */
  30. class ULTRAINFER_DECL PPDetBase : public UltraInferModel {
  31. public:
  32. /** \brief Set path of model file and configuration file, and the
  33. * configuration of runtime
  34. *
  35. * \param[in] model_file Path of model file, e.g ppyoloe/model.pdmodel
  36. * \param[in] params_file Path of parameter file, e.g ppyoloe/model.pdiparams,
  37. * if the model format is ONNX, this parameter will be ignored \param[in]
  38. * config_file Path of configuration file for deployment, e.g
  39. * ppyoloe/infer_cfg.yml \param[in] custom_option RuntimeOption for inference,
  40. * the default will use cpu, and choose the backend defined in
  41. * `valid_cpu_backends` \param[in] model_format Model format of the loaded
  42. * model, default is Paddle format
  43. */
  44. PPDetBase(const std::string &model_file, const std::string &params_file,
  45. const std::string &config_file,
  46. const RuntimeOption &custom_option = RuntimeOption(),
  47. const ModelFormat &model_format = ModelFormat::PADDLE);
  48. /** \brief Clone a new PaddleDetModel with less memory usage when multiple
  49. * instances of the same model are created
  50. *
  51. * \return new PaddleDetModel* type unique pointer
  52. */
  53. virtual std::unique_ptr<PPDetBase> Clone() const;
  54. /// Get model's name
  55. virtual std::string ModelName() const { return "PaddleDetection/BaseModel"; }
  56. /** \brief DEPRECATED Predict the detection result for an input image
  57. *
  58. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  59. * with layout HWC, BGR format \param[in] result The output detection result
  60. * \return true if the prediction succeeded, otherwise false
  61. */
  62. virtual bool Predict(cv::Mat *im, DetectionResult *result);
  63. /** \brief Predict the detection result for an input image
  64. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  65. * with layout HWC, BGR format \param[in] result The output detection result
  66. * \return true if the prediction succeeded, otherwise false
  67. */
  68. virtual bool Predict(const cv::Mat &im, DetectionResult *result);
  69. /** \brief Predict the detection result for an input image list
  70. * \param[in] im The input image list, all the elements come from
  71. * cv::imread(), is a 3-D array with layout HWC, BGR format \param[in] results
  72. * The output detection result list \return true if the prediction succeeded,
  73. * otherwise false
  74. */
  75. virtual bool BatchPredict(const std::vector<cv::Mat> &imgs,
  76. std::vector<DetectionResult> *results);
  77. PaddleDetPreprocessor &GetPreprocessor() { return preprocessor_; }
  78. PaddleDetPostprocessor &GetPostprocessor() { return postprocessor_; }
  79. virtual bool CheckArch();
  80. protected:
  81. virtual bool Initialize();
  82. PaddleDetPreprocessor preprocessor_;
  83. PaddleDetPostprocessor postprocessor_;
  84. };
  85. } // namespace detection
  86. } // namespace vision
  87. } // namespace ultra_infer