/
main.c
93 lines (77 loc) · 2.05 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
/*
* Copyright (c) 2018-2020, Jianjia Ma
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-03-29 Jianjia Ma first implementation
*/
#include <stdio.h>
#include "rtthread.h"
#include "nnom.h"
#include "image.h"
#include "weights.h"
nnom_model_t *model;
int main(void)
{
rt_thread_delay(10);
// create and compile the model
model = nnom_model_create();
// dummy run
model_run(model);
}
#ifdef RT_USING_FINSH
#include <finsh.h>
// ASCII lib from (https://www.jianshu.com/p/1f58a0ebf5d9)
const char codeLib[] = "@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
void print_img(int8_t * buf)
{
for(int y = 0; y < 28; y++)
{
for (int x = 0; x < 28; x++)
{
int index = 69 / 127.0 * (127 - buf[y*28+x]);
if(index > 69) index =69;
if(index < 0) index = 0;
printf("%c",codeLib[index]);
printf("%c",codeLib[index]);
}
printf("\n");
}
}
// Do simple test using image in "image.h" with model created previously.
void mnist(int argc, char** argv)
{
uint32_t tick, time;
uint32_t predic_label;
float prob;
int32_t index = atoi(argv[1]);
if(index >= TOTAL_IMAGE || argc != 2)
{
printf("Please input image number within %d\n", TOTAL_IMAGE-1);
return;
}
printf("\nprediction start.. \n");
tick = rt_tick_get();
// copy data and do prediction
memcpy(nnom_input_data, (int8_t*)&img[index][0], 784);
nnom_predict(model, &predic_label, &prob);
time = rt_tick_get() - tick;
//print original image to console
print_img((int8_t*)&img[index][0]);
printf("Time: %d tick\n", time);
printf("Truth label: %d\n", label[index]);
printf("Predicted label: %d\n", predic_label);
printf("Probability: %d%%\n", (int)(prob*100));
}
FINSH_FUNCTION_EXPORT(mnist, mnist(4) );
MSH_CMD_EXPORT(mnist, mnist);
void nn_stat()
{
model_stat(model);
printf("Total Memory cost (Network and NNoM): %d\n", nnom_mem_stat());
}
MSH_CMD_EXPORT(nn_stat, print nn model);
FINSH_FUNCTION_EXPORT(nn_stat, nn_stat() to print data);
#endif