-
Notifications
You must be signed in to change notification settings - Fork 7
/
Utils.cpp
240 lines (216 loc) · 7.07 KB
/
Utils.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "Utils.h"
using namespace nvinfer1;
using namespace std;
//#define USE_GPU
void prepareImage(cv::Mat& img, float *data, int w, int h, int c, bool cvtColor, bool padCenter, bool pad, bool normalize)
{
float scale = min(float(w) / img.cols, float(h) / img.rows);
auto scaleSize = cv::Size(img.cols * scale, img.rows * scale);
if (scaleSize.height < 1 || scaleSize.width < 1)
pad = false;
#ifdef USE_GPU
cv::cuda::GpuMat rgb_gpu;
cv::cuda::GpuMat img_gpu(img);
cv::cuda::resize(img_gpu, rgb_gpu, scaleSize, 0, 0, cv::INTER_NEAREST);
if (cvtColor)
{
cv::cuda::cvtColor(rgb_gpu, rgb_gpu, CV_BGR2RGB);
}
#else
cv::Mat rgb;
if (pad)
cv::resize(img, rgb, scaleSize, 0, 0, cv::INTER_NEAREST);
else
cv::resize(img, rgb, cv::Size(w, h), 0, 0, cv::INTER_NEAREST);
if (cvtColor)
{
cv::cvtColor(rgb, rgb, CV_BGR2RGB);
}
#endif
//#ifndef USE_GPU
//rgb_gpu.download(rgb);
//#endif
#ifdef USE_GPU
cv::cuda::GpuMat cropped(h, w, CV_8UC3, 127);
#else
cv::Mat cropped(h, w, CV_8UC3, cv::Scalar(127, 127, 127));
#endif
if (pad)
{
#ifdef USE_GPU
if (padCenter)
{
cv::Rect rect((w - scaleSize.width) / 2, (h - scaleSize.height) / 2, scaleSize.width, scaleSize.height);
rgb_gpu.copyTo(cropped(rect));
}
else
{
cv::Rect rect(0, 0, scaleSize.width, scaleSize.height);
rgb_gpu.copyTo(cropped(rect));
}
#else
if (padCenter)
{
cv::Rect rect((w - scaleSize.width) / 2, (h - scaleSize.height) / 2, scaleSize.width, scaleSize.height);
rgb.copyTo(cropped(rect));
}
else
{
cv::Rect rect(0, 0, scaleSize.width, scaleSize.height);
rgb.copyTo(cropped(rect));
}
#endif
}
else
{
rgb.copyTo(cropped);
}
float factor = 1.0;
if (normalize)
factor = 1/255.0;
#ifdef USE_GPU
cv::cuda::GpuMat img_float;
if (c == 3)
cropped.convertTo(img_float, CV_32FC3, factor);
else
cropped.convertTo(img_float, CV_32FC1, factor);
#else
cv::Mat img_float;
if (c == 3)
cropped.convertTo(img_float, CV_32FC3, factor);
else
cropped.convertTo(img_float, CV_32FC1, factor);
#endif
//HWC TO CHW
#ifdef USE_GPU
cv::cuda::GpuMat input_channels_gpu[c];
cv::cuda::split(img_float, input_channels_gpu);
cv::Mat input_channels[c];
for (int i = 0; i < c; ++i)
{
input_channels_gpu[i].download(input_channels[i]);
}
#else
cv::Mat input_channels[c];
cv::split(img_float, input_channels);
#endif
int channelLength = h * w;
for (int i = 0; i < c; ++i)
{
memcpy(data, input_channels[i].data, channelLength * sizeof(float));
data += channelLength;
}
}
void setLayerPrecision(nvinfer1::INetworkDefinition*& network)
{
for (int i = 0; i < network->getNbLayers(); ++i)
{
auto layer = network->getLayer(i);
layer->setPrecision(nvinfer1::DataType::kINT8);
for (int j = 0; j < layer->getNbOutputs(); ++j)
{
std::string tensorName = layer->getOutput(j)->getName();
layer->setOutputType(j, nvinfer1::DataType::kINT8);
}
}
}
void setDynamicRange(nvinfer1::INetworkDefinition*& network)
{
string name = network->getLayer(0)->getInput(0)->getName();
for (int i = 0; i < network->getNbInputs(); ++i)
{
string name = network->getInput(i)->getName();
//network->getInput(i)->setDynamicRange(-mPerTensorDynamicRangeMap.at(name), mPerTensorDynamicRangeMap.at(name));
//for now, use a simplified version:
network->getInput(i)->setDynamicRange(1e-12, 0.01);
}
for (int i = 0; i < network->getNbLayers(); ++i)
{
auto layer = network->getLayer(i);
for (int j = 0; j < layer->getNbOutputs(); ++j)
{
std::string tensorName = layer->getOutput(j)->getName();
layer->getOutput(j)->setDynamicRange(1e-12, 0.01);
}
}
}
void onnxToTRTModel(const std::string& modelFile,
unsigned int maxBatchSize,
IHostMemory*& trtModelStream, Logger &logger, bool useInt8, bool markOutput, IInt8EntropyCalibrator* calibrator)
{
IBuilder* builder = createInferBuilder(logger);
nvinfer1::INetworkDefinition* network = builder->createNetwork();
auto parser = nvonnxparser::createParser(*network, logger);
std::ifstream onnx_file(modelFile.c_str(), std::ios::binary | std::ios::ate);
std::streamsize file_size = onnx_file.tellg();
onnx_file.seekg(0, std::ios::beg);
std::vector<char> onnx_buf(file_size);
if(!onnx_file.read(onnx_buf.data(), onnx_buf.size()) )
{
string msg("failed to open onnx file");
logger.log(nvinfer1::ILogger::Severity::kERROR, msg.c_str());
}
if (!parser->parse(onnx_buf.data(), onnx_buf.size()))
{
string msg("failed to parse onnx file");
logger.log(nvinfer1::ILogger::Severity::kERROR, msg.c_str());
}
builder->setMaxBatchSize(maxBatchSize);
builder->setMaxWorkspaceSize(1 << 20);
if (useInt8 && builder->platformHasFastInt8())
{
builder->setInt8Mode(true);
builder->setInt8Calibrator(calibrator);
//setLayerPrecision(network);
//setDynamicRange(network);
}
else
{
builder->setFp16Mode(true);
}
builder->setStrictTypeConstraints(true);
if (markOutput)
{
network->markOutput(*network->getLayer(network->getNbLayers()-1)->getOutput(0));
}
ICudaEngine* engine = builder->buildCudaEngine(*network);
assert(engine);
// serialize the engine, then close everything down
parser->destroy();
trtModelStream = engine->serialize();
engine->destroy();
network->destroy();
builder->destroy();
}
ICudaEngine* engineFromFiles(string onnxFile, string trtFile, IRuntime *runtime, int batchSize, Logger &logger, bool useInt8, bool markOutput, IInt8EntropyCalibrator* calibrator)
{
ICudaEngine *engine;
fstream file;
file.open(trtFile, ios::binary | ios::in);
if(!file.is_open())
{
IHostMemory* trtModelStream{nullptr};
onnxToTRTModel(onnxFile, batchSize, trtModelStream, logger, useInt8, markOutput, calibrator);
assert(trtModelStream != nullptr);
engine = runtime->deserializeCudaEngine(trtModelStream->data(), trtModelStream->size(), nullptr);
assert(engine != nullptr);
trtModelStream->destroy();
nvinfer1::IHostMemory* data = engine->serialize();
std::ofstream save_file;
save_file.open(trtFile, std::ios::binary | std::ios::out);
save_file.write((const char*)data->data(), data->size());
save_file.close();
}
else
{
file.seekg(0, ios::end);
int length = file.tellg();
file.seekg(0, ios::beg);
std::unique_ptr<char[]> data(new char[length]);
file.read(data.get(), length);
file.close();
engine = runtime->deserializeCudaEngine(data.get(), length, nullptr);
assert(engine != nullptr);
}
return engine;
}