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
1514cv::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-
151149bool 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-
224220IScaleLayer* 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-
258253IElementWiseLayer * 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+
278274ILayer* 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+
287284ILayer* 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-
369363IConcatenationLayer* 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-
414403ILayer* 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-
450437ILayer* 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-
496481ILayer* 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
565550IPluginV2Layer* 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
0 commit comments