-
Notifications
You must be signed in to change notification settings - Fork 30
/
trtDarkNet53.h
153 lines (127 loc) · 4.48 KB
/
trtDarkNet53.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once
#include "argsParser.h"
#include "buffers.h"
#include "common.h"
#include "logger.h"
#include <thread>
#include "NvCaffeParser.h"
#include "NvInfer.h"
#include "NvOnnxParser.h"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cuda_runtime_api.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <dirent.h>
#include <string>
#include <vector>
#include <string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include "yoloPlugin.h"
#include <opencv2/opencv.hpp>
#include <opencv2/opencv.hpp>
#include "trtNetWork.h"
struct BBox
{
float x1, y1, x2, y2;
};
struct BBoxInfo
{
BBox box;
int label;
int classId; // For coco benchmarking
float prob;
};
struct TensorInfo
{
std::string blobName;
uint stride{0};
uint gridSize{0};
uint numClasses{0};
uint numBBoxes{0};
uint64_t volume{0};
std::vector<uint> masks;
std::vector<float> anchors;
int bindingIndex{-1};
float* hostBuffer{nullptr};
};
class YoloIInt8Calibrator : public IInt8EntropyCalibrator2 {
public:
YoloIInt8Calibrator(int bcsz, int bcs, std::string datadir)
:batchsize(bcsz),batches(bcs),clibdatadir(datadir),curbatch(0),
imgindex(0),mInputBlobName("data"){
clbimagepaths = ls(clibdatadir, batchsize * batches);
assert(clbimagepaths.size() == batchsize * batches);
mInputCount = batchsize * 3 * 416 * 416;
CHECK(cudaMalloc(&mDeviceInput, mInputCount * sizeof(float)));
}
virtual int getBatchSize() const override{
return batchsize;
}
virtual bool getBatch(void* bindings[], const char* names[], int nbBindings) override;
virtual const void* readCalibrationCache(std::size_t& length) override{
}
virtual void writeCalibrationCache(const void* ptr, std::size_t length) override{
}
private:
inline cv::Mat getImage(int index){
cv::Mat img = cv::imread(clbimagepaths[index]);
return std::move(img);
}
std::vector<std::string> ls(std::string path,long howmany);
std::string clibdatadir;
int batches;
int batchsize;
int curbatch;
int imgindex;
std::vector<std::string> clbimagepaths;
void* mDeviceInput{nullptr};
int mInputCount;
const char* mInputBlobName;
};
class YoloTrtNet : public trt::trtNetWork{
public:
YoloTrtNet(std::string engineFilePath, trt::ModelInfo minfo,trt::BuildInfo binfo)
:trt::trtNetWork(engineFilePath, minfo, binfo){
}
inline std::vector<BBoxInfo> doDetect(void *data, int contextIndex, int batch){
Inference(data, contextIndex, batch);
std::vector<BBoxInfo> bxinfo;
for (auto& tensor : m_OutputTensors[0])
{
std::vector<BBoxInfo> curBInfo = decodeTensor(0, 416, 416, tensor);
bxinfo.insert(bxinfo.end(), curBInfo.begin(), curBInfo.end());
}
return std::move(nmsAllClasses(0.5, bxinfo, 80));
}
virtual bool CreateNetwork() override;
virtual bool CreateEngineAndSerialize(long maxWorkspaceSize, BuilderFlag flag, int maxBatch) override;
virtual bool EngineDeserialize() override;
private:
std::vector<std::vector<TensorInfo> >m_OutputTensors;
int m_InputH = 416;
int m_InputW = 416;
float m_ProbThresh = 0.5;
std::vector<BBoxInfo> nonMaximumSuppression(const float nmsThresh, std::vector<BBoxInfo> binfo);
std::vector<BBoxInfo> nmsAllClasses(const float nmsThresh, std::vector<BBoxInfo>& binfo,
const uint numClasses);
inline float clamp(const float val, const float minVal, const float maxVal)
{
assert(minVal <= maxVal);
return std::min(maxVal, std::max(minVal, val));
}
void convertBBoxImgRes(const float scalingFactor, const float& xOffset, const float& yOffset,
BBox& bbox);
BBox convertBBoxNetRes(const float& bx, const float& by, const float& bw, const float& bh,
const uint& stride, const uint& netW, const uint& netH);
void addBBoxProposal(const float bx, const float by, const float bw, const float bh,
const uint stride, const float scalingFactor, const float xOffset,
const float yOffset, const int maxIndex, const float maxProb,
std::vector<BBoxInfo>& binfo);
std::vector<BBoxInfo> decodeTensor(const int imageIdx, const int imageH, const int imageW,
const TensorInfo& tensor);
};