[STM32N6]

【STM32N6570-DK测评】2、使用STM32N6快速实现AI视觉类应用(食物识别)

[复制链接]
1397|3
手机看帖
扫描二维码
随时随地手机跟帖
sujingliang|  楼主 | 2025-4-19 21:37 | 显示全部楼层 |阅读模式
本帖最后由 sujingliang 于 2025-4-19 21:48 编辑

STM32N6570-DK 的演示程序提供了一个基于 AI 的人物动作检测应用。要快速实现类似的 AI 视觉应用,ST 提供了完整的解决方案,开发者可能只需调整部分配置即可。
在此之前,仍需完成一些准备工作。下面以 ST 提供的食物识别模型为例,简要说明实现流程:

一、环境准备
简要说明一下:
1、python9或10,版本不要过高或过低。
2、STM32CubeIDE 1.18,后面会命令行方式调用进行编译。
3、STM32Cube programmer2.19,后面会命令行方式调用进行烧录。
4、stedgeai-core,后面会命令行方式调用进行将模型转化为C代码。
stedgeai-core可以作为命令行界面(CLmodelI)使用,并允许自动转换预训练的人工智能算法。包括神经网络和经典机器学习模型,将等效的优化C代码嵌入到应用程序中。
5、stm32ai-modelzoo,可以从中选取需要部署的model,为了快速直接用ST训练好的model
   stm32ai-modelzoo是一个预训练 AI 模型的集合,专门针对 STM32 MCU 优化,涵盖计算机视觉、音频处理、传感器数据分析等任务。
6、stm32ai-modelzoo-services,简单的理解:训练、部署模型,调用相关工具生成C代码,编译,烧录
提供了从用户数据集(BYOD和BYOM)轻松重新训练或微调任何模型的脚本。一组服务和链式服务,用于量化、基准测试、预测和评估任何模型(BYOM)。从用户AI模型自动生成的应用程序代码示例。
简化ST设备上的端到端AI模型集成。这可以与stm32ai-modelzoo结合使用。可用于快速部署。并提供了训练脚本,用于进行迁移学习或在自定义数据集上从头开始训练自己的模型。
stm32ai-modelzoo-services下载后,需在stm32ai-modelzoo-services目录安装依赖:
pip install -r requirements.txt

stm32ai-modelzoo和stm32ai-modelzoo-services需要安装在同级目录下:
6.png

7、n6-ai-getstarted,https://www.st.com/en/development-tools/stm32n6-ai.html
N6的ai快速开发支持,使stm32ai-modelzoo-services支持n6。
下载的zip文件解压后将其中的application_code覆盖stm32ai-modelzoo-services中的同名目录。

另外需要在系统PATH中增加STM32CubeProgrammer的路径,方便调用其中的命令。

用下图描述一下这些工具的关系:

5.png

二、从模型布置到烧录
通过本节可以实现从模型到烧录的过程:
7.png
需要人工修改user_config.yaml(stm32ai_main.py的配置文件)的内容。然后运行stm32ai_main.py完成模型转化C语言、编译、烧录。
1、模型
食品识别的model位于stm32ai-modelzoo\image_classification\mobilenetv2\ST_pretrainedmodel_public_dataset\food-101\mobilenet_v2_0.35_128_fft\mobilenet_v2_0.35_128_fft_int8.tflite

2、配置文件修改
以stm32ai-modelzoo-services\image_classification\src\config_file_examples\deployment_config.yaml为模板,修改自己的配置文件,拷贝一份deployment_config.yaml到stm32ai-modelzoo-services\image_classification\src并更名为user_config.yaml
原模板是一个flower识别模型的配置文件,修改后内容如下:

general:
  # path to a `.tflite` or `.onnx` file.
#  model_path: ../../../stm32ai-modelzoo/image_classification/mobilenetv2/ST_pretrainedmodel_public_dataset/flowers/mobilenet_v2_0.35_128_fft/mobilenet_v2_0.35_128_fft_int8.tflite
model_path: ../../../stm32ai-modelzoo/image_classification/mobilenetv2/ST_pretrainedmodel_public_dataset\food-101\mobilenet_v2_0.35_128_fft/mobilenet_v2_0.35_128_fft_int8.tflite

