一、cube.AI简介及cubeIDE集成
4 W4 q. V2 S# l9 v 1.1 cube.AI介绍% F: H* Y2 y( K9 k8 \
cube.AI准确来说是STM32Cube.AI,它是ST公司的打造的STM32Cube生态体系的扩展包X-CUBE-AI,专用于帮助开发者实现人工智能开发。确切地说,是将基于各种人工智能开发框架训练出来的算法模型,统一转换为c语言支持的源码模型,然后将c模型与STM32的硬件产品结合,实现人工智能模型可直接部署在前端或边缘端设备,实现人工智能就地计算。1 }9 N$ ]/ S7 p q# ]2 b
. K0 c6 N5 G" k7 W; O
cube.AI 以插件形式支持ST相关开发平台如cubeIDE、cubeMX、keil等,整体开发过程分为三个主要部分,1)收集及治理数据,2)训练及验证模型,3)c模型生成及前端或边缘端部署,如下图所示:! A; G+ V4 L+ D5 B
; X0 d7 p1 I& q( Y# k
& {2 r& }, B1 Q. o" [( O
L- I; z, ~8 a9 h0 S( h 目前cube.AI支持各种深度学习框架的训练模型,如Keras和TensorFlow™ Lite,并支持可导出为ONNX标准格式的所有框架,如PyTorch™、Microsoft® Cognitive Toolkit、MATLAB®等,然后通过 cube.MX可视化配置界面导入这些深度学习框架导出的训练模型来配置及生成c模型,进而部署在STM32芯片上。) Z( c4 I0 X: `0 i7 b) Q. q3 U0 |
6 \% V5 k3 p; K2 F% h
/ \& _7 s* {0 i" l( F6 `# V 1.2 cube.AI与cubeIDE集成与安装, c& ^' g2 h' _" t( H% G& X
在cubeIDE的帮助菜单栏,选择嵌入式软件包管理项(Embedded Software Packages Manager)进入X-CUBE-AI扩展包安装页面。选择X-CUBE-AI需要的版本进行安装即可,如下图所示,安装完成后,该版本前面方框呈绿色标记。. T! ^- g: s& i4 t8 N) f9 v( a
* E' C1 z8 ^$ g% W
0 K: b q) M& B R6 P* q/ S5 V) ?2 K+ j; q7 L( g" O
: i. l/ A% O) @1 ]# W1 R 1.3 cube.AI支持硬件平台: ?& z' Q; `- X2 K
得益于ST公司不断的优化及迭代X-CUBE-AI扩展包,神经网络模型生成c模型后得以使用更小的算力资源和几乎无损的算法精度,因此使其能部署到STM32绝大多数的芯片上,目前其支持的MCU及MPU型号如下图所示。( b. D# C" H2 b
4 ]( x i! N, U0 p; R' l
( W7 }. p; E6 i, V0 \) v( C7 S" l) L: `# X1 |( R
1.4 cube.AI应用的好处
6 O4 a, M9 @! B 将神经网络边缘化部署后,减少延迟、节约能源、提高云利用率,并通过大限度地减少互联网上的数据交换来保护隐私,而结合X-CUBE-AI使得神经网络部署在边缘端端的便捷、灵活、低成本,微机智能成为更多产品的选择。
: m- W/ m: D8 i c/ T" D
5 W7 c. N( \6 C9 i4 K9 U
5 V' D; P# B( B+ F4 u
5 T5 E+ ~) [0 ^5 l* z# _& L 二、FP-AI-SENSING1
+ d0 K# A& Q! i8 l 2.1 FP-AI-SENSING1简介
* @; Y2 W1 p' H( c, D. h8 \ a FP-AI-SENSING1是ST公司提供的STM32Cube.AI示例,可通过BLE(低功耗蓝牙)将物联网节点连接到智能手机,并使用STBLESensor应用程序,配置设备,实现数据采集,使得用于训练神经网络模型的数据更贴近实际使用场景,具有更好的训练效果和精度。
: c; {4 |+ i& D/ g/ q* K/ j4 ]7 |
在FP-AI-SENSING1案例页面,下载源码包及其数据手册。
! a. } R. a u- h7 f
+ k0 o, ]* ]% z7 @- U- `$ {
* m, M6 H* Q8 g" w) I
7 b: R% g+ `( x, N 2.2 FP-AI-SENSING1软件包支持硬件平台. m6 r' K; V/ L/ h& Z+ E1 m5 R& o4 `8 m
ST公司为FP-AI-SENSING1示例运行提供了硬件平台,支持开发者快速学习了解FP-AI-SENSING1示例,从而了解Cube.AI的开发过程。
, i# J& {$ F4 o: r
7 ^( N: j- X1 j4 @# p
; ]% q3 B: o# a; u+ M2 w6 a4 ^
5 J# e* ?4 `& S1 Z' B三、FP-AI-SENSING1部署- N9 h- a% ]! h0 b- }/ F
3.1 B-L475E-IOT01A开发板
; X7 ?0 U$ {* q3 i% v 本文采用ST公司的B-L475E-IOT01A开发板,打开CubeMX工具,选择Start my project from ST Board,搜索B-L475E-IOT01A,如下图所示,可以在1、2、3页面下载开发板相关的原理框图、文档及案例、说明手册。" W% F! C" x) J
4 z$ |6 l1 Y8 H+ t/ s `
3 g; F- h1 A. g z8 b
% Z* _& B9 P( R 3.2 FP-AI-SENSING1软件包下载及配置
, R* p% A8 x) K( t& g- t( y 下载FP-AI-SENSING1软件包后,解压下载的源码包:en.fp-ai-sensing1.zip,进入“STM32CubeFunctionPack_SENSING1_V4.0.3\Projects\B-L475E-IOT01A\Applications\SENSING1\STM32CubeIDE”目录,用文本编辑工具打开“CleanSENSING1.bat”,(linux系统的,采用CleanSENSING1.sh文件)。
7 h, G; y5 g5 j' e, a6 ]/ R1 `+ U3 p9 P, h. e) [% |: W
( ?# x) q9 }( z ]( C) F* q& {" S7 B5 Z5 {, H! ?' E
CleanSENSING1.bat运行依赖STM32cube生态的另一个开发工具:STM32CubeProgrammer,该工具可以帮助开发者读取、写入和验证设备内存等。. A" q9 Q# l- |( G# K- d9 C: ?2 }0 D* i
) y o8 M# U7 |. S
在STM32CubeProgrammer工具下载页面,下载该工具及说明手册:5 c( x; t7 A, f' I4 C
5 ?; e+ a. Z6 o# o: v
8 ` p3 k E- C
下载并安装STM32CubeProgrammer工具,例如本文安装目录为:D:\workForSoftware\STM32CubeProgrammer; n$ ]+ a3 v2 y
# G9 A& U, `) z
d- A/ X/ j7 c/ v: S! r0 c( i( r
8 N% Q+ f, D9 I 修改CleanSENSING1.bat依赖工具“STM32CubeProgrammer”的路径:
4 c1 T ^7 H( c: Y. t' `3 Q7 E9 M7 G4 q, f) p7 _5 g$ T
6 K+ I# F( p% G( w. c4 h& o8 ]0 @" S( Y0 `: y: h& Y
3.3 固件烧录
3 A q5 P1 ~; J$ \ 将B-L475E-IOT01A开发板用Micro USB连接到电脑上,8 Y+ @+ k0 Q' X9 B: w
) p& U+ D' ^9 Z" l; G7 w% V+ k
' V0 x, D9 }+ [8 {# T8 L6 @
" D! j ~5 y2 K' L% S5 [ 连接之后,驱动会自动安装,进入设备管理页面,确认串口编号和配置串口参数。
% d9 S5 E+ _+ A. n- J/ f! G8 `4 J( v! z) n7 c
1 |8 \' T9 E# C4 w0 P
0 ^8 B- T2 p' L3 D4 N' A
右键CleanSENSING1.bat文件以管理员身份运行,将在开发板安装引导加载程序和更新固件。. w. V& i$ [9 [! [
* t# |1 D4 _! s6 ^3 [% n$ f
# d E& t( E9 {$ R& r% b: N
F+ _' o# T: l+ h/ [7 T& c
该脚本可以对B-L475E-IOT01A开发板实现以下操作,1 n9 m6 i9 Z" c6 C$ ~ N
•完全闪存擦除" L f1 D7 O7 W5 m& o9 L& e
•在右侧闪存区域加载BootLoader# V3 ^. u; s; n. y
•在右侧闪存区域加载程序(编译后)7 m) y4 \$ t5 r: m# r" I: \
•重置电路板
* u3 |% f C. Y) M
; d6 W+ r* j- |7 }% q/ y% E
$ }0 y; j% ?3 w D+ R2 n. F 3.4 FP-AI-SENSING1示例工程部署/ |% _0 S0 m: K3 e3 r- x4 B
在该目录下,进入“B-L475E-IOT01A”目录,用CubeIDE打开.project,打开FP-AI-SENSING1工程。
$ m8 l& \' n% i2 K7 P/ p2 M4 A1 {/ G* i8 t9 J% _" p8 w4 d! k1 Q& j
1 m6 D$ Y Y9 ?( V0 j$ V% J0 x, d9 g
打开该工程后如下图所示,用户可调整源码在User目录,关于本工程信息请查看readme.txt文件。
; Y5 w4 M7 \+ |" h- F! S4 g; x/ |5 ~
- X) o6 {* f7 p3 U4 E6 m, c2 y$ |7 P; r) G" q' T' f
在main.c函数中找到Init_BlueNRG_Stack函数,该函数可以设置BLE(低功耗蓝牙)的服务名,7 m9 P8 D2 Q1 P5 N! g" m" B
- static void Init_BlueNRG_Stack(void)
9 k$ X# \# }% ^/ j y- t* M" y - {5 v, ^& V2 w b l h. D
- char BoardName[8];
j2 ?/ I/ k6 ]; N) O3 {, d; C - uint16_t service_handle, dev_name_char_handle, appearance_char_handle;# W2 P! O- s( `% t1 I
- int ret;
1 N) y) ?0 @9 x# Y9 H: | -
5 h) P8 ^# V* J( w! `2 E1 J. ? - for(int i=0; i<7; i++) {1 p% V6 f ~& N$ h! e8 T4 w
- BoardName[i]= NodeName[i+1];
) Z: A6 O1 a: @) O i( b$ n - }
复制代码
6 u( i- |7 K; x+ A) }# W8 Z 该函数采用默认的BLE名称,该默认名称定义在SENSING1.h设置,例如:IAI_403
* C) Z- ]4 H1 T) {+ U1 t
- g( h+ V6 f, [. A2 f
: N+ u9 E. p0 S Y" N1 D$ [1 O' q; l( J. ?; H; R
现在调整BLE名称为AI_Test. P9 E' @, ^. L) ^4 W4 j
- static void Init_BlueNRG_Stack(void)
4 R1 Z( f; b" J; j' a1 F - {' W8 ]% G" V, G$ k; }, E1 w5 B% h
- // char BoardName[8];; \9 t8 A, O; P/ b& O, l
- char BoardName[8] = {'A','I','_','T','e','s','t'};
1 @( N# z9 {1 z- M/ F - uint16_t service_handle, dev_name_char_handle, appearance_char_handle;
. i5 ?. A& K7 n" @ - int ret; _6 M0 g9 U! X' S9 p" ?2 Y+ O w4 f
- 4 K8 w! P! N# h" v1 Q: u+ x
- for(int i=0; i<7; i++) {* Q: U z) S6 i6 ?6 f( f
- // BoardName[i]= NodeName[i+1];5 q3 G: o7 N; X3 ^4 ?
- NodeName[i+1] = BoardName[i];' f! d- T+ ~6 X0 L4 o$ Q
- }
复制代码 * O; z: F7 r* E% f; E5 M$ Q( @' B
配置工程输出格式支持如下:
) N- C0 O3 R8 Z+ A1 w7 k2 X' [3 G* H
% b$ e( }1 e5 Q \" Y
/ \; O4 i# \6 R
$ s/ v6 F& v- V6 n/ ~ 配置运行设置如下:. T6 ]3 A% t5 Y. F; [7 d" T
! n9 j- m* S {5 N0 }
9 O3 B# o. w/ u* E
0 q/ T: n, W+ I- s# D q' i& a1 {( d; @
然后编译及下载程序:
& q4 }5 p2 q" E' k& y5 L5 W, V {5 f% \" h
8 p2 U+ J* k3 w+ t- E u. B
/ G0 q& A+ j3 h5 i5 A5 F) i2 J* j) { 打开串口工具,连接上对于串口,点击开发板上的重置按钮(黑色按钮),串口日志输出如下,日志显示BLE模块启动成功:0 l( C/ d0 H3 e
v: _$ O6 q3 Q: g; S
" {: ^! o* }9 m6 I& i) }6 a. H
- r, [; B \2 Q. K/ l! ~5 G5 G9 N( ?
% L3 |$ c" P8 _. N5 S8 z+ I; O6 S4 g
四、数据采集0 f. Z" s2 U$ Z$ @
4.1 STBLE-Sensor软件下载安装
0 x0 ?; ~) u/ i* h8 L 确保手机支持低功耗蓝牙通信,进入ST的BLE传感器应用下载页面,
, I& y+ x8 |2 C% p
3 U8 n3 q" s3 l 下载对应的手机应用程序:6 ~" ~) b+ {5 q8 u, F* }2 T j$ B
, |" g" g9 S, A+ I4 J+ d. D: o
q4 u+ ]5 t. i
; Y! i: ~5 M# q# \0 ~! `' j4 [4 h
4.2 STBLESensor配置数据采集; t: ^) S) I3 j4 N' A+ t6 [
本文用的是华为手机及android系统,安装完成APP后启动进入界面(当前版本是4.14),点击搜索,得到AI_Test蓝牙服务名。6 {* [0 T- {+ X' Z# E) j# _% F
4 \5 ?& k2 |7 O% A1 {
0 N2 k5 ^1 A4 m# @3 [9 k* g& M
$ s1 |7 g4 u9 Y/ H8 d: i S$ ?. @: e3 i
选择AI_Test蓝牙服务后,进入页面,(android)在左上角菜单下拉菜单选择,Data Log(sensing1),进入数据采集页面,选择Accelerometer(三轴加速度计),并设置参数为1.0Hz、26Hz、1.0X。' R7 g$ C. g! y
, W+ U( l% [, v; n/ F V: I; d
- u7 y9 ^8 j. F \) y' C- ?# z
9 W+ N- |) X& V
5 ^" P2 G8 q; z7 E
在数据记录操作页面,先新建标签,例如Jogging(慢跑),Walking(走了),Stationary(静立)等等。+ ], E- J9 D. x. m
1)开启数据采集记录时:先打开标签,再点击START LOGGING按钮开启& c3 ~0 g$ U" e& A7 |* u6 O. Q
2)关闭数据采集记录时,先点击START LOGGING按钮关闭,再关闭标签。3 I: C9 e' X8 f+ i* n' z
+ O+ m0 a! e+ F
% b# n2 f& {9 i4 C, X1 L! N& H
8 Y3 v/ B3 ?. p2 X, a% I. q; Y 例如,本文按上述操作记录了Walking、Jogging两次数据记录,将生成两个.csv文件。
. X6 l) N% D1 {! w8 S
( `1 S! v# `" I' L: _% ~$ E
! l2 C/ I0 W% W e# K: I5 ^五、数据治理及模型训练6 h2 P( y8 K6 F) Z2 S# J
5.1 从开发板取出采集记录数据文件5 N1 g8 r/ b) G
断掉开发板与电脑的USB连接,在开发板背面将在1-2跳线帽拔掉,插入5-6跳线,然后USB连接从ST-LINK连接转到USB-OTG接口接线,如下图(1->2)。
7 o [6 ^: t* A
" D% D6 D! }( @
( x5 ~/ `. y1 S& Q. e0 M2 n
: v& _- Z5 z4 |+ G5 C9 m3 U' v 开发板重新上电后,保持按下user按钮(蓝色),同时按下reset按钮(黑色),然后先松开reset按钮,在松开user按钮,激活USB-OTG。; q( S. N7 f6 ^6 h. p( }1 ~
, R0 L5 F5 \8 S# p: y7 H
( Y' S; s7 \2 ]! s5 A9 s
) H, d# k2 O& W USB-OTG激活后,开发板将作为一个U盘显示在电脑上,里面有刚才数据采集保存的CSV文件。
7 B6 `* t( ]( b) ~( v% \: z
) a/ k5 s5 b7 _" ^& r2 v
6 d: q3 U' h7 X+ r. f+ j
, s1 N1 h+ i: w+ i6 Q
4 z! x% C8 H# q+ X4 [5 f0 s 在“STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR”目录创建一个文件目录Log_data,将该文件拷贝到该目录下:. q' y2 Q2 R4 `' \5 k# O3 ^
S/ o) v4 I9 f, j+ [, I" G0 g- K
, E9 A& h" m; d9 Z
; z7 ~1 p4 {1 w' a
; m5 S$ b* w3 k: ?' g$ W
! w/ R) m# S/ V7 ~ f
该CSV记录数据格式如下,时间、行为、三个传感数值:
, B* c- [6 W, s; ~& y* d+ I( z8 {( K0 s$ j" d! h* e9 [. N0 D# V
8 L% z. K# r" T
; p2 ]1 x% T2 }0 a1 Q1 n/ J2 L8 {) a, B/ V0 t; F0 t4 }
5.2 神经网络模型训练0 ^ W! q$ k b h
“Training Scripts\HAR ”是官方提供的一个人类行为姿态识别训练项目,默认是采用,采用Keras前端+tensorflow后端实现。先安装tensorflow、Keras等支持。; H9 c1 f+ H" |4 C
+ H2 B7 I4 J3 [# G
本文安装如下:/ U: [/ ^: E( R0 A- E( E, c, |; h
- #已安装python3.6( D" J* b$ L, u0 U. b2 y; H3 z m
- pip3 install tensorflow==1.14 -i
# C0 m. F6 }+ P8 p3 v) h - ; U3 R: {/ c# m: l
- ERROR: tensorboard 1.14.0 has requirement setuptools>=41.0.0, but you'll have setuptools 28.8.0 which is incompatible.
3 T2 U6 k% _6 ?, w- g( B7 I - python3 -m pip install --upgrade pip -i
_% ~! L; o4 q* I1 R @* M - pip3 install keras==2.2.4 -i
复制代码
# a+ I5 L' X1 k( b/ L 根据HAR 项目的readme.txt通过pip install -r requirements.txt命令安装requirements.txt文件制定的相关模块,但本文是采用常用命令逐个安装各个模块的“pip3 install 模块名==版本 -i 源”& i+ c! U: A) D0 c) Z9 _% y; V
- numpy==1.16.4) n/ J u2 j: [/ ?6 K# n: \
- argparse
) q$ X5 o$ s8 o5 P - os: i4 p5 M% S9 S: e2 E
- logging
' j2 u, w! I1 k2 S4 U: X) { - warnings2 f3 G w7 {! Q: E
- datetime
! C+ `) M; m6 f2 @3 f# O1 ` - pandas==0.25.1
# T/ m1 m- S9 d# I/ B - scipy==1.3.1, Q7 d" F) N* t# [' u# `) b
- matplotlib==3.1.1
9 w$ u* t9 v! A/ [ - mpl_toolkits
' W2 c$ K! }- _- d# D1 ?1 [ - sklearn-learn==0.21.3
; T8 S4 {. U! u( s( U - keras==2.2.46 p2 e+ \5 s) \: Q1 g
- tensorflow==1.14.0
9 i( g, O j2 r- X - tqdm==4.36.13 Y1 r3 B' G4 n8 Z
- keras-tqdm==2.0.1
复制代码 ' |3 f! S2 ^) |$ I6 T$ V: i5 Y$ K
完成安装后,进入datasets目录,打开ReplaceWithWISDMDataset.txt文件,根据其提供的网址去下载. G, |% n- D! g, m- `& S: t
' b8 [/ A4 b; ^- C1 Y
# ~2 z9 [# f; I0 o' K
- K0 e; i1 C* l; Q8 s6 f4 g' T5 X 下载WISDM实验室的数据集支持。$ g6 B. F# t' x5 c6 {
# |/ C: S% Q! a; y( s% W
; b: d" g8 V( H6 t: S6 P8 Y: J) S! l$ @
下载文件如下,将这些文件拷贝到datasets目录下覆盖。; \/ ~/ d1 u+ h3 U5 p4 Z/ N: h$ b
( Q" O# Y8 F4 P, n
# e* |7 U/ Z& K2 Z: K2 t7 N* M
% q/ z: n7 J) A; y- V
打开RunMe.py文件,可以看到关于各个运行参数的设置:# Q5 a/ p6 P1 h
9 E. d& ], E. A q
9 d. C! T5 d2 t k! l% _3 l
9 `8 e6 |0 Y0 J) c& I: i: M
运行python3 .\RunMe.py -h命令,查看运行参数含义,其中:--dataset使用的是前面下载的WISDM实验室的数据集来训练模型,而--dataDir是指定采用自行采集的数据集训练模型:
" s' s) {/ w+ C) r- PS D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR> python3 .\RunMe.py -h+ G8 J7 b# _. D5 r @
- Using TensorFlow backend.
/ I. j+ {4 d+ ]# `2 P - usage: RunMe.py [-h] [--model MODEL] [--dataset DATASET] [--dataDir DATADIR]% h9 ?' |& X/ v/ Z. R+ t; v
- [--seqLength SEQLENGTH] [--stepSize STEPSIZE] [-m MERGE]
3 M, H8 @$ e0 P - [--preprocessing PREPROCESSING] [--trainSplit TRAINSPLIT]7 m7 i* K4 l$ V" H$ n% L
- [--validSplit VALIDSPLIT] [--epochs N] [--lr LR]
5 m" J- C r, t8 {" s - [--decay DECAY] [--batchSize N] [--verbose N]
5 U% M/ q! X; Z8 s9 m - [--nrSamplesPostValid NRSAMPLESPOSTVALID]
+ y! u' k: o9 P, U6 d$ L -
1 p4 t) I: y; d# P - Human Activity Recognition (HAR) in Keras with Tensorflow as backend on WISDM% s/ B; q6 E& F+ m0 r9 L
- and WISDM + self logged datasets- j8 K; \ P# P/ B& y/ L1 z
-
* h$ ^; O N3 Q+ ^) `" } - optional arguments:
) G5 ?5 e- j) G E - -h, --help show this help message and exit1 F. p+ u3 k# G
- --model MODEL choose one of the two availavle choices, IGN or GMP, (+ h6 L/ w9 X; {$ n
- default = IGN )) Z' M; M7 ~! @) @2 u2 U* C
- --dataset DATASET choose a dataset to use out of two choices, WISDM or. K$ K8 B3 p: y( D# p+ u
- AST, ( default = WISDM )
/ M% d' v, P' g - --dataDir DATADIR path to new data collected using STM32 IoT board7 P& u# e9 g8 N+ O$ c4 G
- recorded at 26Hz as sampling rate, (default = )& _9 g5 s5 p9 G) H
- --seqLength SEQLENGTH
# n D# S7 Q+ X1 d% y6 c - input sequence lenght (default:24)
0 A6 a' u$ A5 ^ - --stepSize STEPSIZE step size while creating segments (default:24, equal6 d$ @9 x# ~- ?& r
- to seqLen)) U* N+ R* G5 i. A+ L2 G }
- -m MERGE, --merge MERGE# x) W8 F6 ?/ Y9 N
- if to merge activities (default: True)
3 y, Z/ ?2 Y: S: l5 z - --preprocessing PREPROCESSING
. f: h: @: ?. Q1 X9 W$ l# I' W |4 l - gravity rotation filter application (default = True)
) D' P0 c# B1 M& J( k5 i2 A8 H - --trainSplit TRAINSPLIT
; A' F& m3 g1 R - train and test split (default = 0.6 (60 precent for# `% O* _/ N$ |9 @3 S
- train and 40 precent for test))+ x. _1 H3 X ?1 j
- --validSplit VALIDSPLIT ?) _' k4 _8 w2 K( f ?
- train and validation data split (default = 0.7 (70
! V/ X6 Q4 g! W - percent for train and 30 precent for validation))
) J4 k) N$ P' _+ F4 I; R% z. W - --epochs N number of total epochs to run (default: 20)
7 ?& L$ L$ B/ {2 ?0 O - --lr LR initial learning rate# c% f1 ]6 f' j
- --decay DECAY decay in learning rate, (default = 1e-6)5 x6 X: u# k' x2 t t2 y# {$ @' o
- --batchSize N mini-batch size (default: 64)
' \2 n* k3 H4 m0 ~# V! [3 S. b. v - --verbose N verbosity of training and test functions in keras, 0,6 q& C0 l) Q/ c9 @. ^# \
- 1, or 2. Verbosity mode. 0 = silent, 1 = progress bar,5 a4 z) ?$ I( E+ s
- 2 = one line per epoch (default: 1)
+ l5 G6 v4 q# J( z- B - --nrSamplesPostValid NRSAMPLESPOSTVALID
& ?2 x8 {7 O) d M; p2 U* d - Number of samples to save from every class for post5 V8 i% |0 M1 o" C# n
- training and CubeAI conversion validation. (default =) J3 Z, h. K# d, L3 D5 x
- 2)$ X8 `1 @3 G) i0 N9 n0 z6 @
- PS D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR>
复制代码 , b* K5 ^/ j0 p O, J7 } f! Q
在RunMe.py文件后面加入下面语句:. [1 V; N/ |7 @6 J5 `
- #保存Cube.AI支持的数据集格式,用于后续验证测试使用+ Y7 X7 @4 q& S* {. o% ]7 K0 J+ d
- testx_f=resultDirName+"testx.npy"$ r j& c2 B0 v5 f5 U6 f( k
- testy_f=resultDirName+"testy.npy"
. l0 [! u( h+ s! H - # Z6 T* W- U. }
- np.save(testx_f,TestX)/ b9 s. c# A) e. @8 m
- np.save(testy_f,TestY)
复制代码 8 Z- B4 t9 @5 l, r8 a! O* P
打开命令工具,输入命令python3 .\RunMe.py --dataDir=Log_data ,可以根据实际需要进行参数设置,本文先采用默认参数训练模型,输出日志如下,这显然是一个分类问题,分类为Jogging 、Stationary 、Stairs 、Walking,有卷积层、池化层、2全连接层、压平层、Dropout层等。
" B3 i7 E: c& l }4 R' c9 }; t- PS D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR> python3 .\RunMe.py --dataDir=Log_data' B( U- z* U- M* s6 J6 [
- Using TensorFlow backend.! @% i; X/ [/ ?1 b5 P& _5 ^' P
- Running HAR on WISDM dataset, with following variables
3 k* m" Q# V* q5 ^# Q! O - merge = True+ @& ]3 r6 u5 R5 D
- modelName = IGN,3 M9 e! n! h2 x, r& `4 v
- segmentLength = 247 ]7 V( }% T* l2 U( j
- stepSize = 24* B5 u0 Z, r& D3 X; G( }- b- z. F
- preprocessing = True% U. m8 ^7 m7 W* ^7 r* W7 z. |) T t& |
- trainTestSplit = 0.6" q& `+ O7 h6 d0 s' {
- trainValidationSplit = 0.7
7 t# e# a8 r) R9 v0 Q - nEpochs = 209 ]% q% r2 m) V7 y/ z% p
- learningRate = 0.0005
: R* U3 C: B& X9 Z, c - decay =1e-06
0 b! F. u% x, f% j; t% Z - batchSize = 64
6 c, l( J7 a0 A- R" j. U3 E h - verbosity = 1/ Z, @( H$ W3 y0 Q. A7 t
- dataDir = Log_data
% q0 u! R {: e2 d* I - nrSamplesPostValid = 2. i" |8 N/ T' m9 B
- Segmenting Train data: b$ J5 }6 E+ ^/ z9 F" P; ^9 Z% h
- Segments built : 100%|███████████████████████████████████████████████████| 27456/27456 [00:28<00:00, 953.24 segments/s]
! |/ n+ P1 E2 ]& \- H( h) f; N - Segmenting Test data1 v& w2 g* }) l/ l' p
- Segments built : 100%|██████████████████████████████████████████████████| 18304/18304 [00:14<00:00, 1282.96 segments/s]- ^; ]& f5 ~5 G; F2 y
- Segmentation finished!: Z) L% }- n' h) A
- preparing data file from all the files in directory Log_data- b" w0 q) P8 n, c8 Z+ [- _$ I! D, i
- parsing data from IoT01-MemsAnn_11_Jan_23_16h_57m_17s.csv
7 K0 R# O3 L% Z' {8 Q+ Z9 h - parsing data from IoT01-MemsAnn_11_Jan_23_16h_57m_53s.csv
I& v- h. R! F# T9 T4 ?" t - Segmenting the AI logged Train data3 m! j. b5 B2 G& V
- Segments built : 100%|████████████████████████████████████████████████████████| 25/25 [00:00<00:00, 3133.35 segments/s]$ ]/ ~$ [* I, t
- Segmenting the AI logged Test data% b1 Y* Z( O3 ?$ q: f
- Segments built : 100%|████████████████████████████████████████████████████████| 17/17 [00:00<00:00, 2852.35 segments/s]) ?4 T: U/ h* L8 q
- Segmentation finished!
9 }- j; @- z4 A9 f, _ - _________________________________________________________________, y4 f0 }3 ?; p2 @0 L- _1 a! O
- Layer (type) Output Shape Param #
9 m5 U+ P6 S. w4 R - =================================================================6 {* l! {; @$ U7 B
- conv2d_1 (Conv2D) (None, 9, 3, 24) 408
& O% v4 m6 i9 Y' \ - _________________________________________________________________" o3 `$ Q u) i. O: ^
- max_pooling2d_1 (MaxPooling2 (None, 3, 3, 24) 0) E3 _' V# U ~4 }' T
- _________________________________________________________________4 z8 Y" b {! P
- flatten_1 (Flatten) (None, 216) 0
/ v+ J0 e; F; e$ ]0 I/ M7 _ - _________________________________________________________________3 P4 n& z, {! i1 G2 w/ r7 @) U: q
- dense_1 (Dense) (None, 12) 2604
4 g1 _4 g) O# L5 d0 H - _________________________________________________________________& l& k$ {5 l. g2 Y
- dropout_1 (Dropout) (None, 12) 06 x/ b! Q- G5 g' p3 N
- _________________________________________________________________, o) F1 a1 ~% L; {+ Y
- dense_2 (Dense) (None, 4) 52) m- R% ?5 `, t, |) Q' R% d
- =================================================================: H# s- ?) L# n* j& c$ b
- Total params: 3,064 }- K5 U( X% R( F
- Trainable params: 3,0642 v( b* @: b9 z
- Non-trainable params: 0! f" S! {' `) o7 x+ B
- _________________________________________________________________: B9 g9 i/ R: \( J
- Train on 19263 samples, validate on 8216 samples
% i3 k% `1 T+ K0 n: c6 ` - Epoch 1/207 Q0 [& f3 u; n% r K2 l
- 2023-01-24 14:41:03.484083: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
) J9 W" ?! @0 q) Z, y# j% a - 19263/19263 [==============================] - 3s 167us/step - loss: 1.1442 - acc: 0.5430 - val_loss: 0.6674 - val_acc: 0.7372
" a/ z6 }4 a5 d8 y2 t; W - Epoch 2/206 a( o! A, i1 Z" c9 o
- 19263/19263 [==============================] - 1s 40us/step - loss: 0.7173 - acc: 0.7089 - val_loss: 0.5126 - val_acc: 0.7928$ V/ z& V9 H- s. O' Y
- Epoch 3/20
" x. J2 d5 S2 K' v8 j1 @; ~8 x3 E - 19263/19263 [==============================] - 1s 40us/step - loss: 0.5954 - acc: 0.7522 - val_loss: 0.4470 - val_acc: 0.8051
& |1 X" `8 k3 ?) ] - Epoch 4/20
* P I. ~' i# I$ Y - 19263/19263 [==============================] - 1s 39us/step - loss: 0.5288 - acc: 0.7810 - val_loss: 0.4174 - val_acc: 0.8335 ?4 _& k+ Q. t- [/ Z$ |+ w% Y
- Epoch 5/20 H/ }! a [9 s- Q
- 19263/19263 [==============================] - 1s 36us/step - loss: 0.4925 - acc: 0.7994 - val_loss: 0.3897 - val_acc: 0.84779 j9 S: B' x5 G0 g* o) U
- Epoch 6/20; G: L9 D6 n. x N, F
- 19263/19263 [==============================] - 1s 35us/step - loss: 0.4647 - acc: 0.8173 - val_loss: 0.3607 - val_acc: 0.8647
2 v [) s. W* H& ?7 t8 u) \ - Epoch 7/20/ O+ p: O: R l& @9 A, j
- 19263/19263 [==============================] - 1s 37us/step - loss: 0.4404 - acc: 0.8301 - val_loss: 0.3493 - val_acc: 0.8777
( K* ^9 L- h9 j: ~$ W - Epoch 8/203 a6 T2 x, @9 n, G$ \/ q3 P) i
- 19263/19263 [==============================] - 1s 38us/step - loss: 0.4200 - acc: 0.8419 - val_loss: 0.3271 - val_acc: 0.88272 C+ s4 c6 f2 u" P& K n. A
- Epoch 9/20
5 \) k- \6 B. u! Q" D9 a - 19263/19263 [==============================] - 1s 38us/step - loss: 0.3992 - acc: 0.8537 - val_loss: 0.3163 - val_acc: 0.8890' r$ M% w( W+ W: X
- Epoch 10/20
- D) G* }" H* f% R3 A. u0 z# M - 19263/19263 [==============================] - 1s 40us/step - loss: 0.3878 - acc: 0.8576 - val_loss: 0.3039 - val_acc: 0.89916 c' f$ U! _1 b- e5 Z
- Epoch 11/20! T$ F5 f! F' L, j
- 19263/19263 [==============================] - 1s 40us/step - loss: 0.3799 - acc: 0.8667 - val_loss: 0.2983 - val_acc: 0.8985
5 v5 |1 q- J9 b* J6 z' I* a - Epoch 12/20
- b1 H/ z0 p0 |) r( S& L5 \ - 19263/19263 [==============================] - 1s 40us/step - loss: 0.3662 - acc: 0.8736 - val_loss: 0.2922 - val_acc: 0.90074 b: s; [& z6 C2 k% S, b2 K8 e$ M
- Epoch 13/20$ `6 B# e! X+ ~& e$ k& `
- 19263/19263 [==============================] - 1s 36us/step - loss: 0.3613 - acc: 0.8760 - val_loss: 0.2837 - val_acc: 0.9051, D5 E2 n. F. f
- Epoch 14/204 L7 T& k$ i+ o6 R' _; l
- 19263/19263 [==============================] - 1s 40us/step - loss: 0.3574 - acc: 0.8775 - val_loss: 0.2910 - val_acc: 0.8985
9 `- q' W; N: H7 }! V5 y* i - Epoch 15/20
7 Q6 s) f5 R, f- ?" x7 z U: ? - 19263/19263 [==============================] - 1s 39us/step - loss: 0.3513 - acc: 0.8796 - val_loss: 0.2814 - val_acc: 0.90806 Z: A$ a) o) j! A' k& U" N
- Epoch 16/20 ?+ e. L6 V- `; d( b- ?
- 19263/19263 [==============================] - 1s 38us/step - loss: 0.3482 - acc: 0.8816 - val_loss: 0.2737 - val_acc: 0.91166 @4 R: T) D/ F. t
- Epoch 17/20
' ~6 ]1 T% M! W( I' {$ s - 19263/19263 [==============================] - 1s 35us/step - loss: 0.3362 - acc: 0.8875 - val_loss: 0.2742 - val_acc: 0.9114
( [% ~0 U% R& |8 n( ?* h, c - Epoch 18/20' O# l( k( B L! y
- 19263/19263 [==============================] - 1s 38us/step - loss: 0.3325 - acc: 0.8892 - val_loss: 0.2661 - val_acc: 0.9137" L Y0 u' U4 X3 r0 Y( t
- Epoch 19/20
4 ]" k, j% u: F& K6 R! e - 19263/19263 [==============================] - 1s 40us/step - loss: 0.3257 - acc: 0.8927 - val_loss: 0.2621 - val_acc: 0.9161
+ V% S% ^7 T U( z n: f$ p - Epoch 20/206 p d( M1 F" B! n/ Y" a) c G+ n
- 19263/19263 [==============================] - 1s 37us/step - loss: 0.3249 - acc: 0.8918 - val_loss: 0.2613 - val_acc: 0.9188* T: Q! [! |+ I$ g; L% `( I9 s
- 12806/12806 [==============================] - 0s 25us/step
9 k8 r7 E/ J- t3 J3 [$ y0 D* W - Accuracy for each class is given below.
3 N6 J% J! V: b0 }' \" R - Jogging : 97.28 %' w( N' b4 N0 O2 U) v; h
- Stationary : 98.77 %
6 F* r6 G6 J7 h n - Stairs : 66.33 %
* _5 w, m3 R+ X. }" V - Walking : 87.49 %
- @ S; R: l! i$ I1 ~9 ~0 a' Y - PS D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR>
复制代码
6 a, T3 o9 B. y5 F6 \7 e 训练模型及相关输出信息在results目录下,每次训练输出依据时间生成一个独立目录,由于是keras训练模型,因此输出训练模型是一个名为*.h5格式文件,例如har_IGN.h5:
% C0 x! i7 `, T. W4 b' T# _# q
; t& g! m0 y- h& D' ?5 P2 D
: H* k" {3 r% Y/ l4 R% u
* h n, ?$ M" J* m5 Z- R+ R5 K
$ q* c# e& y0 ~& N; J+ n
% J a! e4 C3 N; m
4 B$ A; x5 d% \4 M4 c% S
六、 cube.AI将训练模型转换为c语言模型' R" J0 a; @6 w9 c; N; C$ x* s
6.1 创建cube.AI支持的STM32工程
, a* O0 s0 s0 b% ] 在CubeIDE中新建一个STM32项目,在cubeMX中选择以开发板形式创建 W# Z) W0 ]0 d n, ~5 x1 F4 b) H; w* A
* y) }* [( f+ `& R! ^2 {7 D8 b# s
0 }: m7 H) s; z" K9 r. R
7 o0 U0 t5 f2 { 创建一个B-L475E-IOT01A_cube.ai工程名的STM32工程,如下图。: u$ I% x I: b' O/ x& B# h
; [6 b/ }2 V- e# ^, l+ U
% Z2 {0 I' t; t
# c6 C/ e* p% J! V1 a0 }+ ?! s
完成创建后,双击.ioc文件打开cube.MX配置界面。
. M5 k% E: }# A2 u; u
]7 |, D6 n8 {" e- r' Z0 \
) S' r j; Y! x4 g0 N5 [- Y4 W$ E7 R' H
' T* s( ]# E- A" ~( b, a5 ~, U4 [" p 6.2 cube.AI神经网络配置
@7 z$ X8 }( A 选择X-CUBE-AI包支持,回到主页面后,会多出software Packs栏及多出STMicroelectronics .X-CUBE-AI选项,进入该页面,勾选下图标识的2、3 项,在5中选择采用哪个串口支持程序及调试。+ L2 @% A4 l S/ `
' Y- w! q1 x T c
0 T; Z# `: J3 N3 h
' ]( @1 s) Z# T
知识点,在X-CUBE-AI配置选项页面,停靠时,会出现说明框,快捷键“CTRL+D”会进一步出现X-CUBE-AI相关文档,9 P# X& G8 z. s% T( z
' F. V& }5 W' _
9 R7 I. C- y0 ]8 l4 A" t# G" G% u2 v
有详细的文档资料:) G2 n% A5 \& d- d3 _
4 W: g% s: M+ k3 C; C$ e3 [
! m) \$ p9 V1 T& r5 P; e1 Z4 P! v7 N& x
或者也可以从cube.AI安装目录直接进入,例如:D:\workForSoftware\STM32CubeMX\Repository\Packs\STMicroelectronics\X-CUBE-AI\7.3.0\Documentation
* T( T- ^* x {/ H7 v7 z5 d0 _, n( n6 H. K$ _
另外,需要注意,开启X-CUBE-AI支持后,其依赖CRC功能,会自动开启CRC。, K% a/ e1 B! s1 Y, k
2 a, P; g" a! a! N z. a3 U
4 }( O6 Y, ^1 y- @4 d: W+ h* q" h
4 g: a" e% C' K( s) A) h4 u" A2 ? 6.3 模型分析与PC端验证/ F* e% k% V) y# \- j" T/ R$ N+ r
添加(add network)神经网络如下,在3中可以修改神经网络模型名称,在4中选择支持框架及选择模型文件,例如“STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR\results\2023_Jan_24_14_40_13\har_IGN.h5”,在5、6中,可以选择随机数据进行模型研制,也可以选择生成的研制数据进行验证(前面训练模型时,在RunMe.py文件后面加入语句,输出testx.npy、testy.npy文件):
; F; P2 H W9 ~6 e. i, l5 l0 H" |
! ]' O5 H1 e* L: z0 d' i& O
q O# E9 @; [$ U. V$ A7 Q 可以点击设置按钮进入,在该页面下可以对神经网络进行更多设置和更详细信息查看,主要是以模型优化为主,本文先保持默认。
; G! [8 @% A! b- \" p- j( T/ K' R
9 t" O( j+ U" H* J* \: A
7 a: |$ G( ]# D
点击分析按钮(Analyze),输出该模型相关信息及部署模型需要的计算资源(ram、flash等):
& g& g. H% l0 P1 @5 D, E! L
7 `+ X$ e# z k) v- p5 n7 l. y) k J-
9 O& o' t! u4 k# Q: P7 @$ m - Analyzing model ' a& c! ?* d$ E6 Z6 m- ]! H: `: `8 y
- D:/workForSoftware/STM32CubeMX/Repository/Packs/STMicroelectronics/X-CUBE-AI/7.3.0/Utilities/windows/stm32ai analyze --name har_ign -m D:/tools/arm_tool/STM32CubeIDE/STM32CubeFunctionPack_SENSING1_V4.0.3/Utilities/AI_Ressources/Training Scripts/HAR/results/2023_Jan_11_17_50_03/har_IGN.h5 --type keras --compression none --verbosity 1 --workspace C:\Users\py_hp\AppData\Local\Temp\mxAI_workspace465785871649500151581099545474794 --output C:\Users\py_hp\.stm32cubemx\network_output --allocate-inputs --allocate-outputs
5 p1 D; w& N$ ^" t. k y - Neural Network Tools for STM32AI v1.6.0 (STM.ai v7.3.0-RC5) 6 ?) u0 u; d. B a6 a
- 1 z" Y2 {+ c& e( s' U1 U
- Exec/report summary (analyze)
6 K9 ?# [ s; z7 W - ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0 g( [8 W0 h0 [ - model file : D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR\results\2023_Jan_11_17_50_03\har_IGN.h5
8 m, U8 c- `: n7 b( q. x - type : keras
4 ]; D1 r8 M1 B2 T# D) z - c_name : har_ign
. Z" k* W% M1 }5 {, E3 E5 @6 w6 n - compression : none
$ a6 a. t% d$ h$ {0 N' ?6 u. S - options : allocate-inputs, allocate-outputs
% _$ c3 U3 l) h" }2 P$ E7 S3 i - optimization : balanced : H& x1 c! E) |) V
- target/series : generic 2 Z0 S6 [- I0 S- ?3 ~1 i
- workspace dir : C:\Users\py_hp\AppData\Local\Temp\mxAI_workspace465785871649500151581099545474794 % K6 D) ?6 K' r% ^
- output dir : C:\Users\py_hp\.stm32cubemx\network_output / E2 W2 E# p* Y- H( ?( q( p0 |
- model_fmt : float , S9 B9 O& D5 C- K
- model_name : har_IGN
( n) b0 E. E0 T2 p - model_hash : ff0080dbe395a3d8fd3f63243d2326d5
: |- a& w5 P c" w+ L - params # : 3,064 items (11.97 KiB) 9 M' [0 P8 ~6 v* v X
- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4 i: O0 Y* n; h/ U" z- L7 k - input 1/1 : 'input_0' (domain:activations/**default**) : {" m9 j0 U0 d& u& H6 r
- : 72 items, 288 B, ai_float, float, (1,24,3,1)
0 T, g$ ^- U9 J2 P6 `& [) ^$ q2 Q - output 1/1 : 'dense_2' (domain:activations/**default**)
3 X* E/ i6 o0 K! b0 W) Z6 f - : 4 items, 16 B, ai_float, float, (1,1,1,4)
& q$ `* [7 r, X( r! n& X: g5 v M* A - macc : 14,404 0 r! Q: h5 w5 x! {
- weights (ro) : 12,256 B (11.97 KiB) (1 segment) . m3 g; T N- o/ ^' v7 s# U
- activations (rw) : 2,016 B (1.97 KiB) (1 segment) *
0 V$ K2 z5 v5 W: K - ram (total) : 2,016 B (1.97 KiB) = 2,016 + 0 + 0 9 H/ L9 i4 s: e& E& X
- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7 w/ t4 \' [. M - (*) 'input'/'output' buffers can be used from the activations buffer 1 a, q. q/ B! @. u5 X' Z
- 0 v" q* u6 q4 J& S
- Model name - har_IGN ['input_0'] ['dense_2'] 0 p# h' o& b f! n+ S* z. _
- ------------------------------------------------------------------------------------------------------
& y9 T6 G3 L6 S$ A% G3 v K - id layer (original) oshape param/size macc connected to 1 C7 c5 P) z4 E; E. [
- ------------------------------------------------------------------------------------------------------ $ w$ q/ K2 A5 a$ X
- 0 input_0 (None) [b:None,h:24,w:3,c:1]
- q/ e, d" f& ~% a: u. m3 C - conv2d_1_conv2d (Conv2D) [b:None,h:9,w:3,c:24] 408/1,632 10,392 input_0 9 f1 I6 d7 U h7 ?. x6 D
- conv2d_1 (Conv2D) [b:None,h:9,w:3,c:24] 648 conv2d_1_conv2d
1 ^) |) [2 S ?" } - ------------------------------------------------------------------------------------------------------ ! r3 \( p! Z* |! S( V' L
- 1 max_pooling2d_1 (MaxPooling2D) [b:None,h:3,w:3,c:24] 648 conv2d_1 ( \* B! d: K2 m0 ?& I- z- v
- ------------------------------------------------------------------------------------------------------ : l8 ]5 p5 j( X! ~4 _+ \
- 2 flatten_1 (Flatten) [b:None,c:216] max_pooling2d_1
8 t. m% X, j+ R! j/ ]) C* A& \ - ------------------------------------------------------------------------------------------------------ 7 [5 K+ [2 M' A0 _! t
- 3 dense_1_dense (Dense) [b:None,c:12] 2,604/10,416 2,604 flatten_1 2 \: {0 i& g+ S6 W$ {
- ------------------------------------------------------------------------------------------------------ ) X/ F+ j' F" {% r" W
- 5 dense_2_dense (Dense) [b:None,c:4] 52/208 52 dense_1_dense
' W5 t6 U0 J+ B N - dense_2 (Dense) [b:None,c:4] 60 dense_2_dense
& N& O& B1 Z: k4 n - ------------------------------------------------------------------------------------------------------
5 t( y4 G6 u/ e+ a: Y' q - model/c-model: macc=14,404/14,404 weights=12,256/12,256 activations=--/2,016 io=--/0 ; N9 P8 [* N# j: X/ H. S
- ' m4 l' v6 e" a3 V8 [! T+ V8 ]( x B
- Number of operations per c-layer : P, [( ~& W; ^! }- Q0 b/ C- z, M {4 |
- ----------------------------------------------------------------------------------- % y% S( v4 M$ u- |& N& D- S
- c_id m_id name (type) #op (type)
6 U1 x# K; `0 x. z+ P - -----------------------------------------------------------------------------------
, ` H$ }6 o$ X' N$ G8 K# f* v& \ - 0 1 conv2d_1_conv2d (optimized_conv2d) 11,688 (smul_f32_f32) 0 b7 ?9 ~0 B( M1 y
- 1 3 dense_1_dense (dense) 2,604 (smul_f32_f32)
5 @$ C0 y# [# o( W, Z* ^ - 2 5 dense_2_dense (dense) 52 (smul_f32_f32)
% K! ^( h9 ^1 E" N) X2 ]/ ] - 3 5 dense_2 (nl) 60 (op_f32_f32) + J* v+ U% P! y: }" |6 |# c
- -----------------------------------------------------------------------------------
: Z6 S# `( h4 |# E; k% i - total 14,404
7 t6 f) c1 q: \+ g - 5 h3 b! C. r. l$ u+ W! }1 q& g
- Number of operation types 5 `$ Y3 F2 z8 B& L3 E' i
- ---------------------------------------------
" @" Q: j# r$ [, X8 ]" \ w7 d - smul_f32_f32 14,344 99.6% * W& ^( l' u) a* O# l- I0 N, @
- op_f32_f32 60 0.4%
" ^$ k$ k4 h* p; V( @ - - a+ d3 {$ G! w: d6 |) Q, t7 G
- Complexity report (model)
5 ]- @( `; j3 O i - ------------------------------------------------------------------------------------ 3 ^2 G& R: M" S) f. ?, Y
- m_id name c_macc c_rom c_id
1 b" u" r3 d. L8 s! z) C, B) g - ------------------------------------------------------------------------------------ ' h+ F8 P! h( D8 Z, x
- 1 max_pooling2d_1 |||||||||||||||| 81.1% ||| 13.3% [0]
3 k$ {# g9 i1 u: p- E" o - 3 dense_1_dense |||| 18.1% |||||||||||||||| 85.0% [1] " S2 K2 k% J% a, [8 a
- 5 dense_2_dense | 0.8% | 1.7% [2, 3]
# L h. g' s0 I0 C G+ ~. i4 c - ------------------------------------------------------------------------------------
0 R, H. u. K( l- l! A' \% M2 B - macc=14,404 weights=12,256 act=2,016 ram_io=0
7 @6 P k* X' [ _0 A: t- C - Creating txt report file C:\Users\py_hp\.stm32cubemx\network_output\har_ign_analyze_report.txt 9 |$ w) h) Z5 Y
- elapsed time (analyze): 7.692s
( ^) h1 o2 R9 T+ z) h - Getting Flash and Ram size used by the library D9 o5 X3 {5 R1 _ D8 s* z+ g
- Model file: har_IGN.h5 " B" |/ O; D+ X5 [/ O. ~1 I
- Total Flash: 29880 B (29.18 KiB)
4 }6 j: x+ f8 k% } - Weights: 12256 B (11.97 KiB) 3 i. V2 k5 q( E' F2 X
- Library: 17624 B (17.21 KiB)
* v$ j3 J, \* J; R3 K+ A% X - Total Ram: 4000 B (3.91 KiB)
4 g& L& Z( c5 I - Activations: 2016 B (1.97 KiB)
: P% `9 I2 C! x# H: Q( Z; ]* I: b - Library: 1984 B (1.94 KiB) ; e- {$ I" j, \! V3 k* G
- Input: 288 B (included in Activations)
7 p E& H6 @5 _9 j& ]5 W - Output: 16 B (included in Activations) " z1 ]. D7 H7 |
- Done ) s& y, |. @4 w. O) \% i8 O6 i8 y# G% E+ n
- Analyze complete on AI model
复制代码 * e1 T% N3 a( D. \
点击PC桌面验证按钮(validation on desktop),对训练模型进行验证,主要是验证原始模型和转为c语言支持的模型时,验证前后计算资源、模型精度等差异情况,验证数据就是我们刚指定的testx.npy、testy.npy文件。
: x, v# P2 n7 G-
], a% e ^ B- G& n( H0 u$ x/ M - Starting AI validation on desktop with custom dataset : D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR\results\2023_Jan_24_14_40_13\testx.npy...
% A4 l! g: q6 j% h! a' M - D:/workForSoftware/STM32CubeMX/Repository/Packs/STMicroelectronics/X-CUBE-AI/7.3.0/Utilities/windows/stm32ai validate --name har_ign -m D:/tools/arm_tool/STM32CubeIDE/STM32CubeFunctionPack_SENSING1_V4.0.3/Utilities/AI_Ressources/Training Scripts/HAR/results/2023_Jan_11_17_50_03/har_IGN.h5 --type keras --compression none --verbosity 1 --workspace C:\Users\py_hp\AppData\Local\Temp\mxAI_workspace46601041973700012072836595678733048 --output C:\Users\py_hp\.stm32cubemx\network_output --allocate-inputs --allocate-outputs --valoutput D:/tools/arm_tool/STM32CubeIDE/STM32CubeFunctionPack_SENSING1_V4.0.3/Utilities/AI_Ressources/Training Scripts/HAR/results/2023_Jan_24_14_40_13/testy.npy --valinput D:/tools/arm_tool/STM32CubeIDE/STM32CubeFunctionPack_SENSING1_V4.0.3/Utilities/AI_Ressources/Training Scripts/HAR/results/2023_Jan_24_14_40_13/testx.npy
9 p' s7 [% O' I( A7 i# X - Neural Network Tools for STM32AI v1.6.0 (STM.ai v7.3.0-RC5) $ G* U& v; G4 c/ E& r2 S
- Copying the AI runtime files to the user workspace: C:\Users\py_hp\AppData\Local\Temp\mxAI_workspace46601041973700012072836595678733048\inspector_har_ign\workspace
& I9 @/ L* m( ? - 0 n$ M) _% c2 ?! R
- Exec/report summary (validate) % D' G/ t+ d" d) E
- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- G1 k/ \( l3 S! u# j+ |6 j/ s - model file : D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR\results\2023_Jan_11_17_50_03\har_IGN.h5
# H; P: f5 T1 v% ` - type : keras
' H7 n8 B9 l- L+ `9 r" K' k - c_name : har_ign
4 T% F# _* N8 v% [7 d - compression : none
4 {; l( A# e2 h( y - options : allocate-inputs, allocate-outputs 8 r+ e" h% `: z% I
- optimization : balanced 9 o. p" h* y2 t; ?) `9 x
- target/series : generic / n' p5 J- r6 B. y1 f
- workspace dir : C:\Users\py_hp\AppData\Local\Temp\mxAI_workspace46601041973700012072836595678733048
8 b2 V4 s1 Z: e K - output dir : C:\Users\py_hp\.stm32cubemx\network_output 1 c- Z5 M& L1 ^; v5 \% `
- vinput files : D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR\results\2023_Jan_24_14_40_13\testx.npy ) O. w; }. p$ b( ]7 o, a3 W/ ^7 L$ [
- voutput files : D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR\results\2023_Jan_24_14_40_13\testy.npy 6 B, Z, ~2 ? k7 |" s' L2 T
- model_fmt : float
4 G$ G4 T6 c7 H: t3 U - model_name : har_IGN " C8 c* Y3 K. t
- model_hash : ff0080dbe395a3d8fd3f63243d2326d5 8 D2 h; m. ]3 a9 l
- params # : 3,064 items (11.97 KiB)
$ a! @( W% | t i: o - --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 5 z/ t2 {+ q# A1 h; |+ ]! M' s
- input 1/1 : 'input_0' (domain:activations/**default**)
$ w0 K6 ~) T7 n9 Z" A5 B, | - : 72 items, 288 B, ai_float, float, (1,24,3,1)
2 l3 Z q) w5 h8 e& f' X - output 1/1 : 'dense_2' (domain:activations/**default**) & `. z/ W- w5 \) W: z4 m$ {0 A5 W3 P
- : 4 items, 16 B, ai_float, float, (1,1,1,4) + s, g9 P8 }* q- R* y: ^
- macc : 14,404
9 j' w) G2 ^3 I1 q - weights (ro) : 12,256 B (11.97 KiB) (1 segment)
' `8 n$ V$ H1 t4 V9 ] - activations (rw) : 2,016 B (1.97 KiB) (1 segment) *
" ]3 K q" p8 b- ]% ]1 J3 ]' N - ram (total) : 2,016 B (1.97 KiB) = 2,016 + 0 + 0
& M) r* l7 ^6 s6 I - ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 d' f. o" b; F4 y4 F( f( O, R - (*) 'input'/'output' buffers can be used from the activations buffer
& s; S( h. s- I. E0 B - Setting validation data... 4 N! X+ ~6 e7 W
- loading file: D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR\results\2023_Jan_24_14_40_13\testx.npy
( L% e7 R- _3 p0 N. g - - samples are reshaped: (12806, 24, 3, 1) -> (12806, 24, 3, 1) ! @# n+ B2 n0 `* \$ d! d' m8 A
- loading file: D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR\results\2023_Jan_24_14_40_13\testy.npy
. n+ c6 T9 U& t6 X2 j3 H - - samples are reshaped: (12806, 4) -> (12806, 1, 1, 4)
+ P0 R; ?" J! ?# X - I[1]: (12806, 24, 3, 1)/float32, min/max=[-26.319, 32.844], mean/std=[0.075, 5.034], input_0 - p1 n* r |, Z3 o$ O. B9 o
- O[1]: (12806, 1, 1, 4)/float32, min/max=[0.000, 1.000], mean/std=[0.250, 0.433], dense_2
8 D! R6 k+ q1 y - Running the STM AI c-model (AI RUNNER)...(name=har_ign, mode=x86) - `. M0 d9 k O5 h9 n
- X86 shared lib (C:\Users\py_hp\AppData\Local\Temp\mxAI_workspace46601041973700012072836595678733048\inspector_har_ign\workspace\lib\libai_har_ign.dll) ['har_ign'] 1 z5 C% f' Z) \4 f9 g
- Summary "har_ign" - ['har_ign']
" r$ q' r5 s! ] - --------------------------------------------------------------------------------
4 Z9 E- c+ v: H7 E: u/ N- j) n - inputs/outputs : 1/1 9 e' e2 Q. w6 I0 l/ ]. K
- input_1 : (1,24,3,1), float32, 288 bytes, in activations buffer
$ \1 @2 I+ p, g, q+ \$ v7 w - output_1 : (1,1,1,4), float32, 16 bytes, in activations buffer ) i9 B& N( z: ?1 N) p
- n_nodes : 4
! C. E0 T# x# k w - compile_datetime : Jan 25 2023 22:55:51 (Wed Jan 25 22:55:47 2023)
6 I p7 D r- J( v - activations : 2016
3 ?7 E* h) U' f - weights : 12256 , g/ c0 O: U7 _
- macc : 14404 # T4 o+ E0 v2 q. ^0 b- H8 m. j2 D
- -------------------------------------------------------------------------------- ) C. X, ]5 t8 }: H& H# \& M$ ]
- runtime : STM.AI 7.3.0 (Tools 7.3.0) " @0 i: ]7 G; ]0 D) a* Y/ p" [
- capabilities : ['IO_ONLY', 'PER_LAYER', 'PER_LAYER_WITH_DATA'] / ^; \# c# f( ?% x$ n
- device : AMD64 Intel64 Family 6 Model 158 Stepping 9, GenuineIntel (Windows)
2 e8 Y) D/ M7 W4 g0 [4 A; e% D) \- T - -------------------------------------------------------------------------------- ! A5 M# u, n0 a) b- c
- STM.IO: 0%| | 0/12806 [00:00<?, ?it/s]
& F- B( f# c+ P% ]8 Q2 S6 Z - STM.IO: 11%|█ | 1424/12806 [00:00<00:00, 14136.31it/s] ( h# v% g% w! M! t
- STM.IO: 14%|█▍ | 1849/12806 [00:00<00:04, 2293.62it/s] * j* m0 i% Q( @1 [2 P
- STM.IO: 17%|█▋ | 2170/12806 [00:00<00:05, 1774.74it/s] * ^) G4 ~7 d( Y1 d+ S
- STM.IO: 19%|█▉ | 2429/12806 [00:01<00:06, 1520.26it/s]
# K- ?) D8 p; d( p5 _3 B8 N% A - STM.IO: 21%|██ | 2645/12806 [00:01<00:07, 1348.30it/s]
* j+ f9 y' F Z& B1 J - STM.IO: 22%|██▏ | 2828/12806 [00:01<00:07, 1291.67it/s]
! K7 g2 T3 m1 Z1 h; I - STM.IO: 23%|██▎ | 2992/12806 [00:01<00:07, 1245.52it/s]
9 z4 e$ A0 l4 J0 q1 o1 U - STM.IO: 25%|██▍ | 3141/12806 [00:01<00:08, 1194.67it/s] " p2 d& \# Z$ [1 m* s2 f
- STM.IO: 26%|██▌ | 3278/12806 [00:01<00:08, 1107.82it/s] $ _+ D7 R% ?& D
- STM.IO: 27%|██▋ | 3407/12806 [00:02<00:08, 1154.55it/s] ! [3 ^$ Q6 ?8 K+ E3 t0 W! e5 q
- STM.IO: 28%|██▊ | 3548/12806 [00:02<00:07, 1218.70it/s] ! Y3 T6 Q1 v: B
- STM.IO: 29%|██▊ | 3678/12806 [00:02<00:07, 1175.60it/s] / @' N; Y4 K3 z; z; b) V
- STM.IO: 30%|██▉ | 3811/12806 [00:02<00:07, 1215.67it/s] : K! x3 R0 M, O" K
- STM.IO: 31%|███ | 3938/12806 [00:02<00:07, 1139.58it/s]
! Q- e6 \2 [5 e - STM.IO: 32%|███▏ | 4075/12806 [00:02<00:07, 1197.83it/s]
4 n; X2 o) H) w0 E5 X! H2 Z - STM.IO: 33%|███▎ | 4199/12806 [00:02<00:07, 1207.70it/s]
" `+ s3 n/ U9 }) _ - STM.IO: 34%|███▍ | 4323/12806 [00:02<00:07, 1078.59it/s] 2 q4 ^; k7 }$ N- i+ ~/ Y
- STM.IO: 35%|███▍ | 4451/12806 [00:02<00:07, 1129.92it/s]
+ e+ M A3 O# | - STM.IO: 36%|███▌ | 4590/12806 [00:03<00:06, 1194.76it/s]
" Z& d" g' p4 D2 Q4 M0 K+ F3 N - STM.IO: 37%|███▋ | 4718/12806 [00:03<00:06, 1216.59it/s]
% Z5 m/ J. [. ~! H! ?5 J - STM.IO: 38%|███▊ | 4843/12806 [00:03<00:06, 1195.77it/s] / M0 f1 _6 L& E1 K: b
- STM.IO: 39%|███▉ | 4965/12806 [00:03<00:06, 1159.48it/s] ( @) V4 a0 Y* L. B
- STM.IO: 40%|███▉ | 5083/12806 [00:03<00:06, 1116.81it/s] + P4 I" I/ {. V! A
- STM.IO: 41%|████ | 5197/12806 [00:03<00:06, 1095.57it/s] 3 {) [9 U ~( D3 ?
- STM.IO: 41%|████▏ | 5308/12806 [00:03<00:06, 1078.25it/s]
, g) T% T/ o9 U4 W* N - STM.IO: 42%|████▏ | 5433/12806 [00:03<00:06, 1122.47it/s]
5 H6 {. O2 x) I: r1 v% }8 ] - STM.IO: 43%|████▎ | 5547/12806 [00:03<00:06, 1056.59it/s] - Q5 V; P9 N% t# A3 H! \8 A
- STM.IO: 44%|████▍ | 5655/12806 [00:04<00:06, 1055.01it/s] 7 \, ]( [! N; P/ Z7 v6 x
- STM.IO: 45%|████▍ | 5762/12806 [00:04<00:06, 1035.74it/s]
2 r6 }% _2 v* }, N$ t$ ~5 e - STM.IO: 46%|████▌ | 5867/12806 [00:04<00:06, 1022.60it/s]
$ U+ D- S" J, y! f* g& {! ^ - STM.IO: 47%|████▋ | 5981/12806 [00:04<00:06, 1053.06it/s] h4 P" t+ b1 W2 g
- STM.IO: 48%|████▊ | 6098/12806 [00:04<00:06, 1083.31it/s]
5 V! ?5 l5 g* E/ S/ ]7 Q0 [3 r - STM.IO: 48%|████▊ | 6208/12806 [00:04<00:06, 1025.35it/s]
% R8 I1 x1 e7 b2 e! s - STM.IO: 49%|████▉ | 6312/12806 [00:04<00:06, 952.27it/s]
. o7 c5 z" F. ]) c% m. p - STM.IO: 50%|█████ | 6410/12806 [00:04<00:07, 910.42it/s]
, f' @7 L& l5 u: u3 J, s3 @% E - STM.IO: 51%|█████ | 6509/12806 [00:04<00:06, 930.92it/s] ) O! H; L/ f( P& m# F
- STM.IO: 52%|█████▏ | 6620/12806 [00:04<00:06, 976.37it/s]
" H0 g7 s+ |* a9 O' C0 u3 H- f) T - STM.IO: 52%|█████▏ | 6720/12806 [00:05<00:06, 926.81it/s]
( F N. Y( Z) d! D# k9 L, d) n - STM.IO: 53%|█████▎ | 6818/12806 [00:05<00:06, 940.17it/s]
( s9 o7 r9 p. {+ J# V - STM.IO: 54%|█████▍ | 6914/12806 [00:05<00:06, 930.36it/s] ) x A1 I* H8 ?4 E8 s* t
- STM.IO: 55%|█████▍ | 7008/12806 [00:05<00:06, 852.84it/s] : l2 s1 }4 O# ^7 ~
- STM.IO: 55%|█████▌ | 7106/12806 [00:05<00:06, 885.63it/s]
) R* @$ |$ X1 _* I9 R) i - STM.IO: 56%|█████▌ | 7197/12806 [00:05<00:06, 805.83it/s]
0 o/ r f* s8 J; {& F/ \& ?6 \; d - STM.IO: 57%|█████▋ | 7299/12806 [00:05<00:06, 858.49it/s] - y1 `7 |3 q; A. d' R+ _
- STM.IO: 58%|█████▊ | 7388/12806 [00:05<00:07, 744.49it/s] # y4 o& e* r0 z2 h% m
- STM.IO: 58%|█████▊ | 7473/12806 [00:06<00:07, 755.34it/s]
$ l) j3 h5 B0 i0 m S& i - STM.IO: 59%|█████▉ | 7560/12806 [00:06<00:06, 785.88it/s] . q: K1 F i, B
- STM.IO: 60%|█████▉ | 7642/12806 [00:06<00:06, 782.78it/s] 6 l( r; [' q( `( l% B4 V: ?1 a* J
- STM.IO: 60%|██████ | 7723/12806 [00:06<00:06, 768.90it/s] 1 m5 k6 |! A8 Z' W9 n" o
- STM.IO: 61%|██████ | 7825/12806 [00:06<00:06, 828.66it/s]
8 l% G0 R" h9 ?7 y1 `& c - STM.IO: 62%|██████▏ | 7937/12806 [00:06<00:05, 897.30it/s] 4 M4 A7 w; T& d0 C
- STM.IO: 63%|██████▎ | 8033/12806 [00:06<00:05, 913.23it/s] ' s# {* S' b& Q) {+ G1 |
- STM.IO: 63%|██████▎ | 8127/12806 [00:06<00:05, 913.79it/s] ! l9 C5 ~' ^4 ?: U9 ~" L- j2 X$ j
- STM.IO: 64%|██████▍ | 8254/12806 [00:06<00:04, 994.44it/s] 7 A0 {# f" y7 I# y
- STM.IO: 65%|██████▌ | 8358/12806 [00:06<00:04, 1005.50it/s] " J9 z; z1 W5 W# }3 F
- STM.IO: 66%|██████▌ | 8466/12806 [00:07<00:04, 1024.62it/s]
# T4 m3 e, J3 f6 X& Y - STM.IO: 67%|██████▋ | 8579/12806 [00:07<00:04, 1052.03it/s]
* i* N1 v! R; ~5 F9 ] - STM.IO: 68%|██████▊ | 8712/12806 [00:07<00:03, 1111.93it/s] - g; D0 x. e" k! o' n
- STM.IO: 69%|██████▉ | 8826/12806 [00:07<00:03, 1044.19it/s]
$ \1 b8 }& F$ z7 {/ x4 f, L - STM.IO: 70%|██████▉ | 8933/12806 [00:07<00:03, 1005.29it/s] ( s; y: b; B2 J! S/ D: |
- STM.IO: 71%|███████ | 9036/12806 [00:07<00:03, 1010.21it/s]
" ~& S; v2 @1 f5 D+ F4 x. Y) o- O - STM.IO: 71%|███████▏ | 9150/12806 [00:07<00:03, 1043.83it/s] 7 K0 j0 Y9 k. j) f0 O$ d8 D* A
- STM.IO: 72%|███████▏ | 9277/12806 [00:07<00:03, 1100.57it/s] B$ l$ J* }* }/ P! ?8 H9 P
- STM.IO: 73%|███████▎ | 9404/12806 [00:07<00:02, 1144.16it/s]
, _4 f8 }* l9 z8 r$ D - STM.IO: 74%|███████▍ | 9521/12806 [00:08<00:02, 1135.98it/s] 8 T7 E9 _" Y, \8 D& ^
- STM.IO: 75%|███████▌ | 9648/12806 [00:08<00:02, 1170.75it/s] ; W, s2 o/ @: w* d
- STM.IO: 76%|███████▋ | 9780/12806 [00:08<00:02, 1209.41it/s]
: U8 D3 @0 `4 [4 T8 a( x; G - STM.IO: 77%|███████▋ | 9903/12806 [00:08<00:02, 1184.92it/s] 7 e' }3 Q) J' q8 W5 B, M k
- STM.IO: 78%|███████▊ | 10032/12806 [00:08<00:02, 1212.12it/s]
3 @0 t/ N" F5 ?0 c0 w - STM.IO: 79%|███████▉ | 10155/12806 [00:08<00:02, 1214.79it/s]
& c' J* w M' `; q ?6 d2 v1 A - STM.IO: 80%|████████ | 10278/12806 [00:08<00:02, 1096.01it/s]
& B# M M5 K3 w) j& ] - STM.IO: 81%|████████ | 10391/12806 [00:08<00:02, 1100.40it/s] ) l" v: V6 l8 A/ k4 }0 d
- STM.IO: 82%|████████▏ | 10506/12806 [00:08<00:02, 1112.34it/s]
N$ R* A! S: q* [4 O4 n1 w4 y - STM.IO: 83%|████████▎ | 10619/12806 [00:09<00:02, 1035.66it/s]
J) u, K$ ?/ B! U9 u# s - STM.IO: 84%|████████▎ | 10725/12806 [00:09<00:02, 914.43it/s]
e1 Q1 n' s5 T: D( N - STM.IO: 84%|████████▍ | 10821/12806 [00:09<00:02, 889.74it/s]
6 K) A. [. B' R3 t) X- j2 G% } - STM.IO: 85%|████████▌ | 10920/12806 [00:09<00:02, 915.76it/s] 7 Y! I* O0 j7 |3 R( m* x
- STM.IO: 86%|████████▌ | 11014/12806 [00:09<00:02, 819.91it/s] $ a* m8 N2 Q: w& G. w$ f" B
- STM.IO: 87%|████████▋ | 11100/12806 [00:09<00:02, 738.28it/s] , ]7 Z$ r r2 O& W* V4 @
- STM.IO: 87%|████████▋ | 11178/12806 [00:09<00:02, 740.24it/s] ' u' p3 \) { I
- STM.IO: 88%|████████▊ | 11255/12806 [00:09<00:02, 657.58it/s]
% u! R) z1 W; o - STM.IO: 89%|████████▊ | 11364/12806 [00:10<00:02, 702.16it/s]
: T1 _. V! {/ V5 z, J n! A - STM.IO: 89%|████████▉ | 11455/12806 [00:10<00:01, 752.49it/s]
; Y8 i$ @: k# s - STM.IO: 90%|█████████ | 11548/12806 [00:10<00:01, 794.66it/s] X' s$ H+ D: j7 }( i, t( E" y& J
- STM.IO: 91%|█████████ | 11631/12806 [00:10<00:01, 796.56it/s]
% k, m! K9 ~% n0 f9 p+ E - STM.IO: 92%|█████████▏| 11748/12806 [00:10<00:01, 879.46it/s] # z6 l6 W0 @: B4 I! l; K
- STM.IO: 93%|█████████▎| 11853/12806 [00:10<00:01, 922.73it/s] & b% w2 F4 u S9 O: z# O( c
- STM.IO: 93%|█████████▎| 11949/12806 [00:10<00:00, 895.23it/s] + T! ?( s) l+ E1 R, O p0 J
- STM.IO: 94%|█████████▍| 12049/12806 [00:10<00:00, 922.41it/s]
3 U' q* Q0 f4 m3 N - STM.IO: 95%|█████████▍| 12163/12806 [00:10<00:00, 976.60it/s] ' Y6 r$ a" y+ i9 `
- STM.IO: 96%|█████████▌| 12280/12806 [00:10<00:00, 1025.50it/s] 4 @3 O2 q" w3 ^8 h
- STM.IO: 97%|█████████▋| 12412/12806 [00:11<00:00, 1096.80it/s]
( [ j, q; N% | - STM.IO: 98%|█████████▊| 12525/12806 [00:11<00:00, 1072.91it/s]
: S5 t" D- X1 {& ` - STM.IO: 99%|█████████▉| 12663/12806 [00:11<00:00, 1147.57it/s]
# G- q9 V z- K+ a% }* L. U; u- M6 _3 g - STM.IO: 100%|█████████▉| 12781/12806 [00:11<00:00, 1118.51it/s]
, i4 z7 v! _# ?. n2 C( R- ? - Results for 12806 inference(s) - average per inference 0 s! o+ w: h7 L' V
- device : AMD64 Intel64 Family 6 Model 158 Stepping 9, GenuineIntel (Windows) + O. d; G% G- {: [
- duration : 0.057ms ( Z: D$ n v1 r7 S
- c_nodes : 4 4 R5 [$ |1 B9 J
- c_id m_id desc output ms %
- n/ a2 [8 _. I2 O- O1 G, c$ _ - ------------------------------------------------------------------------------- ; f* b' B# R# d4 Q4 Q' j
- 0 1 Conv2dPool (0x109) (1,3,3,24)/float32/864B 0.049 86.5%
- k U% ^' }0 _ - 1 3 Dense (0x104) (1,1,1,12)/float32/48B 0.005 9.1% / Y: ~! f3 |7 q" |
- 2 5 Dense (0x104) (1,1,1,4)/float32/16B 0.001 1.8% . w7 q( f! G5 C* G. B: b& D6 f! {" m+ m
- 3 5 NL (0x107) (1,1,1,4)/float32/16B 0.001 2.5% 1 u4 L0 w& V6 Q* G' u7 y
- ------------------------------------------------------------------------------- 6 H* u, w0 q8 d4 [
- 0.057 ms
9 q. |; Z2 x! _6 i2 D' n( s- J - NOTE: duration and exec time per layer is just an indication. They are dependent of the HOST-machine work-load.
$ c8 y. A' Z0 F8 |4 @* ^ - Running the Keras model... ( v1 }7 M P9 w& p7 `% E
- Saving validation data...
' j+ r2 F1 u9 F1 u( A U4 b0 N/ G - output directory: C:\Users\py_hp\.stm32cubemx\network_output 2 @0 V4 |% d) B3 y/ z) {! Q
- creating C:\Users\py_hp\.stm32cubemx\network_output\har_ign_val_io.npz 6 }7 R5 `9 x! S2 r* k n' s- U
- m_outputs_1: (12806, 1, 1, 4)/float32, min/max=[0.000, 1.000], mean/std=[0.250, 0.376], dense_2 3 T. H J9 x% E$ ~
- c_outputs_1: (12806, 1, 1, 4)/float32, min/max=[0.000, 1.000], mean/std=[0.250, 0.376], dense_2 & h* c* q0 O; W/ }
- Computing the metrics... " `. Y0 I; g4 g! {4 A# g7 C1 ~
- Accuracy report #1 for the generated x86 C-model
) M- ^, s0 P" z8 A# ] - ----------------------------------------------------------------------------------------------------
' G5 J$ }4 U% n# x+ s Y - notes: - computed against the provided ground truth values ' E, ^- [) m4 ~ u* d
- - 12806 samples (4 items per sample) 2 z7 [+ }# B7 \
- acc=86.72%, rmse=0.224433631, mae=0.096160948, l2r=0.496649474, nse=73.14% - t/ u8 K6 H- u0 D+ P0 W3 @
- 4 classes (12806 samples) - q, g' r% n, J0 a, g: `1 n3 V
- ----------------------------
% z# C( Q% m. B. J1 L i - C0 3678 . 62 41
9 d/ t) X& T: s, D8 @/ \; Z @ - C1 . 1124 14 . 4 t+ s3 _" @$ t* b
- C2 254 10 1806 662
- T4 H+ B3 o A$ z4 t, C$ z - C3 66 . 592 4497 7 P, D) F% A; T( G5 L# P8 B
- Accuracy report #1 for the reference model , [! _! [. b3 `% b O
- ---------------------------------------------------------------------------------------------------- r4 G. P$ d, w8 |9 _- [6 _
- notes: - computed against the provided ground truth values
}1 v6 _) B! C: \. u; S d - - 12806 samples (4 items per sample)
% F( }+ h; e! N' j) m - acc=86.72%, rmse=0.224433631, mae=0.096160948, l2r=0.496649474, nse=73.14%
- o" X2 S4 q% g7 Y; X - 4 classes (12806 samples)
) s5 z* s0 q( q. H+ i - ----------------------------
3 ?2 K3 u/ w7 E" ]* u - C0 3678 . 62 41 ) G' [. j- X: Z% r( n+ L3 T# o
- C1 . 1124 14 . & R6 ^( z& S& B% \) t; t& {
- C2 254 10 1806 662
/ |7 x- g4 L% ?% D) v% b: A+ \8 T8 ] - C3 66 . 592 4497 . o( q# ~6 @/ q
- Cross accuracy report #1 (reference vs C-model) & K4 O3 a* z7 {# }' Y, S1 `
- ----------------------------------------------------------------------------------------------------
7 x- U5 R5 p) V% [( R - notes: - the output of the reference model is used as ground truth/reference value
( ?1 j4 i6 t+ `, q$ _8 {( @/ w - - 12806 samples (4 items per sample) , K- y- r3 m; k" t; |
- acc=100.00%, rmse=0.000000063, mae=0.000000024, l2r=0.000000139, nse=100.00% : S' ~# y/ y8 j+ {% \- j% t. k
- 4 classes (12806 samples) * @3 K# c: G+ g3 @0 v) r
- ----------------------------
" D: W$ r1 k# _2 o$ n- I - C0 3998 . . .
# o M: B8 R0 e% m- ~2 p - C1 . 1134 . . $ Y4 L# `' S- @8 ?/ U$ Z* w$ M
- C2 . . 2474 . 9 f) c8 f& O# L5 ]6 Z3 f
- C3 . . . 5200
2 }+ `; `5 |% l7 d) U4 i - . ^4 U9 h/ r% [1 O% ^3 w: T" o8 ?
- Evaluation report (summary) 2 H+ h7 `$ I- J* T% L$ D
- ----------------------------------------------------------------------------------------------------------------------------------------------------------
' T# J# i0 J _4 T4 X - Output acc rmse mae l2r mean std nse tensor
; N! F) C( C$ W& X - ----------------------------------------------------------------------------------------------------------------------------------------------------------
% _8 c* u4 e- t7 \# j O( C# v - x86 c-model #1 86.72% 0.224433631 0.096160948 0.496649474 -0.000000000 0.224435821 0.731362987 dense_2, ai_float, (1,1,1,4), m_id=[5]
0 x2 N3 e+ }' W- [" ]4 e& D8 C - original model #1 86.72% 0.224433631 0.096160948 0.496649474 -0.000000001 0.224435821 0.731362987 dense_2, ai_float, (1,1,1,4), m_id=[5] 6 ~3 s7 D% L, `8 a
- X-cross #1 100.00% 0.000000063 0.000000024 0.000000139 0.000000000 0.000000063 1.000000000 dense_2, ai_float, (1,1,1,4), m_id=[5]
5 Y6 r6 a7 ]- e/ P+ C; j s7 B0 F - ---------------------------------------------------------------------------------------------------------------------------------------------------------- 5 q; p& O0 S* X7 ]
-
+ r* ^( d) m& i2 B9 [: q# Y - rmse : Root Mean Squared Error 9 D! h# G# \1 r# S: {
- mae : Mean Absolute Error
- r5 `/ J" @* `0 d3 j" u8 l - l2r : L2 relative error 6 s, c" L+ b8 h% _4 q" x
- nse : Nash-Sutcliffe efficiency criteria
. v; @5 N* j. _7 l, a7 m: \ - Creating txt report file C:\Users\py_hp\.stm32cubemx\network_output\har_ign_validate_report.txt : |! y5 n6 V) m2 T' e8 q: N
- elapsed time (validate): 26.458s # Y- J7 `( L# G
- Validation
复制代码 $ y2 m' u% |; R( w. P
6.4 c语言神经网络模型生成及源码输出( W% d5 R; `+ Q- [; X/ c
将开发板重新选择ST-LINK连接(5-6跳线帽拔出,插入1-2跳线中)" J; \" N9 V2 g) ]
4 Q( M# N A+ g3 E6 I5 u
为了后续源码讲解方便,只生产c语言的神经网络模型源码,不输出应用示例程序(有个弊端就是在新建程序加载到开发板后,validation on target功能无法使用),如下图所示。
* R9 q: R* f3 H# ~6 j- O7 ]; R2 `% U& b8 Y
* C6 h- l) \( e0 L- d# H2 Y4 w, P. P: P Y1 a3 _3 x/ x. p/ e
配置输出工程/ S6 {" i2 q/ y6 T1 H- _" y
0 @$ h' k& D) l/ }
7 d1 Z& N8 ]$ h3 h( z I1 i
$ w3 o. F7 i4 V
七、c语言神经网络模型使用
$ X* j4 Z0 p/ I; z8 c8 G 7.1 C语言神经网络模型源文件# |4 ?2 }. B0 z# o: a$ [
在cubeMX配置神经网络模型时,指明了名称是har_ign,会生成如下文件har_ign.h/c、har_ign_data.h/c、har_ign_data_params.h/c、har_ign_config.h这些源码文件就是转换后的c语言神经网络模型,提供了一系列的API,这些API通过调用cube.AI软件包的内置功能,工程实现了神经网络计算功能:
8 d, e$ k" q) c) M
) J- O V5 f. h6 S ]( \ W
* H, E# E* v: W/ c0 P1 Y4 c* r5 r9 K
其中har_ign_generate_report.txt文件是生成c语言神经网络模型的过程记录。, U) H- B1 L$ s
. u. C+ h4 o9 t# }3 H4 \
9 w; f* X' ^1 c) W/ i) A1 s
- L" g4 W! z7 u 7.2 串口功能自定义实现
" |9 j. c! p" Y5 e! h9 T, Y 由于本文没有选择生成配套的应用程序代码,因此串口功能还需要自己实现,因此我移植了串口功能代码,在工程目录下,创建了ICore源目录,并创建print、usart子目录,分别在两个子目录加入print.h/c和usart.h/c源码。
3 c A' y! M( j- J0 }
; i# O% j& @+ c) a% W% N) Q
0 J' }/ a. C+ }7 b6 }& U/ s- N( U) f- X5 N/ `; I# {
print.h如下:3 B9 Z2 X( N/ t1 u* g0 p% e1 n
- #ifndef INC_RETARGET_H_% O6 Q# p# `; w6 l; G2 @5 ~
- #define INC_RETARGET_H_
) v: Z- {( {: c2 z# S3 d% `1 S, _2 _5 \ - X5 |6 x1 L; Y
- #include "stm32l4xx_hal.h"# R" k6 N6 D- \5 O) ?2 E( ]: a
- #include "stdio.h"//用于printf函数串口重映射
* U( @% T" z4 o, F, c* p - #include <sys/stat.h>( b$ b0 M/ c- o, o* B/ ~
-
3 } A) H/ x4 o& T. c3 o4 O' u - void ResetPrintInit(UART_HandleTypeDef *huart);& |( t2 i& `0 t* F. Y
- * k$ L% ~7 F: Y2 q; ^4 k
- int _isatty(int fd);& S& L. O" A" U% N5 W
- int _write(int fd, char* ptr, int len);* H2 H& e- X, a8 O, |
- int _close(int fd);
' Z- ]3 P7 [6 `- n - int _lseek(int fd, int ptr, int dir);: S, `9 o7 e, V' m
- int _read(int fd, char* ptr, int len);- M& p0 k$ Y; i# r/ t
- int _fstat(int fd, struct stat* st);4 z* k1 z3 t9 O' y: l
-
0 N7 F! k" n; Z) T - #endif /* INC_RETARGET_H_ */
复制代码 0 Q2 m( n4 w2 k0 W& v7 x, s
print.c如下:
+ L* ]5 {9 u9 _% G- #include <_ansi.h>
\' ~& r. J' U# x - #include <_syslist.h>% u' z6 g: i( \
- #include <errno.h>, r. d+ ^. p/ U: z# v! G }
- #include <sys/time.h>5 n3 L, e# y1 R( C
- #include <sys/times.h>
j5 {' W1 U, q' o& F - #include <limits.h>1 O& k. I0 q0 A; a' k: P0 O. Y
- #include <signal.h>8 g* x% ^. [" ?0 _
- #include <stdint.h>" c$ |+ _+ O6 L/ d
- #include <stdio.h>" f+ Y! k4 k2 b' s n
- # a: a5 y0 G9 n- u* u' E; t0 Q
- #include "print.h"$ Z* l2 b6 `. ^1 \
- * ]% X& b* A' [9 U
- #if !defined(OS_USE_SEMIHOSTING)$ q7 r( c1 e% U
- #define STDIN_FILENO 0
7 t T8 ]# c# j* x) i - #define STDOUT_FILENO 19 p" _& I' \; U. O5 j
- #define STDERR_FILENO 2
; Y4 p' X1 \# P- z) X - " Q) V4 e8 `& k9 r
- UART_HandleTypeDef *gHuart;9 Q3 E5 q! G! w
-
# Q' X5 N9 X7 x/ R# j5 |) x, a - void ResetPrintInit(UART_HandleTypeDef *huart) {
" B7 Z9 [5 L& [) M z - gHuart = huart;) O, n0 O9 X# q/ c
- /* Disable I/O buffering for STDOUT stream, so that
: ]- b* T4 C3 ~9 O+ ~ - * chars are sent out as soon as they are printed. */
2 O4 p7 j( w2 Y+ a$ Y. Y' k - setvbuf(stdout, NULL, _IONBF, 0);
- x6 K1 c! J" m2 P - }
( ~4 S/ U' w5 N0 T' O5 r - int _isatty(int fd) {0 e$ B5 H. m* F
- if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
# F _1 s: v, F- f - return 1;: s- r/ L- k1 P. w. p- k% q4 E
- errno = EBADF;) p% `1 e( q4 o" L6 ~
- return 0;
) A) F3 ^+ z2 A: O; q9 X" {: g9 Y - }
2 ?; i6 ?- q7 T+ J4 i - int _write(int fd, char* ptr, int len) {
0 h1 e7 y( r4 [8 u2 A% E* H0 f: U - HAL_StatusTypeDef hstatus;( K8 Q/ m4 L9 h4 t2 g0 C
- if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {$ m; m+ Y* v& |' {* I" U! r
- hstatus = HAL_UART_Transmit(gHuart, (uint8_t *) ptr, len, HAL_MAX_DELAY);% O# R; C1 S, c; C( \, |6 h% n: ?
- if (hstatus == HAL_OK). z5 S- u8 r: ~( r) B, T8 v
- return len; L+ X* g0 o* A9 W: |% P
- else
: O0 ~% ^- G2 [, b# F v( Z - return EIO;4 M3 B) {# q7 h; `2 u5 Q
- }
( O7 }6 u8 K& F+ o# q& x - errno = EBADF;
3 ^" p2 r8 o- O& b" {. v - return -1;
& X+ k# s7 c. @) k! s* _ - }
+ W& I8 T. N+ H" `& Q$ ? - int _close(int fd) {
: t% r1 R% Q( k' J - if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
# M! D4 s7 v; z0 f& F - return 0;9 H T8 M6 E! N" p/ g: r
- errno = EBADF;
" N# v, }/ X, d ^" o& n5 R - return -1;5 o5 F0 W9 l/ z. Z1 }. J6 q5 H! |+ y1 o
- }
8 d1 |9 N: V2 r; h - int _lseek(int fd, int ptr, int dir) {# B0 x8 x" u; z6 J
- (void) fd;4 ^7 q: B# I6 N3 L* I3 o
- (void) ptr;
2 H& f* f, p3 r! K1 T - (void) dir;; ]( ]; z4 ?5 T
- errno = EBADF;
: x. D8 X: o! }- }/ e/ h - return -1;
: J* G o: }! S& y+ y2 H; r8 c9 |+ M - }. x0 t; Q& Q$ t
- int _read(int fd, char* ptr, int len) {
. B( B& \+ \5 I - HAL_StatusTypeDef hstatus;
" s9 }3 w; R# J2 j& \7 l - if (fd == STDIN_FILENO) {
4 L3 q4 K+ x( s" k9 i* h" S0 n2 o- W; _ - hstatus = HAL_UART_Receive(gHuart, (uint8_t *) ptr, 1, HAL_MAX_DELAY);! M4 ]7 j" y2 B# a
- if (hstatus == HAL_OK)8 \% \1 L& I- r ^- ^- Z
- return 1;1 g( H- G, H: O) I9 a. T* y) o1 D
- else
% G9 Z, r7 D1 L. Q6 \ - return EIO;
' v7 |4 ~: C5 z - }! \1 ^ n) X3 [
- errno = EBADF;
$ [" E6 `& y6 W( x3 u- I - return -1;, v" h3 n2 L f) h
- }4 v4 i/ Q8 X6 _) m% x8 O, R
- int _fstat(int fd, struct stat* st) {
7 H5 ^4 A! @% A9 ` - if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
. u% j5 f* \) i7 h! ^0 Q - st->st_mode = S_IFCHR;
0 j, i! ?& n, D8 q, O/ M* H - return 0;- W! Y3 `# X, A. O$ t
- }
5 Q% {, w" Z5 \# | - errno = EBADF;
# Z- Q3 M3 }9 p - return 0;
6 @+ i# l+ o8 e/ v0 X - }) @; f: Q4 O/ |& x" U9 Z
- 4 ?1 B: [6 P5 N; b x
- #endif //#if !defined(OS_USE_SEMIHOSTING)
复制代码
) |* M: e& A' S2 |) d9 P usart.h
* S+ I# c+ x8 k/ w- #ifndef INC_USART_H_
, {" A% r4 D, f2 Q/ F, `$ e& M - #define INC_USART_H_
, Y9 h$ y6 w, O$ Q -
3 n: { b" }$ c - #include "stm32l4xx_hal.h" //HAL库文件声明2 h: J- V# F1 R1 c* c" B$ R+ C
- #include <string.h>//用于字符串处理的库1 \- y: ~% A7 \& I: m1 Y9 n
- #include "../print/print.h"//用于printf函数串口重映射. E! k/ k0 d; u0 l! d. y
-
. E: j% w: G5 A' v( q9 _ - extern UART_HandleTypeDef huart1;//声明LPUSART的HAL库结构体+ j" e" i; P' @5 K# Z4 d! r: q+ Y- U
-
, |/ ]0 n6 s: l4 G. R - #define USART_REC_LEN 256//定义LPUSART最大接收字节数+ r z; e& i- E$ A4 ~) h+ f
- ) R9 `6 s$ O+ ?# o( {9 _5 E2 W T0 J
- extern uint8_t USART_RX_BUF[USART_REC_LEN];//接收缓冲,最大USART_REC_LEN个字节.末字节为换行符+ T& g, G, n' G# U- D) l, o
- extern uint16_t USART_RX_STA;//接收状态标记
4 z; U) w' ~5 W4 O - extern uint8_t USART_NewData;//当前串口中断接收的1个字节数据的缓存/ {$ q" i" L4 n# c4 Q f6 _
- ) f* T$ N% T6 @2 t
-
! a% e/ \( m8 W4 q2 _! W( o - void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);//串口中断回调函数声明# {2 b' L/ Y2 i2 M/ s3 ]! Q( u
- , n- J) A, X2 `+ V
- #endif /* INC_USART_H_ */
复制代码
6 s; I. P: n+ X7 a usart.c如下:
$ b& d8 T% T! V l* T9 l- #include "usart.h"- Z" v( S) x2 P' v
- 9 E$ B, d6 `) ]5 j# n
- uint8_t USART_RX_BUF[USART_REC_LEN];//接收缓冲,最大USART_REC_LEN个字节.末字节为换行符
# C& ^ V) S; r. o( O. v" W - /*
+ m: Z) Z* f& P7 z3 ^ - * bit15:接收到回车(0x0d)时设置HLPUSART_RX_STA|=0x8000;
3 G) r% a1 F( O% m0 v: X - * bit14:接收溢出标志,数据超出缓存长度时,设置HLPUSART_RX_STA|=0x4000;
6 `# ?0 e5 k" i S, T; m7 p& z. |; X - * bit13:预留4 b3 r# ^6 M. N8 P. M+ Q5 Y7 }, s
- * bit12:预留
, N1 c' j) J$ H8 n3 f - * bit11~0:接收到的有效字节数目(0~4095)
: w/ j5 `/ F7 F- t* @/ k3 X - */
, D0 h3 F6 T$ _! [ - uint16_t USART_RX_STA=0;接收状态标记//bit15:接收完成标志,bit14:接收到回车(0x0d),bit13~0:接收到的有效字节数目
5 v! t; s2 j4 @ - uint8_t USART_NewData;//当前串口中断接收的1个字节数据的缓存
' r& T0 [" o# d, I+ a - ' u! d! ^# K1 G& b, ~
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)//串口中断回调函数6 t* B' @' h' |7 S% D& }, G4 O
- {* k, \& G" }/ e/ W( ? L2 W6 n
- if(huart ==&huart1)//判断中断来源(串口1:USB转串口)
u# Q1 r0 s* Q2 P$ |( B) s - {
2 D1 O+ m" z; q! } - if(USART_NewData==0x0d){//回车标记: }! F$ C9 v" X5 H2 u4 t5 Y
- USART_RX_STA|=0x8000;//标记接到回车
; }8 ?1 _- C2 T8 \/ B# P' ^8 E4 R - }else{
/ D& f' F3 ~$ x" {* y* ] - if((USART_RX_STA&0X0FFF)<USART_REC_LEN){% g2 L7 L4 E) w) P
- USART_RX_BUF[USART_RX_STA&0X0FFF]=USART_NewData; //将收到的数据放入数组7 g4 U7 w$ x, D
- USART_RX_STA++; //数据长度计数加1
! m0 R9 N- W v, R - }else{
- L4 g: \) ?8 r5 ]4 f+ @1 B$ i - USART_RX_STA|=0x4000;//数据超出缓存长度,标记溢出( `* @2 ]3 H) U& R# X' R" _$ K# S
- }
2 E: @7 r' q; D/ y0 F7 O6 ~. D - }
) P4 f" G4 K+ ^- b4 }1 f _ - HAL_UART_Receive_IT(&huart1,(uint8_t *)&USART_NewData,1); //再开启接收中断! w! X- u5 ~) H, ^
- }6 F6 O# V2 u! p2 r5 c
- }
复制代码 2 o, }6 {# w- j1 u( `3 y! Q
7.3 c语言神经网络模型API使用
, Y- Y* _" W/ l) ]( h' F" |4 G P4 S 先不管底层机理,本文给下面代码,看看如何实现这些API调用的,在main.c文件中,通过aiInit函数,实现har_ign模型初始化,并打印模型相关信息。在主函数循环体中,通过串口输入信息,获得数据生成因子,调用acquire_and_process_data进行输入数据生成,然后调用aiRun,并传入生成数据及输出缓存,进行神经网络模型调用。然后调用post_process打印输出信息。$ V# g/ t5 j$ [+ P4 u/ b
- /* USER CODE END Header */; X1 |: Q+ d/ s& d( w
- /* Includes ------------------------------------------------------------------*/- E6 D- _) b- J. ~8 V+ S7 }$ h$ I
- #include "main.h"
- Q" m2 L+ t7 m! l - #include "crc.h"* C9 q) _8 M# X! K1 e
- #include "dfsdm.h"
6 `1 e) G. ~. P V7 O - #include "i2c.h"( l! S( B6 G: N! \! V4 b
- #include "quadspi.h"
# G+ L2 o& |2 Q - #include "spi.h"
+ J5 L! d, L9 s+ e' ~ - #include "usart.h"; @, [+ j1 a* \" v
- #include "usb_otg.h"# y3 f0 K! Z" I- w# E
- #include "gpio.h"( D9 Z4 [: q. V3 v
-
9 d S9 X$ {* G& B% E - /* Private includes ----------------------------------------------------------*/
4 L( _ u) W7 ^. v* c - /* USER CODE BEGIN Includes */
( q5 Z8 A2 N; d - #include "../../ICore/print/print.h"; J6 i2 j7 m6 k; p
- #include "../../ICore/usart/usart.h", Y4 \: R. `. h& j
- #include "../../X-CUBE-AI/app/har_ign.h"
) S5 t3 j7 T" ~, d9 D2 Z - #include "../../X-CUBE-AI/app/har_ign_data.h"
" L1 H+ O' Y2 D1 ~( n# l - /* USER CODE END Includes */$ q$ W7 C9 Z1 a$ W: r' L# w( R
-
0 f) h |" p, {! } - /* Private typedef -----------------------------------------------------------*/4 j3 |. _# I7 J/ q) w% o/ T. E
- /* USER CODE BEGIN PTD */! y0 e+ Z) e' U* y7 i& k
-
% N, ^( J9 I5 W# f3 }& X" N# [3 F7 h& E - /* USER CODE END PTD */
' G8 z+ z! Z, c, a6 P/ c7 e. g - 4 b9 j5 i6 R: M! l& t2 D
- /* Private define ------------------------------------------------------------*/
4 W4 v# s2 c/ \" y0 s3 ^2 R - /* USER CODE BEGIN PD */2 a% A6 v. l$ o5 y6 P. Q
- /* USER CODE END PD */. @+ y) @6 p" u3 Q! x S/ K6 E
-
0 T6 Z+ E t' D! [! H8 o: ^ d - /* Private macro -------------------------------------------------------------*/7 l$ p9 N% t8 {. T3 N5 @
- /* USER CODE BEGIN PM */
, l' t0 o$ }7 L - 9 m3 v p/ {; `4 c7 N
- /* USER CODE END PM */
' C# w' s$ ?' w* R/ t -
9 O3 w& n1 M. X3 N+ b+ t - /* Private variables ---------------------------------------------------------*/
& Q! \% Q8 {0 E( k! z ~ - # _ N8 R. m. x( u/ Q7 D4 w* ^
- /* USER CODE BEGIN PV */
0 b2 O$ K: ?9 {$ ?) o# [9 f4 q - - G+ [: ]+ A3 t# b3 f# M
- /* USER CODE END PV */
* X; d- a8 h' B( D% c4 T -
* g4 M, @, d( f' I0 y - /* Private function prototypes -----------------------------------------------*/
- H! S5 \. \8 b7 U: z - void SystemClock_Config(void);4 Y5 N9 h0 ?1 `4 w. e
- /* USER CODE BEGIN PFP */
% b0 s. Y" A/ L& s9 t# d -
) ^% r$ F8 n% Z - /* USER CODE END PFP */! q( _# _- B& x0 P1 {2 q* D. n
- 2 p) N. M2 r7 u }: C
- /* Private user code ---------------------------------------------------------*/! ~/ b+ F! j, u6 ?4 H) V% M. I
- /* USER CODE BEGIN 0 */4 r" {/ U& i- B r' O/ a& v
- /* Global handle to reference the instantiated C-model */
$ s, R- v* b% }( c/ Q+ X - static ai_handle network = AI_HANDLE_NULL;
2 C: x7 t7 e2 v1 {$ d" M0 J - ! {' Z6 ?& |- Y& P! g
- /* Global c-array to handle the activations buffer */
* Z; l" P/ v( H2 [: |6 _+ w - AI_ALIGNED(32)* H+ ]! a; `; F# j. b6 m
- static ai_u8 activations[AI_HAR_IGN_DATA_ACTIVATIONS_SIZE];
, V/ {% S, M/ l7 S' X# `# e# r -
1 O1 |( c8 `9 Q - /* Array to store the data of the input tensor */" h; i7 N5 m$ k% ^
- AI_ALIGNED(32)
8 B" p9 a7 ^% O7 W0 k - static ai_float in_data[AI_HAR_IGN_IN_1_SIZE];8 X4 G% U2 i. t
- /* or static ai_u8 in_data[AI_HAR_IGN_IN_1_SIZE_BYTES]; */9 O$ ^- |, X* [
-
) R2 d" W- _2 u( R - /* c-array to store the data of the output tensor */; D' r3 D2 e/ l% M" m ]- R
- AI_ALIGNED(32)( `: I l4 J( T$ _, f* B
- static ai_float out_data[AI_HAR_IGN_OUT_1_SIZE];
, i) F, `+ R; K* ?% @6 c+ g! t - /* static ai_u8 out_data[AI_HAR_IGN_OUT_1_SIZE_BYTES]; */9 C2 x! l" V1 m0 C% q+ K& E1 q, u
-
5 @; C! r* Y. T8 j' p - /* Array of pointer to manage the model's input/output tensors */
. w0 F$ I2 f% y* U - static ai_buffer *ai_input;
+ v: ?/ X7 u6 h5 x ` - static ai_buffer *ai_output;
3 S3 i- |3 B6 I5 T! g - static ai_buffer_format fmt_input;
- s. w) {+ C- r9 e* k3 e G9 J! e - static ai_buffer_format fmt_output;
: `/ J _2 Y9 l+ U5 f; m3 y -
, q6 G) ]+ A K( N# Q$ H - void buf_print(void)
! g+ d; _; b$ ~& H% }" Z - {( ] n; _6 J" F7 M; L1 f
- printf("in_data:");
: ^. J. {: O: Q) i - for (int i=0; i<AI_HAR_IGN_IN_1_SIZE; i++)
- I2 G7 Z7 B d# M& H7 n - {6 h$ a% R% y$ n0 u/ t6 @9 S8 R
- printf("%f ",((ai_float*)in_data)[i]);
1 r" N) ~" W" c# C9 w - } ]! N9 o4 n: v. E4 I# L
- printf("\n");
: E4 X/ i) X. n; O: W3 T; }- _1 m - printf("out_data:");# l. S2 w, q; h5 ]; ?7 N H
- for (int i=0; i<AI_HAR_IGN_OUT_1_SIZE; i++)
) X6 u. G( p! D( A! s# h - {+ i, Q E- F$ P. C4 c
- printf("%f ",((ai_float*)out_data)[i]);- Q9 q9 r/ }* D6 _5 T- C, g6 O
- }
5 _8 D1 `6 Y' k# V8 T! k - printf("\n");1 f! I# D. G" u; Z
- }
) _* G2 j L0 d - ( `+ `5 ?9 O7 n2 j
- void aiPrintBufInfo(const ai_buffer *buffer)7 g$ O) w$ G t$ U) H: `/ [6 W
- {
3 s0 B1 z6 A9 \: L - printf("(%lu, %lu, %lu, %lu)", AI_BUFFER_SHAPE_ELEM(buffer, AI_SHAPE_BATCH),( Y$ j5 {" ^. S) ]. Z7 Y' O2 n
- AI_BUFFER_SHAPE_ELEM(buffer, AI_SHAPE_HEIGHT),# s5 I N' e+ u; V; |' ?* e6 l
- AI_BUFFER_SHAPE_ELEM(buffer, AI_SHAPE_WIDTH),
0 V" m j5 [6 K1 q( M" z - AI_BUFFER_SHAPE_ELEM(buffer, AI_SHAPE_CHANNEL));2 V3 p% I4 B: t, K3 o4 c7 f9 p
- printf(" buffer_size:%d ", (int)AI_BUFFER_SIZE(buffer));
: T' @4 V: z6 v2 f) T - }
6 e/ v" d4 f+ p1 }9 j! Q8 @ - ( L4 Z$ K( a J% Q e
- void aiPrintDataType(const ai_buffer_format fmt)2 q, b; `/ c% W' |, }( i
- {
# F. z D7 c- }6 }- C2 S, u - if (AI_BUFFER_FMT_GET_TYPE(fmt) == AI_BUFFER_FMT_TYPE_FLOAT)/ R" }. y% z; T
- printf("float%d ", (int)AI_BUFFER_FMT_GET_BITS(fmt));
$ j0 f! E' a+ i - else if (AI_BUFFER_FMT_GET_TYPE(fmt) == AI_BUFFER_FMT_TYPE_BOOL) {! y% z( }& a2 I3 O- l
- printf("bool%d ", (int)AI_BUFFER_FMT_GET_BITS(fmt));4 ^1 ^7 X! }0 m8 v& w/ K, F
- } else { /* integer type */
* p9 }# R. l! G, @ - printf("%s%d ", AI_BUFFER_FMT_GET_SIGN(fmt)?"i":"u",) U4 _+ q/ \& A) C
- (int)AI_BUFFER_FMT_GET_BITS(fmt));2 D6 V& Y' S/ ?* {5 T. k
- }
: B6 Q% F& R# s% ^ j, ]: I - }$ ?$ F# i/ v4 f7 O
- 4 Z( O( b2 _; p6 `* |8 `
- void aiPrintDataInfo(const ai_buffer *buffer,const ai_buffer_format fmt)
% ~, ~) I% k7 N' X, \( _) k - {4 e* E6 Z9 Z6 o
- if (buffer->data)7 W+ n- M! H0 \; a2 W: X
- printf(" @0x%X/%d \n",
* d! F1 ?* k4 P* s - (int)buffer->data,
$ f6 h! g+ v, M! ^* T+ e - (int)AI_BUFFER_BYTE_SIZE(AI_BUFFER_SIZE(buffer), fmt)( y- e* \( o5 V: V0 x2 q
- ); z1 a% @- c' Y, X. I, w% z
- else3 M! ?( S! ~8 Y& ~* |9 v
- printf(" (User Domain)/%d \n",
; v D/ v0 r- P( c# H9 V% [* ?1 P - (int)AI_BUFFER_BYTE_SIZE(AI_BUFFER_SIZE(buffer), fmt)
% J( u+ k: u7 {9 Q - );
+ M. U! T, `3 H a. E# c" K$ ~ - }/ ?% j, d8 ^; o" @9 ]/ b
- , K- C8 \. h9 b) r) o# v
- void aiPrintNetworkInfo(const ai_network_report report)
- j) X* L5 ^9 _' `! C! \7 F - {8 j, \& U' K) r, B4 @
- printf("Model name : %s\n", report.model_name);& ` W& \& @7 d6 P
- printf(" model signature : %s\n", report.model_signature);
5 j9 \' l$ L6 n+ T! e \! _ - printf(" model datetime : %s\r\n", report.model_datetime);
2 ^ D% u) i/ M2 @ - printf(" compile datetime : %s\r\n", report.compile_datetime);
1 I, @0 ~8 p" d3 U$ n) x4 h& n/ t - printf(" runtime version : %d.%d.%d\r\n",1 u" j! w; S* x- U) z8 Z
- report.runtime_version.major,
) n% L0 N/ j. h* K; O - report.runtime_version.minor,
. o" u: K" V, R) H9 Y - report.runtime_version.micro);* M! |" y6 d6 K, {0 `6 y
- if (report.tool_revision[0]). K: l6 k7 W' ]
- printf(" Tool revision : %s\r\n", (report.tool_revision[0])?report.tool_revision:"");6 c9 m( l7 K3 Z! i. D! {8 Z R
- printf(" tools version : %d.%d.%d\r\n",# m. j0 p9 V: M
- report.tool_version.major,% U! t7 E4 ]" v- r
- report.tool_version.minor, [* u0 ?! b7 r+ R+ L
- report.tool_version.micro);
6 C' t+ B- T0 v" P1 w - printf(" complexity : %lu MACC\r\n", (unsigned long)report.n_macc);6 `% y& X& T. G2 e8 h
- printf(" c-nodes : %d\r\n", (int)report.n_nodes);* _6 _+ f R1 K' V0 ~) |+ f5 C
- 8 X0 e3 ?' f8 `
- printf(" map_activations : %d\r\n", report.map_activations.size);7 F+ Z) u$ A- {% ^# n% u7 E& Q
- for (int idx=0; idx<report.map_activations.size;idx++) {
, p1 C0 w, E+ I, G4 i - const ai_buffer *buffer = &report.map_activations.buffer[idx];
% i9 Z" w) ?" ?; d - printf(" [%d] ", idx);7 B8 T5 m9 G7 y6 G
- aiPrintBufInfo(buffer);
6 C8 W/ U7 n% {! B C9 } - printf("\r\n");
; p6 ], N, u4 q* O6 E8 y, O1 y - }( `" G' n0 q, d+ l7 w: S0 ^
- - `& d! W0 j3 l9 U, S/ F
- printf(" map_weights : %d\r\n", report.map_weights.size);
4 \3 q' V/ e: h0 q' L - for (int idx=0; idx<report.map_weights.size;idx++) {
: a( ^+ Q5 A6 S# o P+ l - const ai_buffer *buffer = &report.map_weights.buffer[idx];2 N. P. J* M% g
- printf(" [%d] ", idx);
! X0 {: J T; J" ?: ^ - aiPrintBufInfo(buffer);9 l6 v2 q: {" C3 l" L
- printf("\r\n");- p) G$ |; W+ J1 }) X: ^
- }+ I+ z" R% R n0 t
- }
* K. Q* t9 ^ `9 O- i, v -
" }2 M. C, C" g4 F/ B0 i - /*
) Y6 {, s6 `4 h2 s# p3 v - * Bootstrap
5 M6 T& _; R' }2 r - */
( P6 S+ }* X! Q8 M; }; l - int aiInit(void) {* p$ x) p3 }3 V" N j
- ai_error err;5 M _: a( P6 f. U
-
7 R+ j+ b( M1 I4 z, j4 X$ S# A- _* b - /* Create and initialize the c-model */
' ]( C9 h1 H' E - const ai_handle acts[] = { activations };, ^6 O9 D$ D9 D4 }6 U
- err = ai_har_ign_create_and_init(&network, acts, NULL);
; ?5 h" z4 V, G3 L- g; A9 B( V - if (err.type != AI_ERROR_NONE) {
' ^8 u) @- }5 x2 g1 G4 z$ n - printf("ai_error_type:%d,ai_error_code:%d\r\n",err.type,err.code);
( y6 E/ F8 N' L- E - };
& i% G5 G5 ~9 J3 ` -
2 ^2 `; r- z% h2 f( _1 h4 g( u - ai_network_report report; Z% ~; N4 J: l+ C: }
- if (ai_har_ign_get_report(network, &report) != true) {4 t5 h6 o; O3 |
- printf("ai get report error\n");; |% j' C$ T! n4 x
- return -1;* e. z3 |' R- s5 F x; L" C/ J
- }
- K$ o- c, N. Y1 i5 g9 P -
# [0 a# E& E d - aiPrintNetworkInfo(report);) l; h& i# P! R p! {( B( |# F9 Z
- ( N( j" E: x+ u# N. `, U
- /* Reteive pointers to the model's input/output tensors */! Q8 r1 g3 D4 i6 f" ^7 y
- ai_input = ai_har_ign_inputs_get(network, NULL);
7 G, s9 r% H. A" p3 [ - ai_output = ai_har_ign_outputs_get(network, NULL);, _& f: ?+ [5 T( {8 |( W) p, P4 I
- //
) a- l! u2 o, e - fmt_input = AI_BUFFER_FORMAT(ai_input);( N( e0 n# N0 P( L; r# A
- fmt_output = AI_BUFFER_FORMAT(ai_output);, T% H* Q, Z. f$ @
-
& O' p" f" E" B" I7 g; b D - printf(" n_inputs/n_outputs : %u/%u\r\n", report.n_inputs,
% p6 ~9 f E# w8 j - report.n_outputs);# f, b$ N8 x* C3 W8 H- e
- printf("input :");/ _' Z6 z3 W( C5 E! T6 A! M4 X
- aiPrintBufInfo(ai_input);
7 t& f. s2 Q( T4 X& f! v2 `) g - aiPrintDataType(fmt_input);( b+ Q' W" \$ |( W! J
- aiPrintDataInfo(ai_input, fmt_input);
2 U& _% @. g5 U( `2 W - //
/ V+ t9 q$ t* W7 N0 _! O - printf("output :");8 q5 J, a* u% T; V: R; Y
- aiPrintBufInfo(ai_output);
' F7 {+ q7 y8 \) D2 \: g4 O - aiPrintDataType(fmt_output);
2 o/ f7 w# B( k6 W - aiPrintDataInfo(ai_output, fmt_output);& m, i' U% L) `. S( H5 l2 h
- return 0;3 c! O! ]% t' [" v* K
- }
6 `. P# U. z. `0 l2 K' M - . @& q, i# E, b$ g+ {& R
- int acquire_and_process_data(void *in_data,int factor)4 ^8 o- N" u2 o& b; C
- {: A9 S! @1 n9 R) [7 B. d% f
- printf("in_data:");9 b+ ?/ H. @. M0 u( L
- for (int i=0; i<AI_HAR_IGN_IN_1_SIZE; i++)
' Y/ v, K. X# F: r' I- o) c - {4 D% b) j M- x* u8 ]+ O8 V) B
- switch(i%3){
& ~6 a$ e' e. i9 ~; n0 w - case 0:
! U9 v- m, o3 \1 K9 [8 s! F' P1 { - ((ai_float*)in_data)[i] = -175+(ai_float)(i*factor*1.2)/10.0;+ `0 J" U/ u9 E+ Z2 x) P# t# a, }
- break;+ O) Q2 t! p8 s. F7 O+ v
- case 1:
6 ]# s( w* N" a, @% h0 v1 C - ((ai_float*)in_data)[i] = 50+(ai_float)(i*factor*0.6)/100.0;6 t% G& K6 r* X- H
- break;
. O; M& P* R, U; v& g- v d - case 2:* r# H2 R! F# q O, V) R5 K2 P
- ((ai_float*)in_data)[i] = 975-(ai_float)(i*factor*1.8)/100.0;
3 z( G; h7 K( d) c! ~ D$ _+ }& G - break;
0 F3 S* `. a1 T - default: `) j7 @7 \! t7 p; t0 h, _
- break;
/ |, d' L0 u' c- |% f - }
/ x7 V/ m S5 {, K - printf("%f ",((ai_float*)in_data)[i]);' o3 a9 l! t" ?, C
- }# t+ _: D% V; u5 M5 l V
- printf("\n");3 A" H5 Y# E8 x# w
- return 0;
; R8 _8 B7 Q2 l# r; r$ E" j - }, S. M/ x/ P8 h* W* r' {
- /*
- m2 G" h, _' B+ a - * Run inference- V' Q9 W% H, y, o2 z
- */
4 t; R; @9 U6 I" |; j, K - int aiRun(const void *in_data, void *out_data) {* c! @6 t0 E- j
- ai_i32 n_batch;
/ k2 i. d$ s+ d0 j4 d - ai_error err; [2 X! S, X' z( X* V
-
. M8 o# K* e1 f/ i/ K% { - /* 1 - Update IO handlers with the data payload */, u: ^& ~# `# \& w
- ai_input[0].data = AI_HANDLE_PTR(in_data);
7 }" l* n+ J C8 I* D* U. S - ai_output[0].data = AI_HANDLE_PTR(out_data);7 h% t8 \- ~: X1 Y' x( s- |# A7 f7 q
- 5 Y4 w9 }" r ^1 w; V
- /* 2 - Perform the inference */
% T( h5 U, N2 I6 Y- V! H& W0 b - n_batch = ai_har_ign_run(network, &ai_input[0], &ai_output[0]);
4 s( @0 n+ E2 H9 k - if (n_batch != 1) {+ {8 O4 y) M7 ^( w& A( k% W) }
- err = ai_har_ign_get_error(network);9 W% Y( k C) e& V/ I
- printf("ai_error_type:%d,ai_error_code:%d\r\n",err.type,err.code);
* B c/ ?0 G4 W1 \ - };
5 i* E1 ^2 P; d( Y -
$ J" ^* P/ i" s# L9 q- } - return 0;( Y4 h7 h" }. j' y
- }
- X# l9 x+ N P -
% `) P0 `# v8 j4 ]4 { - int post_process(void *out_data)
0 s2 a) G# D; r8 n5 z( e - {
9 ~. Y1 H% E" b, J$ e - printf("out_data:");" P- V8 B; M% l6 V9 ?* m: q
- for (int i=0; i<AI_HAR_IGN_OUT_1_SIZE; i++)
8 q& D+ t9 k# y' v% a [1 q8 i - {
[: R4 y9 _; T. K - printf("%f ",((ai_float*)out_data)[i]);4 _8 W" c+ M' t, y+ m: e
- }2 _; K5 ]5 [3 p. |4 T
- printf("\n");" N8 p6 [4 T: l! m$ S
- return 0;# ]2 x# E# p0 L) D. k
- }
: r. |3 L: }; Q) n7 Y( T - /* USER CODE END 0 */$ w1 R G, L& I% n7 r
- # m3 {% S8 E$ U. w
- /**6 H- |+ G+ o0 O6 f# A) ?
- * @brief The application entry point.+ @3 f2 Q! z4 n! v3 ^3 J
- * @retval int7 k& f; F% H% B
- */+ l3 x8 N0 R8 j% A0 a" v3 m3 S
- int main(void)7 a# B! M' }1 [5 t5 l4 i% U
- {
G4 I" @, X: B% i - /* USER CODE BEGIN 1 */- B7 s) T0 ^7 I2 i
-
+ P3 w* ^$ z1 z) e+ N3 G8 J - /* USER CODE END 1 */
5 ~2 z" @* G* d7 u7 I8 j1 e1 p -
, k" P, b, E9 ^7 k7 A( g, [2 ]' P - /* MCU Configuration--------------------------------------------------------*/
: G# d3 @& l8 B; P* X -
1 P1 S9 v) {; }. @ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
1 o' ~) H4 c" C$ l$ w' f$ ] - HAL_Init();5 i5 u$ j) E% h1 S
-
+ u, k; h2 ~; s - /* USER CODE BEGIN Init */3 [+ E4 S4 [5 w6 s
-
2 q: \. o* S7 \4 q! y5 ]/ e - /* USER CODE END Init */
9 d1 L6 [$ T5 _4 L: T/ \4 O - 1 q3 n# e6 t" m' Y2 I4 j8 ^' [
- /* Configure the system clock */2 F$ _% J8 x9 t* Z+ D9 F
- SystemClock_Config();
% Z9 v- m, H$ T+ U& C/ ] -
+ O" z, s8 U; M& }6 I3 s - /* USER CODE BEGIN SysInit */
, K9 M" o3 l% b, M T -
* G1 ^+ q8 I$ n9 w- K6 n - /* USER CODE END SysInit */
" h/ Z2 e8 I- L& g2 P1 i$ G. f - * j* `$ P" l8 [. F& h% |
- /* Initialize all configured peripherals */
$ p' j3 a5 w" [ - MX_GPIO_Init();
& N8 `; Y1 c* w. H1 \1 i) e - MX_DFSDM1_Init();
, r0 M* N5 u9 X - MX_I2C2_Init();& b0 p0 E2 ^) i& j1 D0 Z& I- M3 |
- MX_QUADSPI_Init();
( V1 S# e$ c a; X4 p - MX_SPI3_Init();& k k7 y( C; M `. Q
- MX_USART1_UART_Init();
' _1 U4 {# e9 I$ w: y - MX_USART3_UART_Init(); t' e9 z' t0 T8 C
- MX_USB_OTG_FS_PCD_Init();
' D: q+ Y7 c. W$ @& \2 { - MX_CRC_Init();
+ e) t2 T+ V/ a0 b) R - /* USER CODE BEGIN 2 */
. j+ Q; Z, |9 y& J! a% J - ResetPrintInit(&huart1);
9 u+ ` z: H$ d: _) N+ ` - HAL_UART_Receive_IT(&huart1,(uint8_t *)&USART_NewData, 1); //再开启接收中断
x1 ^; X0 `4 O+ s6 J - USART_RX_STA = 0;/ Y% @4 _& M s7 l) K1 m8 V; t
- aiInit(); |5 z& c& l0 U# ^
- uint8_t factor = 1;
( m- O4 a! {' q& h, u7 V - buf_print();
; E; E1 ?# ?# Y. d7 ?! ]5 J - /* USER CODE END 2 */
8 q# ^: K1 V+ k6 L -
. z" P1 b' f A! \* h7 X - /* Infinite loop */! O2 k. ?. g" ^
- /* USER CODE BEGIN WHILE */# Z& j0 u* O% i+ i' M- Z" _
- while (1)
6 W3 Y8 k4 K. h7 R1 G2 } - {: R3 x$ f0 c' ~' c: f/ \& ~7 d
- if(USART_RX_STA&0xC000){//溢出或换行,重新开始
3 o/ A, i3 D1 t - printf("uart1:%.*s\r\n",USART_RX_STA&0X0FFF, USART_RX_BUF);
7 k5 X2 H4 u0 Y- Z8 p - if(strstr((const char*)USART_RX_BUF,(const char*)"test"))
+ v; ^9 a/ }7 l4 t - {
1 X$ H& ^/ _! i" W0 A - factor = ((uint8_t)USART_RX_BUF[4]-0x30);
* E* K7 V7 n) Q) m - printf("factor:%d\n",factor);+ f& G0 d( `3 ?2 e, ~
- acquire_and_process_data(in_data,factor);
- z, |# |% ?- b% T1 P. p - aiRun(in_data, out_data);: h3 r @4 q! b1 U
- post_process(out_data);0 H9 l' E1 O5 N& W2 G0 `
- }) o# Y b9 s: O; u
- USART_RX_STA=0;//接收错误,重新开始
6 o2 X- F4 O0 @6 W/ M6 T3 A* z - HAL_Delay(100);//等待
/ n% l4 m8 _, U/ |% M3 R - }
. k& X# q, M) I - /* USER CODE END WHILE */! k$ P3 Y: Y1 D
- 2 h- C# s% R1 O/ [
- /* USER CODE BEGIN 3 */
3 w2 r# M& I1 @4 \6 o8 Y - }
, b8 t8 j4 `+ m; g - /* USER CODE END 3 */8 V9 @! k* M3 g9 I
- }6 f1 y j0 ^7 {
- //其他生产代码
2 }, ]) Q) ~" h& R- l) \ - .......
复制代码
" ?. W' P2 G. m 7.4 编译及程序运行测试+ g0 j2 M( c+ K8 D# \
配置工程输出文件格式支持,并设置运行配置:4 D2 }; F8 x( S( y. A* U" e8 U* Y6 I
4 }2 A- n) ~& X) t' W
# h, B( Z6 L2 v
2 b9 J: `5 p4 R8 p) B. b8 F 编译及下载程序:: r0 {% f1 ]# J' l. b
( e8 K" Y# K+ Q
8 H; a6 \( s/ i7 e( f
- M3 r; f' A u6 q 打开串口助手,查看日志输出,发送信息,例如test7,即7作为因子生成输入数据,然后看输出结果。
1 ` Q% m2 o+ x7 v# q8 X8 U# n3 r3 E' I4 O7 \" t
7 P: m' a0 `5 [, z, i2 H
9 t0 E2 G3 h# W" Z: z( R$ _ 7.5 补充说明1 Z, `5 C/ Y4 ^6 \
目前只能说是采用cubeIDE+cube.AI+keras的STM32嵌入式人工智能开发走通了流程,但是串口反馈回来的日志信息是不合理的,因为在数据采集时我们只采集了传感器的三个数值,但在训练模型时,默认的数据输入量是24,显然是不合理的,因此需要还需要重新分析官方提供的HAR训练模型的项目,使得模型训练与采集数据匹配起来,请阅读篇二。
. q) V1 r$ F4 K
, T8 ~5 O. f k1 y
# y4 R; I5 x: y; c6 n% C
3 n# }2 f: d2 T/ @ 但考虑到官方提供的HAR训练模型的工程项目还是过于复杂,不助于学习和了解cube.AI的真正用法,因此后面将抛弃官方提供的HAR训练模型的项目,自行撰写一个训练模型项目+实际采集数据生成神经网络模型,是的数据输入和输出匹配,并将采用传感器实时采集到的数据进行计算评估
) x3 A* V9 d. i& B7 N0 U0 ^: _————————————————
% V. _ {+ Z, ]. K1 s版权声明:py_free-物联智能* i" T2 n; f7 s6 F/ S* s
如有侵权请联系删除: b8 ~, B: l2 {( H
0 g( j* [ F. Y* a3 e$ Y% l5 g
7 J# n- r) Y6 f* |7 i, G1 {0 {6 r
$ H% i# b* {- A, D$ I) U" ~+ I
* U: X7 F! Q( w |