forked from LiuLeif/hello_tensorrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_onnx.cpp
159 lines (131 loc) · 5.04 KB
/
mnist_onnx.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
#include <cuda_runtime_api.h>
#include <cmath>
#include <fstream>
#include <iostream>
#include <memory>
#include <numeric>
#include <sstream>
#include <vector>
#include "NvOnnxParser.h"
#include "NvInfer.h"
#include "NvInferPlugin.h"
#include "plugin.h"
using namespace nvinfer1;
class Logger : public nvinfer1::ILogger {
public:
void log(Severity severity, const char* msg) noexcept override {
std::cout << msg << std::endl;
}
};
class SampleOnnxMNIST
{
public:
SampleOnnxMNIST() {}
bool build();
bool infer();
private:
nvinfer1::Dims mInputDims; //!< The dimensions of the input to the network.
nvinfer1::Dims mOutputDims; //!< The dimensions of the output to the network.
std::shared_ptr<nvinfer1::ICudaEngine> mEngine; //!< The TensorRT engine used to run the network
//!
//! \brief Parses an ONNX model for MNIST and creates a TensorRT network
//!
bool constructNetwork(std::unique_ptr<nvinfer1::IBuilder>& builder,
std::unique_ptr<nvinfer1::INetworkDefinition>& network,
std::unique_ptr<nvinfer1::IBuilderConfig>& config,
std::unique_ptr<nvonnxparser::IParser>& parser);
};
Logger logger;
bool SampleOnnxMNIST::build() {
auto builder = std::unique_ptr<nvinfer1::IBuilder>(
nvinfer1::createInferBuilder(logger));
const auto explicitBatch = 1U << static_cast<uint32_t>(NetworkDefinitionCreationFlag::kEXPLICIT_BATCH);
auto network = std::unique_ptr<nvinfer1::INetworkDefinition>(
builder->createNetworkV2(explicitBatch));
auto config = std::unique_ptr<nvinfer1::IBuilderConfig>(
builder->createBuilderConfig());
auto parser = std::unique_ptr<nvonnxparser::IParser>(
nvonnxparser::createParser(*network, logger));
constructNetwork(builder, network, config, parser);
std::unique_ptr<IHostMemory> plan{
builder->buildSerializedNetwork(*network, *config)};
std::unique_ptr<IRuntime> runtime{createInferRuntime(logger)};
mEngine = std::shared_ptr<nvinfer1::ICudaEngine>(
runtime->deserializeCudaEngine(plan->data(), plan->size()));
mInputDims = network->getInput(0)->getDimensions();
mOutputDims = network->getOutput(0)->getDimensions();
return true;
}
bool SampleOnnxMNIST::constructNetwork(std::unique_ptr<nvinfer1::IBuilder>& builder,
std::unique_ptr<nvinfer1::INetworkDefinition>& network,
std::unique_ptr<nvinfer1::IBuilderConfig>& config,
std::unique_ptr<nvonnxparser::IParser>& parser) {
parser->parseFromFile("model/mnist_sim.onnx",
static_cast<int>(ILogger::Severity::kWARNING));
config->setMaxWorkspaceSize(1 << 20);
return true;
}
inline void readImage(
const std::string& fileName, uint8_t* buffer, int inH, int inW) {
std::ifstream infile(fileName, std::ifstream::binary);
std::string magic, h, w, max;
infile >> magic >> h >> w >> max;
infile.seekg(1, infile.cur);
infile.read(reinterpret_cast<char*>(buffer), inH * inW);
}
bool SampleOnnxMNIST::infer()
{
auto context = std::unique_ptr<nvinfer1::IExecutionContext>(
mEngine->createExecutionContext());
int inputSize = std::accumulate(
mInputDims.d, mInputDims.d + mInputDims.nbDims, 1,
std::multiplies<int>());
int outputSize = std::accumulate(
mOutputDims.d, mOutputDims.d + mOutputDims.nbDims, 1,
std::multiplies<int>());
void* hostInputBuffer = malloc(inputSize * sizeof(float));
void* hostOutputBuffer = malloc(outputSize * sizeof(float));
void* deviceInputBuffer;
void* deviceOutputBuffer;
cudaMalloc(&deviceInputBuffer, inputSize * sizeof(float));
cudaMalloc(&deviceOutputBuffer, outputSize * sizeof(float));
const int inputH = mInputDims.d[2];
const int inputW = mInputDims.d[3];
std::vector<uint8_t> imageData(inputH * inputW);
readImage("data/0.pgm", imageData.data(), inputH, inputW);
for (int i = 0; i < inputH * inputW; i++) {
((float*)hostInputBuffer)[i] = float(imageData[i]);//1.0 - float(imageData[i]/255.0);
}
cudaMemcpy(
deviceInputBuffer, hostInputBuffer, inputSize * sizeof(float),
cudaMemcpyHostToDevice);
cudaStream_t stream;
cudaStreamCreate(&stream);
void* bindings[2] = {
deviceInputBuffer,
deviceOutputBuffer,
};
context->enqueueV2( bindings, stream, nullptr);
cudaError_t error = cudaMemcpy(
hostOutputBuffer, deviceOutputBuffer, outputSize * sizeof(float),
cudaMemcpyDeviceToHost);
cudaStreamSynchronize(stream);
cudaStreamDestroy(stream);
printf("output:\n");
for (int i = 0; i < std::min<int>(outputSize, 16); i++) {
std::cout << ((float*)hostOutputBuffer)[i] << " ";
}
std::cout << std::endl;
for (int i = outputSize - 1; i >= std::max<int>(0, outputSize - 16); i--) {
std::cout << ((float*)hostOutputBuffer)[i] << " ";
}
std::cout << std::endl;
return true;
}
int main(int argc, char** argv) {
REGISTER_ALL_PLUGINS;
SampleOnnxMNIST sample;
sample.build();
sample.infer();
return 0;
}