operation_mode: deployment

dataset:
#  name: flowers_dataset
#  class_names: [daisy, dandelion, roses, sunflowers, tulips]
  name: food-101
  class_names: [apple_pie,baby_back_ribs,baklava,beef_carpaccio,beef_tartare,beet_salad,beignets,bibimbap,bread_pudding,breakfast_burrito,bruschetta,caesarsalad,cannoli,caprese_salad,carrot_cake,ceviche,cheesecake,cheese_plate,chicken_curry,chicken_quesadilla,chicken_wings,chocolate_cake,chocolte_mousse,churros,clam_chowder,club_sandwich,crab_cakes,creme_brulee,croque_madame,cup_cakes,deviled_eggs,donuts,dumplings,edamame,eggs_beneict,escargots,falafel,filet_mignon,fish_and_chips,foie_gras,french_fries,french_onion_soup,french_toast,fried_calamari,fried_rice,frozen_yogrt,garlic_bread,gnocchi,greek_salad,grilled_cheese_sandwich,grilled_salmon,guacamole,gyoza,hamburger,hot_and_sour_soup,hot_dog,huevos_rancheos,hummus,ice_cream,lasagna,lobster_bisque,lobster_roll_sandwich,macaroni_and_cheese,macarons,miso_soup,mussels,nachos,omelette,onion_rings,oysters,pad_hai,paella,pancakes,panna_cotta,peking_duck,pho,pizza,pork_chop,poutine,prime_rib,pulled_pork_sandwich,ramen,ravioli,red_velvet_cake,risotto,smosa,sashimi,scallops,seaweed_salad,shrimp_and_grits,spaghetti_bolognese,spaghetti_carbonara,spring_rolls,steak,strawberry_shortcake,sushi,tacs,takoyaki,tiramisu,tuna_tartare,waffles]
  
preprocessing:
  resizing:
#    interpolation: bilinear
    interpolation: nearest
    aspect_ratio: crop
#  color_mode: rgb # rgb, bgr
  color_mode: rgb

tools:
  stedgeai:
    version: 10.0.0
    optimization: balanced
    on_cloud: False # Not Available For STM32N6
    path_to_stedgeai: C:/ST/STEdgeAI/2.0/Utilities/windows/stedgeai.exe
  path_to_cubeIDE: C:/ST/STM32CubeIDE_1.16.0/STM32CubeIDE/stm32cubeide.exe

deployment:
  c_project_path: ../../application_code/image_classification/STM32N6/
  IDE: GCC
  verbosity: 1
  hardware_setup:
    serie: STM32N6
    board: STM32N6570-DK

hydra:
  run:
    dir: ./experiments_outputs/${now:%Y_%m_%d_%H_%M_%S}

mlflow:
  uri: ./experiments_outputs/mlruns

主要修改的内容:
0、操作类型:
operation_mode: deployment
1、模型地址:
 model_path: ../../../stm32ai-modelzoo/image_classification/mobilenetv2/ST_pretrainedmodel_public_dataset\food-101\mobilenet_v2_0.35_128_fft/mobilenet_v2_0.35_128_fft_int8.tflite
2、class_names食品分类
 class_names: [apple_pie,baby_back_ribs,baklava,beef_carpaccio,beef_tartare,beet_salad,beignets,bibimbap,bread_pudding,breakfast_burrito,bruschetta,caesarsalad,cannoli,caprese_salad,carrot_cake,ceviche,cheesecake,cheese_plate,chicken_curry,chicken_quesadilla,chicken_wings,chocolate_cake,chocolte_mousse,churros,clam_chowder,club_sandwich,crab_cakes,creme_brulee,croque_madame,cup_cakes,deviled_eggs,donuts,dumplings,edamame,eggs_beneict,escargots,falafel,filet_mignon,fish_and_chips,foie_gras,french_fries,french_onion_soup,french_toast,fried_calamari,fried_rice,frozen_yogrt,garlic_bread,gnocchi,greek_salad,grilled_cheese_sandwich,grilled_salmon,guacamole,gyoza,hamburger,hot_and_sour_soup,hot_dog,huevos_rancheos,hummus,ice_cream,lasagna,lobster_bisque,lobster_roll_sandwich,macaroni_and_cheese,macarons,miso_soup,mussels,nachos,omelette,onion_rings,oysters,pad_hai,paella,pancakes,panna_cotta,peking_duck,pho,pizza,pork_chop,poutine,prime_rib,pulled_pork_sandwich,ramen,ravioli,red_velvet_cake,risotto,smosa,sashimi,scallops,seaweed_salad,shrimp_and_grits,spaghetti_bolognese,spaghetti_carbonara,spring_rolls,steak,strawberry_shortcake,sushi,tacs,takoyaki,tiramisu,tuna_tartare,waffles]
3、预处理
  resizing:
#    interpolation: bilinear
    interpolation: nearest
    aspect_ratio: crop
#  color_mode: rgb # rgb, bgr
  color_mode: rgb
4、stedgeai.exe路径
path_to_stedgeai: C:/ST/STEdgeAI/2.0/Utilities/windows/stedgeai.exe
5、stm32cubeide.exe路径
path_to_cubeIDE: C:/ST/STM32CubeIDE_1.16.0/STM32CubeIDE/stm32cubeide.exe
3、模型用于N6使用前的处理
第一次使用st edge ai core(model zoo调用它将您的模型转换为C代码)时,它会询问您是否同意共享信息,并等待您给y打“是”或给n打“否”。但在model zoo做为内部使用时,你不会看到它。
运行一下下面命令解决这个问题:
C:/***/STEdgeAI/2.0/Utilities/windows/stedgeai.exe generate --model C:\***\stm32ai-modelzoo\image_classification\mobilenetv2\ST_pretrainedmodel_public_dataset\food-101\mobilenet_v2_0.35_128_fft\mobilenet_v2_0.35_128_fft_int8.tflite --target stm32n6 --st-neural-art
原因见:https://community.st.com/t5/edge-ai/deploying-the-image-classification-model-on-the-stm32n6570-dk/m-p/792248#M4410

4、运行stm32ai_main.py

运行脚本签,将STM32N6570-DK将BOOT1调到1,从stlink取电,这样运行stm32ai_main.py后可以完成烧录
stm32ai-modelzoo-services\image_classification\src目录下运行
python stm32ai_main.py
如果正常,
将STM32N6570-DK将BOOT1调到0,reset。

三、运行情况
3.jpg

4.jpg




使用特权

评论回复
suncat0504| | 2025-5-13 13:56 | 显示全部楼层
人物动作检测类的应用,是面向什么场景的?防范犯罪?养老防护?准确度如何?对于动作的判定,需要预先定义吗?

使用特权

评论回复
LQClqc| | 2025-5-16 13:14 | 显示全部楼层
你好博主,在最后运行stm32ai_main.py的时候报错,错误是下面代码,请问您有遇到这个问题吗?怎么解决的?

