forked from wang-xinyu/tensorrtx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlenet.cpp
More file actions
351 lines (307 loc) · 12.2 KB
/
lenet.cpp
File metadata and controls
351 lines (307 loc) · 12.2 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <filesystem>
#include <map>
#include <numeric>
#include <opencv2/core/types.hpp>
#include <opencv2/imgcodecs.hpp>
#include <vector>
#include "logging.h"
#include "utils.h"
using M = nvinfer1::MatrixOperation;
using E = nvinfer1::ElementWiseOperation;
// parameters we know about the lenet-5
#define INPUT_H 32
#define INPUT_W 32
#define INPUT_SIZE (INPUT_H * INPUT_W)
#define OUTPUT_SIZE 10
constexpr static const char* NAMES[2] = {"data", "prob"};
constexpr static const int32_t SIZES[2] = {INPUT_H * INPUT_W, 10};
#define WTS_PATH "../models/lenet.wts"
#define ENGINE_PATH "../models/lenet.engine"
static Logger gLogger;
/**
* @brief Creat the engine using only the API and not any parser.
*
* @param N max batch size
* @param runtime runtime
* @param builder builder
* @param config config
* @param dt data type
* @return ICudaEngine*
*/
ICudaEngine* createLenetEngine(int32_t N, IRuntime* runtime, IBuilder* builder, IBuilderConfig* config, DataType dt) {
#if TRT_VERSION >= 10000
auto* network = builder->createNetworkV2(0);
#else
auto* network = builder->createNetworkV2(1u << static_cast<int>(NetworkDefinitionCreationFlag::kEXPLICIT_BATCH));
#endif
// Create input tensor of shape { 1, 1, 32, 32 } with name INPUT_NAME
ITensor* data = network->addInput(NAMES[0], dt, Dims4{N, 1, INPUT_H, INPUT_W});
assert(data);
// Add convolution layer with 6 outputs and a 5x5 filter.
std::filesystem::path wts_path{WTS_PATH};
wts_path = std::filesystem::absolute(wts_path);
std::map<std::string, Weights> weightMap = loadWeights(wts_path.string());
auto* conv1 = network->addConvolutionNd(*data, 6, DimsHW{5, 5}, weightMap["conv1.weight"], weightMap["conv1.bias"]);
assert(conv1);
conv1->setStrideNd(DimsHW{1, 1});
conv1->setName("conv1");
// Add activation layer using the ReLU algorithm.
IActivationLayer* relu1 = network->addActivation(*conv1->getOutput(0), ActivationType::kRELU);
assert(relu1);
relu1->setName("relu1");
// Add max pooling layer with stride of 2x2 and kernel size of 2x2.
IPoolingLayer* pool1 = network->addPoolingNd(*relu1->getOutput(0), PoolingType::kMAX, DimsHW{2, 2});
assert(pool1);
pool1->setStrideNd(DimsHW{2, 2});
pool1->setName("pool1");
// Add second convolution layer with 16 outputs and a 5x5 filter.
auto* conv2 = network->addConvolutionNd(*pool1->getOutput(0), 16, DimsHW{5, 5}, weightMap["conv2.weight"],
weightMap["conv2.bias"]);
assert(conv2);
conv2->setStrideNd(DimsHW{1, 1});
conv2->setName("conv2");
// Add activation layer using the ReLU algorithm.
IActivationLayer* relu2 = network->addActivation(*conv2->getOutput(0), ActivationType::kRELU);
assert(relu2);
// Add second max pooling layer with stride of 2x2 and kernel size of 2x2>
IPoolingLayer* pool2 = network->addPoolingNd(*relu2->getOutput(0), PoolingType::kMAX, DimsHW{2, 2});
assert(pool2);
pool2->setStrideNd(DimsHW{2, 2});
pool2->setName("pool2");
// Add fully connected layer
auto* flatten = network->addShuffle(*pool2->getOutput(0));
flatten->setReshapeDimensions(Dims2{-1, 400});
auto* tensor_fc1w = network->addConstant(Dims2{120, 400}, weightMap["fc1.weight"])->getOutput(0);
auto* fc1w = network->addMatrixMultiply(*tensor_fc1w, M::kNONE, *flatten->getOutput(0), M::kTRANSPOSE);
assert(tensor_fc1w && fc1w);
auto tensor_fc1b = network->addConstant(Dims2{120, 1}, weightMap["fc1.bias"])->getOutput(0);
auto* fc1b = network->addElementWise(*fc1w->getOutput(0), *tensor_fc1b, E::kSUM);
fc1b->setName("fc1b");
assert(tensor_fc1b && fc1b);
// Add activation layer using the ReLU algorithm.
IActivationLayer* relu3 = network->addActivation(*fc1b->getOutput(0), ActivationType::kRELU);
assert(relu3);
auto* flatten_relu3 = network->addShuffle(*relu3->getOutput(0));
flatten_relu3->setReshapeDimensions(Dims2{-1, 120});
auto* fc2w = network->addConstant(Dims2{84, 120}, weightMap["fc2.weight"])->getOutput(0);
auto* fc2b = network->addConstant(Dims2{84, 1}, weightMap["fc2.bias"])->getOutput(0);
auto* fc3w = network->addConstant(Dims2{10, 84}, weightMap["fc3.weight"])->getOutput(0);
auto* fc3b = network->addConstant(Dims2{10, 1}, weightMap["fc3.bias"])->getOutput(0);
assert(fc2w && fc2b && fc3w && fc3b);
// fully connected layer with relu
auto* fc2_0 = network->addMatrixMultiply(*fc2w, M::kNONE, *flatten_relu3->getOutput(0), M::kTRANSPOSE);
assert(fc2_0);
fc2_0->setName("fc2");
auto* fc2_1 = network->addElementWise(*fc2_0->getOutput(0), *fc2b, E::kSUM);
assert(fc2_1);
IActivationLayer* relu4 = network->addActivation(*fc2_1->getOutput(0), ActivationType::kRELU);
assert(relu4);
auto* shuffle = network->addShuffle(*relu4->getOutput(0));
shuffle->setReshapeDimensions(Dims2{-1, 84});
auto* fc3_0 = network->addMatrixMultiply(*fc3w, M::kNONE, *shuffle->getOutput(0), M::kTRANSPOSE);
assert(fc3_0);
auto* fc3_1 = network->addElementWise(*fc3_0->getOutput(0), *fc3b, E::kSUM);
assert(fc3_1);
// clang-format on
// Add softmax layer to determine the probability.
ISoftMaxLayer* prob = network->addSoftMax(*fc3_1->getOutput(0));
assert(prob);
prob->getOutput(0)->setName(NAMES[1]);
network->markOutput(*prob->getOutput(0));
#if TRT_VERSION >= 8400
config->setMemoryPoolLimit(nvinfer1::MemoryPoolType::kWORKSPACE, WORKSPACE_SIZE);
#else
config->setMaxWorkspaceSize(WORKSPACE_SIZE);
builder->setMaxBatchSize(N);
#endif
// Build engine
#if TRT_VERSION >= 8000
IHostMemory* serialized_mem = builder->buildSerializedNetwork(*network, *config);
ICudaEngine* engine = runtime->deserializeCudaEngine(serialized_mem->data(), serialized_mem->size());
delete network;
#else
ICudaEngine* engine = builder->buildEngineWithConfig(*network, *config);
network->destroy();
#endif
// Release host memory
for (auto& mem : weightMap) {
free((void*)(mem.second.values));
}
return engine;
}
/**
* @brief create a model using the API directly and serialize it to a stream
*
* @param N max batch size
* @param runtime runtime
* @param modelStream
*/
void APIToModel(int32_t N, IRuntime* runtime, IHostMemory** modelStream) {
// Create builder
IBuilder* builder = createInferBuilder(gLogger);
IBuilderConfig* config = builder->createBuilderConfig();
// Create model to populate the network, then set the outputs and create an engine
ICudaEngine* engine = createLenetEngine(N, runtime, builder, config, DataType::kFLOAT);
assert(engine != nullptr);
// Serialize the engine
(*modelStream) = engine->serialize();
#if TRT_VERSION >= 8000
delete engine;
delete config;
delete builder;
#else
engine->destroy();
config->destroy();
builder->destroy();
#endif
}
std::vector<std::vector<float>> doInference(IExecutionContext& context, void* input, int batchSize) {
const auto& engine = context.getEngine();
cudaStream_t stream;
CHECK(cudaStreamCreate(&stream));
std::vector<void*> buffers;
#if TRT_VERSION >= 8000
const int32_t nIO = engine.getNbIOTensors();
#else
const int32_t nIO = engine.getNbBindings();
#endif
buffers.resize(nIO);
for (auto i = 0; i < nIO; ++i) {
std::size_t size = 0;
#if TRT_VERSION >= 8000
auto* tensor_name = engine.getIOTensorName(i);
auto s = getSize(engine.getTensorDataType(tensor_name));
size = s * batchSize * SIZES[i];
CHECK(cudaMalloc(&buffers[i], size));
if (i == 0) {
CHECK(cudaMemcpyAsync(buffers[i], input, size, cudaMemcpyHostToDevice, stream));
}
context.setTensorAddress(tensor_name, buffers[i]);
#else
const int32_t idx = engine.getBindingIndex(NAMES[i]);
auto s = getSize(engine.getBindingDataType(idx));
assert(idx == i);
size = s * batchSize * SIZES[i];
CHECK(cudaMalloc(&buffers[i], size));
if (i == 0) {
CHECK(cudaMemcpyAsync(buffers[i], input, size, cudaMemcpyHostToDevice, stream));
}
#endif
}
#if TRT_VERSION >= 8000
assert(context.enqueueV3(stream));
#else
assert(context.enqueueV2(buffers.data(), stream, nullptr));
#endif
std::vector<std::vector<float>> prob;
for (int i = 1; i < nIO; ++i) {
std::vector<float> tmp(batchSize * SIZES[i], std::nan(""));
std::size_t size = batchSize * SIZES[i] * sizeof(float);
CHECK(cudaMemcpyAsync(tmp.data(), buffers[i], size, cudaMemcpyDeviceToHost, stream));
prob.emplace_back(tmp);
}
CHECK(cudaStreamSynchronize(stream));
for (auto& buffer : buffers) {
CHECK(cudaFree(buffer));
}
CHECK(cudaStreamDestroy(stream));
return prob;
}
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "arguments not right!" << std::endl;
std::cerr << "./lenet -s // serialize model to plan file" << std::endl;
std::cerr << "./lenet -d // deserialize plan file and run inference" << std::endl;
return -1;
}
IRuntime* runtime = createInferRuntime(gLogger);
assert(runtime != nullptr);
char* trtModelStream{nullptr};
size_t size{0};
if (std::string(argv[1]) == "-s") {
IHostMemory* modelStream{nullptr};
APIToModel(1, runtime, &modelStream);
assert(modelStream != nullptr);
std::ofstream p(ENGINE_PATH, std::ios::binary | std::ios::trunc);
if (!p) {
std::cerr << "could not open plan output file" << std::endl;
return -1;
}
p.write(reinterpret_cast<const char*>(modelStream->data()), modelStream->size());
#if TRT_VERSION >= 8000
delete modelStream;
#else
modelStream->destroy();
#endif
std::cout << "serialized weights to lenet5.engine" << std::endl;
return 0;
} else if (std::string(argv[1]) == "-d") {
std::ifstream file(ENGINE_PATH, std::ios::binary);
if (file.good()) {
file.seekg(0, file.end);
size = file.tellg();
file.seekg(0, file.beg);
trtModelStream = new char[size];
assert(trtModelStream);
file.read(trtModelStream, size);
file.close();
}
} else {
return -1;
}
// prepare input/output data
auto img = cv::imread("../assets/6.pgm", cv::IMREAD_GRAYSCALE);
cv::resize(img, img, cv::Size(32, 32), 0, 0, cv::INTER_LINEAR);
assert(img.channels() == 1);
img.convertTo(img, CV_32FC1, 0.00392156f, -0.1307f);
img = img / cv::Scalar(0.3081);
assert(img.total() * img.elemSize() == SIZES[0] * sizeof(float));
#if TRT_VERSION >= 8000
ICudaEngine* engine = runtime->deserializeCudaEngine(trtModelStream, size);
#else
ICudaEngine* engine = runtime->deserializeCudaEngine(trtModelStream, size, nullptr);
#endif
assert(engine != nullptr);
IExecutionContext* context = engine->createExecutionContext();
assert(context != nullptr);
// Run inference
for (int32_t i = 0; i < 100; ++i) {
auto _start = std::chrono::system_clock::now();
auto prob = doInference(*context, img.data, 1);
auto _end = std::chrono::system_clock::now();
auto _time = std::chrono::duration_cast<std::chrono::microseconds>(_end - _start).count();
std::cout << "Execution time: " << _time << "us" << std::endl;
for (auto vector : prob) {
int idx = 0;
for (auto v : vector) {
std::cout << std::setprecision(4) << v << ", " << std::flush;
if (++idx > 9) {
std::cout << "\n====" << std::endl;
break;
}
}
}
if (i == 99) {
std::cout << "prediction result: " << std::endl;
int _top = 0;
for (auto& [idx, logits] : topk(prob[0], 3)) {
std::cout << "Top: " << _top++ << " idx: " << idx << ", logits: " << logits << ", label: " << idx
<< std::endl;
}
}
}
#if TRT_VERSION >= 8000
delete context;
delete engine;
delete runtime;
#else
context->destroy();
engine->destroy();
runtime->destroy();
#endif
return 0;
}