TinyML-based Autoencoder for Real-Time Anomaly Detection in Resource-Constrained IoT Sensor Streams
Anomaly detection of sensors within small hardware platforms such as ESP32 is complicated by its lack of memory space and computational capabilities. This paper presents a lightweight anomaly detection model that uses autoencoders, developed and optimized using the TensorFlow Lite Micro framework. The model was trained based solely on normal readings of the sensors without any anomalies in order to deploy it to the ESP32. Additional features were introduced to enhance the performance of anomaly detection, such as motion magnitude (orientation-independent feature) and time of day. In contrast to other approaches, we use the 90th percentile of the reconstruction error to establish the anomaly threshold rather than a commonly used mean-plus-standard deviation technique. Tests on the anomaly detection algorithm using a portion of a big IoT data set having more than 50 thousand samples revealed the accuracy level to be 96.4%, recall rate as 43.29%, precision 31.56%, and the F1-score value of 36.5%. It clearly shows the performance was considerably higher compared to the baseline method that used the fixed threshold value. The same system was further evaluated using an actual ESP32 microcontroller board installed with the PIR sensor, MQ-2 gas sensor, and DHT22 sensor. In all cases, it correctly identified all five faults in a single inference run without generating any false positives for 10 minutes of operation time.