-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
260 lines (217 loc) · 6.3 KB
/
main.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
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
#include <iostream>
#include <inference_engine.hpp>
#include <opencv2/opencv.hpp>
#include <ie_iextension.h>
#include <ext_list.hpp>
using namespace InferenceEngine;
template <typename T>
void matU8ToBlob(const cv::Mat& orig_image, InferenceEngine::Blob::Ptr& blob, int batchIndex = 0)
{
InferenceEngine::SizeVector blobSize = blob->getTensorDesc().getDims();
const size_t width = blobSize[3];
const size_t height = blobSize[2];
const size_t channels = blobSize[1];
T* blob_data = blob->buffer().as<T*>();
cv::Mat resized_image(orig_image);
if (width != orig_image.size().width || height != orig_image.size().height) {
cv::resize(orig_image, resized_image, cv::Size(width, height));
}
int batchOffset = batchIndex * width * height * channels;
for (size_t c = 0; c < channels; c++) {
for (size_t h = 0; h < height; h++) {
for (size_t w = 0; w < width; w++) {
blob_data[batchOffset + c * width * height + h * width + w] = resized_image.at<cv::Vec3b>(h, w)[c];
}
}
}
}
bool LoadPlugin(const std::string& device, InferencePlugin& plugin)
{
bool ret = true;
try
{
// --------------------------- 1. Load Plugin for inference engine -------------------------------------
plugin = PluginDispatcher().getPluginByDevice(device);
if (device == "CPU")
{
plugin.AddExtension(std::make_shared<Extensions::Cpu::CpuExtensions>());
}
}
catch (const std::exception & ex)
{
OutputDebugStringA(ex.what());
ret = false;
}
return ret;
}
bool ReadModel(const std::string &modelPath, CNNNetwork& network)
{
bool ret = true;
CNNNetReader network_reader;
try
{
// --------------------------- 2. Read IR Generated by ModelOptimizer (.xml and .bin files) ------------
network_reader.ReadNetwork(modelPath);
network_reader.ReadWeights(modelPath.substr(0, modelPath.size() - 4) + ".bin");
network_reader.getNetwork().setBatchSize(1);
network = network_reader.getNetwork();
}
catch (const std::exception & ex)
{
OutputDebugStringA(ex.what());
ret = false;
}
return ret;
}
bool ConfigureInput(CNNNetwork& network, InputsDataMap& input_info, std::string& input_name, const Precision precision, const Layout layout)
{
bool ret = true;
try
{
// --------------------------- Prepare input blobs -----------------------------------------------------
input_info = InputsDataMap(network.getInputsInfo());
for (auto&& input : input_info)
{
input_name = input.first;
input.second->setPrecision(precision);
input.second->setLayout(layout);
}
}
catch (const std::exception & ex)
{
OutputDebugStringA(ex.what());
ret = false;
}
return ret;
}
bool ConfigureOutput(CNNNetwork& network, OutputsDataMap& output_info, std::string& output_name, const Precision precision, const Layout layout)
{
bool ret = true;
try
{
// ------------------------------ Prepare output blobs -------------------------------------------------
output_info = OutputsDataMap(network.getOutputsInfo());
for (auto&& output : output_info)
{
output_name = output.first;
output.second->setPrecision(precision);
output.second->setLayout(layout);
}
}
catch (const std::exception & ex)
{
OutputDebugStringA(ex.what());
ret = false;
}
return ret;
}
bool LoadModel(CNNNetwork& network, InferencePlugin& plugin, ExecutableNetwork& executable_network)
{
bool ret = true;
try
{
// --------------------------- 4. Loading model to the plugin ------------------------------------------
executable_network = plugin.LoadNetwork(network, {});
}
catch (const std::exception & ex)
{
OutputDebugStringA(ex.what());
ret = false;
}
return ret;
}
bool CreateInferRequest(ExecutableNetwork& executable_network, InferRequest::Ptr& async_infer_request)
{
bool ret = true;
try
{
// --------------------------- 5. Create infer request -------------------------------------------------
async_infer_request = executable_network.CreateInferRequestPtr();
}
catch (const std::exception & ex)
{
OutputDebugStringA(ex.what());
ret = false;
}
return ret;
}
bool PrepareInput(InferRequest::Ptr& async_infer_request, const std::string & input_name, const cv::Mat & image)
{
bool ret = true;
try
{
Blob::Ptr input = async_infer_request->GetBlob(input_name);
matU8ToBlob<uint8_t>(image, input);
}
catch (const std::exception & ex)
{
OutputDebugStringA(ex.what());
ret = false;
}
return ret;
}
bool Infer(InferRequest::Ptr& async_infer_request)
{
bool ret = true;
try
{
// --------------------------- 7. Do inference ---------------------------------------------------------
async_infer_request->StartAsync();
async_infer_request->Wait(IInferRequest::WaitMode::RESULT_READY);
}
catch (const std::exception & ex)
{
OutputDebugStringA(ex.what());
ret = false;
}
return ret;
}
int ProcessOutput(InferRequest::Ptr& async_infer_request, const std::string& output_name)
{
int result = 0;
float buf= 0;
try
{
const float* oneHotVector = async_infer_request->GetBlob(output_name)->buffer().as<float*>();
for (int i = 0; i < 10; i++)
{
if (oneHotVector[i] > buf)
{
buf = oneHotVector[i];
result = i;
}
}
}
catch (const std::exception & ex)
{
OutputDebugStringA(ex.what());
result = -1;
}
return result;
}
int main(int argc, char** argv)
{
InferencePlugin plugin;
CNNNetwork network;
InputsDataMap input_info;
OutputsDataMap output_info;
ExecutableNetwork executable_network;
InferRequest::Ptr async_infer_request;
std::string input_name;
std::string output_name;
std::string device(argv[1]);
std::string modelPath(argv[2]);
std::string imagePath(argv[3]);
int result = 0;
cv::Mat img = cv::imread(imagePath, 1);
LoadPlugin(device, plugin);
ReadModel(modelPath, network);
ConfigureInput(network, input_info, input_name, Precision::U8, Layout::NCHW);
ConfigureOutput(network, output_info, output_name, Precision::FP32, Layout::NC);
LoadModel(network, plugin, executable_network);
CreateInferRequest(executable_network, async_infer_request);
PrepareInput(async_infer_request, input_name, img);
Infer(async_infer_request);
result = ProcessOutput(async_infer_request, output_name);
printf("result = %d", result);
}