-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmywindow.cpp
492 lines (421 loc) · 13.7 KB
/
mywindow.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include "mywindow.h"
#define ZYNQ_BASE 0x80000000 // base address for 64 sequence of integer
#define CTRL_OFFSET 0x00000100 // for ctrl signal c
#define STATUS_OFFSET 0x00000104 // for status signal
#define ACCELERATOR 1 // use accelerator or not
#if ACCELERATOR == 1
#include <sys/mman.h>
#endif
int max_idx;
/* Class Constructor */
MyWindow::MyWindow(QWidget *parent) : QWidget(parent) {
// 初始化變數
stroke.clear();
lines.clear();
painting = false;
acc_flag = false;
// 主視窗
setWindowTitle("Example");
resize(1200, 480);
move(100, 100);
setStyleSheet("background-color:#424242;");
/* OK 按鈕 */
conf_btn = new QPushButton("OK", this); //按鈕名字
conf_btn->setStyleSheet("QPushButton{border: 2px solid "
"#95A5A6;border-radius:10px;color:rgb(0,0,0);"
"background-color:rgb(195,195,195)}"); //按鈕樣式
conf_btn->setFont(QFont("Courier", 20, QFont::Bold)); //按鈕字體
conf_btn->setGeometry(50, 370, 80, 80); //按鈕位置及大小
connect(
conf_btn, SIGNAL(clicked()), this,
SLOT(
ConfirmButton_clicked())); //點擊按鈕後會呼叫ConfirmButton_clicked函式
/* Clear 按鈕 */
clr_btn = new QPushButton("Clear", this);
clr_btn->setStyleSheet("QPushButton{border: 2px solid "
"#95A5A6;border-radius:10px;color:rgb(0,0,0);"
"background-color:rgb(195,195,195)}");
clr_btn->setFont(QFont("Courier", 20, QFont::Bold));
clr_btn->setGeometry(150, 370, 80, 80);
connect(clr_btn, SIGNAL(clicked()), this, SLOT(ClearButton_clicked()));
/* sw */
sw_btn = new QPushButton("sw", this);
sw_btn->setStyleSheet("QPushButton{border: 2px solid "
"#95A5A6;border-radius:10px;color:rgb(0,0,0);"
"background-color:rgb(195,195,195)}"); //按鈕樣式
sw_btn->setFont(QFont("Courier", 20, QFont::Bold)); //按鈕字體
sw_btn->setFont(QFont("Courier", 20, QFont::Bold));
sw_btn->setGeometry(250, 370, 80, 80);
connect(sw_btn, SIGNAL(clicked()), this, SLOT(SW_clicked()));
}
/* 將繪圖框存成//jpg ppm */
void MyWindow::ConfirmButton_clicked() {
// 選取存圖範圍
QPixmap qpx;
QImage img =
qpx.grabWidget(this, 50, 50, 280, 280)
.scaled(28, 28, Qt::KeepAspectRatio, Qt::SmoothTransformation)
.toImage();
QSize colImgSize = img.size();
int width = colImgSize.rwidth();
int height = colImgSize.rheight();
unsigned char *colImgDataPtr = img.bits();
// 將圖片轉成灰階ppm的格式
QImage grayImg(colImgSize, QImage::Format_Indexed8);
unsigned char *grayImgDataPtr = grayImg.bits();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
*grayImgDataPtr = *(colImgDataPtr + 1);
colImgDataPtr += 4;
grayImgDataPtr++;
}
}
QVector<QRgb> grayColTable;
unsigned int rgb = 0;
for (int i = 0; i < 256; i++) {
grayColTable.append(rgb);
rgb += 0x00010101;
}
grayImg.setColorTable(grayColTable);
grayImg.save(img_name, "ppm");
update();
}
/* 清除畫框 */
void MyWindow::ClearButton_clicked() {
stroke.clear(); // 清除當前軌跡
lines.clear(); // 清除畫過的位置
painting = false;
update();
}
void MyWindow::mousePressEvent(QMouseEvent *event) {
/* 畫筆只能落在此範圍: 325>x>55 && 325>y>55 */
if (event->x() > 55 && event->x() < 325 && event->y() > 55 &&
event->y() < 325) {
painting = true;
stroke.push_back(event->pos()); //存下落筆座標
}
update();
}
void MyWindow::mouseMoveEvent(QMouseEvent *event) {
/* 只有在滑鼠按下後才紀錄鼠標移動過的座標 */
if (event->x() > 55 && event->x() < 325 && event->y() > 55 &&
event->y() < 325 && painting) {
stroke.push_back(event->pos());
update();
}
}
void MyWindow::mouseReleaseEvent(QMouseEvent *) {
/* 按下滑鼠後鬆開會將畫過的軌跡存在畫布上,並清空軌跡 */
if (painting) {
painting = false;
lines.push_back(stroke);
stroke.clear();
}
update();
}
void MyWindow::paintEvent(QPaintEvent *) {
// 設定畫筆樣式
QPainter painter(this);
painter.setFont(QFont("Arial", 10));
painter.setBrush(Qt::white);
painter.setPen(QPen(QColor("#FFFFFF")));
// 建立畫框範圍
painter.drawRect(49, 49, 282, 282);
// 設定畫筆樣式
painter.setPen(QPen(Qt::black, 18));
// 繪出畫過的位置
for (int i = 0; i < lines.size(); i++) {
for (int j = 0; j < lines[i].size(); ++j)
painter.drawPoint(lines[i][j].x(), lines[i][j].y());
}
// 繪出當前落筆後的軌跡
for (int i = 0; i < stroke.size(); i++)
painter.drawPoint(stroke[i].x(), stroke[i].y());
painter.setPen(QPen(Qt::white));
painter.drawRect(424, 49, 282, 282);
painter.drawRect(798, 49, 282, 401);
painter.setBrush(Qt::NoBrush);
painter.setPen(QPen(Qt::black));
painter.drawLine(390, 410, 750, 410);
painter.drawImage(424, 49,
QImage(img_name).scaled(280, 280, Qt::KeepAspectRatio,
Qt::SmoothTransformation));
if (acc_flag) {
char i_str[2];
char str[20];
char res_str[20];
for (int i = 0; i < 10; i++) {
painter.setPen(Qt::NoPen);
if (results[i] > 0)
painter.setBrush(QColor("#3498DB"));
else
painter.setBrush(QColor("#F39C12"));
painter.drawRect(390 + 36 * i, 410, 35, -(results[i] * 1.5));
snprintf(str, sizeof(str), "%.1f", results[i]);
snprintf(i_str, sizeof(i_str), "%d", i);
painter.setPen(QPen(Qt::black));
painter.drawText(390 + 36 * i + 9, 410, 20, -10, Qt::AlignCenter, i_str);
}
painter.drawLine(390, 410, 750, 410);
snprintf(res_str, sizeof(res_str), "%d", max_idx);
painter.setFont(QFont("Arial", 280));
painter.drawText(798, 110, 280, 280, Qt::AlignCenter, res_str);
painter.setBrush(Qt::NoBrush);
}
}
void MyWindow::SW_clicked() {
qDebug() << "--------Start--------";
acc_flag = accuracy();
update();
qDebug() << "--------End--------";
}
#if ACCELERATOR == 1
void FIR_Accelerator(float A[], float &result) {
int i;
volatile int fd =
open("/dev/mem",
O_RDWR); // volatile是一個變數聲明限定詞,可能會在任何時刻被意外的更新
volatile int map_len = 0x400;
volatile unsigned int *io = (volatile unsigned int *)mmap(
NULL, map_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ZYNQ_BASE);
/*
NULL :指向欲映射的核心起始位址,NULL代表讓系統自動選定位址
map_len :代表映射的大小。將文件的多大長度映射到記憶體
參數prot :映射區域的保護方式。可以為以下幾種方式的組合:
PROT_EXEC 映射區域可被執行
PROT_READ 映射區域可被讀取
PROT_WRITE 映射區域可被寫入
PROT_NONE 映射區域不能存取
參數flags :影響映射區域的各種特性。在調用mmap()時必須要指定MAP_SHARED
或MAP_PRIVATE。 MAP_SHARED
允許其他映射該文件的行程共享,對映射區域的寫入數據會複製回文件。 MAP_PRIVATE
不允許其他映射該文件的行程共享,對映射區域的寫入操作會產生一個映射的複製(copy-on-write),對此區域所做的修改不會寫回原文件。
fd :要映射到核心中的文件
ZYNQ_BASE :從文件映射開始處的偏移量
*/
if (io == MAP_FAILED) {
perror("Mapping memory for absolute memory access failed.\n");
exit(1);
}
// printf("Zynq_BASE mapping successful :\n0x%x to 0x%x, size = %d\n", ZYNQ_BASE,
// (int)io, map_len);
// assign input values to accelerator
*(io + (CTRL_OFFSET >> 2)) = 0x2;
*(io + (CTRL_OFFSET >> 2)) = 0x0;
for (i = 0; i < 3; i++) {
*(io + i) = A[i];
// printf("accelerator input = %d\n", *(io + i));
}
// printf("start run\n");
*(io + (CTRL_OFFSET >> 2)) = 0x1;
*(io + (CTRL_OFFSET >> 2)) = 0x0;
// printf("polling busy\n");
// while (*(io + (STATUS_OFFSET >> 2)) != (volatile unsigned int)0) {
// printf("0x%x\n", *(io + (STATUS_OFFSET >> 2)));
// }
// printf("operation finish\n");
/*for (i = 1; i < 3; i++) {
result[i - 1] = *(io + i);
// printf("accelerator result = %d\n", *(io + i));
}*/
result = *(io + 1);
munmap((void *)io,
map_len); //釋放指標io指向的記憶體空間,並設釋放的記憶體大小
close(fd);
}
#endif
/* calculate accuracy */
bool MyWindow::accuracy() {
float w_h1[NODE_LAYER_1][NODE_LAYER_0];
float w_o[NODE_LAYER_2][NODE_LAYER_1];
float b_h1[NODE_LAYER_1];
float b_o[NODE_LAYER_2];
float v_h1[NODE_LAYER_1];
int input[NODE_LAYER_0];
float inputf[NODE_LAYER_0];
FILE *fileprint;
char filename[64];
int i, j;
float max;
// training data //
strcpy(filename, "merge.txt");
fileprint = fopen(filename, "r");
if (fileprint == NULL) {
puts("ERROR: read model.");
exit(-1);
}
for (i = 0; i < NODE_LAYER_1; i++)
for (j = 0; j < NODE_LAYER_0; j++)
fscanf(fileprint, "%x", (unsigned int *)&w_h1[i][j]);
for (i = 0; i < NODE_LAYER_1; i++)
fscanf(fileprint, "%x", (unsigned int *)&b_h1[i]);
for (i = 0; i < NODE_LAYER_2; i++)
for (j = 0; j < NODE_LAYER_1; j++)
fscanf(fileprint, "%x", (unsigned int *)&w_o[i][j]);
for (i = 0; i < NODE_LAYER_2; i++)
fscanf(fileprint, "%x", (unsigned int *)&b_o[i]);
fclose(fileprint);
// input data
unsigned char buffer[BUFFER_SIZE];
fileprint = fopen(img_name, "r");
// fileprint = fopen("00001.ppm", "r");
if (fileprint == NULL) {
puts("err input");
return 0;
}
fseek(fileprint, 14, SEEK_SET);
fread(buffer, 1, BUFFER_SIZE, fileprint);
for (i = 0; i < NODE_LAYER_0; i++) {
input[i] = buffer[i * 3];
}
printf("\n");
fclose(fileprint);
for (i = 0; i < NODE_LAYER_0; i++) {
input[i] = 255 - input[i];
inputf[i] = ((float)input[i]) * (1.0 / 255.0);
}
/*for (i = 0; i < NODE_LAYER_1; i++) {
v_h1[i] = 0.0;
for (j = 0; j < NODE_LAYER_0; j++) {
v_h1[i] += inputf[j] * w_h1[i][j];
}
}*/
float A[3];
float tmp , mat , turnback;
int sign , exp;
for (i = 0; i < NODE_LAYER_1; i++) {
v_h1[i] = 0.0;
for (j = 0; j < NODE_LAYER_0; j++) {
sign = exp = mat = 0;
Get754Value32(inputf[j] , &sign , &exp , &mat);
A[0] = sign | exp | mat;
sign = exp = mat = 0;
Get754Value32( w_h1[i][j] , &sign , &exp , &mat);
A[1] = sign | exp | mat;
A[2] = 1;
FIR_Accelerator(A, tmp);
turnback = 0.0f;
Set754Value32(&turnback, sign, exp, f_mat);
tmp = turnback;
sign = exp = mat = 0;
Get754Value32( v_h1[i] , &sign , &exp , &mat);
A[0] = sign | exp | mat;
sign = exp = mat = 0;
Get754Value32( tmp , &sign , &exp , &mat);
A[1] = sign | exp | mat;
A[2] = 2;
FIR_Accelerator(A, v_h1[i]);
turnback = 0.0f;
Set754Value32(&turnback, sign, exp, f_mat);
v_h1[i] = turnback;
}
}
for (i = 0; i < NODE_LAYER_1; i++) {
v_h1[i] += b_h1[i];
if (v_h1[i] < 0.0)
v_h1[i] = 0.0;
}
/* Calculate output layer. */
f/*or (i = 0; i < NODE_LAYER_2; i++) {
results[i] = 0.0;
for (j = 0; j < NODE_LAYER_1; j++)
results[i] += v_h1[j] * w_o[i][j];
}*/
for (i = 0; i < NODE_LAYER_2; i++) {
results[i] = 0.0;
for (j = 0; j < NODE_LAYER_1; j++) {
sign = exp = mat = 0;
Get754Value32(v_h1[j] , &sign , &exp , &mat);
A[0] = sign | exp | mat;
sign = exp = mat = 0;
Get754Value32(w_o[i][j] , &sign , &exp , &mat);
A[1] = sign | exp | mat;
A[2] = 1;
FIR_Accelerator(A, tmp);
turnback = 0.0f;
Set754Value32(&turnback, sign, exp, f_mat);
tmp = turnback;
sign = exp = mat = 0;
Get754Value32(result[i] , &sign , &exp , &mat);
A[0] = sign | exp | mat;
sign = exp = mat = 0;
Get754Value32(tmp , &sign , &exp , &mat);
A[1] = sign | exp | mat;
A[2] = 2;
FIR_Accelerator(A, results[i]);
turnback = 0.0f;
Set754Value32(&turnback, sign, exp, f_mat);
results[i] = turnback;
}
}
for (i = 0; i < NODE_LAYER_2; i++)
results[i] += b_o[i];
/*for (i = 0; i < 10; i++)
printf("results[%d]: %f\n", i, results[i]);
printf("\n");*/
// Find max value. //
max = results[0];
max_idx = 0;
for (i = 1; i < NODE_LAYER_2; i++)
if (max < results[i]) {
max_idx = i;
max = results[i];
}
printf("Final ans:%d\n", max_idx);
return true;
// painter.drawPixmap(220,100,QPixmap(".ppm"));
}
// float -> IEEE754
void MyWindow::Get754Value32(float v, int *sign, int *exp, float *mat)
{
float pwr = 0.5f;
uint value = *(uint *)&v;
int stop = 0;
*sign = (int)((value & FLT_SIGN_MASK) >> (FLT_MAT_BITS + FLT_EXP_BITS));
*sign <<= 15;
*exp = (int)((value & FLT_EXP_MASK) >> FLT_MAT_BITS) - FLT_BASE_VAL;
*exp <<= 14;
// calculate mantissa field
*mat = 1.0f, value <<= (FLT_SIGN_BITS + FLT_EXP_BITS);
while (value && stop < 10)
{
if (value & FLT_SIGN_MASK)
{
*mat += pwr;
}
else
pwr *= 0.5f;
value <<= 1;
stop++;
}
}
// IEEE754 -> float
void MyWindows::Set754Value32(float *f, int sign, int exp, float mat)
{
uint *pv = (uint *)f;
uint m = *(uint *)&mat;
uint mask = (1U << (23 - 1));
// error defect
exp += FLT_BASE_VAL;
if (exp < 0 || exp >= (1U << FLT_EXP_BITS))
return 0;
if (mat >= 2.0f || mat < 1.0f)
return 0;
*pv = 0U;
// set sign and exp
if (sign)
*pv |= FLT_SIGN_MASK;
*pv |= (exp << FLT_MAT_BITS);
// cal and set mantissa
mat -= 1.0f;
while (mask && mat != 0.0f)
{
if (mask & m)
*pv |= mask;
mat *= 2.0f;
if (mat > 1.0f)
mat -= 1.0f;
mask >>= 1;
}
return;
}