Hi3861使用NNOM實(shí)現(xiàn)人工智能神經(jīng)網(wǎng)絡(luò)之MNIST
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
1、什么是NNOM
請(qǐng)查看之前的帖子:https://ost.51cto.com/posts/12287
2、什么是MNIST
每當(dāng)我們學(xué)習(xí)一門(mén)新的語(yǔ)言時(shí),所有的入門(mén)教程官方都會(huì)提供一個(gè)典型的例子——“Hello World”。而在機(jī)器學(xué)習(xí)中,入門(mén)的例子稱(chēng)之為MNIST。
MNIST是一個(gè)簡(jiǎn)單的視覺(jué)計(jì)算數(shù)據(jù)集,它是像下面這樣手寫(xiě)的數(shù)字圖片:
MNIST 經(jīng)常被用來(lái)做為分類(lèi)任務(wù)的入門(mén)數(shù)據(jù)庫(kù)使用。在這個(gè)簡(jiǎn)單的例子里面,我們也用它來(lái)試試數(shù)據(jù)歸類(lèi)。
3、移植和編譯
移植NNOM庫(kù)的方法,在之前已經(jīng)有說(shuō)明。
$(wildcard $(LIBPATH)/nnom/src/backends/*.c) \
$(wildcard $(LIBPATH)/nnom/src/core/*.c) \
$(wildcard $(LIBPATH)/nnom/src/layers/*.c) \
同時(shí)移植了mnist-simple下面的兩個(gè).h文件。
#include "lib/nnom/examples/mnist-simple/image.h"
#include "lib/nnom/examples/mnist-simple/weights.h"
核心的幾個(gè)函數(shù)。
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;
DEBUG_printf("%c",codeLib[index]);
DEBUG_printf("%c",codeLib[index]);
}
DEBUG_printf("\n");
}
}
// Do simple test using image in "image.h" with model created previously.
void mnist(int index)
{
uint32_t tick, time;
uint32_t predic_label;
float prob;
DEBUG_printf("\nprediction start.. \n");
tick = hi_get_milli_seconds();
// copy data and do prediction
memcpy(nnom_input_data, (int8_t*)&img[index][0], 784);
nnom_predict(model, &predic_label, &prob);
time = hi_get_milli_seconds() - tick;
//print original image to console
print_img((int8_t*)&img[index][0]);
DEBUG_printf("Time: %d tick\n", time);
DEBUG_printf("Truth label: %d\n", label[index]);
DEBUG_printf("Predicted label: %d\n", predic_label);
DEBUG_printf("Probability: %d%%\n", (int)(prob*100));
}
void nn_stat()
{
model_stat(model);
printf("Total Memory cost (Network and NNoM): %d\n", nnom_mem_stat());
}
4、MNIST使用
我是在Micropython下使用的,其實(shí)主要的幾個(gè)函數(shù),如下,可以自行移植測(cè)試。
STATIC mp_obj_t machine_ai_nnom_test(mp_obj_t self_in,mp_obj_t data_in) {
int num = mp_obj_get_int(data_in);
// create and compile the model
model = nnom_model_create();
// dummy run
model_run(model);
mnist(num);
nn_stat();
return mp_const_none;
}
不同的num值,代表候選的要識(shí)別的數(shù)字的字節(jié)圖。比如8,存儲(chǔ)的字節(jié)符合是這樣的。
因?yàn)椴环奖爿斎胧謱?xiě)數(shù)字,只能通過(guò)這種字符的形式進(jìn)行測(cè)試。
5、總結(jié)
這是比較簡(jiǎn)單的例子,對(duì)系統(tǒng)的要求的比較低,識(shí)別的時(shí)間也很短。同時(shí),也可以自己訓(xùn)練模型,進(jìn)行分析。但該部分不是本文的重點(diǎn),敢興趣的同學(xué),可以自行去github上,進(jìn)行深入的學(xué)習(xí)。下一篇,會(huì)針對(duì)KWS的功能測(cè)試,實(shí)現(xiàn)實(shí)時(shí)音頻輸入關(guān)鍵詞識(shí)別的demo的移植和演示。
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??