yolor.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 
  2. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #pragma once
  16. #include "ultra_infer/ultra_infer_model.h"
  17. #include "ultra_infer/vision/common/processors/transform.h"
  18. #include "ultra_infer/vision/common/result.h"
  19. namespace ultra_infer {
  20. namespace vision {
  21. namespace detection {
  22. /*! @brief YOLOR model object used when to load a YOLOR model exported by YOLOR.
  23. */
  24. class ULTRAINFER_DECL YOLOR : 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 ./yolor.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. YOLOR(const std::string &model_file, const std::string &params_file = "",
  36. const RuntimeOption &custom_option = RuntimeOption(),
  37. const ModelFormat &model_format = ModelFormat::ONNX);
  38. virtual std::string ModelName() const { return "YOLOR"; }
  39. /** \brief Predict the detection result for an input image
  40. *
  41. * \param[in] im The input image data, comes from cv::imread()
  42. * \param[in] result The output detection result will be written to this
  43. * structure \param[in] conf_threshold confidence threashold for
  44. * postprocessing, default is 0.25 \param[in] nms_iou_threshold iou threashold
  45. * for NMS, default is 0.5 \return true if the prediction successed, otherwise
  46. * false
  47. */
  48. virtual bool Predict(cv::Mat *im, DetectionResult *result,
  49. float conf_threshold = 0.25,
  50. float nms_iou_threshold = 0.5);
  51. /*! @brief
  52. Argument for image preprocessing step, tuple of (width, height), decide the
  53. target size after resize, default size = {640, 640}
  54. */
  55. std::vector<int> size;
  56. // padding value, size should be the same as channels
  57. std::vector<float> padding_value;
  58. // only pad to the minimum rectangle which height and width is times of stride
  59. bool is_mini_pad;
  60. // while is_mini_pad = false and is_no_pad = true,
  61. // will resize the image to the set size
  62. bool is_no_pad;
  63. // if is_scale_up is false, the input image only can be zoom out,
  64. // the maximum resize scale cannot exceed 1.0
  65. bool is_scale_up;
  66. // padding stride, for is_mini_pad
  67. int stride;
  68. // for offsetting the boxes by classes when using NMS
  69. float max_wh;
  70. private:
  71. bool Initialize();
  72. bool Preprocess(Mat *mat, FDTensor *output,
  73. std::map<std::string, std::array<float, 2>> *im_info);
  74. bool Postprocess(FDTensor &infer_result, DetectionResult *result,
  75. const std::map<std::string, std::array<float, 2>> &im_info,
  76. float conf_threshold, float nms_iou_threshold);
  77. void LetterBox(Mat *mat, const std::vector<int> &size,
  78. const std::vector<float> &color, bool _auto,
  79. bool scale_fill = false, bool scale_up = true,
  80. int stride = 32);
  81. // whether to inference with dynamic shape (e.g ONNX export with dynamic shape
  82. // or not.)
  83. // while is_dynamic_shape if 'false', is_mini_pad will force 'false'. This
  84. // value will
  85. // auto check by ultra_infer after the internal Runtime already initialized.
  86. bool is_dynamic_input_;
  87. };
  88. } // namespace detection
  89. } // namespace vision
  90. } // namespace ultra_infer