Skip to content

Commit 824763e

Browse files
authored
separate model and block (wang-xinyu#1154)
1 parent b654857 commit 824763e

File tree

12 files changed

+2600
-2794
lines changed

12 files changed

+2600
-2794
lines changed

yolov7/include/block.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include "NvInfer.h"
4+
#include <string>
5+
#include <vector>
6+
#include <map>
7+
8+
std::map<std::string, nvinfer1::Weights> loadWeights(const std::string file);
9+
10+
nvinfer1::IElementWiseLayer* convBnSilu(nvinfer1::INetworkDefinition* network, std::map<std::string, nvinfer1::Weights>& weightMap, nvinfer1::ITensor& input, int c2, int k, int s, int p, std::string lname);
11+
12+
nvinfer1::ILayer* ReOrg(nvinfer1::INetworkDefinition* network, std::map<std::string, nvinfer1::Weights>& weightMap, nvinfer1::ITensor& input, int inch);
13+
14+
nvinfer1::ILayer* DownC(nvinfer1::INetworkDefinition* network, std::map<std::string, nvinfer1::Weights>& weightMap, nvinfer1::ITensor& input, int c1, int c2, const std::string& lname);
15+
16+
nvinfer1::IElementWiseLayer* SPPCSPC(nvinfer1::INetworkDefinition* network, std::map<std::string, nvinfer1::Weights>& weightMap, nvinfer1::ITensor& input, int c2, const std::string& lname);
17+
18+
nvinfer1::IElementWiseLayer* RepConv(nvinfer1::INetworkDefinition* network, std::map<std::string, nvinfer1::Weights>& weightMap, nvinfer1::ITensor& input, int c2, int k, int s, const std::string& lname);
19+
20+
nvinfer1::IActivationLayer* convBlockLeakRelu(nvinfer1::INetworkDefinition* network, std::map<std::string, nvinfer1::Weights>& weightMap, nvinfer1::ITensor& input, int outch, int ksize, int s, int p, std::string lname);
21+
22+
nvinfer1::IPluginV2Layer* addYoLoLayer(nvinfer1::INetworkDefinition *network, std::map<std::string, nvinfer1::Weights>& weightMap, std::string lname, std::vector<nvinfer1::IConvolutionLayer*> dets);
23+

yolov7/include/common.hpp

Lines changed: 0 additions & 588 deletions
This file was deleted.

yolov7/include/config.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#define USE_FP16 // set USE_INT8 or USE_FP16 or USE_FP32
4+
5+
// These are used to define input/output tensor names,
6+
// you can set them to whatever you want.
7+
const static char* kInputTensorName = "data";
8+
const static char* kOutputTensorName = "prob";
9+
10+
const static int kNumClass = 80;
11+
12+
// Yolo's input width and height must by divisible by 32
13+
const static int kInputH = 640;
14+
const static int kInputW = 640;
15+
16+
// Maximum number of output bounding boxes from yololayer plugin.
17+
// That is maximum number of output bounding boxes before NMS.
18+
const static int kMaxNumOutputBbox = 1000;
19+
20+
const static int kNumAnchor = 3;
21+
22+
// The bboxes whose conf lower kIgnoreThresh will be ignored in yololayer plugin.
23+
const static float kIgnoreThresh = 0.1f;
24+

yolov7/include/model.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include "NvInfer.h"
4+
#include <string>
5+
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);

yolov7/include/postprocess.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include "types.h"
4+
#include <opencv2/opencv.hpp>
5+
6+
cv::Rect get_rect(cv::Mat& img, float bbox[4]);
7+
8+
void nms(std::vector<Detection>& res, float *output, float conf_thresh, float nms_thresh = 0.5);
9+

yolov7/include/types.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "config.h"
4+
5+
struct YoloKernel {
6+
int width;
7+
int height;
8+
float anchors[kNumAnchor * 2];
9+
};
10+
11+
struct alignas(float) Detection {
12+
//center_x center_y w h
13+
float bbox[4];
14+
float conf; // bbox_conf * cls_conf
15+
float class_id;
16+
};
17+

yolov7/main.cpp

Lines changed: 18 additions & 2163 deletions
Large diffs are not rendered by default.

yolov7/plugin/yololayer.cu

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void read(const char*& buffer, T& val) {
1919
} // namespace Tn
2020

2121
namespace nvinfer1 {
22-
YoloLayerPlugin::YoloLayerPlugin(int classCount, int netWidth, int netHeight, int maxOut, const std::vector<Yolo::YoloKernel>& vYoloKernel) {
22+
YoloLayerPlugin::YoloLayerPlugin(int classCount, int netWidth, int netHeight, int maxOut, const std::vector<YoloKernel>& vYoloKernel) {
2323
mClassCount = classCount;
2424
mYoloV7NetWidth = netWidth;
2525
mYoloV7NetHeight = netHeight;
@@ -28,7 +28,7 @@ YoloLayerPlugin::YoloLayerPlugin(int classCount, int netWidth, int netHeight, in
2828
mKernelCount = vYoloKernel.size();
2929

3030
CUDA_CHECK(cudaMallocHost(&mAnchor, mKernelCount * sizeof(void*)));
31-
size_t AnchorLen = sizeof(float) * Yolo::CHECK_COUNT * 2;
31+
size_t AnchorLen = sizeof(float) * kNumAnchor * 2;
3232
for (int ii = 0; ii < mKernelCount; ii++) {
3333
CUDA_CHECK(cudaMalloc(&mAnchor[ii], AnchorLen));
3434
const auto& yolo = mYoloKernel[ii];
@@ -53,11 +53,11 @@ YoloLayerPlugin::YoloLayerPlugin(const void* data, size_t length) {
5353
read(d, mYoloV7NetHeight);
5454
read(d, mMaxOutObject);
5555
mYoloKernel.resize(mKernelCount);
56-
auto kernelSize = mKernelCount * sizeof(Yolo::YoloKernel);
56+
auto kernelSize = mKernelCount * sizeof(YoloKernel);
5757
memcpy(mYoloKernel.data(), d, kernelSize);
5858
d += kernelSize;
5959
CUDA_CHECK(cudaMallocHost(&mAnchor, mKernelCount * sizeof(void*)));
60-
size_t AnchorLen = sizeof(float) * Yolo::CHECK_COUNT * 2;
60+
size_t AnchorLen = sizeof(float) * kNumAnchor * 2;
6161
for (int ii = 0; ii < mKernelCount; ii++) {
6262
CUDA_CHECK(cudaMalloc(&mAnchor[ii], AnchorLen));
6363
const auto& yolo = mYoloKernel[ii];
@@ -75,15 +75,15 @@ void YoloLayerPlugin::serialize(void* buffer) const TRT_NOEXCEPT {
7575
write(d, mYoloV7NetWidth);
7676
write(d, mYoloV7NetHeight);
7777
write(d, mMaxOutObject);
78-
auto kernelSize = mKernelCount * sizeof(Yolo::YoloKernel);
78+
auto kernelSize = mKernelCount * sizeof(YoloKernel);
7979
memcpy(d, mYoloKernel.data(), kernelSize);
8080
d += kernelSize;
8181

8282
assert(d == a + getSerializationSize());
8383
}
8484

8585
size_t YoloLayerPlugin::getSerializationSize() const TRT_NOEXCEPT {
86-
return sizeof(mClassCount) + sizeof(mThreadCount) + sizeof(mKernelCount) + sizeof(Yolo::YoloKernel) * mYoloKernel.size() + sizeof(mYoloV7NetWidth) + sizeof(mYoloV7NetHeight) + sizeof(mMaxOutObject);
86+
return sizeof(mClassCount) + sizeof(mThreadCount) + sizeof(mKernelCount) + sizeof(YoloKernel) * mYoloKernel.size() + sizeof(mYoloV7NetWidth) + sizeof(mYoloV7NetHeight) + sizeof(mMaxOutObject);
8787
}
8888

8989
int YoloLayerPlugin::initialize() TRT_NOEXCEPT {
@@ -92,7 +92,7 @@ int YoloLayerPlugin::initialize() TRT_NOEXCEPT {
9292

9393
Dims YoloLayerPlugin::getOutputDimensions(int index, const Dims* inputs, int nbInputDims) TRT_NOEXCEPT {
9494
//output the result to channel
95-
int totalsize = mMaxOutObject * sizeof(Yolo::Detection) / sizeof(float);
95+
int totalsize = mMaxOutObject * sizeof(Detection) / sizeof(float);
9696
return Dims3(totalsize + 1, 1, 1);
9797
}
9898

@@ -150,19 +150,19 @@ IPluginV2IOExt* YoloLayerPlugin::clone() const TRT_NOEXCEPT {
150150
__device__ float Logist(float data) { return 1.0f / (1.0f + expf(-data)); };
151151

152152
__global__ void CalDetection(const float *input, float *output, int noElements,
153-
const int netwidth, const int netheight, int maxoutobject, int yoloWidth, int yoloHeight, const float anchors[Yolo::CHECK_COUNT * 2], int classes, int outputElem) {
153+
const int netwidth, const int netheight, int maxoutobject, int yoloWidth, int yoloHeight, const float anchors[kNumAnchor * 2], int classes, int outputElem) {
154154
int idx = threadIdx.x + blockDim.x * blockIdx.x;
155155
if (idx >= noElements) return;
156156

157157
int total_grid = yoloWidth * yoloHeight; // 80*80 40*40 20*20
158158
int bnIdx = idx / total_grid;
159159
idx = idx - total_grid * bnIdx;
160160
int info_len_i = 5 + classes;
161-
const float* curInput = input + bnIdx * (info_len_i * total_grid * Yolo::CHECK_COUNT);
161+
const float* curInput = input + bnIdx * (info_len_i * total_grid * kNumAnchor);
162162

163163
for (int k = 0; k < 3; k++) {
164164
float box_prob = Logist(curInput[idx + k * info_len_i * total_grid + 4 * total_grid]);
165-
if (box_prob < Yolo::IGNORE_THRESH) continue;
165+
if (box_prob < kIgnoreThresh) continue;
166166
int class_id = 0;
167167
float max_cls_prob = 0.0;
168168
for (int i = 5; i < info_len_i; ++i) {
@@ -175,8 +175,8 @@ __global__ void CalDetection(const float *input, float *output, int noElements,
175175
float *res_count = output + bnIdx * outputElem;
176176
int count = (int)atomicAdd(res_count, 1);
177177
if (count >= maxoutobject) return;
178-
char *data = (char*)res_count + sizeof(float) + count * sizeof(Yolo::Detection);
179-
Yolo::Detection *det = (Yolo::Detection*)(data);
178+
char *data = (char*)res_count + sizeof(float) + count * sizeof(Detection);
179+
Detection *det = (Detection*)(data);
180180

181181
int row = idx / yoloWidth;
182182
int col = idx % yoloWidth;
@@ -204,7 +204,7 @@ __global__ void CalDetection(const float *input, float *output, int noElements,
204204
}
205205

206206
void YoloLayerPlugin::forwardGpu(const float* const* inputs, float *output, cudaStream_t stream, int batchSize) {
207-
int outputElem = 1 + mMaxOutObject * sizeof(Yolo::Detection) / sizeof(float);
207+
int outputElem = 1 + mMaxOutObject * sizeof(Detection) / sizeof(float);
208208
for (int idx = 0; idx < batchSize; ++idx) {
209209
CUDA_CHECK(cudaMemsetAsync(output + idx * outputElem, 0, sizeof(float), stream));
210210
}
@@ -255,8 +255,8 @@ IPluginV2IOExt* YoloPluginCreator::createPlugin(const char* name, const PluginFi
255255
int input_w = p_netinfo[1];
256256
int input_h = p_netinfo[2];
257257
int max_output_object_count = p_netinfo[3];
258-
std::vector<Yolo::YoloKernel> kernels(fc->fields[1].length);
259-
memcpy(&kernels[0], fc->fields[1].data, kernels.size() * sizeof(Yolo::YoloKernel));
258+
std::vector<YoloKernel> kernels(fc->fields[1].length);
259+
memcpy(&kernels[0], fc->fields[1].data, kernels.size() * sizeof(YoloKernel));
260260
YoloLayerPlugin* obj = new YoloLayerPlugin(class_count, input_w, input_h, max_output_object_count, kernels);
261261
obj->setPluginNamespace(mNamespace.c_str());
262262
return obj;

yolov7/plugin/yololayer.h

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
1-
#ifndef _YOLO_LAYER_H
2-
#define _YOLO_LAYER_H
1+
#pragma once
32

43
#include "macros.h"
4+
#include "types.h"
55
#include <vector>
66
#include <string>
77

8-
namespace Yolo {
9-
static constexpr int CHECK_COUNT = 3;
10-
static constexpr float IGNORE_THRESH = 0.5f;
11-
struct YoloKernel {
12-
int width;
13-
int height;
14-
float anchors[CHECK_COUNT * 2];
15-
};
16-
static constexpr int MAX_OUTPUT_BBOX_COUNT = 1000;
17-
static constexpr int CLASS_NUM = 80;
18-
static constexpr int INPUT_H = 640; // yolov7's input height and width must be divisible by 32.
19-
static constexpr int INPUT_W = 640;
20-
21-
static constexpr int LOCATIONS = 4;
22-
struct alignas(float) Detection {
23-
//center_x center_y w h
24-
float bbox[LOCATIONS];
25-
float conf; // bbox_conf * cls_conf
26-
float class_id;
27-
};
28-
} // namespace yolo
29-
308
namespace nvinfer1 {
319
class API YoloLayerPlugin : public IPluginV2IOExt {
3210
public:
33-
YoloLayerPlugin(int classCount, int netWidth, int netHeight, int maxOut, const std::vector<Yolo::YoloKernel>& vYoloKernel);
11+
YoloLayerPlugin(int classCount, int netWidth, int netHeight, int maxOut, const std::vector<YoloKernel>& vYoloKernel);
3412
YoloLayerPlugin(const void* data, size_t length);
3513
~YoloLayerPlugin();
3614

@@ -90,7 +68,7 @@ class API YoloLayerPlugin : public IPluginV2IOExt {
9068
int mYoloV7NetWidth;
9169
int mYoloV7NetHeight;
9270
int mMaxOutObject;
93-
std::vector<Yolo::YoloKernel> mYoloKernel;
71+
std::vector<YoloKernel> mYoloKernel;
9472
void** mAnchor;
9573
};
9674

@@ -126,5 +104,3 @@ class API YoloPluginCreator : public IPluginCreator {
126104
REGISTER_TENSORRT_PLUGIN(YoloPluginCreator);
127105
} // namespace nvinfer1
128106

129-
#endif // _YOLO_LAYER_H
130-

0 commit comments

Comments
 (0)