打印
[STM32N6]

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

[复制链接]
201|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 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需要安装在同级目录下:

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的路径,方便调用其中的命令。

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


二、从模型布置到烧录
通过本节可以实现从模型到烧录的过程:
需要人工修改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。

三、运行情况





使用特权

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

本版积分规则

56

主题

112

帖子

0

粉丝