Visualizzazione post con etichetta Tensorflow Lite. Mostra tutti i post
Visualizzazione post con etichetta Tensorflow Lite. Mostra tutti i post

giovedì 11 aprile 2024

Inferenza TF Lite su CPU e TPU

Per questa prova e' stato selezionato il modello di esempio da questo link riguardante il dataset 900+ birds iNaturalist 2017

 

Per prima cosa c'e' da sottolineare che i modelli per CPU non sono compatibili con le TPU e viceversa. Inoltre l'immagine con cui si vuole fare inferenza deve essere dello stesso tipo (float32,uint8) e delle stesse dimensioni del modello.

CPU

python3 esempio.py --model_file mobilenet_v2_1.0_224_inat_bird_quant.tflite --label_file inat_bird_labels.txt --image ./images/th-1232548430.jpg
 

Risultato

0.831373: Cyanistes caeruleus (Eurasian Blue Tit)
0.027451: Parus major (Great Tit)
0.003922: Coereba flaveola (Bananaquit)
0.003922: Vireo philadelphicus (Philadelphia Vireo)
0.003922: Myiozetetes similis (Social Flycatcher)

 

import argparse
import time

import numpy as np
from PIL import Image
import tensorflow as tf


def load_labels(filename):
with open(filename, 'r') as f:
return [line.strip() for line in f.readlines()]


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-i',
'--image',
default='/tmp/grace_hopper.bmp',
help='image to be classified')
parser.add_argument(
'-m',
'--model_file',
default='/tmp/mobilenet_v1_1.0_224_quant.tflite',
help='.tflite model to be executed')
parser.add_argument(
'-l',
'--label_file',
default='/tmp/labels.txt',
help='name of file containing labels')
parser.add_argument(
'--input_mean',
default=127.5, type=float,
help='input_mean')
parser.add_argument(
'--input_std',
default=127.5, type=float,
help='input standard deviation')
parser.add_argument(
'--num_threads', default=None, type=int, help='number of threads')
parser.add_argument(
'-e', '--ext_delegate', help='external_delegate_library path')
parser.add_argument(
'-o',
'--ext_delegate_options',
help='external delegate options, \
format: "option1: value1; option2: value2"')

args = parser.parse_args()

ext_delegate = None
ext_delegate_options = {}

# parse extenal delegate options
if args.ext_delegate_options is not None:
options = args.ext_delegate_options.split(';')
for o in options:
kv = o.split(':')
if (len(kv) == 2):
ext_delegate_options[kv[0].strip()] = kv[1].strip()
else:
raise RuntimeError('Error parsing delegate option: ' + o)

# load external delegate
if args.ext_delegate is not None:
print('Loading external delegate from {} with args: {}'.format(
args.ext_delegate, ext_delegate_options))
ext_delegate = [
tflite.load_delegate(args.ext_delegate, ext_delegate_options)
]

interpreter = tf.lite.Interpreter(
model_path=args.model_file,
experimental_delegates=ext_delegate,
num_threads=args.num_threads)
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# check the type of the input tensor
floating_model = input_details[0]['dtype'] == np.float32

# NxHxWxC, H:1, W:2
height = input_details[0]['shape'][1]
width = input_details[0]['shape'][2]
img = Image.open(args.image).resize((width, height))

# add N dim
input_data = np.expand_dims(img, axis=0)

if floating_model:
input_data = (np.float32(input_data) - args.input_mean) / args.input_std

interpreter.set_tensor(input_details[0]['index'], input_data)

start_time = time.time()
interpreter.invoke()
stop_time = time.time()

output_data = interpreter.get_tensor(output_details[0]['index'])
results = np.squeeze(output_data)

top_k = results.argsort()[-5:][::-1]
labels = load_labels(args.label_file)
for i in top_k:
if floating_model:
print('{:08.6f}: {}'.format(float(results[i]), labels[i]))
else:
print('{:08.6f}: {}'.format(float(results[i] / 255.0), labels[i]))

print('time: {:.3f}ms'.format((stop_time - start_time) * 1000))


 

GPU

python3 inferenza.py (attenzione python e basta genera errore su modulo pathlib)

 

Risultato

Cyanistes caeruleus (Eurasian Blue Tit): 0.81250
 

======================================

import os
import pathlib
from pycoral.utils import edgetpu
from pycoral.utils import dataset
from pycoral.adapters import common
from pycoral.adapters import classify
from PIL import Image

# Specify the TensorFlow model, labels, and image
script_dir = pathlib.Path(__file__).parent.absolute()
model_file = os.path.join(script_dir, 'mobilenet_v2_1.0_224_quant_edgetpu.tflite')
label_file = os.path.join(script_dir, 'imagenet_labels.txt')
image_file = os.path.join(script_dir, 'parrot.jpg')

# Initialize the TF interpreter
interpreter = edgetpu.make_interpreter(model_file)
interpreter.allocate_tensors()

# Resize the image
size = common.input_size(interpreter)
image = Image.open(image_file).convert('RGB').resize(size, Image.ANTIALIAS)

# Run an inference
common.set_input(interpreter, image)
interpreter.invoke()
classes = classify.get_classes(interpreter, top_k=1)

# Print the result
labels = dataset.read_label_file(label_file)
for c in classes:
  print('%s: %.5f' % (labels.get(c.id, c.id), c.score)) 

======================================

sabato 6 aprile 2024

Coral Mini Dev Board

Primi passi con la Coral Dev Board Mini 

Premessa: e' un dispositivo per sola inferenza con Tensorflow Lite, non puo' fare training di modelli


Per prima cosa si connettono due cavi USB-C. Il primo serve solo per la alimentazione, il secondo solo per la USB-OTG a cui e' collegata la seriale (come al solito screen /dev/TTYACMx 115200)

Sulla scheda e' montata una distro Linux derivata Debian. Per fare lo shutdown in modo pulito da shell si lancia il comando, poi si scollega la seriale e dopo la alimentazione

Di default l'utente e'

user : mendel
password : mendel

e' presente di default il sudo

Ci si puo' collegare via SSH in 192.168.100.2 ma attenzione...di default non e' impostato il login via password, deve essere configurato il servizio da shell seriale prima di accedere in SSH.

Si puo' accedere alla shell anche mediante mdt

Per copiare i file si puo' utilizzare scp oppure mdt push

python -m venv .
source bin/activate
python3 -m pip install mendel-development-toolmdt devices
mdt shell

Lo spazio disco (senza SD Card inserita) di 8Gb cosi' distribuito 

mendel@orange-pig:~$ df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       5.4G  2.0G  3.2G  39% /
devtmpfs        1.1G     0  1.1G   0% /dev
tmpfs           1.1G     0  1.1G   0% /dev/shm
tmpfs           1.1G   18M  1.1G   2% /run
tmpfs           5.3M  4.1k  5.3M   1% /run/lock
tmpfs           1.1G     0  1.1G   0% /sys/fs/cgroup
tmpfs           1.1G  283k  1.1G   1% /var/log
/dev/mmcblk0p3  2.1G   41M  1.9G   3% /home
/dev/mmcblk0p2  130M   29M   95M  24% /boot
tmpfs           210M  4.8M  205M   3% /run/user/1000

per una memoria di 2 Gb

Per connettere Wifi si puo' usare

nmcli dev wifi connect <NETWORK_NAME> password <PASSWORD> ifname wlan0

Attenzione ad APT ..come scritto nel precedente post si devono aggiornare le firme GPG dei repository Google Coral


Attenzione anche alla Camera. Mi sono fatto prestare una camera CSI per Raspberry pensando funzionasse senza problema ma il connettore della Coral e' risultato di molto piu' piccolo della flat della camera. Alla fine ho scoperto che la Coral ha un connettore CSI-2 (24 pin) mentre Raspberry una CSI (15 pin)

Link agli esempi

Camera

Audio

Github


 

 

 

Coral Mini dev board e rifiuti su spiaggia

Ho provato ad integrare il modello dei rifiuti su spiaggia visto qui e qui in Tensorflow lite per girare su Coral Mini Dev Board (basato su questo esempio)



Per prima cosa ho scaricato i dati in formato Pascal VOC (quindi con le annotations in formato xml) 

L'albero dei folder e' del tipo

--------------------------------------------------------------------------

dataset
    -train
        --images (solo jpg)
        --annotations (solo xml)
    -test
        --images
        --annotations
    -validation
        --images
        --annotations

-------------------------------------------------------------------------- 


Per questo motivo ho dovuto dividere i file rispetto al file zip scaricato da roboflow sono xml e jpg sono tutti nello stesso folder

Per fare il retrain ho usato il TF Lite Model Maker . Il problema e' che non ne vuole sapere di girare in Colab ed ho tirato su un Docker con il seguente Dockerfile

Inoltre TF Lite Model Maker effettua il retrain di modelli destinati a funzionare on edge e su mobile, in particolare usando EffiecientDet Lite 

-------------------------------------------------------------------------- 

FROM tensorflow/tensorflow:2.13.0-gpu

RUN apt-get update && \
    apt-get install -y libusb-1.0-0 libusb-1.0-0-dev

RUN pip install tflite-model-maker
RUN pip install pycocotools
 

-------------------------------------------------------------------------- 

lanciandolo con il comando

docker build -t litegpu .

docker run --gpus all -it -p  -v $PWD:/tmp -w /tmp litegpu bash

A questo punto si passa alla creazione vera e propria del modello con lo script python. Rispetto all'esempio di Colab, ho dovuto abbassare la batch size a 2 e rimuovere la validazione del modello altrimenti lo script satura tutta la memoria GPU

--------------------------------------------------------------------------

import numpy as np
import os

from tflite_model_maker.config import ExportFormat
from tflite_model_maker import model_spec
from tflite_model_maker import object_detector

import tensorflow as tf
assert tf.__version__.startswith('2')

tf.get_logger().setLevel('ERROR')
from absl import logging
logging.set_verbosity(logging.ERROR)


label_map = {1: 'Glass', 2: 'Metal',3: 'Net',4: 'null',5: 'PET_Bottle',6: 'Plastic_Buoy',7: 'Plastic_Buoy_China',8: 'Plastic_ETC',9: 'Rope',10: 'Styrofoam_Box',11: 'Styrofoam_Buoy',12: 'Styrofoam_Piece'}

train_images_dir = 'dataset/train/images'
train_annotations_dir = 'dataset/train/annotations'
val_images_dir = 'dataset/validation/images'
val_annotations_dir = 'dataset/validation/annotations'
test_images_dir = 'dataset/test/images'
test_annotations_dir = 'dataset/test/annotations'

train_data = object_detector.DataLoader.from_pascal_voc(train_images_dir, train_annotations_dir, label_map=label_map)
validation_data = object_detector.DataLoader.from_pascal_voc(val_images_dir, val_annotations_dir, label_map=label_map)
test_data = object_detector.DataLoader.from_pascal_voc(test_images_dir, test_annotations_dir, label_map=label_map)
print(f'train count: {len(train_data)}')
print(f'validation count: {len(validation_data)}')
print(f'test count: {len(test_data)}')


spec = object_detector.EfficientDetLite0Spec()
model = object_detector.create(train_data=train_data,
                               model_spec=spec,
                               validation_data=validation_data,
                               epochs=10,
                               batch_size=2,
                               train_whole_model=True)
#model.evaluate(test_data)
TFLITE_FILENAME = 'efficientdet-lite-litter.tflite'
LABELS_FILENAME = 'litter-labels.txt'
model.export(export_dir='.', tflite_filename=TFLITE_FILENAME, label_filename=LABELS_FILENAME,
             export_format=[ExportFormat.TFLITE, ExportFormat.LABEL])
model.evaluate_tflite(TFLITE_FILENAME, test_data)
-------------------------------------------------------------------------- 

con risultato finale

659 - loss: 0.8730 - learning_rate: 2.5257e-05 - gradient_norm: 5.0130 - val_det_loss: 0.6480 - val_cls_loss: 0.3706 - val_box_loss: 0.0055 - val_reg_l2_loss: 0.0659 - val_loss: 0.7139


Si potrebbe anche usare il modello 1 cambiando la linea

spec = object_detector.EfficientDetLite1Spec()

ma la mia scheda grafica non ha abbastanza memoria

Per verificare se il modello funziona si puo' all'interno del docker installare tramite pip

python3 -m pip install --extra-index-url https://google-coral.github.io/py-repo/ pycoral

e poi lanciare il comando lo script

-------------------------------------------------------------------------- 

from PIL import ImageDraw
from PIL import ImageFont
import PIL

import tflite_runtime.interpreter as tflite
from pycoral.adapters import common
from pycoral.adapters import detect
from pycoral.utils.dataset import read_label_file

def draw_objects(draw, objs, scale_factor, labels):
  """Draws the bounding box and label for each object."""
  COLORS = np.random.randint(0, 255, size=(len(labels), 3), dtype=np.uint8)
  for obj in objs:
    bbox = obj.bbox
    color = tuple(int(c) for c in COLORS[obj.id])
    draw.rectangle([(bbox.xmin * scale_factor, bbox.ymin * scale_factor),
                    (bbox.xmax * scale_factor, bbox.ymax * scale_factor)],
                   outline=color, width=3)
    font = ImageFont.truetype("LiberationSans-Regular.ttf", size=15)
    draw.text((bbox.xmin * scale_factor + 4, bbox.ymin * scale_factor + 4),
              '%s\n%.2f' % (labels.get(obj.id, obj.id), obj.score),
              fill=color, font=font)

# Load the TF Lite model
labels = read_label_file('./e0/litter-labels.txt')
interpreter = tflite.Interpreter('./e0/efficientdet-lite-litter.tflite')
interpreter.allocate_tensors()

# Resize the image for input
image = Image.open('./e0/1.jpg')
_, scale = common.set_resized_input(
    interpreter, image.size, lambda size: image.resize(size, PIL.Image.Resampling.LANCZOS))

# Run inference
interpreter.invoke()
objs = detect.get_objects(interpreter, score_threshold=0.4, image_scale=scale)

# Resize again to a reasonable size for display
display_width = 500
scale_factor = display_width / image.width
height_ratio = image.height / image.width
image = image.resize((display_width, int(display_width * height_ratio)))
draw_objects(ImageDraw.Draw(image), objs, scale_factor, labels)
image.save("output.jpg")

-------------------------------------------------------------------------- 

il risultato e' l'immagine di apertura del post

A questo punto si compilano i modelli per poter girare su Coral Dev Mini Board installando il compilatore sul PC

wget -O- https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/coral-edgetpu.gpg

apt-get update
apt-get install edgetpu-compiler

e poi lanciando la compilazione con

edgetpu_compiler efficientdet-lite-litter.tflite -d --num_segments=1

a questo punto si copiano i file del modello sulla Dev Board 

scp efficientdet-lite-litter_edgetpu.tflite mendel@192.168.100.2:/home/mendel

e si lancia

python3 detect_image.py --model efficientdet-lite-litter_edgetpu.tflite --labels litter-labels.txt --input CD_10196_jpg.rf.4a2b1adf824f2a18a7eea764b9012f36.jpg --output output.jpg


----INFERENCE TIME----
Note: The first inference is slow because it includes loading the model into Edge TPU memory.
347.37 ms
182.40 ms
181.49 ms
178.24 ms
182.33 ms
-------RESULTS--------
Styrofoam_Piece
  id:     11
  score:  0.453125
  bbox:   BBox(xmin=248, ymin=132, xmax=279, ymax=171)
PET_Bottle
  id:     4
  score:  0.4375
  bbox:   BBox(xmin=274, ymin=206, xmax=299, ymax=246)

 


Attenzione che la versione del runtime del compilatore deve essere compatibile con le librerie sulla Dev Board. In una prima prova avevo avuto l'errore

RuntimeError: Failed to prepare for TPU. Failed precondition: Package requires runtime version (14), which is newer than this runtime version (13).Node number 7 (EdgeTpuDelegateForCustomOp) failed to prepare.

ed infatti il comando  edgetpu_compiler --version rispondeva
Edge TPU Compiler version 16.0.384591198

mentre la runtime version era

python3 -c "import pycoral.utils.edgetpu; print(pycoral.utils.edgetpu.get_runtime_version())"
BuildLabel(COMPILER=6.3.0 20170516,DATE=redacted,TIME=redacted), RuntimeVersion(13)

Questo era dovuto al fatto che apt non scaricava gli aggiornamenti a causa della firma GPG scaduta dei repository Google Coral 

Il problema si e' risolto con

wget -O- https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/coral-edgetpu.gpg


martedì 2 febbraio 2021

Anomaly detection con Tensorflow Lite e Arduino Nano BLE 33

Quest post riguarda un esperimento per utilizzare una rete neurale su microcontrollori

Nello specifico e' stato provato una Anomaly Detection perche' spesso i processi geologici sono difficilmente ripetibili (e se si ripetono non sono nelle stesse condizioni..basti vedere le frane in cui si possono avere neoformazioni) quindi e' praticamente impossibile definire un set di addestramento per la rete neurale....la cosa piu' conveniente e' vedere se ci sono anomalie all'interno di uno stream di dati da un sensore. 

La rete neurale per la Anomaly Detectione e' stata impostata mediante un Autoencoder partendo da questo tutorial https://www.tensorflow.org/tutorials/generative/autoencoder

Per i dati e' stata scelta una Arduino Nano 33 BLE che ha a bordo una accelerometro ed un giroscopio triassiale piu' un sensore di temperatura (dato che serve a gestire la deriva termica dei sensori MEMS)


Riguardo alla scheda un paio di note

1) il sensore di temperatura e' montato sullo stesso PCB del microcontrollore e quindi risente del riscaldamento, seppur minimo, del microcontrollore. La cosa piu' conveniente e' iniziare la registrazione quanto tutto il sistema e' andato in equilibrio termico

2) la velocita' di acquisizione del sensore termico e' molto piu' lenta dell'accelerometro. Se si interroga il sensore termico troppo velocemente il sistema si blocca (e non dice nemmeno il motivo del blocco..)


