-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlprnet.c
182 lines (156 loc) · 5.42 KB
/
lprnet.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
174
175
176
177
178
179
180
181
182
/*
* Copyright (C) 2017 GreenWaves Technologies
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD license. See the LICENSE file for details.
*
*/
#include <stdio.h>
#include "lprnet.h"
#include "lprnetKernels.h"
#include "gaplib/ImgIO.h"
#define __XSTR(__s) __STR(__s)
#define __STR(__s) #__s
#define AT_INPUT_SIZE (AT_INPUT_WIDTH_LPR*AT_INPUT_HEIGHT_LPR*AT_INPUT_COLORS_LPR)
AT_HYPERFLASH_FS_EXT_ADDR_TYPE lprnet_L3_Flash = 0;
static char *CHAR_DICT [71] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "<Anhui>", "<Beijing>", "<Chongqing>", "<Fujian>", "<Gansu>", "<Guangdong>", "<Guangxi>", "<Guizhou>", "<Hainan>", "<Hebei>", "<Heilongjiang>", "<Henan>", "<HongKong>", "<Hubei>", "<Hunan>", "<InnerMongolia>", "<Jiangsu>", "<Jiangxi>", "<Jilin>", "<Liaoning>", "<Macau>", "<Ningxia>", "<Qinghai>", "<Shaanxi>", "<Shandong>", "<Shanghai>", "<Shanxi>", "<Sichuan>", "<Tianjin>", "<Tibet>", "<Xinjiang>", "<Yunnan>", "<Zhejiang>", "<police>", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "_"};
typedef signed char IMAGE_IN_T;
#ifdef NE16
typedef unsigned char OUT_T;
#else
typedef signed char OUT_T;
#endif
OUT_T * Output_1;
OUT_T * Input_1;
static void RunNetwork()
{
#ifdef PERF
printf("Start timer\n");
gap_cl_starttimer();
gap_cl_resethwtimer();
#endif
lprnetCNN( Input_1, Output_1);
printf("Runner completed\n");
int max_prob;
int predicted_char = 70;
int prev_char = 70;
printf("OUTPUT: \n");
for (int i=0; i<88; i++){
max_prob = 0x80000000;
for (int j=0; j<71; j++){
#ifdef NE16
OUT_T char_score = Output_1[i*71+j];
#else
OUT_T char_score = Output_1[i+j*88];
#endif
if (char_score>max_prob){
max_prob = char_score;
predicted_char = j;
}
}
if (predicted_char==70|| prev_char == predicted_char) continue;
prev_char = predicted_char;
printf("%s, ", CHAR_DICT[predicted_char]);
}
printf("\n");
}
int start()
{
#ifdef MEASUREMENTS
pi_gpio_pin_configure(NULL, PI_GPIO_A0_PAD_8_A4, PI_GPIO_OUTPUT);
pi_gpio_pin_write(NULL, PI_GPIO_A0_PAD_8_A4, 0);
#endif
/*-----------------------OPEN THE 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);
if (pi_cluster_open(&cluster_dev))
{
printf("Cluster open failed !\n");
pmsis_exit(-4);
}
pi_freq_set(PI_FREQ_DOMAIN_CL, FREQ_CL*1000*1000);
pi_freq_set(PI_FREQ_DOMAIN_FC, FREQ_FC*1000*1000);
char *ImageName = __XSTR(AT_IMAGE);
//Reading Image from Bridge
Input_1 = pi_l2_malloc(AT_INPUT_WIDTH_LPR*AT_INPUT_HEIGHT_LPR*3*sizeof(char));
if(Input_1==NULL){
printf("Error allocating image buffer\n");
pmsis_exit(-1);
}
/* -------------------- Read Image from bridge ---------------------*/
printf("Reading image\n");
if (ReadImageFromFile(ImageName, AT_INPUT_WIDTH_LPR, AT_INPUT_HEIGHT_LPR, 1, Input_1, AT_INPUT_WIDTH_LPR*AT_INPUT_HEIGHT_LPR*sizeof(char), IMGIO_OUTPUT_CHAR, 0)) {
printf("Failed to load image %s\n", ImageName);
return 1;
}
for(int i=0; i<AT_INPUT_WIDTH_LPR*AT_INPUT_HEIGHT_LPR; i++){
#ifdef NE16
int temp = Input_1[i];
#else
int temp = Input_1[i] - 128;
#endif
Input_1[i] = temp;
Input_1[i+AT_INPUT_WIDTH_LPR*AT_INPUT_HEIGHT_LPR] = temp;
Input_1[i+2*AT_INPUT_WIDTH_LPR*AT_INPUT_HEIGHT_LPR] = temp;
}
printf("Finished reading image\n");
//Allocate output buffers:
Output_1 = (OUT_T *)AT_L2_ALLOC(0, 71*88);
if(Output_1==NULL){
printf("Error Allocating CNN output buffers");
return 1;
}
// IMPORTANT - MUST BE CALLED AFTER THE CLUSTER IS SWITCHED ON!!!!
if (lprnetCNN_Construct())
{
printf("Graph constructor exited with an error\n");
return 1;
}
printf("Graph constructor was OK\n");
/*--------------------------TASK SETUP------------------------------*/
struct pi_cluster_task task;
pi_cluster_task(&task, (void (*)(void *))RunNetwork, NULL);
pi_cluster_task_stacks(&task, NULL, SLAVE_STACK_SIZE);
#ifdef MEASUREMENTS
for (int i=0; i<1000; i++){
pi_time_wait_us(50000);
pi_gpio_pin_write(NULL, PI_GPIO_A0_PAD_8_A4, 1);
// Execute the function "RunNetwork" on the cluster.
pi_cluster_send_task_to_cl(&cluster_dev, &task);
pi_gpio_pin_write(NULL, PI_GPIO_A0_PAD_8_A4, 0);
}
#else
// Execute the function "RunNetwork" on the cluster.
pi_cluster_send_task_to_cl(&cluster_dev, &task);
#endif
#ifdef PERF
{
unsigned int TotalCycles = 0, TotalOper = 0;
printf("\n");
for (unsigned int i=0; i<(sizeof(LPR_Monitor)/sizeof(unsigned int)); i++) {
TotalCycles += LPR_Monitor[i]; TotalOper += LPR_Op[i];
}
for (unsigned int i=0; i<(sizeof(LPR_Monitor)/sizeof(unsigned int)); i++) {
printf("%45s: Cycles: %12u, Cyc%%: %5.1f%%, Operations: %12u, Op%%: %5.1f%%, Operations/Cycle: %f\n", LPR_Nodes[i], LPR_Monitor[i], 100*((float) (LPR_Monitor[i]) / TotalCycles), LPR_Op[i], 100*((float) (LPR_Op[i]) / TotalOper), ((float) LPR_Op[i])/ LPR_Monitor[i]);
}
printf("\n");
printf("%45s: Cycles: %12u, Cyc%%: 100.0%%, Operations: %12u, Op%%: 100.0%%, Operations/Cycle: %f\n", "Total", TotalCycles, TotalOper, ((float) TotalOper)/ TotalCycles);
printf("\n");
}
#endif
lprnetCNN_Destruct();
pmsis_exit(0);
printf("Ended\n");
return 0;
}
int main(int argc, char *argv[])
{
printf("\n\n\t *** NNTOOL LPRNET ***\n\n");
start();
return 0;
}