Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

10 Commits

Repository files navigation

TFMe: Time–Frequency–Memory-Enhanced Transformer for Anomaly Detection

TFMe is a reconstruction-based anomaly detector for LiDAR time series collected in car-parking environments. It couples a dual-branch Transformer autoencoder — one branch operating in the time domain, one in the frequency domain — with a multi-head memory module that constrains reconstructions to patterns seen during normal-only training.

Because the model is trained exclusively on normal data and never learns to reconstruct anomalies, a sample is flagged when either branch fails to reconstruct it within a fixed error threshold.

Architecture

ComponentRole
PositionalEncodingStandard sinusoidal encoding applied to both branches
MultiHeadMemoryLearned memory bank queried by cosine similarity, with hard shrinkage to sparsify addressing
TimeBranchTransformer encoder over the raw temporal sequence, followed by memory read-out and reconstruction
SpectralBranchReal FFT of the input, Transformer encoder over frequency bins, memory read-out, inverse FFT back to the time domain
DualDomainTransformerWraps both branches; returns both reconstructions and both attention maps

Memory module

Each input embedding is split across n_heads and matched against a bank of MEMORY_SLOTS learned prototypes via cosine similarity. The resulting attention is softmaxed, then passed through a hard-shrinkage operator with threshold λ = 1 / MEMORY_SLOTS, which zeroes out weakly matched slots and re-normalizes the remainder. The reconstruction is a sparse convex combination of memory items, so inputs unlike anything in the training distribution cannot be reconstructed accurately.

Detection rule

Per-sample mean squared reconstruction error is computed independently for each branch and compared against a 3-sigma threshold calibrated on the training set:

BranchThreshold
Time0.007409659097902477
Frequency0.005404344177804887

A sample is labelled anomalous if either branch exceeds its threshold.

Data

dataset.zip contains dataset.npy, a float32 array of shape (71045, 340). The first 305 columns hold the LiDAR features used by the model; the remaining columns are ignored by the notebook.

Each row is reshaped to (5, 61) — 5 consecutive timesteps × 61 azimuth bins — giving one sample per row. Features are scaled with a MinMaxScaler fitted on the training split only.

SplitRow rangeSamples
Train0 : 5033550,335
Validation50335 : 6034110,006
Test60341 : 7104510,704

The test split contains normal samples only, so the evaluation in the notebook reports false positives and true negatives.

Configuration

ParameterValue
N_LAYERS4
D_MODEL128
N_HEAD8
FFN512
MEMORY_SLOTS250
TIMESTEPS5
FEATURES61
BATCH_SIZE256
EPOCHS500
LEARNING_RATE1e-4

Training uses MSE reconstruction loss with early stopping (patience-based, restoring best weights).

Prerequisites

Conda is recommended for managing the environment.

python 3.8.20
pytorch 2.4.1+cu118
cudatoolkit 11.8.0
cudnn 9.10.1.4
numpy 1.24.1
scikit-learn 1.3.2

A CUDA device is used automatically when available; the notebook falls back to CPU otherwise.

Usage

  1. Extract the dataset into the repository root:

    unzip dataset.zip
  2. Open TFMe_Model_Architecture.ipynb and run the cells in order. The notebook loads and splits the data, defines the full architecture, restores the pretrained weights from best.pth, and evaluates the detector on the test split.

To use the model directly:

importtorchmodel=DualDomainTransformer()
model.load_state_dict(torch.load("best.pth", map_location=DEVICE))
model.to(DEVICE).eval()
withtorch.no_grad():
time_recon, freq_recon, time_att, freq_att=model(inputs) # inputs: (B, 5, 61)

Repository Contents

FileDescription
TFMe_Model_Architecture.ipynbFull model definition, pretrained-weight loading, and evaluation walkthrough
best.pthPretrained DualDomainTransformer checkpoint
dataset.zipCompressed dataset.npy feature array

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages