-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgiexec.cpp
215 lines (189 loc) · 6.95 KB
/
giexec.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
/*
Dan 2018.6.27
*/
#include <assert.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <sys/stat.h>
#include <time.h>
#include <cuda_runtime_api.h>
#include "NvInfer.h"
#include "NvCaffeParser.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
using namespace nvinfer1;
using namespace nvcaffeparser1;
#define CHECK(status) \
{ \
if (status != 0) \
{ \
std::cout << "Cuda failure: " << status; \
abort(); \
} \
}
// stuff we know about the network and the caffe input/output blobs
static const int INPUT_D = 3; //input 3 channels(BGR)
static const int INPUT_H = 227; //input image height
static const int INPUT_W = 227; //input image width
static const int OUTPUT_SIZE = 3; //output size(3 types)
const char* INPUT_BLOB_NAME = "data"; //input name(defined in prototxt)
const char* OUTPUT_BLOB_NAME = "prob"; //output name(defined in prototxt)
// 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;
// JPG reader using stb_image
void readJPG(const std::string& fileName, unsigned char*& buffer)
{
int w,h,bpp;
buffer = stbi_load(fileName.c_str(), &w, &h, &bpp, 3);
}
void caffeToGIEModel(const std::string& deployFile, // name for caffe prototxt
const std::string& modelFile, // name for model
const std::vector<std::string>& outputs, // network outputs
unsigned int maxBatchSize, // batch size - NB must be at least as large as the batch we want to run with)
IHostMemory *&gieModelStream) // output buffer for the GIE model
{
// 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.c_str(),
modelFile.c_str(),
*network,
DataType::kFLOAT);
// specify which tensors are outputs
for (auto& s : outputs)
network->markOutput(*blobNameToTensor->find(s.c_str()));
// Build the engine
builder->setMaxBatchSize(maxBatchSize);
builder->setMaxWorkspaceSize(1 << 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
gieModelStream = engine->serialize();
engine->destroy();
builder->destroy();
shutdownProtobufLibrary();
}
void doInference(IExecutionContext& context, float* input, float* output, int batchSize)
{
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() == 2);
void* buffers[2];
// 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(INPUT_BLOB_NAME),
outputIndex = engine.getBindingIndex(OUTPUT_BLOB_NAME);
// create GPU buffers and a stream
CHECK(cudaMalloc(&buffers[inputIndex], batchSize * INPUT_D * INPUT_H * INPUT_W * sizeof(float)));
CHECK(cudaMalloc(&buffers[outputIndex], batchSize * OUTPUT_SIZE * sizeof(float)));
cudaStream_t stream;
CHECK(cudaStreamCreate(&stream));
cudaEvent_t start, end; //calculate run time
CHECK(cudaEventCreate(&start));
CHECK(cudaEventCreate(&end));
// DMA the input to the GPU, execute the batch asynchronously, and DMA it back:
float ms;
CHECK(cudaMemcpyAsync(buffers[inputIndex], input, batchSize * INPUT_D * INPUT_H * INPUT_W * sizeof(float), cudaMemcpyHostToDevice, stream));
cudaEventRecord(start, stream);
context.enqueue(batchSize, buffers, stream, nullptr);
CHECK(cudaMemcpyAsync(output, buffers[outputIndex], batchSize * OUTPUT_SIZE * sizeof(float), cudaMemcpyDeviceToHost, stream));
cudaEventRecord(end, stream);
cudaEventSynchronize(end);
cudaEventElapsedTime(&ms, start, end);
cudaStreamSynchronize(stream);
cudaEventDestroy(start);
cudaEventDestroy(end);
std::cout<<"execution time:"<<ms<<"ms."<<std::endl;
// release the stream and the buffers
cudaStreamDestroy(stream);
CHECK(cudaFree(buffers[inputIndex]));
CHECK(cudaFree(buffers[outputIndex]));
}
int main(int argc, char** argv)
{
// create a GIE model from the caffe model and serialize it to a stream
IHostMemory *gieModelStream{nullptr};
std::cout<<"Converting from caffe model..."<<std::endl;
caffeToGIEModel("/home/nvidia/caffemodel/deploy.prototxt", "/home/nvidia/caffemodel/mycaffenet_train_iter_450000.caffemodel", std::vector < std::string > { OUTPUT_BLOB_NAME }, 1, gieModelStream);
std::cout<<"Convertion successful!"<<std::endl;
unsigned char* fileData;
std::cout<<"Reading jpg image..."<<std::endl;
std::string fname;
std::ifstream in("file.txt");
in>>fname;
readJPG(fname, fileData);
std::cout<<"Reading successful!"<<std::endl;
std::cout<<"Converting data..."<<std::endl;
unsigned char* newData = (unsigned char*)malloc(INPUT_D*INPUT_H*INPUT_W*sizeof(unsigned char));
//reformatting
int start = 2;
for(int i=0;i<227*227;i++)
{
newData[i] = fileData[start]-97.25115522; //magic, don't modify!(mean image)
start += 3;
}
start = 1;
for(int i=227*227;i<227*227*2;i++)
{
newData[i] = fileData[start]-108.84620235; //magic, don't modify!(mean image)
start += 3;
}
start = 0;
for(int i=227*227*2;i<227*227*3;i++)
{
newData[i] = fileData[start]-116.72652473; //magic, don't modify!(mean image)
start += 3;
}
float data[INPUT_D*INPUT_H*INPUT_W];
for (int i = 0; i < INPUT_D*INPUT_H*INPUT_W; i++)
data[i] = float(newData[i]);
std::cout<<"Converted."<<std::endl;
std::cout<<"Deserializing the engine..."<<std::endl;
// deserialize the engine
IRuntime* runtime = createInferRuntime(gLogger);
ICudaEngine* engine = runtime->deserializeCudaEngine(gieModelStream->data(), gieModelStream->size(), nullptr);
if (gieModelStream) gieModelStream->destroy();
IExecutionContext *context = engine->createExecutionContext();
std::cout<<"Deserialized."<<std::endl;
// run inference
float prob[OUTPUT_SIZE];
std::cout<<"Running inference..."<<std::endl;
doInference(*context, data, prob, 1);
std::cout<<"Inference successful!"<<std::endl;
// destroy the engine
context->destroy();
engine->destroy();
runtime->destroy();
// print a histogram of the output distribution
float maxP = 0.0;
int pos = 0;
for (unsigned int i = 0; i < 3; i++)
{
std::cout<<prob[i]<<std::endl;
if(prob[i]>maxP)
{
maxP = prob[i];
pos = i;
}
}
std::ofstream out("type.txt");
out<<pos<<std::endl;
return 0;
}