Skip to content

Repository files navigation

πŸ” NETAI: Network Anomaly Detection Models

Deep learning models for automated network anomaly detection using perfSONAR telemetry β€” a GSoC 2026 prototype for the National Research Platform (NRP).

Python 3.10+ PyTorch License Tests


Overview

This project implements reconstruction-based anomaly detection for network telemetry from the National Research Platform. It automatically identifies:

  • 🐌 Slow links β€” degraded throughput with elevated latency
  • πŸ“‰ High packet loss β€” excessive packet loss percentages
  • πŸ”„ Excessive retransmits β€” abnormal TCP retransmission counts
  • ❌ Failed tests β€” complete test failures (zero throughput)
  • πŸ“Š High jitter β€” unstable latency patterns

Three model architectures are implemented, trained, and evaluated:

Model Architecture Parameters ROC-AUC PR-AUC
Autoencoder Fully-connected encoder-decoder 13,796 0.820 0.216
LSTM Bidirectional LSTM with attention 176,125 0.932 0.990
Transformer Multi-head self-attention encoder 120,380 0.945 0.992

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    NETAI Anomaly Detection                       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Data Layer β”‚   Models     β”‚  Training    β”‚  Serving            β”‚
β”‚             β”‚              β”‚              β”‚                     β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚ β”‚SQLite DBβ”‚ β”‚ β”‚Autoencoderβ”‚ β”‚ β”‚ Trainer  β”‚ β”‚ β”‚ FastAPI REST  β”‚  β”‚
β”‚ β”‚(perfSON.)β”‚ β”‚ β”‚          β”‚ β”‚ β”‚          β”‚ β”‚ β”‚  /predict     β”‚  β”‚
β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”‚  /predict/bat.β”‚  β”‚
β”‚      β”‚      β”‚ β”‚  LSTM    β”‚ β”‚ β”‚Checkpointβ”‚ β”‚ β”‚  /health      β”‚  β”‚
β”‚ β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β” β”‚ β”‚(BiLSTM)  β”‚ β”‚ β”‚  Mgmt    β”‚ β”‚ β”‚  /model/info  β”‚  β”‚
β”‚ β”‚Feature  β”‚ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚ β”‚Pipeline β”‚ β”‚ β”‚Transform.β”‚ β”‚ β”‚ Early    β”‚ β”‚         β”‚          β”‚
β”‚ β”‚(rolling,β”‚ β”‚ β”‚(Attn.)   β”‚ β”‚ β”‚ Stopping β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚ β”‚ lag,    β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚  Kubernetes   β”‚  β”‚
β”‚ β”‚ diff,   β”‚ β”‚              β”‚              β”‚ β”‚  Deployment   β”‚  β”‚
β”‚ β”‚ scale)  β”‚ β”‚              β”‚              β”‚ β”‚  (GPU pods)   β”‚  β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚              β”‚              β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Project Structure

β”œβ”€β”€ src/netai_anomaly/
β”‚   β”œβ”€β”€ data/
β”‚   β”‚   β”œβ”€β”€ schema.py          # SQLite schema (perfSONAR-style tables)
β”‚   β”‚   β”œβ”€β”€ generator.py       # Synthetic telemetry data generator
β”‚   β”‚   β”œβ”€β”€ features.py        # Feature engineering pipeline
β”‚   β”‚   └── dataset.py         # PyTorch Dataset classes
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ base.py            # Base model + registry
β”‚   β”‚   β”œβ”€β”€ autoencoder.py     # FC Autoencoder
β”‚   β”‚   β”œβ”€β”€ lstm.py            # BiLSTM with temporal attention
β”‚   β”‚   └── transformer.py     # Transformer encoder
β”‚   β”œβ”€β”€ training/
β”‚   β”‚   β”œβ”€β”€ trainer.py         # Training loop with early stopping
β”‚   β”‚   └── utils.py           # Seed management
β”‚   β”œβ”€β”€ evaluation/
β”‚   β”‚   β”œβ”€β”€ metrics.py         # Precision, Recall, F1, ROC-AUC, PR-AUC
β”‚   β”‚   └── visualize.py       # Training curves, ROC, PR, score plots
β”‚   └── inference/
β”‚       └── service.py         # FastAPI REST inference service
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ generate_data.py       # Data generation CLI
β”‚   β”œβ”€β”€ train.py               # Model training CLI
β”‚   β”œβ”€β”€ evaluate.py            # Evaluation & plotting CLI
β”‚   └── serve.py               # Inference server CLI
β”œβ”€β”€ configs/                   # YAML configuration files
β”œβ”€β”€ tests/                     # 56 comprehensive tests
β”œβ”€β”€ k8s/                       # Kubernetes manifests
β”œβ”€β”€ Dockerfile                 # Inference container
β”œβ”€β”€ Dockerfile.training        # GPU training container
└── docker-compose.yaml        # Local development

Quick Start

1. Installation

# Clone the repository
git clone https://github.com/your-username/NETAI-Network-Anomaly-Detection-Models.git
cd NETAI-Network-Anomaly-Detection-Models

