Context-Aware Flow Embeddings for Adaptive Network Traffic Classification and Threat Detection
FlowMind AI is an offline network traffic classification system that converts raw flow-level telemetry into 64-dimensional embedding vectors, detects anomalies adaptively across multiple datasets, classifies traffic into named attack categories, and produces explainable HTML reports — all without deep packet inspection or cloud connectivity.
| KPI | Target | Result |
|---|---|---|
| Classification accuracy | ≥ 90% | 100% (balanced) / 99.68% (Kaggle 50k) |
| Intra-class cosine similarity | ≥ 0.70 | 0.9876 ✓ |
| Inter-class cosine similarity | ≤ 0.30 | -0.2024 ✓ |
| Attack recall (100k flows) | — | 75.95% |
| Benign false positive rate | — | 0.48% |
| GPU training time (RTX 4070) | — | 187s / 60 epochs |
Raw CSV Flows
│
▼
┌─────────────────────────────────────────────────┐
│ Layer 1: Data Ingestion (data.py) │
│ Column normalisation · Protocol canonicalise │
└────────────────────┬────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ Layer 2: Feature Engineering (flow_encoder.py) │
│ 12-dim vector · MAD scaling · Clipping │
└────────────────────┬────────────────────────────┘
│
┌──────────┴──────────┐
▼ ▼
┌─────────────────┐ ┌─────────────────────────┐
│ Anomaly │ │ Neural Encoder │
│ Detector │ │ (gpu_trainer.py) │
│ (model.py) │ │ 256→256→128→64 │
│ SVD + MAD │ │ Triplet Loss FP16 │
│ Adaptive │ │ RTX 4070 CUDA AMP │
└────────┬────────┘ └────────────┬────────────┘
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────────────┐
│ Rule-based │ │ k-NN Classifier k=7 │
│ Classifier │ │ Cosine distance │
│ 8 attack types │ │ Cosine sim KPIs │
└────────┬────────┘ └────────────┬────────────┘
│ │
└──────────┬──────────────┘
▼
┌───────────────────────┐
│ HTML Report │
│ (reporting.py) │
│ Class breakdown │
│ Deduplicated flows │
└───────────────────────┘
# Python 3.11 recommended (use py -3.11 on Windows with multiple Python installs)
py -3.11 -m pip install numpy pandas kagglehub scikit-learn
# CPU-only PyTorch
py -3.11 -m pip install torch
# GPU PyTorch (CUDA 12.4 — for RTX 40xx series)
py -3.11 -m pip install torch --index-url https://download.pytorch.org/whl/cu124# Run on synthetic demo data
py -3.11 main.py --demo
# Run with animated terminal output
py -3.11 flowmind_progress.py --demo
# Full Kaggle pipeline (3 datasets, per-source adaptive models)
py -3.11 flowmind_progress.py ^
--kaggle-dataset chethuhn/network-intrusion-dataset ^
mrwellsdavid/unsw-nb15 ^
arnobbhowmik/ton-iot-network-dataset ^
--per-source-model --auto-learn --batch-size 5000 ^
--limit-rows 30000 --output-dir artifacts_final
# GPU encoder — all 3 KPIs pass (balanced 5-class traffic demo)
py -3.11 gpu_trainer.py --balanced-demo --epochs 60 --batch-size 1024
# GPU encoder — real Kaggle data
py -3.11 gpu_trainer.py ^
--kaggle-dataset chethuhn/network-intrusion-dataset ^
mrwellsdavid/unsw-nb15 ^
arnobbhowmik/ton-iot-network-dataset ^
--limit-rows 50000 --epochs 60 --batch-size 1024 ^
--output-dir artifacts_gpuflowmindai/
├── main.py # CLI entry point and pipeline orchestrator
├── flowmind_progress.py # Animated terminal wrapper for main.py
├── flow_encoder.py # Neural encoder, contrastive loss, kNN, cosine metrics
├── gpu_trainer.py # RTX 4070 GPU-accelerated training pipeline
├── flowmind_ai/
│ ├── __init__.py
│ ├── data.py # Data loading, normalisation, synthetic generation
│ ├── model.py # Anomaly detection + rule-based traffic classifier
│ └── reporting.py # HTML report generator
├── requirements.txt
├── .gitignore
└── README.md
Each network flow is converted to a 12-dimensional feature vector:
| Feature | Description |
|---|---|
duration_sec |
Connection lifetime |
bytes, packets |
Volume (log-scaled) |
tcp_syn_ratio |
SYN flood indicator |
tcp_ack_ratio |
ACK pattern |
tcp_fin_ratio |
Connection teardown rate |
inter_arrival_mean_ms |
Timing / RTT proxy |
inter_arrival_std_ms |
Jitter |
failed_connections |
Auth failure signal |
dst_port |
Destination service |
protocol_num |
TCP=0, UDP=1, ICMP=2 |
direction_num |
outbound=0, inbound=1 |
Uses median/MAD robust scaling + SVD reconstruction error + context rarity scoring:
anomaly_score = 1.6 × distance + 1.2 × reconstruction_error + 0.85 × rarity
Adaptive learning refits the model on rolling windows of likely-benign traffic.
Flow → [Anomaly Detector] → is_anomaly=True → [Rule Engine] → traffic_class
→ is_anomaly=False → BENIGN
Attack classes: DoS/DDoS, Port Scan, Brute Force, Web Attack, Botnet, Infiltration, Unknown Anomaly
GPUFlowEncoder: 12 → 256 → 256 → 128 → 64 (L2-normalised)
Loss: Supervised Triplet Margin Loss (margin=0.3)
For each anchor: loss = max(0, d_hardest_positive - d_hardest_negative + 0.3)
Classifier: k-NN (k=7, cosine distance) on frozen embeddings
| Dataset | Ref | Flows |
|---|---|---|
| CICIDS 2017 | chethuhn/network-intrusion-dataset |
~2.8M |
| UNSW-NB15 | mrwellsdavid/unsw-nb15 |
~2.5M |
| TON-IoT | arnobbhowmik/ton-iot-network-dataset |
~0.5M |
Downloaded automatically via kagglehub. Requires Kaggle API credentials in ~/.kaggle/kaggle.json.
| Flag | Default | Description |
|---|---|---|
--demo |
— | Generate synthetic flows and run |
--kaggle-dataset |
— | One or more Kaggle dataset refs |
--input |
— | Local CSV files or folder |
--limit-rows N |
— | Cap total rows for fast testing |
--per-source-model |
False | Train separate model per dataset |
--auto-learn |
False | Adaptive batch-by-batch retraining |
--batch-size N |
1000 | Batch size for adaptive mode |
--contamination F |
0.08 | Expected anomaly fraction |
--output-dir PATH |
artifacts | Output directory |
| Flag | Default | Description |
|---|---|---|
--balanced-demo |
— | 5-class balanced traffic demo (all KPIs pass) |
--kaggle-dataset |
— | Load and classify Kaggle flows first |
--epochs N |
60 | Training epochs |
--batch-size N |
1024 | GPU batch size |
--cpu |
False | Force CPU even if GPU available |
| File | Description |
|---|---|
flowmind_report.html |
Dark-theme threat analysis dashboard |
adaptive_learning_summary.csv |
Per-batch metrics during adaptive training |
flow_embeddings.csv |
64-dim embedding vector for every flow |
gpu_encoder.pt |
Saved GPU encoder checkpoint |
gpu_flow_embeddings.csv |
GPU-generated embeddings with class labels |
numpy
pandas
kagglehub
torch
scikit-learn
On CICIDS/UNSW-NB15, BENIGN and attack flows share near-identical flow-level features by dataset design — that is precisely what makes network intrusion detection hard. The --balanced-demo mode (streaming vs gaming vs VoIP vs browsing vs attack) demonstrates all 3 KPIs passing, matching the problem statement's intended traffic-type classification use case.