動作
Feature #48
已結束是由 宏益 廖 於 約 1 年 前更新
- 檔案 clipboard-202310041629-brmxi.png clipboard-202310041629-brmxi.png 已新增
- 檔案 clipboard-202310041629-oew8k.png clipboard-202310041629-oew8k.png 已新增
*TensorFlow Lite¶
TensorFlow Lite(TFLite)是由Google開發的開源深度學習框架TensorFlow的輕量級版本,
可以在行動裝置、嵌入式裝置和邊緣裝置上執行機器學習模型。
主要目標是使機器學習模型能夠在資源有限的環境中有效運行,包括智慧型手機、物聯網設備、嵌入式系統和其他邊緣設備。¶
============================================================================================¶
*使用函示庫 >> EloquentTinyML.h¶
因為PlatformIO上的版本太舊,所以請自行到Github下載
"並按照上圖說明安裝"
Github: https://github.com/eloquentarduino/EloquentTinyML¶
============================================================================================¶
*Sine程式¶
教學: https://alankrantas.medium.com/%E6%9A%A2%E6%89%80%E6%AC%B2%E8%A8%80%E7%9A%84-tinyml-%E4%BD%BF%E7%94%A8-eloquenttinyml-%E8%B6%85%E8%BC%95%E9%AC%86%E4%BD%88%E7%BD%B2-tensorflow-lite-%E7%A5%9E%E7%B6%93%E7%B6%B2%E8%B7%AF%E6%A8%A1%E5%9E%8B%E5%88%B0%E5%BE%AE%E6%8E%A7%E5%88%B6%E5%99%A8%E4%B8%8A-%E5%A6%82-esp32-%E5%8F%8A-arduino-nano-33-ble-75900f5f9fb9¶
#include <Arduino.h>
#include "EloquentTinyML.h"
#include "eloquent_tinyml/tensorflow.h"
#include "sine_model.h" // TinyML 模型
#define NUMBER_OF_INPUTS 1
#define NUMBER_OF_OUTPUTS 1
#define TENSOR_ARENA_SIZE 2 * 1024 // 模型使用記憶體大小
Eloquent::TinyML::TensorFlow::MutableTensorFlow<NUMBER_OF_INPUTS, NUMBER_OF_OUTPUTS, TENSOR_ARENA_SIZE> tf;
void setup() {
Serial.begin(115200);
tf.addBuiltinOp(BuiltinOperator_FULLY_CONNECTED, Register_FULLY_CONNECTED(), 1, 9);
tf.begin((unsigned char*) model_data); // 匯入模型
}
void loop() {
// 隨機產生 x 和 y 當預測資料
float x = 3.14 * random(101) / 100;
float y = sin(x) * cos(x);
float input[1] = {x};
float predicted = tf.predict(input);
Serial.print("Data: f(");
Serial.print(x);
Serial.print(") = ");
Serial.print(y);
Serial.print("\t predicted: ");
Serial.println(predicted);
delay(100);
}
============================================================================================¶
*範例程式¶
#include "EloquentTinyML.h"
#include "eloquent_tinyml/tensorflow.h"
#include "spectrum_model.h"
#define NUMBER_OF_INPUTS 25
#define NUMBER_OF_OUTPUTS 2
#define TENSOR_ARENA_SIZE 4 * 1024 // 模型使用記憶體大小
Eloquent::TinyML::TensorFlow::MutableTensorFlow <NUMBER_OF_INPUTS, NUMBER_OF_OUTPUTS, TENSOR_ARENA_SIZE> tf;
void setup() {
Serial.begin(115200);
tf.addBuiltinOp(BuiltinOperator_FULLY_CONNECTED, Register_FULLY_CONNECTED(), 1, 9);
tf.addBuiltinOp(BuiltinOperator_SOFTMAX, Register_SOFTMAX(), 1, 1);
tf.addBuiltinOp(BuiltinOperator_LOGISTIC, Register_LOGISTIC(), 1, 1);
tf.begin((unsigned char*) model_data);
float test_list[25] = {0.0, 0.003030717, 0.018056406, 0.007386915, 0.0, 0.001869699, 0.000449, 0.000718, 0.006982542, 0.016715601, 0.024452891, 0.028154038, 0.047208875, 0.047339678, 0.033032645, 0.029521797, 0.03147052, 0.026025888, 0.033922244, 0.060511492, 0.07208954, 0.056529667, 0.035075348, 0.06048418, 0.022952799};
Serial.printf("Std features = ");
/*for(int i = 0; i < NOP; i++){
if(i % 3 == 0){
std_features[i / 3] = (float) std_spec[i];
Serial.printf("%.6f ", std_features[i / 3]);
}
}*/
float prediction[2] = {};
tf.predict(test_list, prediction);
//tf.predict(std_features, prediction);
Serial.printf("\nReslut of predict : %.2f, %.2f", prediction[0], prediction[1]);
}
void loop() {
}
動作