Skip to content

Commit 816eef5

Browse files
authored
Refactor yolov7 (wang-xinyu#1155)
* separate model and block * move thresh to config * use trt8 api * move batchsize to config * update readme * refactor batch infer logic * move max input image size to config
1 parent 4a01957 commit 816eef5

File tree

11 files changed

+522
-477
lines changed

11 files changed

+522
-477
lines changed

yolov7/README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
# yolov7
1+
# YOLOv7
22

33
The Pytorch implementation is [WongKinYiu/yolov7](https://github.com/WongKinYiu/yolov7).
44

55
The tensorrt code is derived from [QIANXUNZDL123/tensorrtx-yolov7](https://github.com/QIANXUNZDL123/tensorrtx-yolov7)
66

7+
## Contributors
8+
9+
<a href="https://github.com/QIANXUNZDL123"><img src="https://avatars.githubusercontent.com/u/46549527?v=4?s=48" width="40px;" alt=""/></a>
10+
<a href="https://github.com/lindsayshuo"><img src="https://avatars.githubusercontent.com/u/45239466?v=4?s=48" width="40px;" alt=""/></a>
11+
<a href="https://github.com/wang-xinyu"><img src="https://avatars.githubusercontent.com/u/15235574?s=48&v=4" width="40px;" alt=""/></a>
12+
13+
## Requirements
14+
15+
- TensorRT 8.0+
16+
- OpenCV 3.4.0+
17+
718
## Different versions of yolov7
819

920
Currently, we support yolov7 v0.1
1021

1122
- For yolov7 v0.1, download .pt from [yolov7 release v0.1](https://github.com/WongKinYiu/yolov7/releases/tag/v0.10), then follow how-to-run in current page.
1223

13-
1424
## Config
1525

1626
- Choose the model tiny/v7/x/d6/w6/e6/e6e from command line arguments.
17-
- Input shape defined in yololayer.h
18-
- Number of classes defined in yololayer.h, **DO NOT FORGET TO ADAPT THIS, If using your own model**
19-
- INT8/FP16/FP32 can be selected by the macro in yolov7.cpp, **INT8 need more steps, pls follow `How to Run` first and then go the `INT8 Quantization` below**
20-
- GPU id can be selected by the macro in yolov7.cpp
21-
- NMS thresh in yolov7.cpp
22-
- BBox confidence thresh in yolov7.cpp
23-
- Batch size in yolov7.cpp
27+
- Check more configs in [include/config.h](./include/config.h)
2428

2529
## How to Run, yolov7-tiny as example
2630

@@ -38,7 +42,7 @@ python gen_wts.py
3842

3943
```
4044
cd {tensorrtx}/yolov7/
41-
// update CLASS_NUM in yololayer.h if your model is trained on custom dataset
45+
// update kNumClass in config.h if your model is trained on custom dataset
4246
mkdir build
4347
cd build
4448
cp {WongKinYiu}/yolov7/yolov7.wts {tensorrtx}/yolov7/build
@@ -67,18 +71,15 @@ python yolov7_trt.py
6771

6872
2. unzip it in yolov7/build
6973

70-
3. set the macro `USE_INT8` in yolov7.cpp and make
74+
3. set the macro `USE_INT8` in config.h and make
7175

7276
4. serialize the model and test
7377

7478
<p align="center">
75-
<img src="https://user-images.githubusercontent.com/15235574/78247927-4d9fac00-751e-11ea-8b1b-704a0aeb3fcf.jpg">
76-
</p>
77-
78-
<p align="center">
79-
<img src="https://user-images.githubusercontent.com/15235574/78247970-60b27c00-751e-11ea-88df-41473fed4823.jpg">
79+
<img src="https://user-images.githubusercontent.com/15235574/78247927-4d9fac00-751e-11ea-8b1b-704a0aeb3fcf.jpg" height="360px;">
8080
</p>
8181

8282
## More Information
8383

8484
See the readme in [home page.](https://github.com/wang-xinyu/tensorrtx)
85+

yolov7/include/config.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#pragma once
22

3+
/* --------------------------------------------------------
4+
* These configs are related to tensorrt model, if these are changed,
5+
* please re-compile and re-serialize the tensorrt model.
6+
* --------------------------------------------------------*/
7+
8+
// For INT8, you need prepare the calibration dataset, please refer to
9+
// https://github.com/wang-xinyu/tensorrtx/tree/master/yolov7#int8-quantization
310
#define USE_FP16 // set USE_INT8 or USE_FP16 or USE_FP32
411

512
// These are used to define input/output tensor names,
@@ -8,6 +15,7 @@ const static char* kInputTensorName = "data";
815
const static char* kOutputTensorName = "prob";
916

1017
const static int kNumClass = 80;
18+
const static int kBatchSize = 1;
1119

1220
// Yolo's input width and height must by divisible by 32
1321
const static int kInputH = 640;
@@ -19,6 +27,20 @@ const static int kMaxNumOutputBbox = 1000;
1927

2028
const static int kNumAnchor = 3;
2129

22-
// The bboxes whose conf lower kIgnoreThresh will be ignored in yololayer plugin.
30+
// The bboxes whose confidence is lower than kIgnoreThresh will be ignored in yololayer plugin.
2331
const static float kIgnoreThresh = 0.1f;
2432

33+
/* --------------------------------------------------------
34+
* These configs are not related to tensorrt model, if these are changed,
35+
* please re-compile, but no need to re-serialize the tensorrt model.
36+
* --------------------------------------------------------*/
37+
38+
// NMS overlapping thresh and final detection confidence thresh
39+
const static float kNmsThresh = 0.45f;
40+
const static float kConfThresh = 0.5f;
41+
42+
const static int kGpuId = 0;
43+
44+
// If your image size is larger than 4096 * 3112, please increase this value
45+
const static int kMaxInputImageSize = 4096 * 3112;
46+

yolov7/include/model.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#include "NvInfer.h"
44
#include <string>
55

6-
nvinfer1::ICudaEngine* build_engine_yolov7e6e(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
7-
nvinfer1::ICudaEngine* build_engine_yolov7d6(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
8-
nvinfer1::ICudaEngine* build_engine_yolov7e6(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
9-
nvinfer1::ICudaEngine* build_engine_yolov7w6(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
10-
nvinfer1::ICudaEngine* build_engine_yolov7x(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
11-
nvinfer1::ICudaEngine* build_engine_yolov7(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
12-
nvinfer1::ICudaEngine* build_engine_yolov7_tiny(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, std::string& wts_name);
6+
nvinfer1::IHostMemory* build_engine_yolov7e6e(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
7+
nvinfer1::IHostMemory* build_engine_yolov7d6(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
8+
nvinfer1::IHostMemory* build_engine_yolov7e6(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
9+
nvinfer1::IHostMemory* build_engine_yolov7w6(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
10+
nvinfer1::IHostMemory* build_engine_yolov7x(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
11+
nvinfer1::IHostMemory* build_engine_yolov7(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, const std::string& wts_path);
12+
nvinfer1::IHostMemory* build_engine_yolov7_tiny(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, std::string& wts_name);

yolov7/include/postprocess.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ cv::Rect get_rect(cv::Mat& img, float bbox[4]);
77

88
void nms(std::vector<Detection>& res, float *output, float conf_thresh, float nms_thresh = 0.5);
99

10+
void batch_nms(std::vector<std::vector<Detection>>& batch_res, float *output, int batch_size, int output_size, float conf_thresh, float nms_thresh = 0.5);
11+
12+
void draw_bbox(std::vector<cv::Mat>& img_batch, std::vector<std::vector<Detection>>& res_batch);
13+

yolov7/include/preprocess.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
#ifndef __PREPROCESS_H
2-
#define __PREPROCESS_H
1+
#pragma once
32

43
#include <cuda_runtime.h>
54
#include <cstdint>
6-
7-
8-
struct AffineMatrix{
9-
float value[6];
10-
};
11-
12-
13-
void preprocess_kernel_img(uint8_t* src, int src_width, int src_height,
5+
#include <opencv2/opencv.hpp>
6+
7+
void cuda_preprocess_init(int max_image_size);
8+
void cuda_preprocess_destroy();
9+
void cuda_preprocess(uint8_t* src, int src_width, int src_height,
10+
float* dst, int dst_width, int dst_height,
11+
cudaStream_t stream);
12+
void cuda_batch_preprocess(std::vector<cv::Mat>& img_batch,
1413
float* dst, int dst_width, int dst_height,
1514
cudaStream_t stream);
16-
#endif // __PREPROCESS_H
15+

0 commit comments

Comments
 (0)