scaledyolov4.h 4.1 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. namespace detection {
  21. /*! @brief ScaledYOLOv4 model object used when to load a ScaledYOLOv4 model
  22. * exported by ScaledYOLOv4.
  23. */
  24. class ULTRAINFER_DECL ScaledYOLOv4 : 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 ./scaled_yolov4.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. ScaledYOLOv4(const std::string &model_file,
  36. const std::string &params_file = "",
  37. const RuntimeOption &custom_option = RuntimeOption(),
  38. const ModelFormat &model_format = ModelFormat::ONNX);
  39. virtual std::string ModelName() const { return "ScaledYOLOv4"; }
  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. * threashold for postprocessing, default is 0.25 \param[in] nms_iou_threshold
  46. * iou threshold for NMS, default is 0.5 \return true if the prediction
  47. * successed, otherwise false
  48. */
  49. virtual bool Predict(cv::Mat *im, DetectionResult *result,
  50. float conf_threshold = 0.25,
  51. float nms_iou_threshold = 0.5);
  52. /*! @brief
  53. Argument for image preprocessing step, tuple of (width, height), decide the
  54. target size after resize, default size = {640, 640}
  55. */
  56. std::vector<int> size;
  57. // padding value, size should be the same as channels
  58. std::vector<float> padding_value;
  59. // only pad to the minimum rectangle which height and width is times of stride
  60. bool is_mini_pad;
  61. // while is_mini_pad = false and is_no_pad = true,
  62. // will resize the image to the set size
  63. bool is_no_pad;
  64. // if is_scale_up is false, the input image only can be zoom out,
  65. // the maximum resize scale cannot exceed 1.0
  66. bool is_scale_up;
  67. // padding stride, for is_mini_pad
  68. int stride;
  69. // for offsetting the boxes by classes when using NMS
  70. float max_wh;
  71. private:
  72. bool Initialize();
  73. bool Preprocess(Mat *mat, FDTensor *output,
  74. std::map<std::string, std::array<float, 2>> *im_info);
  75. bool Postprocess(FDTensor &infer_result, DetectionResult *result,
  76. const std::map<std::string, std::array<float, 2>> &im_info,
  77. float conf_threshold, float nms_iou_threshold);
  78. void LetterBox(Mat *mat, const std::vector<int> &size,
  79. const std::vector<float> &color, bool _auto,
  80. bool scale_fill = false, bool scale_up = true,
  81. int stride = 32);
  82. // whether to inference with dynamic shape (e.g ONNX export with dynamic shape
  83. // or not.)
  84. // while is_dynamic_shape if 'false', is_mini_pad will force 'false'. This
  85. // value will
  86. // auto check by ultra_infer after the internal Runtime already initialized
  87. bool is_dynamic_input_;
  88. };
  89. } // namespace detection
  90. } // namespace vision
  91. } // namespace ultra_infer