Skip to content

Commit fcdf274

Browse files
authored
Refactor yolov7 (wang-xinyu#1153)
* use cmake 3.10 * refactor yolov7 code * refactor yolov7 parse_args * remove anchor definition
1 parent 9071fa2 commit fcdf274

File tree

18 files changed

+451
-696
lines changed

18 files changed

+451
-696
lines changed

yolov7/CMakeLists.txt

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,45 @@
1-
cmake_minimum_required(VERSION 2.6)
1+
cmake_minimum_required(VERSION 3.10)
22

33
project(yolov7)
44

55
add_definitions(-std=c++11)
66
add_definitions(-DAPI_EXPORTS)
7-
option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
87
set(CMAKE_CXX_STANDARD 11)
98
set(CMAKE_BUILD_TYPE Debug)
109

11-
find_package(CUDA REQUIRED)
12-
13-
if(WIN32)
10+
set(CMAKE_CUDA_COMPILER /usr/local/cuda/bin/nvcc)
1411
enable_language(CUDA)
15-
endif(WIN32)
1612

1713
include_directories(${PROJECT_SOURCE_DIR}/include)
18-
# include and link dirs of cuda and tensorrt, you need adapt them if yours are different
19-
14+
include_directories(${PROJECT_SOURCE_DIR}/plugin)
2015

16+
# include and link dirs of cuda and tensorrt, you need adapt them if yours are different
2117
if (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
22-
message("embed_platform on")
23-
include_directories(/usr/local/cuda/targets/aarch64-linux/include)
24-
link_directories(/usr/local/cuda/targets/aarch64-linux/lib)
18+
message("embed_platform on")
19+
include_directories(/usr/local/cuda/targets/aarch64-linux/include)
20+
link_directories(/usr/local/cuda/targets/aarch64-linux/lib)
2521
else()
26-
message("embed_platform off")
27-
# cuda
28-
include_directories(/usr/local/cuda/include)
29-
link_directories(/usr/local/cuda/lib64)
30-
31-
# tensorrt
32-
include_directories(/home/na/TensorRT-7.2.2.3/include)
33-
link_directories(/home/na/TensorRT-7.2.2.3/lib)
22+
message("embed_platform off")
23+
# cuda
24+
include_directories(/usr/local/cuda/include)
25+
link_directories(/usr/local/cuda/lib64)
26+
27+
# tensorrt
28+
include_directories(/home/nvidia/TensorRT-8.2.5.1/include)
29+
link_directories(/home/nvidia/TensorRT-8.2.5.1/lib)
3430
endif()
3531

36-
37-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Ofast -g -Wfatal-errors -D_MWAITXINTRIN_H_INCLUDED")
38-
cuda_add_library(myplugins SHARED yololayer.cu)
32+
add_library(myplugins SHARED ${PROJECT_SOURCE_DIR}/plugin/yololayer.cu)
3933
target_link_libraries(myplugins nvinfer cudart)
4034

4135
find_package(OpenCV)
4236
include_directories(${OpenCV_INCLUDE_DIRS})
4337

44-
cuda_add_executable(yolov7 calibrator.cpp yolov7.cpp preprocess.cu)
38+
file(GLOB_RECURSE SRCS ${PROJECT_SOURCE_DIR}/src/*.cpp ${PROJECT_SOURCE_DIR}/src/*.cu)
39+
add_executable(yolov7 main.cpp ${SRCS})
4540

4641
target_link_libraries(yolov7 nvinfer)
4742
target_link_libraries(yolov7 cudart)
4843
target_link_libraries(yolov7 myplugins)
4944
target_link_libraries(yolov7 ${OpenCV_LIBS})
5045

51-
if(UNIX)
52-
add_definitions(-O2 -pthread)
53-
endif(UNIX)
54-
55-

yolov7/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ sudo ./yolov7 -s [.wts] [.engine] [t/v7/x/w6/e6/d6/e6e gd gw] // serialize mode
4848
sudo ./yolov7 -d [.engine] [image folder] // deserialize and run inference, the images in [image folder] will be processed.
4949
// For example yolov7
5050
sudo ./yolov7 -s yolov7.wts yolov7.engine v7
51-
sudo ./yolov7 -d yolov7.engine ../samples
51+
sudo ./yolov7 -d yolov7.engine ../images
5252
```
5353

5454
3. check the images generated, as follows. _zidane.jpg and _bus.jpg

yolov7/images

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../yolov3-spp/samples
File renamed without changes.

yolov7/common.hpp renamed to yolov7/include/common.hpp

Lines changed: 4 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#ifndef YOLOV7_COMMON_H_
22
#define YOLOV7_COMMON_H_
33

4+
#include "yololayer.h"
45
#include <fstream>
56
#include <map>
67
#include <sstream>
78
#include <vector>
89
#include <opencv2/opencv.hpp>
910
#include "NvInfer.h"
10-
#include "yololayer.h"
11-
using namespace nvinfer1;
12-
1311

12+
using namespace nvinfer1;
1413

1514
cv::Rect get_rect(cv::Mat& img, float bbox[4]) {
1615
float l, r, t, b;
@@ -147,7 +146,6 @@ float ciou(float lbox[4], float rbox[4]) {
147146
return ciou;
148147
}
149148

150-
151149
bool cmp(const Yolo::Detection& a, const Yolo::Detection& b) {
152150
return a.conf > b.conf;
153151
}
@@ -219,8 +217,6 @@ std::map<std::string, Weights> loadWeights(const std::string file) {
219217
return weightMap;
220218
}
221219

222-
223-
224220
IScaleLayer* addBatchNorm2d(INetworkDefinition* network, std::map<std::string, Weights>& weightMap, ITensor& input, std::string lname, float eps) {
225221
float* gamma = (float*)weightMap[lname + ".weight"].values;
226222
float* beta = (float*)weightMap[lname + ".bias"].values;
@@ -254,7 +250,6 @@ IScaleLayer* addBatchNorm2d(INetworkDefinition* network, std::map<std::string, W
254250
return scale_1;
255251
}
256252

257-
258253
IElementWiseLayer * convBnSilu(INetworkDefinition * network, std::map<std::string, Weights>&weightMap, ITensor & input, int c2, int k, int s, int p, std::string lname) {
259254
Weights emptywts{ DataType::kFLOAT, nullptr, 0 };
260255

@@ -275,6 +270,7 @@ IElementWiseLayer * convBnSilu(INetworkDefinition * network, std::map<std::strin
275270
assert(ew1);
276271
return ew1;
277272
}
273+
278274
ILayer* ReOrg(INetworkDefinition* network, std::map<std::string, Weights>& weightMap, ITensor& input, int inch) {
279275
ISliceLayer* s1 = network->addSlice(input, Dims3{ 0, 0, 0 }, Dims3{ inch, Yolo::INPUT_H / 2, Yolo::INPUT_W / 2 }, Dims3{ 1, 2, 2 });
280276
ISliceLayer* s2 = network->addSlice(input, Dims3{ 0, 1, 0 }, Dims3{ inch, Yolo::INPUT_H / 2, Yolo::INPUT_W / 2 }, Dims3{ 1, 2, 2 });
@@ -284,6 +280,7 @@ ILayer* ReOrg(INetworkDefinition* network, std::map<std::string, Weights>& weigh
284280
auto cat = network->addConcatenation(inputTensors, 4);
285281
return cat;
286282
}
283+
287284
ILayer* DownC(INetworkDefinition* network, std::map<std::string, Weights>& weightMap, ITensor& input, int c1, int c2, const std::string& lname)
288285
{
289286
int c_ = int(c2 * 0.5);
@@ -363,9 +360,6 @@ IElementWiseLayer* RepConv(INetworkDefinition* network, std::map<std::string, We
363360
return ew2;
364361
}
365362

366-
367-
368-
369363
IConcatenationLayer* MPC3(INetworkDefinition* network, std::map<std::string, Weights>& weightMap, ITensor& input, int outch, std::string lname1, std::string lname2, std::string lname3) {
370364
Weights emptywts{ DataType::kFLOAT, nullptr, 0 };
371365

@@ -406,11 +400,6 @@ IElementWiseLayer* C9_1(INetworkDefinition* network, std::map<std::string, Weigh
406400
return conv10;
407401
}
408402

409-
410-
411-
412-
413-
414403
ILayer* convBlock(INetworkDefinition* network, std::map<std::string, Weights>& weightMap, ITensor& input, int outch, int ksize, int s, int g, std::string lname) {
415404
Weights emptywts{ DataType::kFLOAT, nullptr, 0 };
416405
int p = ksize / 3;
@@ -445,8 +434,6 @@ IActivationLayer* convBlockLeakRelu(INetworkDefinition* network, std::map<std::s
445434
return ew1;
446435
}
447436

448-
449-
450437
ILayer* focus(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int inch, int outch, int ksize, std::string lname) {
451438
ISliceLayer *s1 = network->addSlice(input, Dims3{ 0, 0, 0 }, Dims3{ inch, Yolo::INPUT_H / 2, Yolo::INPUT_W / 2 }, Dims3{ 1, 2, 2 });
452439
ISliceLayer *s2 = network->addSlice(input, Dims3{ 0, 1, 0 }, Dims3{ inch, Yolo::INPUT_H / 2, Yolo::INPUT_W / 2 }, Dims3{ 1, 2, 2 });
@@ -491,8 +478,6 @@ ILayer* bottleneckCSP(INetworkDefinition *network, std::map<std::string, Weights
491478
return cv4;
492479
}
493480

494-
495-
496481
ILayer* C3(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int c1, int c2, int n, bool shortcut, int g, float e, std::string lname) {
497482
int c_ = (int)((float)c2 * e);
498483
auto cv1 = convBlock(network, weightMap, input, c_, 1, 1, 1, lname + ".cv1");
@@ -565,25 +550,6 @@ std::vector<std::vector<float>> getAnchors(std::map<std::string, Weights>& weigh
565550
IPluginV2Layer* addYoLoLayer(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, std::string lname, std::vector<IConvolutionLayer*> dets) {
566551
auto creator = getPluginRegistry()->getPluginCreator("YoloLayer_TRT", "1");
567552
auto anchors = getAnchors(weightMap, lname);
568-
float a[3][6] = { {12.0,16.0,19.0,36.0,40.0,28.0},{36.0,75.0,76.0,55.0,72.0,146.0},{142.0,110.0,192.0,243.0,459.0,401.0} };
569-
//std::vector<std::vector<float>> anchors = { {12.0,16.0,19.0,36.0,40.0,28.0},{36.0,75.0,76.0,55.0,72.0,146.0},{142.0,110.0,192.0,243.0,459.0,401.0} };
570-
/*for (int i = 0; i < 3; i++)
571-
{
572-
for (int j = 0; j < 6; j++)
573-
{
574-
anchors[i][j] = a[i][j];
575-
576-
}
577-
}*/
578-
// for (int i = 0; i < anchors.size(); i++)
579-
// {
580-
// for (int j = 0; j < anchors[i].size(); j++)
581-
// {
582-
// std::cout << anchors[i][j] << " ";
583-
//
584-
// }
585-
// std::cout << std::endl;
586-
// }
587553

588554
PluginField plugin_fields[2];
589555
int netinfo[4] = {Yolo::CLASS_NUM, Yolo::INPUT_W, Yolo::INPUT_H, Yolo::MAX_OUTPUT_BBOX_COUNT};
@@ -618,61 +584,5 @@ IPluginV2Layer* addYoLoLayer(INetworkDefinition *network, std::map<std::string,
618584
return yolo;
619585
}
620586

621-
622-
IPluginV2Layer* addYoLoLayer2(INetworkDefinition* network, std::map<std::string, Weights>& weightMap, std::string lname, std::vector<IShuffleLayer*> dets) {
623-
auto creator = getPluginRegistry()->getPluginCreator("YoloLayer_TRT", "1");
624-
auto anchors = getAnchors(weightMap, lname);
625-
float a[3][6] = { {12.0,16.0,19.0,36.0,40.0,28.0},{36.0,75.0,76.0,55.0,72.0,146.0},{142.0,110.0,192.0,243.0,459.0,401.0} };
626-
//std::vector<std::vector<float>> anchors = { {12.0,16.0,19.0,36.0,40.0,28.0},{36.0,75.0,76.0,55.0,72.0,146.0},{142.0,110.0,192.0,243.0,459.0,401.0} };
627-
/*for (int i = 0; i < 3; i++)
628-
{
629-
for (int j = 0; j < 6; j++)
630-
{
631-
anchors[i][j] = a[i][j];
632-
633-
}
634-
}*/
635-
// for (int i = 0; i < anchors.size(); i++)
636-
// {
637-
// for (int j = 0; j < anchors[i].size(); j++)
638-
// {
639-
// std::cout << anchors[i][j] << " ";
640-
//
641-
// }
642-
// std::cout << std::endl;
643-
// }
644-
645-
PluginField plugin_fields[2];
646-
int netinfo[4] = { Yolo::CLASS_NUM, Yolo::INPUT_W, Yolo::INPUT_H, Yolo::MAX_OUTPUT_BBOX_COUNT };
647-
plugin_fields[0].data = netinfo;
648-
plugin_fields[0].length = 4;
649-
plugin_fields[0].name = "netinfo";
650-
plugin_fields[0].type = PluginFieldType::kFLOAT32;
651-
int scale = 8;
652-
653-
std::vector<Yolo::YoloKernel> kernels;
654-
for (size_t i = 0; i < anchors.size(); i++) {
655-
Yolo::YoloKernel kernel;
656-
kernel.width = Yolo::INPUT_W / scale;
657-
kernel.height = Yolo::INPUT_H / scale;
658-
memcpy(kernel.anchors, &anchors[i][0], anchors[i].size() * sizeof(float));
659-
kernels.push_back(kernel);
660-
scale *= 2;
661-
}
662-
plugin_fields[1].data = &kernels[0];
663-
plugin_fields[1].length = kernels.size();
664-
plugin_fields[1].name = "kernels";
665-
plugin_fields[1].type = PluginFieldType::kFLOAT32;
666-
PluginFieldCollection plugin_data;
667-
plugin_data.nbFields = 2;
668-
plugin_data.fields = plugin_fields;
669-
IPluginV2* plugin_obj = creator->createPlugin("yololayer", &plugin_data);
670-
std::vector<ITensor*> input_tensors;
671-
for (auto det : dets) {
672-
input_tensors.push_back(det->getOutput(0));
673-
}
674-
auto yolo = network->addPluginV2(&input_tensors[0], input_tensors.size(), *plugin_obj);
675-
return yolo;
676-
}
677587
#endif
678588

File renamed without changes.
File renamed without changes.

yolov7/macros.h renamed to yolov7/include/macros.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef __MACROS_H
22
#define __MACROS_H
33

4+
#include "NvInfer.h"
5+
46
#ifdef API_EXPORTS
57
#if defined(_MSC_VER)
68
#define API __declspec(dllexport)
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)