Closed zoldaten closed 4 months ago
ok. i fix it.
*sensor inhouse so temp >0.
09_classifier.ino:
// Set to 1 if you are using the Arduino Nano 33 BLE Sense Rev2
#include <DHT.h>
// Note: Make sure you have tweaked the DHT sensor library, as reported
// in the TinyML Cookbook 2E
//#include "model.h"
#include <TensorFlowLite.h>
#include "model.h"
//#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include <tensorflow/lite/micro/all_ops_resolver.h>
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/version.h"
//#include <tensorflow/lite/micro/micro_interpreter.h>
//#include <tensorflow/lite/micro/micro_log.h>
//#include <tensorflow/lite/micro/system_setup.h>
//#include <tensorflow/lite/schema/schema_generated.h>
//#include "tensorflow/lite/version.h"
// Set to 1 if you want to see whether your model can forecast the snow
#define DEBUG_SNOW 0
// Set to 1 if you are using the Arduino Nano 33 BLE Sense Rev2
#define ARDUINO_ARDUINO_NANO33BLE_REV2 0
tflite::ErrorReporter* error_reporter = nullptr;
const tflite::Model* tflu_model = nullptr;
tflite::MicroInterpreter* tflu_interpreter = nullptr;
TfLiteTensor* tflu_i_tensor = nullptr;
TfLiteTensor* tflu_o_tensor = nullptr;
float tflu_i_scale = 1.0f;
int32_t tflu_i_zero_point = 0;
float tflu_o_scale = 1.0f;
int32_t tflu_o_zero_point = 0;
constexpr int t_sz = 2088;//4096
uint8_t tensor_arena[t_sz] __attribute__((aligned(16)));
tflite::AllOpsResolver tflu_ops_resolver;
constexpr int num_hours = 3;
int8_t t_vals [num_hours] = {0};
int8_t h_vals [num_hours] = {0};
int cur_idx = 0;
#if defined(ARDUINO_ARDUINO_NANO33BLE)
#if ARDUINO_ARDUINO_NANO33BLE_REV2 == 0
#include <Arduino_HTS221.h>
#define SENSOR HTS
#else
#include <Arduino_HS300x.h>
#define SENSOR HS300x
#endif
void setup() {
Serial.begin(9600);
while (!Serial);
if (!SENSOR.begin()) {
Serial.println("Failed sensor initialization!");
while (1);
}
Serial.print("Test Temperature = ");
Serial.print(SENSOR.readTemperature(), 2);
Serial.println(" °C");
Serial.print("Test Humidity = ");
Serial.print(SENSOR.readHumidity(), 2);
Serial.println(" %");
tflu_model = tflite::GetModel(snow_model_tflite);
static tflite::MicroInterpreter static_interpreter(
tflu_model,
tflu_ops_resolver,
tensor_arena,
t_sz);
tflu_interpreter = &static_interpreter;
tflu_interpreter->AllocateTensors();
tflu_i_tensor = tflu_interpreter->input(0);
tflu_o_tensor = tflu_interpreter->output(0);
const auto* i_quant = reinterpret_cast<TfLiteAffineQuantization*>(tflu_i_tensor->quantization.params);
const auto* o_quant = reinterpret_cast<TfLiteAffineQuantization*>(tflu_o_tensor->quantization.params);
tflu_i_scale = i_quant->scale->data[0];
tflu_i_zero_point = i_quant->zero_point->data[0];
tflu_o_scale = o_quant->scale->data[0];
tflu_o_zero_point = o_quant->zero_point->data[0];
}
#endif
#if defined(ARDUINO_RASPBERRY_PI_PICO)
#include <DHT.h>
// Arduino pin number
const int gpio_pin_dht_pin = 10;
DHT dht(gpio_pin_dht_pin, DHT22);
#define SENSOR dht
void setup() {
Serial.begin(9600);
while(!Serial);
SENSOR.begin();
delay(2000);
Serial.print("Test Temperature = ");
Serial.print(SENSOR.readTemperature(), 2);
Serial.println(" °C");
Serial.print("Test Humidity = ");
Serial.print(SENSOR.readHumidity(), 2);
Serial.println(" %");
tflu_model = tflite::GetModel(snow_model_tflite);
static tflite::MicroErrorReporter micro_error_reporter;
error_reporter = µ_error_reporter;
if (tflu_model->version() != TFLITE_SCHEMA_VERSION) {
TF_LITE_REPORT_ERROR(error_reporter,
"Model provided is schema version %d not equal "
"to supported version %d.",
tflu_model->version(), TFLITE_SCHEMA_VERSION);
return;
}
static tflite::MicroInterpreter static_interpreter(
tflu_model,
tflu_ops_resolver,
tensor_arena,
t_sz,
error_reporter);
tflu_interpreter = &static_interpreter;
tflu_interpreter->AllocateTensors();
tflu_i_tensor = tflu_interpreter->input(0);
tflu_o_tensor = tflu_interpreter->output(0);
const auto* i_quant = reinterpret_cast<TfLiteAffineQuantization*>(tflu_i_tensor->quantization.params);
const auto* o_quant = reinterpret_cast<TfLiteAffineQuantization*>(tflu_o_tensor->quantization.params);
tflu_i_scale = i_quant->scale->data[0];
tflu_i_zero_point = i_quant->zero_point->data[0];
tflu_o_scale = o_quant->scale->data[0];
tflu_o_zero_point = o_quant->zero_point->data[0];
}
#endif
constexpr int num_reads = 3;
void loop() {
float t = 0.0f;
float h = 0.0f;
#if DEBUG_SNOW == 1
t = -10.0f;
h = 100.0f;
#else
for(int i = 0; i < num_reads; ++i) {
t += SENSOR.readTemperature();
h += SENSOR.readHumidity();
delay(3000);
}
t /= (float)num_reads;
h /= (float)num_reads;
#endif
// Use the mean and standard deviation
// extracted from your dataset
constexpr float t_mean = 2.08993f;
constexpr float h_mean = 87.22773f;
constexpr float t_std = 6.82158f;
constexpr float h_std = 14.21543f;
t = (t - t_mean) / t_std;
h = (h - h_mean) / h_std;
t = (t / tflu_i_scale);
t += (float)tflu_i_zero_point;
h = (h / tflu_i_scale);
h += (float)tflu_i_zero_point;
t_vals[cur_idx] = t;
h_vals[cur_idx] = h;
cur_idx = (cur_idx + 1) % num_hours;
int idx0 = cur_idx;
int idx1 = (cur_idx - 1 + num_hours) % num_hours;
int idx2 = (cur_idx - 2 + num_hours) % num_hours;
tflu_i_tensor->data.int8[0] = t_vals[idx2];
tflu_i_tensor->data.int8[1] = t_vals[idx1];
tflu_i_tensor->data.int8[2] = t_vals[idx0];
tflu_i_tensor->data.int8[3] = h_vals[idx2];
tflu_i_tensor->data.int8[4] = h_vals[idx1];
tflu_i_tensor->data.int8[5] = h_vals[idx0];
tflu_interpreter->Invoke();
float out_int8 = tflu_o_tensor->data.int8[0];
float out_f = (out_int8 - tflu_o_zero_point);
out_f *= tflu_o_scale;
if (out_f > 0.5) {
Serial.println("Yes, it snows");
}
else {
Serial.println("No, it does not snow");
}
delay(2000);
}
additionally i have to include debug_log.cpp from Documents\Arduino\libraries\Arduino_TensorFlowLite\examples\hello_world
The Adafruit DHT sensor library should work. This is listed as "DHT sensor library" It works with the Pico. DHT sensor library
You can find the Arduino_TensorFlowLite.zip under the following: https://github.com/PacktPublishing/TinyML-Cookbook_2E/tree/main/ArduinoLibs
To install them, follow the Arduino IDE Install Libraries doc: https://docs.arduino.cc/software/ide-v1/tutorials/installing-libraries/
Thanks a lot @jomoengineer for your support. Much appreciated! I hope @zoldaten you managed to fix your issue. I believe the main problem was the missing Arduino TensorFlow Lite lib that you can find in this repo. Do not hesitate to open a new issue if you need any further help
You can find the Arduino_TensorFlowLite.zip under the following: https://github.com/PacktPublishing/TinyML-Cookbook_2E/tree/main/ArduinoLibs
To install them, follow the Arduino IDE Install Libraries doc: https://docs.arduino.cc/software/ide-v1/tutorials/installing-libraries/
this lib gives error:
C:\Users\{user}\Desktop\TinyML-Cookbook\Chapter05\ArduinoSketches\07_indoor_scene_recognition\07_indoor_scene_recognition.ino:9:10: fatal error: tensorflow/lite/micro/micro_error_reporter.h: No such file or directory
#include <tensorflow/lite/micro/micro_error_reporter.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
Compilation error: tensorflow/lite/micro/micro_error_reporter.h: No such file or directory
hello. firstly, it was hard to find Adafruit library for dht library in arduino ide, so to fix it: Adafruit_Unified_Sensor should be installed in arduino ide library manager
secondly, i spent hours searching for Arduino_TensorFlowLite lib as it removed from arduino ide and found it here - https://www.ardu-badge.com/Arduino_TensorFlowLite/zip
thirdly, i cant compile 09_classifier.ino as it gives:
But 'hello world' example from Arduino_TensorFlowLite works well on raspberry pico:
how to fix this ?