TinyML之Hello world----基于Arduino Nano 33 BLE Sense Rev2的呼吸灯

本文介绍了如何通过TensorFlowLiteMicro的最新示例实现HelloWorld项目,包括环境设置(如Bazel和Colab的使用),模型训练和量化,以及将模型移植到ArduinoNano33BLESenseRev2的步骤。
摘要由CSDN通过智能技术生成

早期版本的Hello World

这应该是一个逼格比较高的呼吸灯了,用ML来实现呼吸灯功能,之前已经有大佬发过类似的文章:https://blog.csdn.net/weixin_45116099/article/details/126310816

当前版本的Hello World

这是一个ML的入门例程,但是随着版本的变更,现在最新的例程已经采用了新的方法:https://github.com/tensorflow/tflite-micro/tree/main/tensorflow/lite/micro/examples/hello_world

说实话,我一看到里面的bazel,直接就蒙了,光搭建开发环境就可以把我们菜鸟劝退。

好在,有colab,这里不得不表扬一下人家业界巨佬的奉献精神,有了colab,好多基础环境人家Google已经帮你搭建好了。

环境搭建

其实,所谓的环境搭建,就是在colab里安装bazel,下载github工程:

!npm install -g @bazel/bazelisk
!git clone https://github.com/tensorflow/tflite-micro.git

一条代码搞定。

Run the evaluate.py script on a development machine

把目录切换到/content/tflite-micro,这里插一句,colab的目录切换用!cd好像不太好用,还是得用python

import os
path = "/content/tflite-micro"
os.chdir(path)
print(os.getcwd())

然后运行:

bazel build tensorflow/lite/micro/examples/hello_world:evaluate
bazel run tensorflow/lite/micro/examples/hello_world:evaluate
bazel run tensorflow/lite/micro/examples/hello_world:evaluate -- --use_tflite

Run the evaluate_test.py script on a development machine

bazel build tensorflow/lite/micro/examples/hello_world:evaluate_test
bazel run tensorflow/lite/micro/examples/hello_world:evaluate_test

Run the tests on a development machine

bazel run tensorflow/lite/micro/examples/hello_world:hello_world_test
make -f tensorflow/lite/micro/tools/make/Makefile test_hello_world_test

Train your own model

bazel build tensorflow/lite/micro/examples/hello_world:train
bazel-bin/tensorflow/lite/micro/examples/hello_world/train --save_tf_model 
--save_dir=/tmp/model_created/
bazel build tensorflow/lite/micro/examples/hello_world/quantization:ptq
bazel-bin/tensorflow/lite/micro/examples/hello_world/quantization/ptq  
--source_model_dir=/tmp/model_created --target_dir=/tmp/quant_model/

这样,就在/tmp/quant_model/下生成了量化后的模型参数,用下列代码转换成Arduino可以运行的模型参数:

import os
path = "/tmp/quant_model"
os.chdir(path)
print(os.getcwd())
!apt get -qq install xxd
!xxd -i hello_world_int8.tflite > hello.cc
!cat hello.cc

在Arduino Nano 33 BLE Sense Rev2上运行

在Arduino IDE里打开Harvard_TinyMLx下的hello world工程,将里面的model.cpp里面的模型替换为上面生成的模型,编译上传即可看到呼吸灯了。
打开串品绘图窗口,如下:
在这里插入图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值