-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcmsis_nn_001.txt
186 lines (79 loc) · 2.9 KB
/
cmsis_nn_001.txt
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
https://www.taterli.com/5376/
CMSIS-NN 调通了~
由 TaterLi2019年5月4日2 评论
下载地址:CMSIS-NN
由于是8位定点的推算算法,所以精度会差点,但是速度快啊,需要把图片存TF卡里,或者你自己开发驱动,我用的是STM32F769 DISCO.
关键代码:
int8_t NN_OpenReadFile(const char *BmpName)
{
uint32_t size = 0;
int32_t h_index = 0;
int32_t w_index = 0;
int32_t h_total = 0;
int32_t w_total = 0;
int32_t h_nn = 0;
int32_t w_nn = 0;
UINT BytesRead;
FIL bmpfile;
q7_t output_data[10]; /* 输出数据,分别代表{"Plane", "Car", "Bird", "Cat", "Deer", "Dog", "Frog", "Horse", "Ship", "Truck"} */
int8_t max_ind = 0;
int8_t max_val = -128;
BmpHeader *pbmpheader = (BmpHeader *)aBuffer;
/* 打开BMP文件 */
f_open(&bmpfile, BmpName, FA_READ);
/* 移动到文件头 */
f_lseek(&bmpfile, 0);
/* 读取头信息 */
f_read(&bmpfile, &aBuffer, BITMAP_HEADER_SIZE, &BytesRead);
/* 取出原图的长宽 */
h_total = pbmpheader->h;
w_total = pbmpheader->w;
/* 计算出缩小比例 */
h_nn = pbmpheader->h / 32;
w_nn = pbmpheader->w / 32;
/* 取出一次读出数量 */
size = pbmpheader->w * (pbmpheader->bpp / 8) + ((pbmpheader->w * (pbmpheader->bpp / 8)) % 4);
if (pbmpheader->bpp != 24)
{
return -1; /* 保存为24Bit图像吧. */
}
if (size > BITMAP_BUFFER_SIZE)
{
return -2; /* 图片太大,超过缓冲区大小. */
}
/* 偏移到图像内容,然后提取图像内容到32*32的范围内. */
f_lseek(&bmpfile, pbmpheader->offset);
for (h_index = 0; h_index < h_total; h_index++)
{
f_read(&bmpfile, &aBuffer, size, &BytesRead);
if (h_index % h_nn == 0)
{
for (w_index = 0; w_index < w_total; w_index++)
{
if (w_index % w_nn == 0)
{
BmpBuffer[(h_index) / h_nn][w_index / w_nn][2] = aBuffer[3 * w_index];
BmpBuffer[(h_index) / h_nn][w_index / w_nn][1] = aBuffer[3 * w_index + 1];
BmpBuffer[(h_index) / h_nn][w_index / w_nn][0] = aBuffer[3 * w_index + 2];
}
}
}
}
/* 关闭文件 */
f_close(&bmpfile);
/* 运行NN算法 */
run_nn((q7_t *)BmpBuffer, output_data);
arm_softmax_q7(output_data, IP1_OUT_DIM, output_data);
/* 找出最可信结果 */
for (int i = 0; i < 10; i++)
{
if (max_val < output_data[i])
{
max_val = output_data[i];
max_ind = i;
}
}
/* 返回可信结果 */
return max_ind;
}
返回值如果是正,就是有结果,0 ~ 9分别对应”Plane”, “Car”, “Bird”, “Cat”, “Deer”, “Dog”, “Frog”, “Horse”, “Ship”, “Truck”这么几种,推荐用Windows 10画图工具保存为24位BMP,最好的分辨率是32*32,如果不是32*32,则在程序里也有缩放,但是效果不太好,因为我是直接间隔抽取的,良好的缩放算法应该考虑颜色权重问题.