-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtensorRTClassifier.cpp
226 lines (195 loc) · 7.81 KB
/
tensorRTClassifier.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
/*
* Copyright 1993-2016 NVIDIA Corporation. All rights reserved.
*
* NOTICE TO USER:
*
* This source code is subject to NVIDIA ownership rights under U.S. and
* international Copyright laws.
*
* NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE
* CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR
* IMPLIED WARRANTY OF ANY KIND. NVIDIA DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
* IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL,
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
* OR PERFORMANCE OF THIS SOURCE CODE.
*
* U.S. Government End Users. This source code is a "commercial item" as
* that term is defined at 48 C.F.R. 2.101 (OCT 1995), consisting of
* "commercial computer software" and "commercial computer software
* documentation" as such terms are used in 48 C.F.R. 12.212 (SEPT 1995)
* and is provided to the U.S. Government only as a commercial end item.
* Consistent with 48 C.F.R.12.212 and 48 C.F.R. 227.7202-1 through
* 227.7202-4 (JUNE 1995), all U.S. Government End Users acquire the
* source code with only those rights set forth herein.
*/
#include "tensorRTClassifier.h"
#include "nvUtils.h"
#include <nvToolsExt.h>
TensorRTClassifier::TensorRTClassifier(const char *deployFile, // caffe prototxt file
const char *modelFile, // trained caffe model
const char *meanFile, // mean file
const std::string& inputs,
const std::vector<std::string >& outputs,
const int maxBatchSize,
const int devID,
nvcaffeparser1::IPluginFactory* pPluginFactory,
std::string table)
: maxBatchSize_(maxBatchSize), devID_(devID)
{
// set input and outputs of the neural network
inputBlobName_ = inputs;
vOutputBlobNames_ = outputs;
pPluginFactory_ = pPluginFactory;
calibrationTable_ = table;
LOG_DEBUG(logger, "TensorRTClassifier: parse caffe model...");
if(!calibrationTable_.empty()) {
LOG_ERROR(logger, "Use INT8 calibration table");
pCalibrator_ = new Int8Calibrator(calibrationTable_);
assert(nullptr != pCalibrator_);
}
cudaSetDevice(devID_);
pCaffeParser_ = createCaffeParser();
caffeToTensorRTModel(deployFile, modelFile, pCaffeParser_);
if (meanFile) {
pMeanBlob_ = pCaffeParser_->parseBinaryProto(meanFile);
}
pContext_ = pEngine_->createExecutionContext();
initInfer();
}
TensorRTClassifier::~TensorRTClassifier() {
cudaSetDevice(devID_);
pContext_->destroy();
pEngine_->destroy();
pCaffeParser_->destroy();
if (nullptr != pCalibrator_) {
delete pCalibrator_;
}
if (pMeanBlob_) {
pMeanBlob_->destroy();
}
ck(cudaFree(apBuffers_[inputIndex_]));
for (int i = 0; i < nOutputs_; ++i) {
ck(cudaFree(apBuffers_[vOutputIndexs_[i]]));
}
}
void TensorRTClassifier::setInputData(float *pBGR,
const int nWidth,
const int nHeight,
const int nBatchSize) {
assert(inputDim_.w() == nWidth);
assert(inputDim_.h() == nHeight);
assert(maxBatchSize_ == nBatchSize);
assert(inputSize_ == 3 * nWidth * nHeight * nBatchSize * sizeof(float));
ck(cudaMemcpy(apBuffers_[inputIndex_], pBGR, inputSize_, cudaMemcpyHostToDevice));
}
void TensorRTClassifier::forward(INFER_OUTPUT_PARAMS *pInferOutputParams) {
cudaSetDevice(devID_);
pContext_->execute(maxBatchSize_, apBuffers_);
pInferOutputParams->vpInferResults_.resize(nOutputs_, nullptr);
pInferOutputParams->vnLens_.resize(nOutputs_, 0);
pInferOutputParams->vOutputDims_.resize(nOutputs_);
pInferOutputParams->nBatchSize_ = maxBatchSize_;
//pInferOutputParams->nInferLen_ = outputDims_.c;
//pInferOutputParams->dpInferResults_ = reinterpret_cast<float* >(apBuffers_[outputIndex_]);
for (int j = 0; j < nOutputs_; ++j) {
pInferOutputParams->vpInferResults_[j] = reinterpret_cast<float* >(apBuffers_[vOutputIndexs_[j]]);
pInferOutputParams->vnLens_[j] = vOutputDims_[j].c() * vOutputDims_[j].h() * vOutputDims_[j].w();
pInferOutputParams->vOutputDims_[j] = vOutputDims_[j];
}
}
int TensorRTClassifier::getInferWidth() const {
return inputDim_.w();
}
int TensorRTClassifier::getInferHeight() const {
return inputDim_.h();
}
std::vector<float > TensorRTClassifier::getMeanValues() const {
return vMeanValues_;
}
void TensorRTClassifier::initInfer() {
//LOG_DEBUG(logger, "TensorRTClassifier: init inference...");
ck(cudaSetDevice(devID_));
// input tensor param
nInputs_ = 1;
inputIndex_ = pEngine_->getBindingIndex(inputBlobName_.c_str());
inputDim_ = static_cast<DimsCHW&&>(pEngine_->getBindingDimensions(inputIndex_));
inputSize_ = maxBatchSize_ * inputDim_.c() * inputDim_.h() *
inputDim_.w() * sizeof(float);
// output tensor param
nOutputs_ = vOutputBlobNames_.size();
vOutputIndexs_.resize(nOutputs_, -1);
vOutputDims_.resize(nOutputs_, {0,0,0});
vOutputSizes_.resize(nOutputs_, -1);
for (int i = 0; i < nOutputs_; ++i) {
vOutputIndexs_[i] = pEngine_->getBindingIndex(vOutputBlobNames_[i].c_str());
vOutputDims_[i] = static_cast<DimsCHW&&>(pEngine_->getBindingDimensions(vOutputIndexs_[i]));
vOutputSizes_[i] = maxBatchSize_ * vOutputDims_[i].c() * vOutputDims_[i].h() *
vOutputDims_[i].w() * sizeof(float);
}
// allocate GPU buffers
ck(cudaMalloc(&apBuffers_[inputIndex_], inputSize_));
ck(cudaMemset(apBuffers_[inputIndex_], 0, inputSize_));
for (int i = 0; i < nOutputs_; ++i) {
ck(cudaMalloc(&apBuffers_[vOutputIndexs_[i]], vOutputSizes_[i]));
ck(cudaMemset(apBuffers_[vOutputIndexs_[i]], 0, vOutputSizes_[i]));
}
//Dims4 dim;
DimsNCHW dim;
if (pMeanBlob_) {
const float *meanData = reinterpret_cast<const float*>(pMeanBlob_->getData());
dim = pMeanBlob_->getDimensions();
float avg[3] = {0.0, 0.0, 0.0};
for (int i = 0; i < dim.c(); ++i) {
for (int j = 0; j < dim.h() * dim.w(); ++j) {
avg[i] += meanData[i*dim.h()*dim.w()+j];
}
avg[i] /= dim.w()*dim.h();
}
vMeanValues_[0] = avg[0];
vMeanValues_[1] = avg[1];
vMeanValues_[2] = avg[2];
}
LOG_DEBUG(logger, " ");
LOG_DEBUG(logger, "=========== Network Parameters Begin ===========");
LOG_DEBUG(logger, "Network Input:");
LOG_DEBUG(logger, " >Channel :" << inputDim_.c());
LOG_DEBUG(logger, " >Height :" << inputDim_.h());
LOG_DEBUG(logger, " >Width :" << inputDim_.w());
for (int i = 0; i < nOutputs_; ++i) {
LOG_DEBUG(logger, "Network Output [" << i << "] :" << vOutputBlobNames_[i]);
LOG_DEBUG(logger, " >Channel :" << vOutputDims_[i].c());
LOG_DEBUG(logger, " >Height :" << vOutputDims_[i].h());
LOG_DEBUG(logger, " >Width :" << vOutputDims_[i].w());
}
LOG_DEBUG(logger, "=========== Network Parameters End ===========");
}
void TensorRTClassifier::caffeToTensorRTModel(const char *deployFile, const char *modelFile, ICaffeParser *parser)
{
IBuilder* builder = createInferBuilder(logger_);
INetworkDefinition* network = builder->createNetwork();
if (nullptr != pPluginFactory_) {
parser->setPluginFactory(pPluginFactory_);
}
const IBlobNameToTensor* blobNameToTensor = parser->parse(deployFile,
modelFile,
*network,
DataType::kFLOAT);
for (auto& s : vOutputBlobNames_)
network->markOutput(*blobNameToTensor->find(s.c_str()));
// Build the engine
builder->setMaxBatchSize(maxBatchSize_);
builder->setMaxWorkspaceSize(16 << 20);
if (nullptr != pCalibrator_) {
builder->setInt8Mode(pCalibrator_ != nullptr);
builder->setInt8Calibrator(pCalibrator_);
}
pEngine_ = builder->buildCudaEngine(*network);
assert(pEngine_);
// we don't need the network any more, and we can destroy the parser
network->destroy();
builder->destroy();
}