forked from HirokiNakahara/GUINNESS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_cpp_r7_main.cpp
125 lines (102 loc) · 3.18 KB
/
template_cpp_r7_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
/*
* C++ Templete for a Binarized CNN
*
* Created on: 2017/07/01
* Author: H. Nakahara
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <bitset>
#include <ap_int.h>
#ifdef __SDSCC__
#include "sds_lib.h"
#else
#define sds_alloc(x)(malloc(x))
#define sds_free(x)(free(x))
#endif
void BinCNN(
#ifdef __SDSCC__
int *t_bin_convW,
int *t_BNFb,
ap_int<64> t_in_img[(IMGSIZ)*(IMGSIZ)],
int fc_result[(OUT_DENSE_SIZ)],
int init
#else
int t_bin_convW[(WEIGHT_SIZ)],
int t_BNFb[(BIAS_SIZ)],
ap_int<64> t_in_img[(IMGSIZ)*(IMGSIZ)],
int fc_result[(OUT_DENSE_SIZ)],
int init
#endif
);
//--------------------------------------------------------------------
// Main Function
//--------------------------------------------------------------------
int main( int argc, char *argv[])
{
ap_int<64> *t_tmp_img;
t_tmp_img = (ap_int<64> *)sds_alloc(((IMGSIZ)*(IMGSIZ))*sizeof(ap_int<64>));
int fc_result[(OUT_DENSE_SIZ)];
int rgb, y, x, i, offset;
// copy input image to f1
for( y = 0; y < (IMGSIZ); y++){
for( x = 0; x < (IMGSIZ); x++){
t_tmp_img[y*(IMGSIZ)+x] = 0;
}
}
// ------------------------------------------------------------------
printf("load weights\n");
int *t_bin_convW;
int *t_BNFb;
t_bin_convW = (int *)sds_alloc(((WEIGHT_SIZ))*sizeof(int));
t_BNFb = (int *)sds_alloc(((BIAS_SIZ))*sizeof(int));
int of, inf, d_value;
FILE *fp;
char line[256];
(READ_BIAS_MEM)
(READ_WEIGHT_MEM)
printf("setup... \n");
BinCNN( t_bin_convW, t_BNFb, t_tmp_img, fc_result, 1);
char image_name[256];
int cnt;
#ifdef __SDSCC__
sscanf( argv[1], "%s", image_name); // 1st argument: test image (text file)
sscanf( argv[2], "%d", &cnt); // 2nd argument: # of inferences
#else
sprintf( image_name, "test_img.txt");
cnt = 1;
#endif
int pixel;
printf("LOAD TESTBENCH %s ... ", image_name);
if( (fp = fopen(image_name, "r")) == NULL)fprintf(stderr,"CANNOT OPEN\n");
for( y = 0; y < (IMGSIZ); y++){
for( x = 0; x < (IMGSIZ); x++){
ap_int<64>tmp = 0;
for( rgb = (NUMIMG) - 1; rgb >= 0 ; rgb--){
if( fgets( line, 256, fp) == NULL)
fprintf(stderr,"EMPTY FILE READ\n");
sscanf( line, "%d", &d_value);
tmp = tmp << 20;
pixel = d_value;
tmp |= ( pixel & 0xFFFFF);
}
t_tmp_img[ y * (IMGSIZ) + x] = tmp;
}
}
printf("OK\n");
fclose(fp);
printf("Inference %d times ... ", cnt);
for( i = 0; i < cnt; i++){
BinCNN( t_bin_convW, t_BNFb, t_tmp_img, fc_result, 0);
}
printf("OK\n");
printf("Result\n");
for( i = 0; i < (OUT_DENSE_SIZ); i++)printf("%5d ", fc_result[i]);
printf("\n");
sds_free( t_tmp_img); sds_free( t_bin_convW); sds_free( t_BNFb);
return 0;
}
// ------------------------------------------------------------------
// END OF PROGRAM
// ------------------------------------------------------------------