Repository navigation
Expand file tree
/
Copy pathgie.cpp
More file actions
160 lines (131 loc) · 5.13 KB
/
Copy pathgie.cpp
File metadata and controls
160 lines (131 loc) · 5.13 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
#include <assert.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cmath>
#include <sys/stat.h>
#include <cmath>
#include <time.h>
#include <cuda_runtime_api.h>
#include <TH/TH.h>
#include "NvInfer.h"
#include "NvCaffeParser.h"
using namespace nvinfer1;
using namespace nvcaffeparser1;
#define CHECK(status) \
{ \
if (status != 0) \
{ \
std::cout << "Cuda failure: " << status; \
abort(); \
} \
}
extern "C"
{
void init(void* handle[1],
const char* deployFile,
const char* modelFile,
unsigned int maxBatchSize);
void caffeToGIEModel_(const char* deployFile,
const char* modelFile,
unsigned int maxBatchSize,
std::ostream& gieModelStream);
void doInference(void* handle[1], THFloatTensor* input, THFloatTensor* output_mask, THFloatTensor* output_box, int batchSize);
}
// Logger for GIE info/warning/errors
class Logger : public ILogger
{
void log(Severity severity, const char* msg) override
{
// suppress info-level messages
if (severity != Severity::kINFO)
std::cout << msg << std::endl;
}
} gLogger;
void init(void* handle[1],
const char* deployFile,
const char* modelFile,
unsigned int maxBatchSize)
{
// create a GIE model from the caffe model and serialize it to a stream
std::stringstream gieModelStream;
caffeToGIEModel_(deployFile, modelFile, maxBatchSize, gieModelStream);
// deserialize the engine
gieModelStream.seekg(0, gieModelStream.beg);
IRuntime* runtime = createInferRuntime(gLogger);
ICudaEngine* engine = runtime->deserializeCudaEngine(gieModelStream);
IExecutionContext *context = engine->createExecutionContext();
handle[1] = context;
}
void caffeToGIEModel_(const char* deployFile,
const char* modelFile,
unsigned int maxBatchSize,
std::ostream& gieModelStream)
{
// create the builder
IBuilder* builder = createInferBuilder(gLogger);
// parse the caffe model to populate the network, then set the outputs
INetworkDefinition* network = builder->createNetwork();
ICaffeParser* parser = createCaffeParser();
const IBlobNameToTensor* blobNameToTensor = parser->parse(deployFile,
modelFile,
*network,
DataType::kFLOAT);
// specify which tensors are outputs
network->markOutput(*blobNameToTensor->find("coverage"));
network->markOutput(*blobNameToTensor->find("bboxes"));
// Build the engine
builder->setMaxBatchSize(maxBatchSize);
builder->setMaxWorkspaceSize(16 << 20);
ICudaEngine* engine = builder->buildCudaEngine(*network);
assert(engine);
// we don't need the network any more, and we can destroy the parser
network->destroy();
parser->destroy();
// serialize the engine, then close everything down
engine->serialize(gieModelStream);
engine->destroy();
builder->destroy();
shutdownProtobufLibrary();
}
void doInference(void* handle[1], THFloatTensor* input, THFloatTensor* output_mask, THFloatTensor* output_box, int batchSize)
{
int im_h = THFloatTensor_size(input, 1);
int im_w = THFloatTensor_size(input, 2);
int mask_h = THFloatTensor_size(output_mask, 1);
int mask_w = THFloatTensor_size(output_mask, 2);
IExecutionContext* context = (IExecutionContext *)(handle[1]);
const ICudaEngine& engine = context->getEngine();
// input and output buffer pointers that we pass to the engine - the engine requires exactly IEngine::getNbBindings(),
// of these, but in this case we know that there is exactly one input and one output.
//assert(engine.getNbBindings() == 3);
void* buffers[3];
// In order to bind the buffers, we need to know the names of the input and output tensors.
// note that indices are guaranteed to be less than IEngine::getNbBindings()
int inputIndex = engine.getBindingIndex("data"),
outputMaskIndex = engine.getBindingIndex("coverage"),
outputBoxIndex = engine.getBindingIndex("bboxes");
int inputSize = 1 * 4 * im_h * im_w * sizeof(float);
int outMaskSize = 1 * mask_h * mask_w * sizeof(float);
int outBoxSize = 1 * 4 * mask_h * mask_w * sizeof(float);
// create GPU buffers and a stream
CHECK(cudaMalloc(&buffers[inputIndex], inputSize));
CHECK(cudaMalloc(&buffers[outputMaskIndex], outMaskSize));
CHECK(cudaMalloc(&buffers[outputBoxIndex], outBoxSize));
float *mInputCPU = THFloatTensor_data(input);
float *mOutMaskCPU = THFloatTensor_data(output_mask);
float *mOutBoxCPU = THFloatTensor_data(output_box);
cudaStream_t stream;
CHECK(cudaStreamCreate(&stream));
// DMA the input to the GPU, execute the batch asynchronously, and DMA it back:
CHECK(cudaMemcpyAsync(buffers[inputIndex], mInputCPU, inputSize, cudaMemcpyHostToDevice, stream));
context->enqueue(batchSize, buffers, stream, nullptr);
CHECK(cudaMemcpyAsync(mOutMaskCPU, buffers[outputMaskIndex], outMaskSize, cudaMemcpyDeviceToHost, stream));
CHECK(cudaMemcpyAsync(mOutBoxCPU, buffers[outputBoxIndex], outBoxSize, cudaMemcpyDeviceToHost, stream));
cudaStreamSynchronize(stream);
// release the stream and the buffers
cudaStreamDestroy(stream);
CHECK(cudaFree(buffers[inputIndex]));
CHECK(cudaFree(buffers[outputMaskIndex]));
CHECK(cudaFree(buffers[outputBoxIndex]));
}