rvm.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /** \brief All image/video matting model APIs are defined inside this namespace
  21. *
  22. */
  23. namespace matting {
  24. /*! @brief RobustVideoMatting model object used when to load a
  25. * RobustVideoMatting model exported by RobustVideoMatting
  26. */
  27. class ULTRAINFER_DECL RobustVideoMatting : 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 rvm/rvm_mobilenetv3_fp32.onnx
  33. * \param[in] params_file Path of parameter file, if the model format is ONNX,
  34. * this parameter will be ignored \param[in] custom_option RuntimeOption for
  35. * inference, the default will use cpu, and choose the backend defined in
  36. * `valid_cpu_backends` \param[in] model_format Model format of the loaded
  37. * model, default is ONNX format
  38. */
  39. RobustVideoMatting(const std::string &model_file,
  40. const std::string &params_file = "",
  41. const RuntimeOption &custom_option = RuntimeOption(),
  42. const ModelFormat &model_format = ModelFormat::ONNX);
  43. /// Get model's name
  44. std::string ModelName() const { return "matting/RobustVideoMatting"; }
  45. /** \brief Predict the matting result for an input image
  46. *
  47. * \param[in] im The input image data, comes from cv::imread()
  48. * \param[in] result The output matting result will be written to this
  49. * structure \return true if the prediction succeeded, otherwise false
  50. */
  51. bool Predict(cv::Mat *im, MattingResult *result);
  52. /// Preprocess image size, the default is (1080, 1920)
  53. std::vector<int> size;
  54. /// Whether to open the video mode, if there are some irrelevant pictures, set
  55. /// it to false, the default is true // NOLINT
  56. bool video_mode;
  57. /// Whether convert to RGB, Set to false if you have converted YUV format
  58. /// images to RGB outside the model, default true // NOLINT
  59. bool swap_rb;
  60. private:
  61. bool Initialize();
  62. /// Preprocess an input image, and set the preprocessed results to `outputs`
  63. bool Preprocess(Mat *mat, FDTensor *output,
  64. std::map<std::string, std::array<int, 2>> *im_info);
  65. /// Postprocess the inferenced results, and set the final result to `result`
  66. bool Postprocess(std::vector<FDTensor> &infer_result, MattingResult *result,
  67. const std::map<std::string, std::array<int, 2>> &im_info);
  68. /// Init dynamic inputs datas
  69. std::vector<std::vector<float>> dynamic_inputs_datas_ = {
  70. {0.0f}, // r1i
  71. {0.0f}, // r2i
  72. {0.0f}, // r3i
  73. {0.0f}, // r4i
  74. {0.25f}, // downsample_ratio
  75. };
  76. /// Init dynamic inputs dims
  77. std::vector<std::vector<int64_t>> dynamic_inputs_dims_ = {
  78. {1, 1, 1, 1}, // r1i
  79. {1, 1, 1, 1}, // r2i
  80. {1, 1, 1, 1}, // r3i
  81. {1, 1, 1, 1}, // r4i
  82. {1}, // downsample_ratio
  83. };
  84. };
  85. } // namespace matting
  86. } // namespace vision
  87. } // namespace ultra_infer