Lo sketch di acquisizione dati e' banale e semplicemente manda sulla seriale delle stringhe formattate in modo da costituire un semplice file csv nel formato ax,ay,az,gx,gy,gz,temp

===================================================

/*
  nano-33-sense-serial-example.ino
  Copyright (c) 2020 Dale Giancono. All rights reserved..
  This program outputs all raw sensor data from the Arduino Nano 33 BLE 
  Sense board via serial at a 20Hz rate. It also calculates the RMS 
  value of the microphone buffer and outputs that data. It is intended
  as a quick way to become familiar with some of the sensor libraries 
  available with the Nano 33 BLE Sense, and highlight some of the 
  difficulties when using a super loop architecture with this board.
  
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
/**********/
/*INCLUDES*/
/**********/
/* For MP34DT05 microphone */
#include <PDM.h>
/* For LSM9DS1 9-axis IMU sensor */
#include <Arduino_LSM9DS1.h>
/* For APDS9960 Gesture, light, and proximity sensor */
#include <Arduino_APDS9960.h>
/* For LPS22HB barometric barometricPressure sensor */
#include <Arduino_LPS22HB.h>
/* For HTS221 Temperature and humidity sensor */
#include <Arduino_HTS221.h>
#include <arm_math.h>
/********/
/*MACROS*/
/********/
/* Set these macros to true with you want the output plotted in a way that
 * can be viewed with serial plotter. Having them all true creates a pretty
 * meaningless graph, as the scaling will be way off for each sensor, and there
 * will be too much data to view */
#define SERIAL_PLOT_MP34DT05    (false)
#define SERIAL_PLOT_LSM9DS1     (true)
#define SERIAL_PLOT_APDS9960    (false)
#define SERIAL_PLOT_LPS22HB     (false)
#define SERIAL_PLOT_HTS221      (true)
/* This value was also used in the PDM example, seems like a good enough reason to
 * continue using it. With this value and 16kHz sampling frequency, the RMS sampling
 * period will be 16mS */
#define MICROPHONE_BUFFER_SIZE_IN_WORDS (256U)
#define MICROPHONE_BUFFER_SIZE_IN_BYTES (MICROPHONE_BUFFER_SIZE_IN_WORDS * sizeof(int16_t))
/******************/
/*LOCAL VARIABLES*/
/******************/
/******************/
/*GLOBAL VARIABLES*/
/******************/
/* MP34DT05 Microphone data buffer with a bit depth of 16. Also a variable for the RMS value */
int16_t microphoneBuffer[MICROPHONE_BUFFER_SIZE_IN_WORDS];
int16_t microphoneRMSValue;
/* variables to hold LSM9DS1 accelerometer data */
float accelerometerX, accelerometerY, accelerometerZ;
/* variables to hold LSM9DS1 gyroscope data */
float gyroscopeX, gyroscopeY, gyroscopeZ;
/* variables to hold LSM9DS1 magnetic data */
float magneticX, magneticY, magneticZ;
/* variables to hold LPS22HB  barometric pressure data */
float barometricPressure;
/* variables to hold APDS9960 proximity, gesture and colour data */
int proximity, gesture, colourR, colourG, colourB;
/* variables to hold HTS221 temperature and humidity data */
float temperature, humidity;
/* Used to count 1000ms intervals in loop() */
int oldMillis;
int newMillis;
/* Used as a simple flag to know when microphone buffer is full and RMS value
 * can be computed. */
bool microphoneBufferReadyFlag;
/****************************/
/*LOCAL FUNCTION PROTOTYPES*/
/****************************/
/****************************/
/*GLOBAL FUNCTION PROTOTYPES*/
/****************************/
/* This function is called each time PDM data is available. It will be used to fill the
 * microphone buffer that we will then use to calculate RMS values */
void Microphone_availablePDMDataCallback(void);
/* This function computes the RMS value based on the data contained within the microphoneBuffer
 * If the microphone buffer has a word length of 256, and the sample rate for the microphone is 16kHz,
 * then this RMS value is taken over (1/16000)*256 = 16mS */
