ESP32部署TensorFlow Lite

本来是想找一篇中文教程,不过只看到一个英文官方的,也行吧,虽然效率会慢丢丢。

GitHub - espressif/esp-tflite-micro: TensorFlow Lite Micro for Espressif Chipsets

看了一圈,有个中文的:

esp-dl/README_cn.md at master · espressif/esp-dl · GitHub

目前阶段还是照着做,然后写一点感想。。。后面有机会和能力再深度写一下。

之前都是用micropython,觉得简单。不过涉及到tensorflow lite的好像都是原厂环境。。。

没办法,搞了一阵终于把环境弄出来了。可以Hello了。。。

继续往机器学习的方向走,遇到的最大的问题就是不管是tensorflow还是tflite-micro,都无法下载。但是网上的很多教程都是先要拿到tensorflow lite。然后基于tensorflow lite的例子,送到idf环境去编译。

后面看了历险记(参考中)那个文章,说是现在乐鑫自己也有tflite的例子,可以曲线救国,先跑通乐鑫自己的例子,然后用python下的tensorflow生成lite模型,最后用xxd命令转换成model.cc。这个model.cc刚好又是乐鑫里面带了的。这样就可以闭环了。

照着他写的一步步来,晚上把乐鑫自己的hello world跑通了,还是很简单。(真不知道网上这些大神哪里找的教程,我是翻了半天都没翻到)

又试了下用tensorflow生成模型(生成模型和转换是在ubuntu上做的哈):

复制代码
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import math

x_values = np.random.uniform(low=0, high=2 * math.pi, size=1000).astype(np.float32)

np.random.shuffle(x_values)

y_values = np.sin(x_values).astype(np.float32)

plt.plot(x_values, y_values,"b.")
plt.show()

y_values += 0.1 * np.random.randn(*y_values.shape)

plt.plot(x_values, y_values, "b.")
plt.show()

TRAIN_SPLIT = int(0.6 * 1000)
TEST_SPLIT = int(0.2 * 1000 + TRAIN_SPLIT)

x_train, x_validate, x_test = np.split(x_values, [TRAIN_SPLIT, TEST_SPLIT])
y_train, y_validate, y_test = np.split(y_values, [TRAIN_SPLIT, TEST_SPLIT])

plt.plot(x_train, y_train, "b.", label="Train")
plt.plot(x_validate, y_validate, "y.", label="Validate")
plt.plot(x_test, y_test, "r.", label="Test")
plt.legend()
plt.show()

model = tf.keras.Sequential()

# First layer takes a scalar input and feeds it through 16 "neurons". The
# neurons decide whether to activate based on the 'relu' activation function.
model.add(tf.keras.layers.Dense(16, activation='relu', input_shape=(1,)))

# The new second and third layer will help the network learn more complex
# representations
model.add(tf.keras.layers.Dense(16, activation='relu'))

# Final layer is a single neuron, since we want to output a single value
model.add(tf.keras.layers.Dense(1))

# Compile the model using the standard 'adam' optimizer and the mean squared
# error or 'mse' loss function for regression.
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
model.summary()

model.fit(x_values,
          y_values,
          epochs=400,
          validation_split=0.2,
          batch_size=64,
          verbose=2)
save_dir = './modes/test'
tf.saved_model.save(model, save_dir)

converter = tf.lite.TFLiteConverter.from_saved_model(save_dir)
tflite_model = converter.convert()

import pathlib

tflite_model_file = pathlib.Path('model.tflite')
tflite_model_file.write_bytes(tflite_model)

def representative_dataset(num_samples=500):
    for i in range(num_samples):
      yield [x_values[i].reshape(1, 1)]

saved_model_dir = "./modes/test"
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
converter.representative_dataset = representative_dataset
tflite_quant_model = converter.convert()
open("sine_model_quantized.tflite","wb").write(tflite_quant_model)

运行python之后生成sine_model_quantized.tflite

按照例程中说的安装xdd

复制代码
sudo apt install xxd

然后运行转换命令。xdd命令也没什么特别的,就是在Linux中用于以二进制或十六进制显示文件的内容。-i 选项允许以C语言风格的数组形式输出文件内容。就是把二进制转成C的数组。

复制代码
xxd -i sine_model_quantized.tflite > model.cc

之后把model.cc拷贝回esp32的hello world环境。但是代码要稍微改改,主要是要把模型数组换成g_model,否则要报错。

model.cc

复制代码
#include "model.h"

alignas(8) const unsigned char g_model[] = {
  0x20, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x00, 0x00, 0x00, 0x00,
  0x14, 0x00, 0x20, 0x00, 0x1c, 0x00, 0x18, 0x00, 0x14, 0x00, 0x10, 0x00,
  0x0c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00, 0x00,
  0x1c, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0xe8, 0x00, 0x00, 0x00,
  0xa4, 0x03, 0x00, 0x00, 0xb4, 0x03, 0x00, 0x00, 0x0c, 0x0a, 0x00, 0x00,
  0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x0a, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x04, 0x00,
  0x0a, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
  0x38, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x73, 0x65, 0x72, 0x76,
  0x69, 0x6e, 0x67, 0x5f, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x98, 0xff, 0xff, 0xff,
  0x09, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  0x64, 0x65, 0x6e, 0x73, 0x65, 0x5f, 0x32, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x04, 0x00, 0x00, 0x00, 0xea, 0xfc, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00,
  0x0b, 0x00, 0x00, 0x00, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x5f, 0x69, 0x6e,
  0x70, 0x75, 0x74, 0x00, 0x02, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
  0x04, 0x00, 0x00, 0x00, 0xdc, 0xff, 0xff, 0xff, 0x0c, 0x00, 0x00, 0x00,
  0x04, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x43, 0x4f, 0x4e, 0x56,
  0x45, 0x52, 0x53, 0x49, 0x4f, 0x4e, 0x5f, 0x4d, 0x45, 0x54, 0x41, 0x44,
  0x41, 0x54, 0x41, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x04, 0x00,
  0x08, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  0x13, 0x00, 0x00, 0x00, 0x6d, 0x69, 0x6e, 0x5f, 0x72, 0x75, 0x6e, 0x74,
  0x69, 0x6d, 0x65, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x00,
  0x0d, 0x00, 0x00, 0x00, 0xb8, 0x02, 0x00, 0x00, 0xb0, 0x02, 0x00, 0x00,
  0x9c, 0x02, 0x00, 0x00, 0x74, 0x02, 0x00, 0x00, 0x24, 0x02, 0x00, 0x00,
  0x14, 0x01, 0x00, 0x00, 0xc4, 0x00, 0x00, 0x00, 0xa4, 0x00, 0x00, 0x00,
  0x9c, 0x00, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x8c, 0x00, 0x00, 0x00,
  0x6c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x96, 0xfd, 0xff, 0xff,
  0x04, 0x00, 0x00, 0x00, 0x58, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  0x08, 0x00, 0x0e, 0x00, 0x08, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00,
  0x10, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
  0x08, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0xeb, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
  0x10, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x04, 0x00, 0x0a, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  0x06, 0x00, 0x00, 0x00, 0x32, 0x2e, 0x31, 0x33, 0x2e, 0x31, 0x00, 0x00,
  0xfa, 0xfd, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  0x31, 0x2e, 0x31, 0x34, 0x2e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x84, 0xfd, 0xff, 0xff, 0x88, 0xfd, 0xff, 0xff,
  0x8c, 0xfd, 0xff, 0xff, 0x22, 0xfe, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00,
  0x10, 0x00, 0x00, 0x00, 0x81, 0x87, 0x92, 0x83, 0x60, 0x58, 0xc4, 0x24,
  0xa3, 0x72, 0xaf, 0x32, 0x94, 0x9d, 0x30, 0xe8, 0x3e, 0xfe, 0xff, 0xff,
  0x04, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xf2, 0x08, 0x00, 0x00, 0xb0, 0xf7, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  0x8c, 0xf8, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x31, 0xff, 0xff, 0xff,
  0x00, 0x00, 0x00, 0x00, 0x97, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x17, 0xf6, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  0x8a, 0xfe, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  0x44, 0xdb, 0xdb, 0x31, 0x2d, 0xf6, 0xfa, 0xba, 0x33, 0x09, 0xd7, 0x48,
  0xed, 0xcd, 0xc7, 0x21, 0x4d, 0x39, 0x14, 0x23, 0x16, 0x15, 0xb6, 0x40,
  0x37, 0x4f, 0xf0, 0xbc, 0xf2, 0xe5, 0xd6, 0xeb, 0xdf, 0xe7, 0x11, 0xf2,
  0x1d, 0x44, 0xde, 0x34, 0xf8, 0xda, 0xc2, 0x24, 0x05, 0x00, 0x32, 0xf3,
  0xd2, 0xeb, 0xbf, 0xe7, 0xf6, 0x34, 0xd9, 0x36, 0xb7, 0x14, 0x27, 0x0e,
  0xb1, 0x31, 0xf4, 0x22, 0xf1, 0x47, 0xd0, 0x08, 0x09, 0x15, 0x4c, 0x5c,
  0x39, 0x3f, 0x2f, 0xce, 0x0f, 0xdc, 0x20, 0x0b, 0x53, 0xf5, 0x33, 0x08,
  0xe8, 0x46, 0x13, 0x10, 0xf9, 0xc0, 0x29, 0x21, 0xd2, 0xd0, 0xf4, 0x37,
  0xee, 0xcc, 0xb5, 0xb5, 0xbf, 0x22, 0x2a, 0x3c, 0x39, 0x37, 0xfc, 0xb8,
  0x0d, 0x22, 0xba, 0xc6, 0xd0, 0x1e, 0xc0, 0x1a, 0xfc, 0xb2, 0xdc, 0xf7,
  0x34, 0x36, 0xe4, 0xdd, 0xcb, 0x32, 0xd4, 0x4d, 0x01, 0xf5, 0xbf, 0xc2,
  0xdd, 0xd8, 0xe7, 0xea, 0xcc, 0x39, 0xea, 0xcb, 0x11, 0x20, 0x33, 0xd9,
  0xbf, 0xce, 0xf5, 0x2e, 0x0f, 0x5e, 0x53, 0x0a, 0xd2, 0xe4, 0x1c, 0x1d,
  0x46, 0x0d, 0x39, 0xc7, 0xae, 0xb5, 0x40, 0xc0, 0x05, 0xba, 0x3a, 0xfe,
  0xca, 0x81, 0xd7, 0xa4, 0x09, 0xef, 0xee, 0x14, 0x29, 0xc6, 0x0c, 0xf2,
  0xe2, 0x20, 0xe3, 0xf9, 0xec, 0x42, 0x51, 0x46, 0xc7, 0xcd, 0x48, 0xe7,
  0x41, 0xd3, 0x40, 0xf9, 0xe2, 0x50, 0x02, 0xdc, 0x39, 0xb7, 0x15, 0xd1,
  0xe1, 0x34, 0x35, 0x0b, 0xeb, 0x0d, 0x0a, 0xcd, 0xf8, 0x0e, 0xb4, 0x1a,
  0xd3, 0xf8, 0xce, 0xb1, 0xee, 0x43, 0xbb, 0x4b, 0x2f, 0x18, 0x3e, 0x2d,
  0x33, 0x0f, 0x40, 0x3d, 0x02, 0xcd, 0xe1, 0xab, 0x29, 0xb7, 0x22, 0x41,
  0xe2, 0x49, 0x4b, 0x35, 0x20, 0xed, 0x50, 0xe4, 0x41, 0x24, 0xf7, 0x31,
  0x1d, 0xdf, 0xb8, 0xfa, 0x96, 0xff, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00,
  0x40, 0x00, 0x00, 0x00, 0xe6, 0x0b, 0x00, 0x00, 0xcd, 0xf2, 0xff, 0xff,
  0x10, 0xf5, 0xff, 0xff, 0x05, 0x0b, 0x00, 0x00, 0xb0, 0xf6, 0xff, 0xff,
  0x89, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xb0, 0xfe, 0xff, 0xff,
  0x00, 0x00, 0x00, 0x00, 0x45, 0xf5, 0xff, 0xff, 0x50, 0x1c, 0x00, 0x00,
  0x6f, 0xef, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0xfe, 0xff, 0xff, 0x31, 0x0c, 0x00, 0x00, 0xe2, 0xff, 0xff, 0xff,
  0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x50, 0xd0, 0xe9, 0x0b,
  0xbb, 0xcd, 0xe8, 0xfd, 0xca, 0xf0, 0x81, 0xd9, 0xfe, 0xe9, 0xe4, 0x32,
  0x00, 0x00, 0x06, 0x00, 0x08, 0x00, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00,
  0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xb0, 0x0a, 0x00, 0x00,
  0x84, 0xff, 0xff, 0xff, 0x88, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00,
  0x4d, 0x4c, 0x49, 0x52, 0x20, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74,
  0x65, 0x64, 0x2e, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x0e, 0x00, 0x18, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0c, 0x00,
  0x08, 0x00, 0x04, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
  0x1c, 0x00, 0x00, 0x00, 0xdc, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00,
  0xe4, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e,
  0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x84, 0x00, 0x00, 0x00,
  0x3c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x96, 0xff, 0xff, 0xff,
  0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x00, 0x00, 0x00,
  0x14, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  0x08, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0xca, 0xff, 0xff, 0xff, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  0x10, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0xba, 0xff, 0xff, 0xff,
  0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  0x03, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x16, 0x00, 0x00, 0x00,
  0x10, 0x00, 0x0c, 0x00, 0x0b, 0x00, 0x04, 0x00, 0x0e, 0x00, 0x00, 0x00,
  0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x18, 0x00, 0x00, 0x00,
  0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x08, 0x00, 0x07, 0x00,
  0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00,
  0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x06, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x0a, 0x00, 0x00, 0x00, 0xbc, 0x04, 0x00, 0x00, 0x2c, 0x04, 0x00, 0x00,
  0xa8, 0x03, 0x00, 0x00, 0x2c, 0x03, 0x00, 0x00, 0xc0, 0x02, 0x00, 0x00,
  0x4c, 0x02, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x34, 0x01, 0x00, 0x00,
  0x80, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x82, 0xfb, 0xff, 0xff,
  0x00, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  0x3c, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
  0x50, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  0x01, 0x00, 0x00, 0x00, 0x6c, 0xfb, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00,
  0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0xee, 0x06, 0x3c,
  0x19, 0x00, 0x00, 0x00, 0x53, 0x74, 0x61, 0x74, 0x65, 0x66, 0x75, 0x6c,
  0x50, 0x61, 0x72, 0x74, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x65, 0x64, 0x43,
  0x61, 0x6c, 0x6c, 0x3a, 0x30, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xfa, 0xfb, 0xff, 0xff,
  0x00, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  0x40, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
  0x88, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  0x10, 0x00, 0x00, 0x00, 0xe4, 0xfb, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00,
  0x14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x13, 0xa5, 0xce, 0x3b, 0x4c, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75,
  0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65,
  0x5f, 0x31, 0x2f, 0x4d, 0x61, 0x74, 0x4d, 0x75, 0x6c, 0x3b, 0x73, 0x65,
  0x71, 0x75, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f, 0x64, 0x65, 0x6e,
  0x73, 0x65, 0x5f, 0x31, 0x2f, 0x52, 0x65, 0x6c, 0x75, 0x3b, 0x73, 0x65,
  0x71, 0x75, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f, 0x64, 0x65, 0x6e,
  0x73, 0x65, 0x5f, 0x31, 0x2f, 0x42, 0x69, 0x61, 0x73, 0x41, 0x64, 0x64,
  0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x10, 0x00, 0x00, 0x00, 0xaa, 0xfc, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
  0x18, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x80, 0x00, 0x00, 0x00,
  0x02, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x10, 0x00, 0x00, 0x00,
  0x94, 0xfc, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0e, 0x2c, 0x46, 0x3c,
  0x46, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69,
  0x61, 0x6c, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x2f, 0x4d, 0x61, 0x74,
  0x4d, 0x75, 0x6c, 0x3b, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69,
  0x61, 0x6c, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x2f, 0x52, 0x65, 0x6c,
  0x75, 0x3b, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c,
  0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x2f, 0x42, 0x69, 0x61, 0x73, 0x41,
  0x64, 0x64, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x10, 0x00, 0x00, 0x00, 0xde, 0xfd, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
  0x14, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x09, 0x44, 0x00, 0x00, 0x00, 0x2c, 0xfd, 0xff, 0xff,
  0x08, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x01, 0xc6, 0x8e, 0x3b, 0x17, 0x00, 0x00, 0x00,
  0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f, 0x64,
  0x65, 0x6e, 0x73, 0x65, 0x2f, 0x4d, 0x61, 0x74, 0x4d, 0x75, 0x6c, 0x00,
  0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x46, 0xfe, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x14, 0x00, 0x00, 0x00,
  0x30, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
  0x50, 0x00, 0x00, 0x00, 0x94, 0xfd, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00,
  0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0xf8, 0xe0, 0x38,
  0x27, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69,
  0x61, 0x6c, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x2f, 0x42, 0x69, 0x61,
  0x73, 0x41, 0x64, 0x64, 0x2f, 0x52, 0x65, 0x61, 0x64, 0x56, 0x61, 0x72,
  0x69, 0x61, 0x62, 0x6c, 0x65, 0x4f, 0x70, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x10, 0x00, 0x00, 0x00, 0xb6, 0xfe, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
  0x14, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x09, 0x44, 0x00, 0x00, 0x00, 0x04, 0xfe, 0xff, 0xff,
  0x08, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x43, 0x4f, 0xa9, 0x3b, 0x19, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75,
  0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65,
  0x5f, 0x31, 0x2f, 0x4d, 0x61, 0x74, 0x4d, 0x75, 0x6c, 0x00, 0x00, 0x00,
  0x02, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  0x1e, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x14, 0x00, 0x00, 0x00,
  0x34, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
  0x58, 0x00, 0x00, 0x00, 0x6c, 0xfe, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00,
  0x14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x71, 0x10, 0x83, 0x38, 0x29, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75,
  0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65,
  0x5f, 0x31, 0x2f, 0x42, 0x69, 0x61, 0x73, 0x41, 0x64, 0x64, 0x2f, 0x52,
  0x65, 0x61, 0x64, 0x56, 0x61, 0x72, 0x69, 0x61, 0x62, 0x6c, 0x65, 0x4f,
  0x70, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  0x96, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x14, 0x00, 0x00, 0x00,
  0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
  0x44, 0x00, 0x00, 0x00, 0xe4, 0xfe, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00,
  0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x6e, 0x72, 0x29, 0x3c,
  0x19, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69,
  0x61, 0x6c, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x5f, 0x32, 0x2f, 0x4d,
  0x61, 0x74, 0x4d, 0x75, 0x6c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00,
  0x1c, 0x00, 0x18, 0x00, 0x17, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x08, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x16, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x01, 0x14, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
  0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x54, 0x00, 0x00, 0x00,
  0x64, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x58, 0xc7, 0x88, 0x38, 0x29, 0x00, 0x00, 0x00,
  0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f, 0x64,
  0x65, 0x6e, 0x73, 0x65, 0x5f, 0x32, 0x2f, 0x42, 0x69, 0x61, 0x73, 0x41,
  0x64, 0x64, 0x2f, 0x52, 0x65, 0x61, 0x64, 0x56, 0x61, 0x72, 0x69, 0x61,
  0x62, 0x6c, 0x65, 0x4f, 0x70, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x20, 0x00, 0x1c, 0x00,
  0x1b, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x08, 0x00, 0x07, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  0x18, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x64, 0x00, 0x00, 0x00,
  0x02, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00,
  0x0c, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00,
  0x0c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xcb, 0xb0, 0xc9, 0x3c,
  0x1d, 0x00, 0x00, 0x00, 0x73, 0x65, 0x72, 0x76, 0x69, 0x6e, 0x67, 0x5f,
  0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x5f, 0x64, 0x65, 0x6e, 0x73,
  0x65, 0x5f, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x3a, 0x30, 0x00, 0x00, 0x00,
  0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x10, 0x00,
  0x0f, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00,
  0x09, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09
};
unsigned int sine_model_quantized_tflite_len = 2664;

