三、工程调整
从前面分析来看,输入数据数量还是采用默认数值24问题不大(各位粉丝可以自行调整其大小来测试及观察效果),输出数据数量由于有WISDM数据集参与,保持'Jogging', 'Stationary', 'Stairs', 'Walking'是中分类也OK。现注意到,其实输入数据是经过了预处理转换实际加速度值,而非是传感器原始输出数值。
由于自行采集数据增加了Stationary数据文件,因此在cubeMX上采用新的训练模型文件重新生成c语言的神经网络模型。
然后调整输入数据及预处理函数acquire_and_process_data,采用真实数据来测试
该函数有原来的随机赋值,
- int acquire_and_process_data(void *in_data,int factor)
- {
- printf("in_data:");
- for (int i=0; i<AI_HAR_IGN_IN_1_SIZE; i++)
- {
- switch(i%3){
- case 0:
- ((ai_float*)in_data) = -175+(ai_float)(i*factor*1.2)/10.0;
- break;
- case 1:
- ((ai_float*)in_data) = 50+(ai_float)(i*factor*0.6)/100.0;
- break;
- case 2:
- ((ai_float*)in_data) = 975-(ai_float)(i*factor*1.8)/100.0;
- break;
- default:
- break;
- }
- printf("%.4f ",((ai_float*)in_data));
- }
- printf("\n");
- return 0;
- }
复制代码
调整为
- ai_float in_buf[] =
- {
- -1.9654135467252831,-0.14388957575400915,9.36735860765576,
- -1.6297827960727935,0.6647544204312931,9.618930851170278,
- -1.7208332169161387,0.38462856310059845,9.492079217583663,
- -1.6814190066807724,0.5346365723749872,9.648173176699613,
- -1.7298486510592452,0.42164964981080416,9.557259289818587,
- -1.7618787694384546,0.45864558999786653,9.653153776935605,
- -1.7410197123193858,0.4236369742675384,9.55486293595946,
- -1.7600076822930908,0.46214612481362705,9.594426626710453,
- -1.5761631958773263,0.3715109598910308,9.436853714636964,
- -1.5920827364351244,0.37070313540523914,9.66189484448469,
- -1.6178308849438598,0.37500917334673567,9.695719226290015,
- -1.4388296833472143,0.6108605310285585,9.464814699883437,
- -1.5651621282887258,0.5691273914891515,9.513897717476588,
- -1.4637992479412343,0.5105873209777632,9.501636895304161,
- -0.6794677157685166,0.5024637601753793,8.96404801376064,
- 0.2600149177042748,0.6699546179356337,8.903349009412763,
- 1.0712686735261918,1.4889662656074603,9.520348132500752,
- 0.3914123345764725,1.4210706041563634,10.557387805652848,
- 1.0779003359396493,1.0582703827741018,10.454469820960814,
- 0.12433283758079197,-0.27273511643713033,10.328552286632643,
- -0.010219096051988997,0.2961821896002729,9.483084545625971,
- -1.6910112286007235,-0.2898761724876157,9.704755735796937,
- -2.693651827312974,-0.41126025575408387,9.825328217800239,
- -2.8416981790648177,-0.14586229740441406,9.880552703938179
- };
- int acquire_and_process_data(void *in_data,int factor)
- {
- printf("in_data:");
- for (int i=0; i<AI_HAR_IGN_IN_1_SIZE; i++)
- {
- ((ai_float*)in_data) =in_buf;
- printf("%.4f ",((ai_float*)in_data));
- }
- printf("\n");
- return 0;
- }
复制代码
重新编辑及加载到开发板,打开串口助手,输入test*,查看测试效果如下,显然和期待有很大出入:
四、测试有误问题
上述结果和HAR项目中的PrepareDataset.py分类似乎并没匹配上,那么就要分析PrepareDataset.py分类和生成后的c语言模型时如何对应的,以及用来测试的数据是否符合模型要求。
为此,再次回到HAR训练模型项目,在RunMe.py文件中追加以下语句:
- print("TestX:")
- print(TestX[0:1])
- print("TestY:")
- print(TestY[0:1])
复制代码
以获取用来测试的真实模拟数据,再次运行python3 .\RunMe.py --dataDir=Log_data命令,最后部分输出如下:
- TestX:
- [[[[-3.64077855e-16]
- [-4.69612372e-16]
- [ 3.68092310e-10]]
- [[ 1.02756571e+01]
- [-1.14305305e+01]
- [ 2.61872125e+01]]
- [[-2.84181689e+00]
- [-3.54747048e+00]
- [-5.51206446e+00]]
- [[-3.82102513e+00]
- [-1.41233186e+01]
- [-4.59900586e+00]]
- [[ 6.68010824e+00]
- [ 9.39457601e+00]
- [-2.96397789e+00]]
- [[-1.71771782e+01]
- [ 1.19374149e+01]
- [ 3.05770680e+00]]
- [[-6.65782005e+00]
- [ 2.39062819e+00]
- [ 3.22844912e+00]]
- [[ 4.59021292e+00]
- [-6.27548028e+00]
- [-4.92783556e+00]]
- [[ 8.03018658e+00]
- [-2.72208600e+00]
- [-6.35796053e+00]]
- [[ 7.73164454e+00]
- [-6.31879160e+00]
- [-5.90723810e+00]]
- [[ 8.53803514e-01]
- [-9.75763211e+00]
- [ 1.02466115e+01]]
- [[ 1.11299171e+01]
- [-1.70658346e+01]
- [ 2.18511283e+01]]
- [[ 3.92044994e-01]
- [ 5.94768181e+00]
- [ 4.30131750e+00]]
- [[-5.61807988e+00]
- [ 1.97310400e+01]
- [-2.22512540e+00]]
- [[ 3.86836548e+00]
- [ 1.71617325e+00]
- [-5.86292387e+00]]
- [[ 7.65913325e+00]
- [-7.19628424e+00]
- [ 2.01628025e+00]]
- [[-7.52357836e+00]
- [ 3.68102584e+00]
- [-1.22753233e+01]]
- [[-5.12351958e+00]
- [ 1.23941669e+01]
- [-1.77385540e+00]]
- [[-4.86155823e-01]
- [ 1.26333902e+01]
- [ 5.93595914e+00]]
- [[-1.96569165e+01]
- [ 1.00467317e+01]
- [ 9.47374003e+00]]
- [[-4.34050581e+00]
- [ 5.16311148e-01]
- [-5.63004156e-01]]
- [[-3.57974669e+00]
- [ 4.87240857e-01]
- [-9.38271247e-01]]
- [[ 6.11930536e+00]
- [ 5.99067573e+00]
- [-7.68834262e+00]]
- [[ 1.12153409e+01]
- [ 2.37168199e+00]
- [-7.40963357e+00]]]]
- TestY:
- [[1. 0. 0. 0.]]
- PS D:\tools\arm_tool\STM32CubeIDE\STM32CubeFunctionPack_SENSING1_V4.0.3\Utilities\AI_Ressources\Training Scripts\HAR>
复制代码
显然输入24*3的一组数据,输出结果是[1. 0. 0. 0.],再将该数据用于测试 c语言模型神经网络模型API调用。
- ai_float in_buf[] =
- {
- -3.64077855e-16,-4.69612372e-16,3.68092310e-10,
- 1.02756571e+01,-1.14305305e+01,2.61872125e+01,
- -2.84181689e+00,-3.54747048e+00,-5.51206446e+00,
- -3.82102513e+00,-1.41233186e+01,-4.59900586e+00,
- 6.68010824e+00, 9.39457601e+00,-2.96397789e+00,
- -1.71771782e+01,1.19374149e+01,3.05770680e+00,
- -6.65782005e+00,2.39062819e+00,3.22844912e+00,
- 4.59021292e+00,-6.27548028e+00,-4.92783556e+00,
- 8.03018658e+00,-2.72208600e+00,-6.35796053e+00,
- 7.73164454e+00,-6.31879160e+00,-5.90723810e+00,
- 8.53803514e-01,-9.75763211e+00, 1.02466115e+01,
- 1.11299171e+01,-1.70658346e+01, 2.18511283e+01,
- 3.92044994e-01, 5.94768181e+00, 4.30131750e+00,
- -5.61807988e+00, 1.97310400e+01,-2.22512540e+00,
- 3.86836548e+00, 1.71617325e+00,-5.86292387e+00,
- 7.65913325e+00,-7.19628424e+00, 2.01628025e+00,
- -7.52357836e+00, 3.68102584e+00,-1.22753233e+01,
- -5.12351958e+00, 1.23941669e+01,-1.77385540e+00,
- -4.86155823e-01, 1.26333902e+01, 5.93595914e+00,
- -1.96569165e+01, 1.00467317e+01, 9.47374003e+00,
- -4.34050581e+00, 5.16311148e-01,-5.63004156e-01,
- -3.57974669e+00, 4.87240857e-01,-9.38271247e-01,
- 6.11930536e+00, 5.99067573e+00,-7.68834262e+00,
- 1.12153409e+01, 2.37168199e+00,-7.40963357e+00
- };
- int acquire_and_process_data(void *in_data,int factor)
- {
- printf("in_data:");
- for (int i=0; i<AI_HAR_IGN_IN_1_SIZE; i++)
- {
- ((ai_float*)in_data) =in_buf;
- printf("%.4f ",((ai_float*)in_data));
- }
- printf("\n");
- return 0;
- }
复制代码
再次编译下载程序,采用串口助手测试如下,输出数据为,【0.943687 0.000000 0.000294 0.056019】,而实际数据是[1. 0. 0. 0.],显然能对应上,只是精度问题:
五、精度问题
再次回到HAR训练项目上,如前文所述,训练结果按时间生成一个目录进行输出:
输出了训练的acc 、loss变化曲线图和混淆矩阵图,观察可以看到,用于训练数据其主要还是来自于WISDM的数据集占据大头,从混淆矩阵也可以看出,为何前面测试时输出结果0.943687 0.000000 0.000294 0.056019】,显然训练过程中,'Jogging'姿态会有一部分判断 'Stairs', 'Walking'姿态。
在训练过程日志显示:
再次改写RunMe.py,加入下面打印语句,查看用于训练、验证及测试的数据集量级:
- print("TrainX:"+str(len(TrainX)))
- print("TrainY:"+str(len(TrainY)))
- print("ValidationX:"+str(len(ValidationX)))
- print("ValidationY:"+str(len(ValidationY)))
- print("TestX:"+str(len(TestX)))
- print("TestY:"+str(len(TestY)))
复制代码
再次运行python3 .\RunMe.py --dataDir=Log_data命令:
通常在实际项目中,不断优化算法模型是主要原因,但相信ST公司及其合作团队应该就算法模型做了足够好,而本文测试验证为何还出现精度相差这么大,应该是以下几点:
1)本文用于训练模型的数据集不是同一设备,同一批次,统一采集方法获得足够量数据集。
2)本文自行采集的数量偏小,可能预WISDM采集数据方法存在差异而出现精确性问题,并加入其中,还是会影响模型训练精度的(若仅采用WISDM采集数据集)。
3)本文仅采用了HAR训练项目建议的默认参数,实际项目中,可能需要我们根据数据集及部署环境,不断尝试不同参数设置来训练和使用神经网络模型。
4)cube.AI将keras训练的神经网络模型转换为c语言的神经网络模型虽然损耗极小,但还是存在一定精度损失的。
篇一、二,是基于FP-AI-SENSING1案例和配套的B-L475E-IOT01A开发板来使用cube.AI软件包的,那么在实际使用cube.AI时,需要与我们项目实际硬件平台,并基于该硬件平台采集的数据进行数据预处理和神经网络模型构建、训练、验证及测试,然后再通过cubeMX和cube.AI将训练好的神经网络模型转换为c语言支持的AI模型,通过嵌入式程序加载(AI模型+cube.AI)从而实现神经网络计算的前端或边缘端部署,请阅读偏三。
|