void Micophone_computeRMSValue(void);
/****************************/
/*IMPLEMENTATION*/
/****************************/
void setup()
{
  /* Serial setup for UART debugging */
  Serial.begin(115200);
  /* Wait for serial to be available */
  while(!Serial);
  /* PDM setup for MP34DT05 microphone */
  /* configure the data receive callback to transfer data to local buffer */
  PDM.onReceive(Microphone_availablePDMDataCallback);
  /* Initialise single PDM channel with a 16KHz sample rate (only 16kHz or 44.1kHz available */
  if (!PDM.begin(1, 16000))
  {
    Serial.println("Failed to start PDM!");
    /* Hacky way of stopping program executation in event of failure. */
    while(1);
  }
  else
  {
    /* Gain values can be from 0 to 80 (around 38db). Check out nrf_pdm.h
     * from the nRF528x-mbedos core to confirm this. */
    /* This has to be done after PDM.begin() is called as begin() always
     *  sets the gain as the default PDM.h value (20).
     */
    PDM.setGain(50);
  }
  /* IMU setup for LSM9DS1*/
  /* default setup has all sensors active in continous mode. Sample rates
   *  are as follows: magneticFieldSampleRate = 20Hz, gyroscopeYroscopeSampleRate = 109Hz,
   * accelerationSampleRate = 109Hz */
  if (!IMU.begin())
  {
    Serial.println("Failed to initialize IMU!");
    /* Hacky way of stopping program executation in event of failure. */
    while(1);
  }

  /* Set sensitivity from 0 to 100. Higher is more sensitive. In
   * my experience it requires quite a bit of experimentation to
   * get this right, as if it is too sensitive gestures will always
   * register as GESTURE_DOWN or GESTURE_UP and never GESTURE_LEFT or
   * GESTURE_RIGHT. This can be called before APDS.begin() as it just
   * sets an internal sensitivity value.*/
  APDS.setGestureSensitivity(50);
  if (!APDS.begin())
  {
    Serial.println("Error initializing APDS9960 sensor.");
    /* Hacky way of stopping program executation in event of failure. */
    while(1);
  }
  /* As per Arduino_APDS9960.h, 0=100%, 1=150%, 2=200%, 3=300%. Obviously more
   * boost results in more power consumption. */
  APDS.setLEDBoost(0);
  /* Barometric sensor setup for LPS22HB*/
  if (!BARO.begin())
  {
    Serial.println("Failed to initialize barometricPressure sensor!");
    while (1);
  }
  /* Temperature/Humidity sensor setup for HTS221*/
  if (!HTS.begin())
  {
    Serial.println("Failed to initialize humidity temperature sensor!");
    /* Hacky way of stopping program executation in event of failure. */
    while(1);
  }
  /* Initialise timing variables. */
  oldMillis = 0;
  newMillis = 0;
  /* Initialise micophone buffer ready flag */
  microphoneBufferReadyFlag = false;
}
void loop()
{
  /* The sensors that use I2C must be checked to see if data is available, so
   *  this is checked each loop. This include the IMU and Gesture/light/proximity
   *  sensors. Other sensors (barometric pressure and temperature/humidity)
   *  will give a value when we ask for it. These values are requested each
   *  1000ms using millis() in a hacky way, but it works.
   *
   *  Data is output via serial every 50ms (20Hz). There is no good way to plot of
   *  the data from the sensors together due the differing sample rates, but this
   *  represented a decent compromise as changes will still be observable in all
   *  sensor data.
   */
  /* Get the new millis() value which helps time serial plotting and getting
   * of pressure, temperature, and humidity values. */
  newMillis = millis();
  /* Every 50ms plot all data to serial plotter. */
  if((newMillis - oldMillis) % 50)
  {
  #if (SERIAL_PLOT_MP34DT05 == true)
      Serial.printf("%d,", microphoneRMSValue);
  #endif
  #if (SERIAL_PLOT_LSM9DS1 == true)
    Serial.printf("%f,%f,%f,", accelerometerX, accelerometerY, accelerometerZ);
    Serial.printf("%f,%f,%f,", gyroscopeX, gyroscopeY, gyroscopeZ);
    //Serial.printf("%f,%f,%f,", magneticX, magneticY, magneticZ);
  #endif
  #if (SERIAL_PLOT_LPS22HB == true)
    Serial.printf("%f,", barometricPressure);
  #endif
  #if (SERIAL_PLOT_APDS9960 == true)
    Serial.printf("%d,%d,%d,%d,%d,", proximity, gesture, colourR, colourG, colourB);
  #endif
  #if (SERIAL_PLOT_HTS221 == true)
    //Serial.printf("%f, %f", temperature, humidity);
    Serial.printf("%f", temperature);
  #endif
    Serial.println();
  }
  /* Every 1000ms get the pressure, temperature, and humidity data */
  if((newMillis - oldMillis) > 1000)
  {
    barometricPressure = BARO.readPressure();
    temperature = HTS.readTemperature();
    humidity = HTS.readHumidity();
    oldMillis = newMillis;
  }

  /* If new acceleration data is available on the LSM9DS1 get the data.*/
  if(IMU.accelerationAvailable())
  {
    IMU.readAcceleration(accelerometerX, accelerometerY, accelerometerZ);
  }
  /* If new gyroscope data is available on the LSM9DS1 get the data.*/
  if(IMU.gyroscopeAvailable())
  {
    IMU.readGyroscope(gyroscopeX, gyroscopeY, gyroscopeZ);
  }
  /* If new magnetic data is available on the LSM9DS1 get the data.*/
  if (IMU.magneticFieldAvailable())
  {
    IMU.readMagneticField(magneticX, magneticY, magneticZ);
  }
  /* If new proximity data is available on the APDS9960 get the data.*/
  if (APDS.proximityAvailable())
  {
    proximity = APDS.readProximity();
  }
  /* If new colour data is available on the APDS9960 get the data.*/
  if (APDS.colorAvailable())
  {
    APDS.readColor(colourR, colourG, colourB);
  }
  /* If new gesture data is available on the APDS9960 get the data.*/
  if (APDS.gestureAvailable())
  {
    gesture = APDS.readGesture();
  }
  /* If the microphone buffer is full, compute the RMS value */
  if(microphoneBufferReadyFlag)
  {
    Micophone_computeRMSValue();
    microphoneBufferReadyFlag = false;
  }
}
void Microphone_availablePDMDataCallback()
{
  // query the number of bytes available
  int bytesAvailable = PDM.available();
  if(bytesAvailable == MICROPHONE_BUFFER_SIZE_IN_BYTES)
  {
    PDM.read(microphoneBuffer, bytesAvailable);
    microphoneBufferReadyFlag = true;
  }
}
void Micophone_computeRMSValue(void)
{
  //arm_rms_q15((q15_t*)microphoneBuffer, MICROPHONE_BUFFER_SIZE_IN_WORDS, (q15_t*)&microphoneRMSValue);
}

