Crowd Anomaly Detection
Deep learning pipeline for detecting crowd violence, unusual behavior, and density in video
· Jul 2024 – Nov 2025 · PyTorch · YOLOv8n · 3D CNN · Computer Vision
YOLOv8n Behavior Detection
Behavior bounding boxes on HAJJv2 validation frames (0.955 mAP@0.5, 0.713 mAP@0.5:0.95).
What It Does
I worked on this remotely with a team in Saudi Arabia to detect abnormal crowd behavior and estimate crowd density from surveillance video. We trained and evaluated across two benchmarks: binary Crowd Violence Detection and HAJJv2 (both 7-class frame detection and 4-class multi-label clip classification).
Binary Violence (Conv3D + LSTM)
Moved from 2D and 3D CNN baselines to Conv3D + LSTM with 5-Fold Stratified CV (90-frame sequences, Masking, L2), hitting 83.93% ± 6.83% CV accuracy, 82.35% test accuracy, and 0.88 ± 0.08 AUC.
YOLOv8n Crowd Detection
Trained YOLOv8n (640x640, mosaic augmentation, Soft-NMS) on HAJJv2, getting 0.955 validation mAP@0.5 and showing why single-frame models struggle on motion classes.
Hybrid YOLO3DCNN + Motion
Fused 10 pretrained YOLOv8n layers (256 ch) with a frame-difference motion channel (cv2.absdiff, 257 ch) into a 3D CNN head plus log-density regression.
Focal Loss & 4-Class Grouping
Balanced rare behaviors using FocalLoss(alpha=0.25, gamma=2.0) and merged 7 classes into 4 macro groups, raising test F1 macro from 0.725 to 0.865.
Trade-Offs & Decisions
01 · Problem
Frame-by-frame YOLOv8n looked great on validation (0.955 mAP@0.5 on train3) but fell to 0.417 mAP@0.5 on unseen test footage (val4). Still frames miss motion clues like running or walking against the crowd, and rare classes were heavily outnumbered.
02 · Approach
We took 10 pretrained YOLOv8n spatial feature layers (256 channels), stacked a cv2.absdiff grayscale motion channel (257 channels total) into a Conv3d temporal head with FocalLoss(alpha=0.25, gamma=2.0) and log1p density prediction, and grouped the 7 uneven labels into 4 macro classes.
03 · Outcome
Held-out test generalization jumped from 0.725 to 0.865 F1 macro, with 0.948 F1 macro and 0.956 Jaccard on validation.
Architecture & Build
- Phase 1 (src/violence_detection/): TensorFlow/Keras 2D CNN, 3D CNN, Conv3D + LSTM with 5-Fold CV, and an ablation across 5 attention mechanisms.
- Phase 2 (src/hajjv2_detection/): PyTorch YOLO3DCNN and YOLO3DCNN_MultiLabel models sampling 16-frame clips with log1p crowd density regression.
- Dataset scripts: Portable YOLOv8 config (configs/hajjv2.yaml) and CSV-to-YOLO converter (prepare_yolo_dataset.py).
- CLI inference script (infer_4c.py) and repeatable JSON, CSV, and TXT evaluation outputs in reports/.
Tech Stack
- Python
- PyTorch
- Ultralytics YOLOv8
- TensorFlow / Keras
- OpenCV
- NumPy
- Pandas
- scikit-learn
- Matplotlib / Seaborn
Workflow & Commands
pip install -r requirements.txtpython src/hajjv2_detection/train_yolo_3dcnn_4c.pypython src/hajjv2_detection/infer_4c.py --source <video_or_frames> --checkpoint best_model_4classes.ptEgypt bnbn Travel