-
Notifications
You must be signed in to change notification settings - Fork 150
/
top5_main.cc
302 lines (258 loc) · 7.95 KB
/
top5_main.cc
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
/*
* Copyright (c) 2016-2018 DeePhi Tech, Inc.
*
* All Rights Reserved. No part of this source code may be reproduced
* or transmitted in any form or by any means without the prior written
* permission of DeePhi Tech, Inc.
*
* Filename: main.cc
* Version: 1.10
*
* Description:
* Sample source code showing how to deploy ResNet50 neural network on
* DeePhi DPU@Zynq7020 platform.
*/
// modified by daniele.bagni@xilinx.com for minVggNet CNN.
// date 20 April 2018
#include <assert.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <atomic>
#include <sys/stat.h>
#include <unistd.h>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <queue>
#include <mutex>
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <dnndk/dnndk.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
using namespace std::chrono;
int threadnum;
//#define RESNET50_WORKLOAD_CONV (7.71f)
//#define RESNET50_WORKLOAD_FC (4.0f / 1000)
#define KERNEL_CONV "miniVggNet_0"
//#define KERNEL_FC "resnet50_2"
#define CONV_INPUT_NODE "conv1"
#define CONV_OUTPUT_NODE "fc2"
//#define FC_INPUT_NODE "fc1" //"fc1000"
//#define FC_OUTPUT_NODE "fc2" //"fc1000"
const string baseImagePath = "./test_images/";
//#define SHOWTIME
#ifdef SHOWTIME
#define _T(func) \
{ \
auto _start = system_clock::now(); \
func; \
auto _end = system_clock::now(); \
auto duration = (duration_cast<microseconds>(_end - _start)).count(); \
string tmp = #func; \
tmp = tmp.substr(0, tmp.find('(')); \
cout << "[TimeTest]" << left << setw(30) << tmp; \
cout << left << setw(10) << duration << "us" << endl; \
}
#else
#define _T(func) func;
#endif
/**
* @brief put image names to a vector
*
* @param path - path of the image direcotry
* @param images - the vector of image name
*
* @return none
*/
void ListImages(string const &path, vector<string> &images) {
images.clear();
struct dirent *entry;
/*Check if path is a valid directory path. */
struct stat s;
lstat(path.c_str(), &s);
if (!S_ISDIR(s.st_mode)) {
fprintf(stderr, "Error: %s is not a valid directory!\n", path.c_str());
exit(1);
}
DIR *dir = opendir(path.c_str());
if (dir == nullptr) {
fprintf(stderr, "Error: Open %s path failed.\n", path.c_str());
exit(1);
}
while ((entry = readdir(dir)) != nullptr) {
if (entry->d_type == DT_REG || entry->d_type == DT_UNKNOWN) {
string name = entry->d_name;
//cout << "DBG ListImages: " << name << endl;
string ext = name.substr(name.find_last_of(".") + 1);
if ((ext == "JPEG") || (ext == "jpeg") || (ext == "JPG") || (ext == "jpg") ||
(ext == "PNG") || (ext == "png")) {
images.push_back(name);
}
}
}
closedir(dir);
}
/**
* @brief load kinds from file to a vector
*
* @param path - path of the kind file
* @param kinds - the vector of kinds string
*
* @return none
*/
void LoadWords(string const &path, vector<string> &kinds) {
kinds.clear();
fstream fkinds(path);
if (fkinds.fail()) {
fprintf(stderr, "Error : Open %s failed.\n", path.c_str());
exit(1);
}
string kind;
while (getline(fkinds, kind)) {
kinds.push_back(kind);
}
fkinds.close();
}
/**
* @brief softmax operation
*
* @param data - pointer to input buffer
* @param size - size of input buffer
* @param result - calculation result
*
* @return none
*/
void CPUCalcSoftmax(const float *data, size_t size, float *result) {
assert(data && result);
double sum = 0.0f;
for (size_t i = 0; i < size; i++) {
result[i] = exp(data[i]);
sum += result[i];
}
for (size_t i = 0; i < size; i++) {
result[i] /= sum;
}
}
/**H
* @brief Get top k results according to its probability
*
* @param d - pointer to input data
* @param size - size of input data
* @param k - calculation result
* @param vkinds - vector of kinds
*
* @return none
*/
void TopK(const float *d, int size, int k, vector<string> &vkind) {
assert(d && size > 0 && k > 0);
priority_queue<pair<float, int>> q;
for (auto i = 0; i < size; ++i) {
q.push(pair<float, int>(d[i], i));
}
for (auto i = 0; i < k; ++i)
{
pair<float, int> ki = q.top();
printf("[Top]%d prob = %-8f name = %s\n", i, d[ki.second], vkind[ki.second].c_str());
q.pop();
}
}
/**
* @brief Run CONV Task for miniVggNet
*
* @param taskConv - pointer to miniVggNet CONV Task
*
* @return none
*/
// daniele.bagni@xilinx.com
vector<string> kinds, images;
void run_miniVggNet(DPUTask *taskConv, Mat img) {
assert(taskConv );
int channel = dpuGetOutputTensorChannel(taskConv, CONV_OUTPUT_NODE);
float *softmax = new float[channel];
float *FCResult = new float[channel];
_T(dpuSetInputImage2(taskConv, CONV_INPUT_NODE, img));
_T(dpuRunTask(taskConv));
_T(dpuGetOutputTensorInHWCFP32(taskConv, CONV_OUTPUT_NODE, FCResult, channel));
_T(CPUCalcSoftmax(FCResult, channel, softmax));
_T(TopK(softmax, channel, 5, kinds));
delete[] softmax;
delete[] FCResult;
}
//void classifyEntry(DPUKernel *kernelconv, DPUKernel *kernelfc) {
void classifyEntry(DPUKernel *kernelconv)
{
ListImages(baseImagePath, images);
if (images.size() == 0) {
cerr << "\nError: Not images exist in " << baseImagePath << endl;
return;
} else {
cout << "total image : " << images.size() << endl;
}
/* Load all kinds words.*/
LoadWords(baseImagePath + "labels.txt", kinds);
if (kinds.size() == 0) {
cerr << "\nError: Not words exist in words.txt." << endl;
return;
}
thread workers[threadnum];
auto _start = system_clock::now();
for (auto i = 0; i < threadnum; i++)
{
workers[i] = thread([&,i]()
{
#define DPU_MODE_NORMAL 0
#define DPU_MODE_PROF 1
#define DPU_MODE_DUMP 2
// Create DPU Tasks from DPU Kernel
DPUTask *taskconv = dpuCreateTask(kernelconv, DPU_MODE_NORMAL); // profiling not enabled
//DPUTask *taskconv = dpuCreateTask(kernelconv, DPU_MODE_PROF); // profiling enabled
//enable profiling
//int res1 = dpuEnableTaskProfile(taskconv);
//if (res1!=0) printf("ERROR IN ENABLING TASK PROFILING FOR CONV KERNEL\n");
for(unsigned int ind = i ;ind < images.size();ind+=threadnum)
{
Mat img = imread(baseImagePath + images.at(ind));
cout << "DBG imread " << baseImagePath + images.at(ind) << endl;
//Size sz(32,32);
//Mat img2; resize(img, img2, sz); //DB
//run_miniVggNet(taskconv,img2); //DB: images are already 32x32 and do not need any resize
run_miniVggNet(taskconv,img);
}
// Destroy DPU Tasks & free resources
dpuDestroyTask(taskconv);
});
}
// Release thread resources.
for (auto &w : workers) {
if (w.joinable()) w.join();
}
auto _end = system_clock::now();
auto duration = (duration_cast<microseconds>(_end - _start)).count();
cout << "[Time]" << duration << "us" << endl;
cout << "[FPS]" << images.size()*1000000.0/duration << endl;
}
/**
* @brief Entry for running miniVggNet neural network
*
*/
int main(int argc ,char** argv)
{
if(argc == 2)
threadnum = stoi(argv[1]);
DPUKernel *kernelConv;
dpuOpen();
kernelConv = dpuLoadKernel(KERNEL_CONV);
classifyEntry(kernelConv);
dpuDestroyKernel(kernelConv);
dpuClose();
return 0;
}