===================================================

 i dati sono stati registrati su due file in cui uno e' il train delle rete neurale (sistema completamente statico) mentre sul secondo e' stato registrato un evento con un colpetto sull'asse Z

le elaborazioni sono state effettuate mediante il seguente Notebook di Colab

Train 26000 dati

 

Evento

i dati sono stati normalizzati e sono stati inseriti nella seguente neurale

class AnomalyDetector(Model):
def __init__(self):
super(AnomalyDetector, self).__init__()
self.encoder = tf.keras.Sequential([
layers.Dense(32, activation="relu"),
layers.Dense(16, activation="relu"),
layers.Dense(8, activation="relu")])

self.decoder = tf.keras.Sequential([
layers.Dense(16, activation="relu"),
layers.Dense(32, activation="relu"),
layers.Dense(7, activation="sigmoid")])

def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded

autoencoder = AnomalyDetector()
autoencoder.compile(optimizer='adam', loss='mae')
history = autoencoder.fit(train_data, train_data,
epochs=7,
batch_size=20,
validation_data=(test_data, test_data),
shuffle=True)



La soglia del modello e' stata individuata, in modo non supervisionato in ,

Threshold:  0.003555521

questo e' l'evento individuato dalla rete neurale nel dataset di test


la rete e' stata convertita nel formato di Tensorflow Lite e quindi tramite il comando xxd e' stata convertita in un array di byte per l'utilizzo in Tensorflow per microcontrollori

Lo sketch di Arduino e' stato ripreso dagli esempi del libro

TinyML: Machine Learning With Tensorflow Lite on Arduino and Ultra-Low-Power Microcontrollers: Machine Learning with TensorFlow on Arduino, and Ultra-Low Power Micro-Controllers

 di cui si trovano gli esempi su GitHub

Si copia il risultato dell'array derivante da xxd sul file model.h

Lo sketch che ho utilizzato per invocare la rete neurale e' il seguente

=====================================================

/*
  IMU Classifier
  This example uses the on-board IMU to start reading acceleration and gyroscope
  data from on-board IMU, once enough samples are read, it then uses a
  TensorFlow Lite (Micro) model to try to classify the movement as a known gesture.
  Note: The direct use of C/C++ pointers, namespaces, and dynamic memory is generally
        discouraged in Arduino examples, and in the future the TensorFlowLite library
        might change to make the sketch simpler.
  The circuit:
  - Arduino Nano 33 BLE or Arduino Nano 33 BLE Sense board.
  Created by Don Coleman, Sandeep Mistry
  Modified by Dominic Pajak, Sandeep Mistry
  This example code is in the public domain.
*/
#include <Arduino_LSM9DS1.h>
#include <Arduino_HTS221.h>
#include <TensorFlowLite.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 "model.h"
tflite::MicroErrorReporter tflErrorReporter;
tflite::AllOpsResolver tflOpsResolver;
const tflite::Model* tflModel = nullptr;
tflite::MicroInterpreter* tflInterpreter = nullptr;
TfLiteTensor* tflInputTensor = nullptr;
TfLiteTensor* tflOutputTensor = nullptr;
constexpr int tensorArenaSize = 120 * 1024;
byte tensorArena[tensorArenaSize];
int oldMillis;
int newMillis;
float oldvalue;

