-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
173 lines (153 loc) · 5.18 KB
/
main.c
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
#include "pmsis.h"
#include "bsp/fs.h"
#include "bsp/fs/hostfs.h"
#include "gaplib/ImgIO.h"
#include "Gap.h"
#include "setup.h"
#include "MCUNetKernels.h"
#include "measurments_utils.h"
#define __XSTR(__s) __STR(__s)
#define __STR(__s) #__s
#ifdef MODEL_NE16
unsigned char network_output[NETWORK_CLASSES];
#else
signed char network_output[NETWORK_CLASSES];
#endif
int detector_status;
static void nn_inference()
{
#ifdef PERF
printf("Start timer\n");
gap_cl_starttimer();
gap_cl_resethwtimer();
#endif
GPIO_HIGH();
detector_status = MCUNetCNN(network_output);
GPIO_LOW();
return;
}
int main(int argc, char* argv[])
{
OPEN_GPIO_MEAS();
GPIO_LOW();
/* Configure And open cluster. */
struct pi_device cluster_dev;
struct pi_cluster_conf cl_conf;
pi_cluster_conf_init(&cl_conf);
cl_conf.id = 0;
cl_conf.cc_stack_size = STACK_SIZE;
pi_open_from_conf(&cluster_dev, (void *) &cl_conf);
pi_cluster_open(&cluster_dev);
pi_freq_set(PI_FREQ_DOMAIN_FC, FREQ_FC*1000*1000);
pi_freq_set(PI_FREQ_DOMAIN_CL, FREQ_CL*1000*1000);
pi_freq_set(PI_FREQ_DOMAIN_PERIPH, FREQ_PE*1000*1000);
printf("Set FC Frequency = %d MHz, CL Frequency = %d MHz, PERIIPH Frequency = %d MHz\n",
pi_freq_get(PI_FREQ_DOMAIN_FC), pi_freq_get(PI_FREQ_DOMAIN_CL), pi_freq_get(PI_FREQ_DOMAIN_PERIPH));
#ifdef VOLTAGE
pi_pmu_voltage_set(PI_PMU_VOLTAGE_DOMAIN_CHIP, VOLTAGE);
pi_pmu_voltage_set(PI_PMU_VOLTAGE_DOMAIN_CHIP, VOLTAGE);
printf("Voltage: %dmV\n", VOLTAGE);
#endif
int status = MCUNetCNN_Construct();
PRINTF("Detector initialized with status %d\n", status);
if (status)
{
pmsis_exit(-4);
}
PRINTF("Network init done\n");
int input_size = IMAGE_WIDTH*IMAGE_HEIGHT*sizeof(char);
//char* dir_name = "quant_data";
char* filename = __XSTR(AT_IMAGE);
if (ReadImageFromFile(filename,
IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS,
Input_1, INPUT_SIZE*sizeof(char), IMGIO_OUTPUT_CHAR, 0))
{
PRINTF("Image read failed\n");
pmsis_exit(-2);
}
#ifndef MODEL_NE16 // HWC does not have the image formatter in front
for (int i=0; i<INPUT_SIZE; i++) Input_1[i] -= 128;
#endif
printf("Finished reading image\n");
struct pi_cluster_task cl_task;
PRINTF("Stack size is %d and %d\n",STACK_SIZE,SLAVE_STACK_SIZE );
pi_cluster_task(&cl_task, nn_inference, NULL);
pi_cluster_task_stacks(&cl_task, NULL, SLAVE_STACK_SIZE);
#if defined(__GAP8__)
cl_task.stack_size = STACK_SIZE;
cl_task.slave_stack_size = SLAVE_STACK_SIZE;
#endif
// Execute the function "nn_inference" on the cluster.
pi_cluster_send_task_to_cl(&cluster_dev, &cl_task);
MCUNetCNN_Destruct();
short int outclass = 0;
#ifdef MODEL_NE16
unsigned char max_score = 0;
#else
signed char max_score = -128;
#endif
for(int i=0; i < NETWORK_CLASSES; i++){
//PRINTF("%d\n", network_output[i]);
if (network_output[i] > max_score){
outclass = i;
max_score = network_output[i];
}
}
PRINTF("Predicted class:\t%d\n", outclass + 1);
PRINTF("With confidence:\t%d\n", max_score);
#ifdef PERF
unsigned int TotalCycles = 0, TotalOper = 0;
{
printf("\n");
for (unsigned int i=0; i<(sizeof(AT_GraphPerf)/sizeof(unsigned int)); i++) {
TotalCycles += AT_GraphPerf[i]; TotalOper += AT_GraphOperInfosNames[i];
}
for (unsigned int i=0; i<(sizeof(AT_GraphPerf)/sizeof(unsigned int)); i++) {
printf("%45s: Cycles: %10u, Cyc%%: %5.1f%%, Operations: %10u, Op%%: %5.1f%%, Operations/Cycle: %f\n", AT_GraphNodeNames[i], AT_GraphPerf[i], 100*((float) (AT_GraphPerf[i]) / TotalCycles), AT_GraphOperInfosNames[i], 100*((float) (AT_GraphOperInfosNames[i]) / TotalOper), ((float) AT_GraphOperInfosNames[i])/ AT_GraphPerf[i]);
}
printf("\n");
printf("%45s: Cycles: %10u, Cyc%%: 100.0%%, Operations: %10u, Op%%: 100.0%%, Operations/Cycle: %f\n", "Total", TotalCycles, TotalOper, ((float) TotalOper)/ TotalCycles);
printf("\n");
}
#endif
#ifdef CI
if(outclass + 1 != 2 && max_score < 65){
printf("Eval Error...\n");
pmsis_exit(-1);
}
#endif
#ifdef PERF_CI
if(TotalCycles > PERF_CI){
printf("Perf Regression, expected cycles: %d\n", PERF_CI);
pmsis_exit(-1);
}
#endif
#ifdef WRITE_RESULTS
struct pi_hostfs_conf host_fs_conf;
struct pi_device host_fs;
pi_hostfs_conf_init(&host_fs_conf);
pi_open_from_conf(&host_fs, &host_fs_conf);
if (pi_fs_mount(&host_fs))
{
PRINTF("pi_fs_mount failed\n");
pmsis_exit(-4);
}
char* gap_result = "../../../gap_result.csv";
PRINTF("Writing output to %s\n", gap_result);
pi_fs_file_t* host_file = pi_fs_open(&host_fs, gap_result, PI_FS_FLAGS_WRITE);
if (host_file == NULL)
{
PRINTF("Failed to open file, %s\n", gap_result);
pmsis_exit(-5);
}
char buf[2];
for(int i=0; i < NETWORK_CLASSES; i++){
sprintf(buf, "%d;", network_output[i]);
//printf("%s\n", buf);
pi_fs_write(host_file, buf, strlen(buf));
}
pi_fs_close(host_file);
pi_fs_unmount(&host_fs);
#endif
pmsis_exit(0);
}