nanodet_plus.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 NanoDetPlus model object used when to load a NanoDetPlus model
  22. * exported by NanoDet.
  23. */
  24. class ULTRAINFER_DECL NanoDetPlus : 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 ./nanodet_plus_320.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. NanoDetPlus(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. /// Get model's name
  40. std::string ModelName() const { return "nanodet"; }
  41. /** \brief Predict the detection result for an input image
  42. *
  43. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  44. * with layout HWC, BGR format \param[in] result The output detection result
  45. * will be written to this structure \param[in] conf_threshold confidence
  46. * threashold for postprocessing, default is 0.35 \param[in] nms_iou_threshold
  47. * iou threshold for NMS, default is 0.5 \return true if the prediction
  48. * successed, otherwise false
  49. */
  50. virtual bool Predict(cv::Mat *im, DetectionResult *result,
  51. float conf_threshold = 0.35f,
  52. float nms_iou_threshold = 0.5f);
  53. /*! @brief
  54. Argument for image preprocessing step, tuple of input size (width, height),
  55. default (320, 320)
  56. */
  57. std::vector<int> size;
  58. // padding value, size should be the same as channels
  59. std::vector<float> padding_value;
  60. // keep aspect ratio or not when perform resize operation.
  61. // This option is set as `false` by default in NanoDet-Plus
  62. bool keep_ratio;
  63. // downsample strides for NanoDet-Plus to generate anchors,
  64. // will take (8, 16, 32, 64) as default values
  65. std::vector<int> downsample_strides;
  66. // for offsetting the boxes by classes when using NMS, default 4096
  67. float max_wh;
  68. /*! @brief
  69. Argument for image postprocessing step, reg_max for GFL regression, default 7
  70. */
  71. int reg_max;
  72. private:
  73. bool Initialize();
  74. bool Preprocess(Mat *mat, FDTensor *output,
  75. std::map<std::string, std::array<float, 2>> *im_info);
  76. bool Postprocess(FDTensor &infer_result, DetectionResult *result,
  77. const std::map<std::string, std::array<float, 2>> &im_info,
  78. float conf_threshold, float nms_iou_threshold);
  79. bool IsDynamicInput() const { return is_dynamic_input_; }
  80. // whether to inference with dynamic shape (e.g ONNX export with dynamic shape
  81. // or not.)
  82. // RangiLyu/nanodet official 'export_onnx.py' script will export static ONNX
  83. // by default.
  84. // This value will auto check by ultra_infer after the internal Runtime
  85. // initialized.
  86. bool is_dynamic_input_;
  87. };
  88. } // namespace detection
  89. } // namespace vision
  90. } // namespace ultra_infer