model.h 4.0 KB

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