void setup() {
  oldMillis = 0;
  newMillis = 0;
  oldvalue = 0.0;
  Serial.begin(9600);
  while (!Serial);
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
  if (!HTS.begin())
  {
    Serial.println("Failed to initialize humidity temperature sensor!");
    while(1);
  }
  tflModel = tflite::GetModel(drive_My_Drive_autoencode_modello_model_tflite);
  if (tflModel->version() != TFLITE_SCHEMA_VERSION) {
    Serial.println("Model schema mismatch!");
    while (1);
  }
  // Create an interpreter to run the model
  tflInterpreter = new tflite::MicroInterpreter(tflModel, tflOpsResolver, tensorArena, tensorArenaSize, &tflErrorReporter);
  // Allocate memory for the model's input and output tensors
  tflInterpreter->AllocateTensors();
  // Get pointers for the model's input and output tensors
  tflInputTensor = tflInterpreter->input(0);
  tflOutputTensor = tflInterpreter->output(0);
}
void loop() {
  float aX, aY, aZ, gX, gY, gZ,temperature;
  newMillis = millis();
  if((newMillis - oldMillis) > 1000)
          {
            temperature = HTS.readTemperature();
            oldMillis = newMillis;
          }
    if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
          IMU.readAcceleration(aX, aY, aZ);
          IMU.readGyroscope(gX, gY, gZ);
          tflInputTensor->data.f[0] = (aX + 4.0) / 8.0;
          tflInputTensor->data.f[1] = (aY + 4.0) / 8.0;
          tflInputTensor->data.f[2] = (aZ + 4.0) / 8.0;
          tflInputTensor->data.f[3] = (gX + 2000.0) / 4000.0;
          tflInputTensor->data.f[4] = (gY + 2000.0) / 4000.0;
          tflInputTensor->data.f[5] = (gZ + 2000.0) / 4000.0;
          tflInputTensor->data.f[6] = (temperature) / 50.0;
          TfLiteStatus invokeStatus = tflInterpreter->Invoke();
          if (invokeStatus != kTfLiteOk) {
                Serial.println("Invoke failed!");
                while (1);
                return;
                }
        //Serial.println(tflOutputTensor->data.f[0], 6);
       // si applica il threshold ricavato dalla rete neurale 0.00355
        if (tflOutputTensor->data.f[0] > oldvalue+0.00355){
          Serial.println("1");
        }
          else{
          Serial.println("0");
        }
        oldvalue = tflOutputTensor->data.f[0];
        }   
}

=====================================================

C'e' da notare che se, a seguito di un evento, la scheda assume un assetto differente da quello di partenza il sistema segnalera' sempre una anomalia in quanto la rete neurale non viene ricalcolata


venerdì 31 gennaio 2020

Tensorflow Lite su Arduino Ble 33

A questo link e' comparso un interessante esempio su come utilizzare i sensori (accelerometro e giroscopio) di una Arduino BLE per addestrare una rete neurale con Tensorflow Lite e poi utilizzare la stessa libreria per classificare le gesture sempre tramite  Arduino



Per prova ho inserito la Arduino sotto ad un guanto (per tenerla bloccata) e poi registrando un centinaio di rotazione del polso verso l'interno e verso l'esterno


L'ultima modifica che ho apportato e' di modificare a 50 il numero di campioni per gesture
Una volta salvata due file csv (uno per movimento) ho utilizzato questo Colab per creare il file model. h


Grafico accelerazione di riferimento
Come si deve dai grafici l'errore del modello in train/validation e' estremamente ridotto

Grafico giroscopi di riferimento

Training and validation loss


Stesso grafico precedente eliminando i primi 100 dati


Risultati validazione

Il risultato del Notebook sono un file di modello di Tensorflow Lite ed un file model.h che deve essere copiato nello sketch ImuClassifier per fare il deploy del modello

Devo ammettere che il sistema funziona ma e' lontano dall'essere perfetto. Devo ancora capire se e' un problema di posizionamento del sensore durante le varie proprie (che per ovvi motivi non e' sempre identico) od un addestramento errato




Geologi

  E so anche espatriare senza praticamente toccare strada asfaltata