首页 > 评测 > 【NUCLEO-U575ZI-Q测评】使用轻量级AI推理框架TinyMaix实现手写数字识别
【NUCLEO-U575ZI-Q测评】使用轻量级AI推理框架TinyMaix实现手写数字识别
- [导读]
- 本帖最后由 xusiwei1236 于 2023-3-13 22:02 编辑 【NUCLEO-U575ZI-Q测评】使用轻量级AI推理框架TinyMaix实现手写数字识别 一、TinyMaix简介TinyMaix是国内sipeed团队开发一个轻量级AI推理框架,官方介绍如下
本帖最后由 xusiwei1236 于 2023-3-13 22:02 编辑
【NUCLEO-U575ZI-Q测评】使用轻量级AI推理框架TinyMaix实现手写数字识别
一、TinyMaix简介
TinyMaix 是面向单片机的超轻量级的神经网络推理库,即 TinyML 推理库,可以让你在任意单片机上运行轻量级深度学习模型。
-
CPU: Cortex-M33内核,160 MHz
-
Flash: 2 MB
-
RAM: 786 KB
1.1 TinyMaix开源项目
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
-
模型函数,包括模型加载、卸载、预处理、推理;
-
工具函数,包含FP32和uint8的互转;
-
统计函数,用于输出模型中间层信息;
- /******************************* 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();}
二、计时和打印支持
2.1 创建CubeMX项目
2.2 使用CubeMX生成Keil项目
-
STM32Cube MCU packages and embedded software packs中,选择Copy only the necessary library files(只拷贝必要的库文件);
-
Generated files中,选择Generate peripheral initialization as a pair of '.c/.h' filers per peripheral(每个外设的初始化生成独立的.c/.h文件对);
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 基本功能测试
- /* 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编译问题
-
右键TinyMaix-U575打开Options for Target 'TinyMaix-U575'配置窗口;
-
点击C/C++标签页,找到Include paths栏;
-
点击Include paths栏右侧的“...”按钮,弹出Setup compiler include paths界面;
-
在此界面中,将项目的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 添加手写数字识别示例
-
将mnist目录内的main.c重命名为mnist_main.c;
-
将该文件中的main函数重命名为mnist_main;
- int mnist_main(int argc, char** argv);
- mnist_main(0, 0);
四、原理解读4.1 手写数字识别示例源码
- #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 打印模型结构等信息;
-
tm_load 将模型加载到内存;
-
tm_preprocess 输入数据预处理;
-
tm_run 模型推理,得到输出;
-
tm_unload 模型卸载,释放内存;
五、参考链接
-
【ST官网】NUCLEO-U575ZI-Q开发板产品页: https://www.st.com/en/evaluation-tools/nucleo-u575zi-q.html
-
【ST官网】NUCLEO-U575ZI-Q开发板原理图:https://www.st.com/resource/en/schematic_pack/mb1549-u575ziq-c03_schematic.pdf
-
【ST官网】STM32U575ZI芯片产品页: https://www.st.com/en/microcontrollers-microprocessors/stm32u575zi.html
-
【Keil官网】Pack包下载:https://www.keil.com/dd2/pack/
-
Keil中如何重定向printf/scanf到UART: http://www.ocfreaks.com/retarget-redirect-printf-scanf-uart-keil/
- 本文系21ic原创,未经许可禁止转载!
网友评论
- 联系人:巧克力娃娃
- 邮箱:board@21ic.com
- 我要投稿
-
欢迎入驻,开放投稿
-
人均百万?英伟达中国员工收入曝光! 2024-08-29
-
《黑神话:悟空》玩家硬盘升级攻略:提升游戏体验,畅享3A大作 2024-08-29
-
数睿数据参加《系统与软件工程 低代码开发平台通用技术要求》国家标准编制 2024-08-29
- NRF52810蓝牙数字耳机找人定制
预算:¥30005天前
- 125KW模块式PCS软硬件外包开发
预算:¥1100000015小时前
- 12V汽车启动电源项目BMS设计
预算:¥50000023小时前
- 数据可视化软件 开发
预算:¥5000023小时前
- PLC项目调试修改
预算:¥100001天前
- 起动电机控制器开发
预算:¥1100001天前