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

发布于:2024-05-01 ⋅ 阅读:(26) ⋅ 点赞:(0)

早期版本的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里面的模型替换为上面生成的模型,编译上传即可看到呼吸灯了。
打开串品绘图窗口,如下:
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到