首頁(yè) > 評(píng)測(cè) > 【NUCLEO-U575ZI-Q測(cè)評(píng)】使用輕量級(jí)AI推理框架TinyMaix實(shí)現(xiàn)手寫數(shù)字識(shí)別
【NUCLEO-U575ZI-Q測(cè)評(píng)】使用輕量級(jí)AI推理框架TinyMaix實(shí)現(xiàn)手寫數(shù)字識(shí)別
- [導(dǎo)讀]
- 本帖最后由 xusiwei1236 于 2023-3-13 22:02 編輯 【NUCLEO-U575ZI-Q測(cè)評(píng)】使用輕量級(jí)AI推理框架TinyMaix實(shí)現(xiàn)手寫數(shù)字識(shí)別 一、TinyMaix簡(jiǎn)介TinyMaix是國(guó)內(nèi)sipeed團(tuán)隊(duì)開發(fā)一個(gè)輕量級(jí)AI推理框架,官方介紹如下
本帖最后由 xusiwei1236 于 2023-3-13 22:02 編輯
【NUCLEO-U575ZI-Q測(cè)評(píng)】使用輕量級(jí)AI推理框架TinyMaix實(shí)現(xiàn)手寫數(shù)字識(shí)別
一、TinyMaix簡(jiǎn)介
TinyMaix 是面向單片機(jī)的超輕量級(jí)的神經(jīng)網(wǎng)絡(luò)推理庫(kù),即 TinyML 推理庫(kù),可以讓你在任意單片機(jī)上運(yùn)行輕量級(jí)深度學(xué)習(xí)模型。
-
CPU: Cortex-M33內(nèi)核,160 MHz
-
Flash: 2 MB
-
RAM: 786 KB
1.1 TinyMaix開源項(xiàng)目
1.2 TinyMaix核心API
- /******************************* MODEL FUNCTION ************************************/
- tm_err_t tm_load (tm_mdl_t* mdl, const uint8_t* bin, uint8_t*buf, tm_cb_t cb, tm_mat_t* in); //load model
- void tm_unload(tm_mdl_t* mdl); //remove model
- tm_err_t tm_preprocess(tm_mdl_t* mdl, tm_pp_t pp_type, tm_mat_t* in, tm_mat_t* out); //preprocess input data
- tm_err_t tm_run (tm_mdl_t* mdl, tm_mat_t* in, tm_mat_t* out); //run model
- /******************************* UTILS FUNCTION ************************************/
- uint8_t TM_WEAK tm_fp32to8(float fp32);
- float TM_WEAK tm_fp8to32(uint8_t fp8);
- /******************************* STAT FUNCTION ************************************/
- #if TM_ENABLE_STAT
- tm_err_t tm_stat(tm_mdlbin_t* mdl); //stat model
- #endif
-
模型函數(shù),包括模型加載、卸載、預(yù)處理、推理;
-
工具函數(shù),包含F(xiàn)P32和uint8的互轉(zhuǎn);
-
統(tǒng)計(jì)函數(shù),用于輸出模型中間層信息;
- /******************************* LAYER FUNCTION ************************************/
- tm_err_t tml_conv2d_dwconv2d(tm_mat_t* in, tm_mat_t* out, wtype_t* w, btype_t* b, \
- int kw, int kh, int sx, int sy, int dx, int dy, int act, \
- int pad_top, int pad_bottom, int pad_left, int pad_right, int dmul, \
- sctype_t* ws, sctype_t in_s, zptype_t in_zp, sctype_t out_s, zptype_t out_zp);
- tm_err_t tml_gap(tm_mat_t* in, tm_mat_t* out, sctype_t in_s, zptype_t in_zp, sctype_t out_s, zptype_t out_zp);
- tm_err_t tml_fc(tm_mat_t* in, tm_mat_t* out, wtype_t* w, btype_t* b, \
- sctype_t* ws, sctype_t in_s, zptype_t in_zp, sctype_t out_s, zptype_t out_zp);
- tm_err_t tml_softmax(tm_mat_t* in, tm_mat_t* out, sctype_t in_s, zptype_t in_zp, sctype_t out_s, zptype_t out_zp);
- tm_err_t tml_reshape(tm_mat_t* in, tm_mat_t* out, sctype_t in_s, zptype_t in_zp, sctype_t out_s, zptype_t out_zp);
- tm_err_t tml_add(tm_mat_t* in0, tm_mat_t* in1, tm_mat_t* out, \
- sctype_t in_s0, zptype_t in_zp0, sctype_t in_s1, zptype_t in_zp1, sctype_t out_s, zptype_t out_zp);
1.3 TinyMaix底層依賴
- #define TM_ARCH_CPU (0) //default, pure cpu compute
- #define TM_ARCH_ARM_SIMD (1) //ARM Cortex M4/M7, etc.
- #define TM_ARCH_ARM_NEON (2) //ARM Cortex A7, etc.
- #define TM_ARCH_ARM_MVEI (3) //ARMv8.1: M55, etc.
- #define TM_ARCH_RV32P (4) //T-head E907, etc.
- #define TM_ARCH_RV64V (5) //T-head C906,C910, etc.
- #define TM_ARCH_CSKYV2 (6) //cskyv2 with dsp core
- #define TM_ARCH_X86_SSE2 (7) //x86 sse2
- #define TM_GET_US() ((uint32_t)((uint64_t)clock()*1000000/CLOCKS_PER_SEC))
- #define TM_DBGT_INIT() uint32_t _start,_finish;float _time;_start=TM_GET_US();
- #define TM_DBGT_START() _start=TM_GET_US();
- #define TM_DBGT(x) {_finish=TM_GET_US();\
- _time = (float)(_finish-_start)/1000.0;\
- TM_PRINTF("===%s use %.3f ms\n", (x), _time);\
- _start=TM_GET_US();}
二、計(jì)時(shí)和打印支持
2.1 創(chuàng)建CubeMX項(xiàng)目
2.2 使用CubeMX生成Keil項(xiàng)目
-
STM32Cube MCU packages and embedded software packs中,選擇Copy only the necessary library files(只拷貝必要的庫(kù)文件);
-
Generated files中,選擇Generate peripheral initialization as a pair of '.c/.h' filers per peripheral(每個(gè)外設(shè)的初始化生成獨(dú)立的.c/.h文件對(duì));
2.3 添加printf打印支持
- int fputc(int ch, FILE* f)
- {
- (void) f;
- uint8_t data[] = {ch};
- if (HAL_UART_Transmit(&huart1, data, sizeof(data), 1000) != HAL_OK)
- {
- return EOF;
- }
- return ch;
- }
2.4 基本功能測(cè)試
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
- HAL_Delay(1000);
- printf("Low\r\n");
- HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
- HAL_Delay(1000);
- printf("High\r\n");
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
三、TinyMaix移植3.1 添加TinyMaix源碼
git clone https://github.com/sipeed/TinyMaix.git
3.2 解決TinyMaix編譯問(wèn)題
-
右鍵TinyMaix-U575打開Options for Target 'TinyMaix-U575'配置窗口;
-
點(diǎn)擊C/C++標(biāo)簽頁(yè),找到Include paths欄;
-
點(diǎn)擊Include paths欄右側(cè)的“...”按鈕,彈出Setup compiler include paths界面;
-
在此界面中,將項(xiàng)目的TinyMaix/include子目錄添加到搜索路徑列表中。
- #include "stm32u5xx_hal.h"
- #define TM_GET_MS() HAL_GetTick()
- #define TM_DBGT_INIT() uint32_t _start, _finish; int _time; _start = TM_GET_MS();
- #define TM_DBGT_START() _start = TM_GET_MS();
- #define TM_DBGT(x) _finish = TM_GET_MS(); \
- _time = (_finish - _start); \
- TM_PRINTF("===%s use %d ms\n", (x), _time); \
- _start = TM_GET_MS();
3.3 添加手寫數(shù)字識(shí)別示例
-
將mnist目錄內(nèi)的main.c重命名為mnist_main.c;
-
將該文件中的main函數(shù)重命名為mnist_main;
- int mnist_main(int argc, char** argv);
- mnist_main(0, 0);
四、原理解讀4.1 手寫數(shù)字識(shí)別示例源碼
- #if TM_MDL_TYPE == TM_MDL_INT8
- #include "../../tools/tmdl/mnist_valid_q.h"
- //#include "../../tools/tmdl/mnist_resnet_q.h"
- #elif TM_MDL_TYPE == TM_MDL_FP32
- #include "../../tools/tmdl/mnist_valid_f.h"
- //#include "../../tools/tmdl/mnist_resnet_f.h"
- #elif TM_MDL_TYPE == TM_MDL_FP16
- #include "../../tools/tmdl/mnist_valid_fp16.h"
- #elif TM_MDL_TYPE == TM_MDL_FP8_143
- #include "../../tools/tmdl/mnist_fp8_143.h"
- #elif TM_MDL_TYPE == TM_MDL_FP8_152
- #include "../../tools/tmdl/mnist_fp8_152.h"
- #endif
python3 tflite2tmdl.py tflite/mnist_valid_f.tflite tmdl/mbnet_fp8.tmdl fp8_152 1 28,28,1 10
-
tm_stat 打印模型結(jié)構(gòu)等信息;
-
tm_load 將模型加載到內(nèi)存;
-
tm_preprocess 輸入數(shù)據(jù)預(yù)處理;
-
tm_run 模型推理,得到輸出;
-
tm_unload 模型卸載,釋放內(nèi)存;
五、參考鏈接
-
【ST官網(wǎng)】NUCLEO-U575ZI-Q開發(fā)板產(chǎn)品頁(yè): https://www.st.com/en/evaluation-tools/nucleo-u575zi-q.html
-
【ST官網(wǎng)】NUCLEO-U575ZI-Q開發(fā)板原理圖:https://www.st.com/resource/en/schematic_pack/mb1549-u575ziq-c03_schematic.pdf
-
【ST官網(wǎng)】STM32U575ZI芯片產(chǎn)品頁(yè): https://www.st.com/en/microcontrollers-microprocessors/stm32u575zi.html
-
【Keil官網(wǎng)】Pack包下載:https://www.keil.com/dd2/pack/
-
Keil中如何重定向printf/scanf到UART: http://www.ocfreaks.com/retarget-redirect-printf-scanf-uart-keil/
- 本文系21ic原創(chuàng),未經(jīng)許可禁止轉(zhuǎn)載!
網(wǎng)友評(píng)論
- 聯(lián)系人:巧克力娃娃
- 郵箱:board@21ic.com
- 我要投稿
-
歡迎入駐,開放投稿
- NRF52810藍(lán)牙數(shù)字耳機(jī)找人定制
預(yù)算:¥30005天前
- 125KW模塊式PCS軟硬件外包開發(fā)
預(yù)算:¥1100000015小時(shí)前
- 12V汽車啟動(dòng)電源項(xiàng)目BMS設(shè)計(jì)
預(yù)算:¥50000023小時(shí)前
- 數(shù)據(jù)可視化軟件 開發(fā)
預(yù)算:¥5000023小時(shí)前
- PLC項(xiàng)目調(diào)試修改
預(yù)算:¥100001天前
- 起動(dòng)電機(jī)控制器開發(fā)
預(yù)算:¥1100001天前