yolov5lite.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. //NOLINT
  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 detection {
  21. /*! @brief YOLOv5Lite model object used when to load a YOLOv5Lite model exported
  22. * by YOLOv5Lite.
  23. */
  24. class ULTRAINFER_DECL YOLOv5Lite : 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 ./yolov5lite.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. YOLOv5Lite(const std::string &model_file, const std::string &params_file = "",
  36. const RuntimeOption &custom_option = RuntimeOption(),
  37. const ModelFormat &model_format = ModelFormat::ONNX);
  38. ~YOLOv5Lite();
  39. virtual std::string ModelName() const { return "YOLOv5-Lite"; }
  40. /** \brief Predict the detection result for an input image
  41. *
  42. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  43. * with layout HWC, BGR format \param[in] result The output detection result
  44. * will be written to this structure \param[in] conf_threshold confidence
  45. * threshold for postprocessing, default is 0.45 \param[in] nms_iou_threshold
  46. * iou threshold for NMS, default is 0.25 \return true if the prediction
  47. * succeeded, otherwise false
  48. */
  49. virtual bool Predict(cv::Mat *im, DetectionResult *result,
  50. float conf_threshold = 0.45,
  51. float nms_iou_threshold = 0.25);
  52. void UseCudaPreprocessing(int max_img_size = 3840 * 2160);
  53. /*! @brief
  54. Argument for image preprocessing step, tuple of (width, height), decide the
  55. target size after resize, size = {640, 640}
  56. */
  57. std::vector<int> size;
  58. // padding value, size should be the same as channels
  59. std::vector<float> padding_value;
  60. // only pad to the minimum rectangle which height and width is times of stride
  61. bool is_mini_pad;
  62. // while is_mini_pad = false and is_no_pad = true,
  63. // will resize the image to the set size
  64. bool is_no_pad;
  65. // if is_scale_up is false, the input image only can be zoom out,
  66. // the maximum resize scale cannot exceed 1.0
  67. bool is_scale_up;
  68. // padding stride, for is_mini_pad
  69. int stride;
  70. // for offsetting the boxes by classes when using NMS
  71. float max_wh;
  72. // downsample strides for YOLOv5Lite to generate anchors,
  73. // will take (8,16,32) as default values, might have stride=64.
  74. std::vector<int> downsample_strides;
  75. // anchors parameters, downsample_strides will take (8,16,32),
  76. // each stride has three anchors with width and height
  77. std::vector<std::vector<float>> anchor_config;
  78. /*! @brief
  79. whether the model_file was exported with decode module. The official
  80. YOLOv5Lite/export.py script will export ONNX file without
  81. decode module. Please set it 'true' manually if the model file
  82. was exported with decode module.
  83. false : ONNX files without decode module.
  84. true : ONNX file with decode module. default false.
  85. */
  86. bool is_decode_exported;
  87. private:
  88. // necessary parameters for GenerateAnchors to generate anchors when ONNX file
  89. // without decode module.
  90. struct Anchor {
  91. int grid0;
  92. int grid1;
  93. int stride;
  94. float anchor_w;
  95. float anchor_h;
  96. };
  97. bool Initialize();
  98. bool Preprocess(Mat *mat, FDTensor *output,
  99. std::map<std::string, std::array<float, 2>> *im_info);
  100. bool CudaPreprocess(Mat *mat, FDTensor *output,
  101. std::map<std::string, std::array<float, 2>> *im_info);
  102. bool Postprocess(FDTensor &infer_result, DetectionResult *result,
  103. const std::map<std::string, std::array<float, 2>> &im_info,
  104. float conf_threshold, float nms_iou_threshold);
  105. // the official YOLOv5Lite/export.py will export ONNX file without decode
  106. // module.
  107. // this function support the postporocess for ONNX file without decode module.
  108. // set the `is_decode_exported = false`, this function will work.
  109. bool PostprocessWithDecode(
  110. FDTensor &infer_result, DetectionResult *result,
  111. const std::map<std::string, std::array<float, 2>> &im_info,
  112. float conf_threshold, float nms_iou_threshold);
  113. void LetterBox(Mat *mat, const std::vector<int> &size,
  114. const std::vector<float> &color, bool _auto,
  115. bool scale_fill = false, bool scale_up = true,
  116. int stride = 32);
  117. // generate anchors for decoding when ONNX file without decode module.
  118. void GenerateAnchors(const std::vector<int> &size,
  119. const std::vector<int> &downsample_strides,
  120. std::vector<Anchor> *anchors, const int num_anchors = 3);
  121. // whether to inference with dynamic shape (e.g ONNX export with dynamic shape
  122. // or not.)
  123. // while is_dynamic_shape if 'false', is_mini_pad will force 'false'. This
  124. // value will
  125. // auto check by ultra_infer after the internal Runtime already initialized.
  126. bool is_dynamic_input_;
  127. // CUDA host buffer for input image
  128. uint8_t *input_img_cuda_buffer_host_ = nullptr;
  129. // CUDA device buffer for input image
  130. uint8_t *input_img_cuda_buffer_device_ = nullptr;
  131. // CUDA device buffer for TRT input tensor
  132. float *input_tensor_cuda_buffer_device_ = nullptr;
  133. // Whether to use CUDA preprocessing
  134. bool use_cuda_preprocessing_ = false;
  135. // CUDA stream
  136. void *cuda_stream_ = nullptr;
  137. };
  138. } // namespace detection
  139. } // namespace vision
  140. } // namespace ultra_infer