7.7.4. Generic Time Series Anomaly Detection
This example serves as a “Hello World” introduction to time series anomaly detection using the TinyML ModelZoo toolchain. It demonstrates how to use autoencoder-based anomaly detection with our toolchain.
7.7.4.1. Overview
Task: Time series anomaly detection (autoencoder-based)
Dataset: Synthetic sinusoidal signals with frequency/amplitude anomalies
Model: AD_17k (~17,000 parameters)
Target: F28P55 (NPU device)
7.7.4.2. Running the Example
cd tinyml-modelzoo
./run_tinyml_modelzoo.sh examples/generic_timeseries_anomalydetection/config.yaml
cd tinyml-modelzoo
run_tinyml_modelzoo.bat examples\generic_timeseries_anomalydetection\config.yaml
7.7.4.3. Understanding the Dataset
The dataset uses a synthetic sinusoidal pattern:
Normal Pattern:
Signal: y = 1.2 sin(2pi*f*t) + 0.8 cos(2pi*f*t)
Base frequency: f = 1.0 Hz
Amplitude variation: +/- 10%
Frequency variation: +/- 5%
Anomaly Types:
Type |
Description |
|---|---|
Frequency Faster |
Signal frequency increases by 30-70% |
Frequency Slower |
Signal frequency decreases by 20-50% |
Amplitude Higher |
Signal amplitude increases by 60-120% |
Amplitude Lower |
Signal amplitude decreases by 40-70% |
Dataset split:
Normal samples: 60 files (train: 50%, val: 10%, test: 40%)
Anomaly samples: 32 files (all in test set)
Each file contains 5000 samples (50 seconds at 100 Hz).
7.7.4.4. Dataset Format
For anomaly detection, ModelZoo expects this folder structure:
dataset_name/
|
|-- classes/
|-- Normal/
| |-- normal_0000.csv
| |-- normal_0001.csv
| |-- ...
|
|-- Anomaly/
|-- freq_faster_0000.csv
|-- freq_slower_0000.csv
|-- amp_higher_0000.csv
|-- amp_lower_0000.csv
|-- ...
Important
The autoencoder is trained only on Normal data. All Anomaly files are used exclusively for testing.
7.7.4.5. Configuration
common section:
common:
target_module: 'timeseries'
task_type: 'generic_timeseries_anomalydetection'
target_device: 'F28P55'
dataset section:
dataset:
enable: True
dataset_name: generic_timeseries_anomalydetection
input_data_path: https://software-dl.ti.com/C2000/esd/mcu_ai/01_03_00/datasets/generic_timeseries_anomalydetection.zip
split_factor: [0.5, 0.1, 0.4]
data_processing_feature_extraction section:
data_processing_feature_extraction:
data_proc_transforms:
- SimpleWindow
- Downsample
frame_size: 100
sampling_rate: 100
new_sr: 10
variables: 1
training section:
training:
enable: True
model_name: 'AD_17k'
batch_size: 64
training_epochs: 50
num_gpus: 1
learning_rate: 0.001
quantization: 1
output_int: False
Important
output_int must be False for anomaly detection since reconstruction
error must be in float for threshold comparison.
7.7.4.6. How Autoencoder Detection Works
Training Phase (Normal Data Only):
Autoencoder learns to compress and reconstruct normal patterns
For normal samples: Reconstruction error is LOW (~0.008)
Model learns: “This is what normal looks like”
Testing Phase (Normal + Anomaly Data):
Normal sample:
Reconstruction Error: ~0.008 -> Correctly identified as normal
Anomaly sample:
Reconstruction Error: ~0.69 -> Flagged as anomaly (88x higher!)
Detection Logic:
if reconstruction_error > threshold:
-> ANOMALY
else:
-> NORMAL
7.7.4.7. Why Frame Size Matters
Anomaly detection requires temporal context - the model must see multiple consecutive samples to understand the pattern.
Frame Size |
Cycles Visible |
Detection Quality |
|---|---|---|
1 - 10 |
< 0.1 cycles |
Insufficient |
20 - 50 |
0.2 - 0.5 |
Weak (partial cycle) |
100 |
1.0 cycle |
Good (full cycle visible) |
>200 |
2.0+ cycles |
Excellent |
Rule of thumb: Frame size should capture at least 1 complete cycle.
7.7.4.8. Expected Results
Reconstruction Error Statistics:
Data Type |
Mean |
Std Dev |
|---|---|---|
Normal training data |
0.0078 |
0.0022 |
Normal test data |
0.0081 |
0.0023 |
Anomaly test data |
0.6898 |
0.4616 |
Performance at Threshold k=4.5:
Metric |
Value |
|---|---|
Threshold |
0.0178 |
Accuracy |
99.97% |
Precision |
99.95% |
Recall (Detection Rate) |
100.00% |
F1 Score |
99.97% |
False Positive Rate |
0.07% |
Confusion Matrix:
Predicted Normal |
Predicted Anomaly |
|
|---|---|---|
Ground Truth: Normal |
9,617 (TN) |
7 (FP) |
Ground Truth: Anomaly |
0 (FN) |
12,832 (TP) |
The model successfully detects all 4 anomaly types with 100% recall!
7.7.4.9. Output Location
Results are saved to:
../tinyml-modelmaker/data/projects/{dataset_name}/run/{date-time}/{model_name}/
Key outputs:
training/base/- Float model training resultstraining/quantization/- Quantized model resultstraining/quantization/post_training_analysis/- Threshold analysistraining/quantization/post_training_analysis/threshold_performance.csv- Metrics tabletraining/quantization/post_training_analysis/reconstruction_error_histogram.png- Error distributioncompilation/artifacts/mod.a- Compiled for device
7.7.4.10. Next Steps
After successfully running this generic example:
Try Anomaly Detection Example - DC arc fault anomaly detection
Try Fan Blade Fault Classification - Fan blade anomaly detection
Try Motor Bearing Fault - Motor vibration anomaly detection
Read Anomaly Detection - Learn more about anomaly detection