# Create virtual environment
python -m venv .venv
source .venv/bin/activate

# Install with all dependencies
pip install -e ".[dev,plots]"

2. Generate Synthetic Data

python scripts/generate_data.py --num-samples 50000 --anomaly-ratio 0.05

This creates a SQLite database at data/network_telemetry.db with realistic perfSONAR-style measurements including throughput, latency, packet loss, retransmits, and jitter.

3. Train a Model

# Train any of the three architectures
python scripts/train.py --model autoencoder --epochs 50
python scripts/train.py --model lstm --epochs 30
python scripts/train.py --model transformer --epochs 30

# With GPU acceleration
python scripts/train.py --model transformer --device cuda

4. Evaluate

python scripts/evaluate.py --checkpoint checkpoints/transformer_best.pt

Generates evaluation metrics and plots in outputs/<model>/.

5. Serve the Model

python scripts/serve.py --checkpoint checkpoints/transformer_best.pt --port 8000

Then query the API:

# Single prediction
curl -X POST http://localhost:8000/predict \
  -H "Content-Type: application/json" \
  -d '{
    "throughput_mbps": 500.0,
    "latency_ms": 200.0,
    "packet_loss_pct": 15.0,
    "retransmits": 150,
    "jitter_ms": 45.0
  }'

# Response:
# {"is_anomaly": true, "anomaly_score": 0.523, "threshold": 0.154, "confidence": 0.87}

# Batch prediction
curl -X POST http://localhost:8000/predict/batch \
  -H "Content-Type: application/json" \
  -d '{"samples": [{"throughput_mbps": 9500, "latency_ms": 5, "packet_loss_pct": 0.01, "retransmits": 2, "jitter_ms": 0.5}]}'

Testing

# Run all 56 tests
python -m pytest tests/ -v

# With coverage
python -m pytest tests/ --cov=netai_anomaly --cov-report=term-missing

Test coverage includes:

  • Data layer: SQLite schema, data generation, reproducibility, roundtrip I/O
  • Feature engineering: Rolling stats, lag features, normalization, pipeline fit/transform
  • Models: Forward pass shapes, anomaly scores, gradient flow, all architectures
  • Training: Loss convergence, checkpointing, threshold computation, early stopping
  • Inference API: All endpoints, error handling, batch processing, validation

Kubernetes Deployment

Inference Service

# Build and deploy
docker build -t netai-anomaly:latest .
kubectl apply -f k8s/namespace.yaml
kubectl apply -f k8s/configmap.yaml
kubectl apply -f k8s/deployment-inference.yaml
kubectl apply -f k8s/service-inference.yaml

GPU Training Job

# Build training image and launch on NRP GPU cluster
docker build -f Dockerfile.training -t netai-anomaly-training:latest .
kubectl apply -f k8s/job-training.yaml

The training job requests NVIDIA GPU resources and includes proper tolerations for GPU-enabled nodes on the NRP Kubernetes cluster.

Feature Engineering Pipeline

The pipeline transforms raw telemetry into model-ready features:

  1. Rolling statistics β€” Mean and standard deviation over windows of 5, 15, and 30 time steps
  2. Lag features β€” Previous values at lags of 1, 3, 5, and 10 steps
  3. Rate of change β€” First-order differences for trend detection
  4. Normalization β€” StandardScaler, MinMaxScaler, or RobustScaler

Starting from 5 raw metrics, the pipeline produces 60 engineered features per sample.

Model Details

Autoencoder

Fully-connected encoder-decoder network that compresses telemetry into a low-dimensional latent space. Anomalies produce high reconstruction error because the model has only learned to reconstruct normal patterns.

LSTM (Long Short-Term Memory)

Bidirectional LSTM with temporal attention that captures sequential dependencies in network time series. Processes sliding windows of 60 time steps to detect anomalous temporal patterns.

Transformer

Multi-head self-attention encoder with sinusoidal positional encoding. Excels at capturing long-range dependencies and achieves the highest ROC-AUC (0.945) among all models.

Configuration

All hyperparameters are managed through YAML files in configs/:

# configs/default.yaml
data:
  sequence_length: 60
  anomaly_ratio: 0.05
feature_engineering:
  rolling_windows: [5, 15, 30]
  lag_steps: [1, 3, 5, 10]
  scaler: "standard"
training:
  epochs: 50
  batch_size: 64
  learning_rate: 0.001
  patience: 10
  scheduler: "cosine"

Technologies

Category Technologies
Deep Learning PyTorch, Autoencoder, LSTM, Transformer
ML/Data scikit-learn, Pandas, NumPy
Storage SQLite (perfSONAR telemetry)
API FastAPI, Pydantic, Uvicorn
Infrastructure Docker, Kubernetes, GPU Pods
Testing pytest (56 tests), pytest-cov
Config YAML, argparse

License

Apache License 2.0 β€” see LICENSE.

Acknowledgments

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages