TensorFlow Lite 是一种用于低运算能力终端的开源深度学习框架。它适用于微控制器和其他一些仅有数千字节内存的设备。它可以直接在“裸机”上运行,不需要操作系统支持、任何标准 C/C++ 库和动态内存分配。核心运行时(core runtime)在 Cortex M3 上运行时仅需 16KB,加上足以用来运行语音关键字检测模型的操作,也只需 22KB 的空间。
微控制器通常是小型、低能耗的计算设备,经常嵌入在只需要进行基本运算的硬件中,包括家用电器和物联网设备等。每年都有数十亿个微控制器被生产出来。微控制器通常针对低能耗和小尺寸进行优化,但代价是降低了处理能力、内存和存储。一些微控制器具有用来优化机器学习任务性能的功能。
通过在微控制器上运行机器学习推断,开发人员可以在不依赖于网络连接的情况下将 AI 添加到各种各样的硬件设备中,这经常用来克服带宽、功率以及由它们所导致的高延迟而造成的约束。在设备上运行推断也可以帮助保护隐私,因为没有数据从设备中发送出去。开发人员通过在大型设备上生成模型,使用专用程序转换模型,然后部署模型和处理程序在低运算能力的终端上实现智能应用。下面介绍我的实现过程。我的开发环境如下:
硬件:ESP-CAM
开发框架:Arduino 1.8.10
在arduino的库管理界面添加tensorflow lite和JPEGdecoder。然后在libraries/Arduino_TensorFlowLite/src/tensorflow/lite/experimental/micro/arduino/debug_log.cpp中查看波特率参数9600,你可以修改参数或者修改串口监视器的参数,总之要保持一致。
我打开tensorflow lite自带的案例person_detect,它的案例主要是在 SparkFun Edge(Apollo3 Blue)、Arduino MKRZERO、
STM32F746G 探索板(Discovery Board)实现。我手头有ESP-CAM开发板,主要修改了摄像头采集部分的程序。
修改arduino_image_provider.cpp
#include <JPEGDecoder.h>
#include “Arduino.h”
// The size of our temporary buffer for holding
// JPEG data received from the Arducam module
#define MAX_JPEG_BYTES 8182
// Camera library instance
// Temporary buffer for holding JPEG data from camera
uint8_t jpeg_buffer;
// Length of the JPEG data currently in the buffer
uint32_t jpeg_length = 0;
// iamge buffer
camera_fb_t * fb = NULL;
// Get the camera module ready
TfLiteStatus InitCamera(tflite::ErrorReporter error_reporter) {
error_reporter->Report(“Attempting to start Arducam”);
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
//init with high specs to pre-allocate larger buffers
if(psramFound()){
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf(“Camera init failed with error 0x%x”, err);
return kTfLiteError;
}
sensor_t * s = esp_camera_sensor_get();
//initial sensors are flipped vertically and colors are a bit saturated
if (s->id.PID == OV3660_PID) {
s->set_vflip(s, 1);//flip it back
s->set_brightness(s, 1);//up the blightness just a bit
s->set_saturation(s, -2);//lower the saturation
}
//drop down frame size for higher initial frame rate
s->set_framesize(s, FRAMESIZE_QVGA);
delay(100);
return kTfLiteOk;
}
// Begin the capture and wait for it to finish
TfLiteStatus PerformCapture(tflite::ErrorReporter* error_reporter) {
error_reporter->Report(“Starting capture”);
fb = esp_camera_fb_get();
if (!fb) {
Serial.println(“Camera capture failed”);
error_reporter->Report(“Camera capture failed”);
return kTfLiteError;
}
return kTfLiteOk;
}
// Read data from the camera module into a local buffer
TfLiteStatus ReadData(tflite::ErrorReporter* error_reporter) {
// This represents the total length of the JPEG data
jpeg_length = fb->len;
error_reporter->Report(“Reading %d bytes from Arducam”, jpeg_length);
// Ensure there’s not too much data for our buffer
if (jpeg_length > MAX_JPEG_BYTES) {
error_reporter->Report(“Too many bytes in buffer (%d)”,
MAX_JPEG_BYTES);
return kTfLiteError;
}
if (jpeg_length == 0) {
error_reporter->Report(“No data in esp-cam buffer”);
return kTfLiteError;
}
jpeg_buffer = fb->buf;
delayMicroseconds(15);
error_reporter->Report(“Finished reading”);
return kTfLiteOk;
}
修改arduino_detection_responder.cpp
#include “detection_responder.h”
#include “Arduino.h”
// Flash the blue LED after each inference
void RespondToDetection(tflite::ErrorReporter* error_reporter,
uint8_t person_score, uint8_t no_person_score) {
if(person_score>no_person_score){
Serial.println(“有人”);
}
error_reporter->Report(“Person score: %d No person score: %d”, person_score,
no_person_score);
}
运行结果如下: