modnet.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. namespace ultra_infer {
  19. namespace vision {
  20. namespace matting {
  21. /*! @brief MODNet model object used when to load a MODNet model exported by
  22. * MODNet.
  23. */
  24. class ULTRAINFER_DECL MODNet : public UltraInferModel {
  25. public:
  26. /** \brief Set path of model file and the configuration of runtime.
  27. *
  28. * \param[in] model_file Path of model file, e.g ./modnet.onnx
  29. * \param[in] params_file Path of parameter file, e.g ppyoloe/model.pdiparams,
  30. * if the model format is ONNX, this parameter will be ignored \param[in]
  31. * custom_option RuntimeOption for inference, the default will use cpu, and
  32. * choose the backend defined in "valid_cpu_backends" \param[in] model_format
  33. * Model format of the loaded model, default is ONNX format
  34. */
  35. MODNet(const std::string &model_file, const std::string &params_file = "",
  36. const RuntimeOption &custom_option = RuntimeOption(),
  37. const ModelFormat &model_format = ModelFormat::ONNX);
  38. std::string ModelName() const { return "matting/MODNet"; }
  39. /*! @brief
  40. Argument for image preprocessing step, tuple of (width, height), decide the
  41. target size after resize, default (256, 256)
  42. */
  43. std::vector<int> size;
  44. /*! @brief
  45. Argument for image preprocessing step, parameters for normalization, size
  46. should be the the same as channels, default alpha = {1.f / 127.5f, 1.f /
  47. 127.5f, 1.f / 127.5f}
  48. */
  49. std::vector<float> alpha;
  50. /*! @brief
  51. Argument for image preprocessing step, parameters for normalization, size
  52. should be the the same as channels, default beta = {-1.f, -1.f, -1.f}
  53. */
  54. std::vector<float> beta;
  55. /*! @brief
  56. Argument for image preprocessing step, whether to swap the B and R channel,
  57. such as BGR->RGB, default true.
  58. */
  59. bool swap_rb;
  60. /** \brief Predict the matting 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 matting result
  64. * will be written to this structure \return true if the prediction succeeded,
  65. * otherwise false
  66. */
  67. bool Predict(cv::Mat *im, MattingResult *result);
  68. private:
  69. bool Initialize();
  70. bool Preprocess(Mat *mat, FDTensor *output,
  71. std::map<std::string, std::array<int, 2>> *im_info);
  72. bool Postprocess(std::vector<FDTensor> &infer_result, MattingResult *result,
  73. const std::map<std::string, std::array<int, 2>> &im_info);
  74. };
  75. } // namespace matting
  76. } // namespace vision
  77. } // namespace ultra_infer