FastDeploy  latest
Fast & Easy to Deploy!
yolov5lite.h
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 
15 #pragma once
16 #include "fastdeploy/fastdeploy_model.h"
17 #include "fastdeploy/vision/common/processors/transform.h"
18 #include "fastdeploy/vision/common/result.h"
19 
20 namespace fastdeploy {
21 namespace vision {
22 namespace detection {
25 class FASTDEPLOY_DECL YOLOv5Lite : public FastDeployModel {
26  public:
34  YOLOv5Lite(const std::string& model_file, const std::string& params_file = "",
35  const RuntimeOption& custom_option = RuntimeOption(),
36  const ModelFormat& model_format = ModelFormat::ONNX);
37 
38  ~YOLOv5Lite();
39 
40  virtual std::string ModelName() const { return "YOLOv5-Lite"; }
49  virtual bool Predict(cv::Mat* im, DetectionResult* result,
50  float conf_threshold = 0.45,
51  float nms_iou_threshold = 0.25);
52 
53 
54  void UseCudaPreprocessing(int max_img_size = 3840 * 2160);
55 
59  std::vector<int> size;
60  // padding value, size should be the same as channels
61 
62  std::vector<float> padding_value;
63  // only pad to the minimum rectange which height and width is times of stride
64  bool is_mini_pad;
65  // while is_mini_pad = false and is_no_pad = true,
66  // will resize the image to the set size
67  bool is_no_pad;
68  // if is_scale_up is false, the input image only can be zoom out,
69  // the maximum resize scale cannot exceed 1.0
70  bool is_scale_up;
71  // padding stride, for is_mini_pad
72  int stride;
73  // for offseting the boxes by classes when using NMS
74  float max_wh;
75  // downsample strides for YOLOv5Lite to generate anchors,
76  // will take (8,16,32) as default values, might have stride=64.
77  std::vector<int> downsample_strides;
78  // anchors parameters, downsample_strides will take (8,16,32),
79  // each stride has three anchors with width and hight
80  std::vector<std::vector<float>> anchor_config;
90 
91  private:
92  // necessary parameters for GenerateAnchors to generate anchors when ONNX file
93  // without decode module.
94  struct Anchor {
95  int grid0;
96  int grid1;
97  int stride;
98  float anchor_w;
99  float anchor_h;
100  };
101 
102  bool Initialize();
103 
104  bool Preprocess(Mat* mat, FDTensor* output,
105  std::map<std::string, std::array<float, 2>>* im_info);
106 
107 
108  bool CudaPreprocess(Mat* mat, FDTensor* output,
109  std::map<std::string, std::array<float, 2>>* im_info);
110 
111  bool Postprocess(FDTensor& infer_result, DetectionResult* result,
112  const std::map<std::string, std::array<float, 2>>& im_info,
113  float conf_threshold, float nms_iou_threshold);
114 
115  // the official YOLOv5Lite/export.py will export ONNX file without decode
116  // module.
117  // this fuction support the postporocess for ONNX file without decode module.
118  // set the `is_decode_exported = false`, this function will work.
119  bool PostprocessWithDecode(
120  FDTensor& infer_result, DetectionResult* result,
121  const std::map<std::string, std::array<float, 2>>& im_info,
122  float conf_threshold, float nms_iou_threshold);
123 
124  void LetterBox(Mat* mat, const std::vector<int>& size,
125  const std::vector<float>& color, bool _auto,
126  bool scale_fill = false, bool scale_up = true,
127  int stride = 32);
128 
129  // generate anchors for decodeing when ONNX file without decode module.
130  void GenerateAnchors(const std::vector<int>& size,
131  const std::vector<int>& downsample_strides,
132  std::vector<Anchor>* anchors, const int num_anchors = 3);
133 
134  // whether to inference with dynamic shape (e.g ONNX export with dynamic shape
135  // or not.)
136  // while is_dynamic_shape if 'false', is_mini_pad will force 'false'. This
137  // value will
138  // auto check by fastdeploy after the internal Runtime already initialized.
139  bool is_dynamic_input_;
140  // CUDA host buffer for input image
141  uint8_t* input_img_cuda_buffer_host_ = nullptr;
142  // CUDA device buffer for input image
143  uint8_t* input_img_cuda_buffer_device_ = nullptr;
144  // CUDA device buffer for TRT input tensor
145  float* input_tensor_cuda_buffer_device_ = nullptr;
146  // Whether to use CUDA preprocessing
147  bool use_cuda_preprocessing_ = false;
148  // CUDA stream
149  void* cuda_stream_ = nullptr;
150 };
151 } // namespace detection
152 } // namespace vision
153 } // namespace fastdeploy
Option object used when create a new Runtime object.
Definition: runtime_option.h:40
Base model object for all the vision models.
Definition: fastdeploy_model.h:21
ModelFormat
Definition: enum_variables.h:67
FDTensor object used to represend data matrix.
Definition: fd_tensor.h:31
YOLOv5Lite model object used when to load a YOLOv5Lite model exported by YOLOv5Lite.
Definition: yolov5lite.h:25
Detection result structure for all the object detection models and instance segmentation models...
Definition: result.h:106
std::vector< int > size
Argument for image preprocessing step, tuple of (width, height), decide the target size after resize...
Definition: yolov5lite.h:59
FDMat is a structure for replace cv::Mat.
Definition: mat.h:34
virtual std::string ModelName() const
Get model&#39;s name.
Definition: yolov5lite.h:40
bool is_decode_exported
whether the model_file was exported with decode module. The official YOLOv5Lite/export.py script will export ONNX file without decode module. Please set it &#39;true&#39; manually if the model file was exported with decode module. false : ONNX files without decode module. true : ONNX file with decode module. default false.
Definition: yolov5lite.h:89
Model with ONNX format.
Definition: enum_variables.h:70
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16