yolox.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 YOLOX model object used when to load a YOLOX model exported by YOLOX.
  22. */
  23. class ULTRAINFER_DECL YOLOX : public UltraInferModel {
  24. public:
  25. /** \brief Set path of model file and the configuration of runtime.
  26. *
  27. * \param[in] model_file Path of model file, e.g ./yolox.onnx
  28. * \param[in] params_file Path of parameter file, e.g ppyoloe/model.pdiparams,
  29. * if the model format is ONNX, this parameter will be ignored \param[in]
  30. * custom_option RuntimeOption for inference, the default will use cpu, and
  31. * choose the backend defined in "valid_cpu_backends" \param[in] model_format
  32. * Model format of the loaded model, default is ONNX format
  33. */
  34. YOLOX(const std::string &model_file, const std::string &params_file = "",
  35. const RuntimeOption &custom_option = RuntimeOption(),
  36. const ModelFormat &model_format = ModelFormat::ONNX);
  37. std::string ModelName() const { return "YOLOX"; }
  38. /** \brief Predict the detection result for an input image
  39. *
  40. * \param[in] im The input image data, comes from cv::imread(), is a 3-D array
  41. * with layout HWC, BGR format \param[in] result The output detection result
  42. * will be written to this structure \param[in] conf_threshold confidence
  43. * threshold for postprocessing, default is 0.25 \param[in] nms_iou_threshold
  44. * iou threshold for NMS, default is 0.5 \return true if the prediction
  45. * succeeded, otherwise false
  46. */
  47. virtual bool Predict(cv::Mat *im, DetectionResult *result,
  48. float conf_threshold = 0.25,
  49. float nms_iou_threshold = 0.5);
  50. /*! @brief
  51. Argument for image preprocessing step, tuple of (width, height), decide the
  52. target size after resize, default size = {640, 640}
  53. */
  54. std::vector<int> size;
  55. // padding value, size should be the same as channels
  56. std::vector<float> padding_value;
  57. /*! @brief
  58. whether the model_file was exported with decode module. The official
  59. YOLOX/tools/export_onnx.py script will export ONNX file without
  60. decode module. Please set it 'true' manually if the model file
  61. was exported with decode module. default false.
  62. */
  63. bool is_decode_exported;
  64. // downsample strides for YOLOX to generate anchors,
  65. // will take (8,16,32) as default values, might have stride=64
  66. std::vector<int> downsample_strides;
  67. // for offsetting the boxes by classes when using NMS, default 4096
  68. float max_wh;
  69. private:
  70. bool Initialize();
  71. bool Preprocess(Mat *mat, FDTensor *outputs,
  72. std::map<std::string, std::array<float, 2>> *im_info);
  73. bool Postprocess(FDTensor &infer_result, DetectionResult *result,
  74. const std::map<std::string, std::array<float, 2>> &im_info,
  75. float conf_threshold, float nms_iou_threshold);
  76. bool PostprocessWithDecode(
  77. FDTensor &infer_result, DetectionResult *result,
  78. const std::map<std::string, std::array<float, 2>> &im_info,
  79. float conf_threshold, float nms_iou_threshold);
  80. bool IsDynamicInput() const { return is_dynamic_input_; }
  81. // whether to inference with dynamic shape (e.g ONNX export with dynamic shape
  82. // or not.)
  83. // megvii/YOLOX official 'export_onnx.py' script will export static ONNX by
  84. // default.
  85. // while is_dynamic_shape if 'false', is_mini_pad will force 'false'. This
  86. // value will
  87. // auto check by ultra_infer after the internal Runtime already initialized.
  88. bool is_dynamic_input_;
  89. };
  90. } // namespace detection
  91. } // namespace vision
  92. } // namespace ultra_infer