AnomalyNet: files and card
Browse files- README.md +99 -0
- anomaly_net.pt +3 -0
- anomaly_net.py +34 -0
- executorch/fp32.pte +0 -0
- executorch/int8-cmsis-m0plus.pte +0 -0
- executorch/int8-cmsis-m33.pte +0 -0
- executorch/int8-cmsis-m4.pte +0 -0
- executorch/int8-cmsis-m7.pte +0 -0
- test_vectors.h +12 -0
- tflite/fp32.tflite +3 -0
- tflite/int8.tflite +3 -0
README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
library_name: pytorch
|
| 4 |
+
pipeline_tag: tabular-classification
|
| 5 |
+
tags:
|
| 6 |
+
- executorch
|
| 7 |
+
- zephyr
|
| 8 |
+
- tinyml
|
| 9 |
+
- anomaly-detection
|
| 10 |
+
- time-series
|
| 11 |
+
- industrial
|
| 12 |
+
- cortex-m
|
| 13 |
+
- int8
|
| 14 |
+
datasets:
|
| 15 |
+
- eoinedge/heater-anomaly-windows
|
| 16 |
+
metrics:
|
| 17 |
+
- accuracy
|
| 18 |
+
- precision
|
| 19 |
+
- recall
|
| 20 |
+
model-index:
|
| 21 |
+
- name: AnomalyNet
|
| 22 |
+
results:
|
| 23 |
+
- task:
|
| 24 |
+
type: tabular-classification
|
| 25 |
+
name: Temperature-window anomaly detection
|
| 26 |
+
dataset:
|
| 27 |
+
type: eoinedge/heater-anomaly-windows
|
| 28 |
+
name: heater-anomaly-windows (test split)
|
| 29 |
+
metrics:
|
| 30 |
+
- type: accuracy
|
| 31 |
+
value: 0.9962
|
| 32 |
+
- type: precision
|
| 33 |
+
value: 1.0
|
| 34 |
+
- type: recall
|
| 35 |
+
value: 0.9925
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
# AnomalyNet
|
| 39 |
+
|
| 40 |
+
An 842-parameter MLP that looks at 32 consecutive temperature samples (3.2 s at 10 Hz) and
|
| 41 |
+
says whether the window is normal or contains an anomaly: a spike, a dropout, a runaway
|
| 42 |
+
ramp or a burst of noise. It is the "second brain" in my dual-brain PLC demos: ExecuTorch
|
| 43 |
+
runs it on the microcontroller next to a ladder-logic scan task or a Linux SoC, and only a
|
| 44 |
+
shared variable (`p(anomaly)`) crosses between them.
|
| 45 |
+
|
| 46 |
+
```
|
| 47 |
+
fc1 32->24, ReLU
|
| 48 |
+
fc2 24->2 logits [normal, anomaly]
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
Input: `(1, 32)` float, Celsius normalised as `(t - 50) / 20`. Output: 2 logits; softmax
|
| 52 |
+
gives `p(anomaly)`.
|
| 53 |
+
|
| 54 |
+
## Files
|
| 55 |
+
|
| 56 |
+
| File | What |
|
| 57 |
+
|---|---|
|
| 58 |
+
| `anomaly_net.py` | model definition and the normalisation constants |
|
| 59 |
+
| `anomaly_net.pt` | trained state dict, fp32 |
|
| 60 |
+
| `executorch/fp32.pte` | ExecuTorch program, portable fp32 kernels (5.5 KB) |
|
| 61 |
+
| `executorch/int8-cmsis-m0plus.pte`, `-m4.pte`, `-m33.pte`, `-m7.pte` | int8 via the ExecuTorch Cortex-M quantizer (3.6 KB) |
|
| 62 |
+
| `tflite/fp32.tflite`, `tflite/int8.tflite` | the same weights through a Keras twin for TensorFlow Lite Micro |
|
| 63 |
+
| `test_vectors.h` | one normal and one anomaly window with reference logits |
|
| 64 |
+
|
| 65 |
+
## Results
|
| 66 |
+
|
| 67 |
+
Held-out synthetic windows (dataset test split, 4,000 windows, 50 % anomalies):
|
| 68 |
+
|
| 69 |
+
| Metric | Value |
|
| 70 |
+
|---|---|
|
| 71 |
+
| Accuracy | 99.62 % |
|
| 72 |
+
| Precision | 100 % |
|
| 73 |
+
| Recall | 99.25 % |
|
| 74 |
+
| Parameters | 842 |
|
| 75 |
+
| Inference, STM32U585 @ 160 MHz, fp32 | 1.6 ms |
|
| 76 |
+
| Inference, nRF52833 @ 64 MHz, fp32 | 4.3 ms |
|
| 77 |
+
| Inference, nRF9160 @ 64 MHz, fp32 | 4.0 ms |
|
| 78 |
+
|
| 79 |
+
## Training
|
| 80 |
+
|
| 81 |
+
Synthetic data only: the generator in `train/train_anomaly.py` of the
|
| 82 |
+
[UNO Q repo](https://github.com/eoinjordan/executorch-zephyr-arduino-uno-q) simulates a
|
| 83 |
+
heater loop around 50 C with slow drift and sensor noise, and injects four anomaly types.
|
| 84 |
+
The same generator produced the
|
| 85 |
+
[heater-anomaly-windows](https://huggingface.co/datasets/eoinedge/heater-anomaly-windows)
|
| 86 |
+
dataset. Adam, cross-entropy, a few hundred epochs on CPU in under a minute.
|
| 87 |
+
|
| 88 |
+
## Where it runs
|
| 89 |
+
|
| 90 |
+
- [executorch-zephyr-arduino-uno-q](https://github.com/eoinjordan/executorch-zephyr-arduino-uno-q): STM32U585 + Linux side, 13x8 LED matrix chart
|
| 91 |
+
- [executorch-zephyr-arduino-opta](https://github.com/eoinjordan/executorch-zephyr-arduino-opta): beside a ladder-logic scan task on a PLC
|
| 92 |
+
- micro:bit v2, Thingy:91, LAUNCHXL-CC1352P1 self-tests, and the
|
| 93 |
+
[ExecuTorch vs TFLM benchmarks](https://github.com/eoinjordan/executorch-vs-tflm-benchmarks)
|
| 94 |
+
|
| 95 |
+
Background: [AI for Industrial Control Systems: Bringing Edge AI to PLCs](https://www.edgeimpulse.com/blog/transform-industrial-control-systems-with-ai-anomaly-detection-made-easy/).
|
| 96 |
+
|
| 97 |
+
## License
|
| 98 |
+
|
| 99 |
+
Apache-2.0.
|
anomaly_net.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:556e735a12c213f1fdb79064655decdfa05ed9676969339c66e81bf481411bbf
|
| 3 |
+
size 5829
|
anomaly_net.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) 2026 Eoin Jordan
|
| 2 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 3 |
+
"""AnomalyNet: a tiny MLP that classifies a window of temperature samples.
|
| 4 |
+
|
| 5 |
+
Input: 32 consecutive readings (3.2 s at 10 Hz), normalised as (t - 50) / 20.
|
| 6 |
+
Output: 2 logits, [normal, anomaly]. About 850 parameters, so the fp32
|
| 7 |
+
portable ExecuTorch kernels run it in well under a millisecond on a
|
| 8 |
+
Cortex-M7 and the whole program is a few KB of flash.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
import torch.nn as nn
|
| 13 |
+
import torch.nn.functional as F
|
| 14 |
+
|
| 15 |
+
WINDOW = 32
|
| 16 |
+
SAMPLE_HZ = 10
|
| 17 |
+
MEAN = 50.0 # degrees C
|
| 18 |
+
SCALE = 20.0 # degrees C
|
| 19 |
+
CLASSES = ("normal", "anomaly")
|
| 20 |
+
INPUT_SHAPE = (1, WINDOW)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class AnomalyNet(nn.Module):
|
| 24 |
+
def __init__(self) -> None:
|
| 25 |
+
super().__init__()
|
| 26 |
+
self.fc1 = nn.Linear(WINDOW, 24)
|
| 27 |
+
self.fc2 = nn.Linear(24, len(CLASSES))
|
| 28 |
+
|
| 29 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 30 |
+
return self.fc2(F.relu(self.fc1(x)))
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def normalise(celsius: torch.Tensor) -> torch.Tensor:
|
| 34 |
+
return (celsius - MEAN) / SCALE
|
executorch/fp32.pte
ADDED
|
Binary file (5.54 kB). View file
|
|
|
executorch/int8-cmsis-m0plus.pte
ADDED
|
Binary file (3.61 kB). View file
|
|
|
executorch/int8-cmsis-m33.pte
ADDED
|
Binary file (3.61 kB). View file
|
|
|
executorch/int8-cmsis-m4.pte
ADDED
|
Binary file (3.61 kB). View file
|
|
|
executorch/int8-cmsis-m7.pte
ADDED
|
Binary file (3.61 kB). View file
|
|
|
test_vectors.h
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Generated by models/export.py; do not edit. */
|
| 2 |
+
#pragma once
|
| 3 |
+
#define TEST_INPUT_NUMEL 32
|
| 4 |
+
#define TEST_OUTPUT_NUMEL 2
|
| 5 |
+
#define TEST_OUTPUT_ARGMAX 1
|
| 6 |
+
static const float kTestInput[TEST_INPUT_NUMEL] = {
|
| 7 |
+
-0.0812852f, -0.1146014f, -0.1071035f, -0.0881012f, -0.1321746f, -0.0936299f, -0.1091498f, -0.0997824f,
|
| 8 |
+
-0.1035501f, -0.1003723f, -0.0939772f, -0.1167474f, -0.0850353f, -0.0964328f, -0.0956384f, -0.0917874f,
|
| 9 |
+
-0.0984082f, -0.0985181f, -0.1109982f, -0.8844818f, -0.8660494f, -0.8765051f, -0.8872373f, -0.8629481f,
|
| 10 |
+
-0.8762767f, -0.8841140f, -0.8852250f, -0.8664614f, -0.8660097f, -0.8524494f, -0.8733858f, -0.8584345f,
|
| 11 |
+
};
|
| 12 |
+
static const float kTestOutput[TEST_OUTPUT_NUMEL] = {-15.6083870f, 16.0947399f};
|
tflite/fp32.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2e0e52c8e2e1907d8ef4abad5ca0a76f56727115a5bf2bcf299537a74989b335
|
| 3 |
+
size 4832
|
tflite/int8.tflite
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6ed444b9ee05d928b2117f042e3bc7adcecde0f6e873d44382a6f703daea19a8
|
| 3 |
+
size 3216
|