const int g_model_len = 2664;

然后就是编译三部曲:

复制代码
idf.py set-target esp32
idf.py build
idf.py flash monitor

最后就能看到结果:

好吧,虽然还是很low,全部都只是照着弄,但是总算是跑通了。我有时候甚至觉得搭建环境算是成功了一半。。。接着有时间我会慢慢来分析分析的。

https://github.com/espressif/esp-tflite-micro/tree/master

https://github.com/tensorflow/tflite-micro

参考:

Windows 平台工具链的标准设置 - ESP32 - --- ESP-IDF 编程指南 v5.2.2 文档

ESPFriends之ESP32模型部署训练历险记(一)_terserflowlite部署esp32-CSDN博客

【TinyML】用ESP32实现弱监督人体定位

最简单体验TinyML、TensorFlow Lite------ESP32跑机器学习(全代码)-CSDN博客

ESPFriends之ESP32模型部署训练历险记(一)_terserflowlite部署esp32-CSDN博客

Tensorflow Lite从入门到精通-CSDN博客

相关推荐
小鸡吃米…5 天前
基于 TensorFlow 的图像识别
人工智能·python·tensorflow
小鸡吃米…5 天前
TensorFlow - 构建计算图
人工智能·python·tensorflow
A懿轩A5 天前
【2026 最新】TensorFlow 安装配置详细指南 同时讲解安装CPU和GPU版本 小白也能轻松上手!逐步带图超详细展示(Windows 版)
人工智能·windows·python·深度学习·tensorflow
鲁邦通物联网5 天前
Python实现蜂窝链路监控:基于全认证边缘计算网关的开发实战
边缘计算·数据采集·工业数据采集·边缘网关·边缘计算网关·5g数采
CV@CV5 天前
具身智能平台设计实战|基于ROS+边缘计算,从搭建到部署
人工智能·边缘计算·具身智能
电子科技圈5 天前
XMOS推动智能音频等媒体处理技术从嵌入式系统转向全新边缘计算
人工智能·mcu·物联网·设计模式·音视频·边缘计算·iot
智驱力人工智能6 天前
地铁隧道轨道障碍物实时检测方案 守护城市地下动脉的工程实践 轨道障碍物检测 高铁站区轨道障碍物AI预警 铁路轨道异物识别系统价格
人工智能·算法·yolo·目标检测·计算机视觉·边缘计算
小鸡吃米…6 天前
TensorFlow 实现异或(XOR)运算
人工智能·python·tensorflow·neo4j
智驱力人工智能6 天前
机场鸟类活动智能监测 守护航空安全的精准工程实践 飞鸟检测 机场鸟击预防AI预警系统方案 机场停机坪鸟类干扰实时监测机场航站楼鸟击预警
人工智能·opencv·算法·安全·yolo·目标检测·边缘计算
小鸡吃米…6 天前
TensorFlow 实现梯度下降优化
人工智能·python·tensorflow·neo4j