LOAD ERROR: WindowsPath('E:/07_Stm32/stm32N657/stm32ai-modelzoo-main/stm32ai-modelzoo-services-main/stm32ai-modelzoo-services/image_classification/src/deployment_outputs/2025_05_16_13_00_27/network.h') and WindowsPath('E:/07_Stm32/stm32N657/stm32ai-modelzoo-main/stm32ai-modelzoo-services-main/stm32ai-modelzoo-services/image_classification/src/deployment_outputs/2025_05_16_13_00_27/network.h') are the same file
Error executing job with overrides: []
Traceback (most recent call last):
  File "E:\07_Stm32\stm32N657\stm32ai-modelzoo-main\stm32ai-modelzoo-services-main\stm32ai-modelzoo-services\image_classification\src\stm32ai_main.py", line 421, in <module>
    main()
  File "D:\26_anaconda3\envs\n61\lib\site-packages\hydra\main.py", line 94, in decorated_main
    _run_hydra(
  File "D:\26_anaconda3\envs\n61\lib\site-packages\hydra\_internal\utils.py", line 394, in _run_hydra
    _run_app(
  File "D:\26_anaconda3\envs\n61\lib\site-packages\hydra\_internal\utils.py", line 457, in _run_app
    run_and_report(
  File "D:\26_anaconda3\envs\n61\lib\site-packages\hydra\_internal\utils.py", line 223, in run_and_report
    raise ex
  File "D:\26_anaconda3\envs\n61\lib\site-packages\hydra\_internal\utils.py", line 220, in run_and_report
    return func()
  File "D:\26_anaconda3\envs\n61\lib\site-packages\hydra\_internal\utils.py", line 458, in <lambda>
    lambda: hydra.run(
  File "D:\26_anaconda3\envs\n61\lib\site-packages\clearml\binding\hydra_bind.py", line 91, in _patched_hydra_run
    return PatchHydra._original_hydra_run(self, config_name, task_function, overrides, *args, **kwargs)
  File "D:\26_anaconda3\envs\n61\lib\site-packages\hydra\_internal\hydra.py", line 132, in run
    _ = ret.return_value
  File "D:\26_anaconda3\envs\n61\lib\site-packages\hydra\core\utils.py", line 260, in return_value
    raise self._return_value
  File "D:\26_anaconda3\envs\n61\lib\site-packages\hydra\core\utils.py", line 186, in run_job
    ret.return_value = task_function(task_cfg)
  File "D:\26_anaconda3\envs\n61\lib\site-packages\clearml\binding\hydra_bind.py", line 230, in _patched_task_function
    return task_function(a_config, *a_args, **a_kwargs)
  File "E:\07_Stm32\stm32N657\stm32ai-modelzoo-main\stm32ai-modelzoo-services-main\stm32ai-modelzoo-services\image_classification\src\stm32ai_main.py", line 407, in main
    process_mode(mode=mode, configs=cfg, train_ds=train_ds, valid_ds=valid_ds, quantization_ds=quantization_ds,
  File "E:\07_Stm32\stm32N657\stm32ai-modelzoo-main\stm32ai-modelzoo-services-main\stm32ai-modelzoo-services\image_classification\src\stm32ai_main.py", line 302, in process_mode
    deploy(cfg=configs)
  File "E:\07_Stm32\stm32N657\stm32ai-modelzoo-main\stm32ai-modelzoo-services-main\stm32ai-modelzoo-services\image_classification\src\../deployment\deploy.py", line 90, in deploy
    stm32ai_deploy_stm32n6(target=board, stlink_serial_number=stlink_serial_number, stm32ai_version=stm32ai_version, c_project_path=c_project_path,
  File "E:\07_Stm32\stm32N657\stm32ai-modelzoo-main\stm32ai-modelzoo-services-main\stm32ai-modelzoo-services\image_classification\src\../../common/deployment\common_deploy.py", line 469, in stm32ai_deploy_stm32n6
    stmaic_local_call(session)
  File "E:\07_Stm32\stm32N657\stm32ai-modelzoo-main\stm32ai-modelzoo-services-main\stm32ai-modelzoo-services\image_classification\src\../../common/deployment\common_deploy.py", line 443, in stmaic_local_call
    stmaic.compile(session=session, options=opt, target=session._board_config)
  File "E:\07_Stm32\stm32N657\stm32ai-modelzoo-main\stm32ai-modelzoo-services-main\stm32ai-modelzoo-services\image_classification\src\../../common\stm32ai_local\compile.py", line 208, in cmd_compile
    raise Exception('Error during compilation')
Exception: Error during compilation

使用特权

评论回复
LQClqc| | 2025-5-16 16:59 | 显示全部楼层
本帖最后由 LQClqc 于 2025-5-16 17:11 编辑

博主我看您C:/ST/STEdgeAI/2.0/Utilities/windows/stedgeai.exe这个路径下edgeai版本是2.0请问是如何下载的,我按照您上面那链接只能下载2.1和1.0,下载2.0的时候,需要导入stedgeai-stneuralart-10.0.0.zip。我无法找到这个是在哪里下载的。
我使用cubemx下载10.0.0后,修改路径到cubemx下载的10.0.0中,编译还是报错,显示下面错误,找不到Neural-Art.。
E011(ConfigurationError): Neural-Art configuration: Cannot find the Neural-Art compiler
        Add it to your PATH
        Specify a path to a Neural-Art compiler in the json file (NA_binary key in Globals)
        From a pack, it should come embedded with the pack
$ cwd:  None
这是我的user_config.yaml
general:
  # path to a `.tflite` or `.onnx` file.
#  model_path: ../../../stm32ai-modelzoo/image_classification/mobilenetv2/ST_pretrainedmodel_public_dataset/flowers/mobilenet_v2_0.35_128_fft/mobilenet_v2_0.35_128_fft_int8.tflite
model_path: ../../../stm32ai-modelzoo/image_classification/mobilenetv2/ST_pretrainedmodel_public_dataset\food-101\mobilenet_v2_0.35_128_fft/mobilenet_v2_0.35_128_fft_int8.tflite

operation_mode: deployment

dataset:
#  name: flowers_dataset
#  class_names: [daisy, dandelion, roses, sunflowers, tulips]
  name: food-101
  class_names: [apple_pie,baby_back_ribs,baklava,beef_carpaccio,beef_tartare,beet_salad,beignets,bibimbap,bread_pudding,breakfast_burrito,bruschetta,caesarsalad,cannoli,caprese_salad,carrot_cake,ceviche,cheesecake,cheese_plate,chicken_curry,chicken_quesadilla,chicken_wings,chocolate_cake,chocolte_mousse,churros,clam_chowder,club_sandwich,crab_cakes,creme_brulee,croque_madame,cup_cakes,deviled_eggs,donuts,dumplings,edamame,eggs_beneict,escargots,falafel,filet_mignon,fish_and_chips,foie_gras,french_fries,french_onion_soup,french_toast,fried_calamari,fried_rice,frozen_yogrt,garlic_bread,gnocchi,greek_salad,grilled_cheese_sandwich,grilled_salmon,guacamole,gyoza,hamburger,hot_and_sour_soup,hot_dog,huevos_rancheos,hummus,ice_cream,lasagna,lobster_bisque,lobster_roll_sandwich,macaroni_and_cheese,macarons,miso_soup,mussels,nachos,omelette,onion_rings,oysters,pad_hai,paella,pancakes,panna_cotta,peking_duck,pho,pizza,pork_chop,poutine,prime_rib,pulled_pork_sandwich,ramen,ravioli,red_velvet_cake,risotto,smosa,sashimi,scallops,seaweed_salad,shrimp_and_grits,spaghetti_bolognese,spaghetti_carbonara,spring_rolls,steak,strawberry_shortcake,sushi,tacs,takoyaki,tiramisu,tuna_tartare,waffles]

preprocessing:
  resizing:
#    interpolation: bilinear
    interpolation: nearest
    aspect_ratio: crop
#  color_mode: rgb # rgb, bgr
  color_mode: rgb

tools:
  stedgeai:
    version: 10.0.0
    optimization: balanced
    on_cloud: False # Not Available For STM32N6
    path_to_stedgeai: C:/Users/liuqingchang/STM32Cube/Repository/Packs/STMicroelectronics/X-CUBE-AI/10.0.0/Utilities/windows/stedgeai.exe #D:/16_STEdgeAI/2.1/Utilities/windows/stedgeai.exe
  path_to_cubeIDE: D:/15_STM32CubeIDE_1.18.0/stm32cubeide1.17/STM32CubeIDE_1.17.0/STM32CubeIDE/stm32cubeide.exe

deployment:
  c_project_path: ../../application_code/image_classification/STM32N6/
  IDE: GCC
  verbosity: 1
  hardware_setup:
    serie: STM32N6
    board: STM32N6570-DK

hydra:
  run:
    dir: ./experiments_outputs/${now:%Y_%m_%d_%H_%M_%S}
#    dir: ./deployment_outputs/${now:%Y_%m_%d_%H_%M_%S}
mlflow:
  uri: ./experiments_outputs/mlruns


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

62

主题

120

帖子

2

粉丝