irfanalee commited on
Commit
c2da52a
Β·
verified Β·
1 Parent(s): a0a6e75

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +216 -0
README.md ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ license: mit
5
+ tags:
6
+ - video-anomaly-detection
7
+ - jepa
8
+ - unsupervised
9
+ - pytorch
10
+ - cctv
11
+ - world-model
12
+ - vit
13
+ datasets:
14
+ - ucsd-ped2
15
+ - shanghaitech
16
+ metrics:
17
+ - roc_auc
18
+ ---
19
+
20
+ <div align="center">
21
+
22
+ # πŸ›‘οΈ WorldGuard
23
+
24
+ ### JEPA-Inspired Video World Model for Unsupervised CCTV Anomaly Detection
25
+
26
+ *Inspired by Yann LeCun's AMI Labs world model thesis β€” implemented locally on a single RTX A4000*
27
+
28
+ <br/>
29
+
30
+ ![Release](https://img.shields.io/badge/RELEASE-v0.1.0-brightgreen?style=flat-square)
31
+ ![Model](https://img.shields.io/badge/ARCHITECTURE-V--JEPA-8a2be2?style=flat-square)
32
+ ![Paradigm](https://img.shields.io/badge/PARADIGM-World%20Model-ff6b35?style=flat-square)
33
+ ![Labels](https://img.shields.io/badge/LABELS-Zero-blue?style=flat-square)
34
+ ![Python](https://img.shields.io/badge/Python-3.10%2B-3776ab?style=flat-square&logo=python&logoColor=white)
35
+ ![PyTorch](https://img.shields.io/badge/PyTorch-2.x-ee4c2c?style=flat-square&logo=pytorch&logoColor=white)
36
+ ![CUDA](https://img.shields.io/badge/CUDA-12.x-76b900?style=flat-square&logo=nvidia&logoColor=white)
37
+ ![License](https://img.shields.io/badge/LICENSE-MIT-yellow?style=flat-square)
38
+
39
+ </div>
40
+
41
+ ---
42
+
43
+ ## πŸ“Š Results
44
+
45
+ ### Benchmark Performance (Frame-level AUROC)
46
+
47
+ | Dataset | Run 1 (ShanghaiTech only) | Run 2 (Balanced) | Change |
48
+ |---|---|---|---|
49
+ | UCSD Ped2 | 0.545 | **0.788** | +24% |
50
+ | ShanghaiTech | 0.639 | **0.614** | -2.5% |
51
+
52
+ Run 1 trained on ShanghaiTech data only. Run 2 retrained on balanced UCSD Ped2 + ShanghaiTech clips β€” UCSD Ped2 jumped dramatically once the model saw that scene during training. ShanghaiTech dropped slightly, a normal trade-off when splitting training capacity between two datasets.
53
+
54
+ ### Context vs Published Work
55
+
56
+ | Method | UCSD Ped2 AUROC |
57
+ |---|---|
58
+ | Supervised SOTA | ~0.96–0.99 |
59
+ | Unsupervised / reconstruction-based | ~0.82–0.92 |
60
+ | **WorldGuard (this model, 50 epochs)** | **0.788** |
61
+
62
+ WorldGuard achieves competitive unsupervised performance with zero anomaly labels during training, on a single consumer GPU in 50 epochs.
63
+
64
+ ### To Push Further
65
+ - Train 100+ epochs β€” loss was still decreasing at epoch 50
66
+ - Add more ShanghaiTech training data (currently 2077 clips; full set = 330 videos)
67
+
68
+ ---
69
+
70
+ ## 🧠 Why This Exists β€” The World Model Thesis
71
+
72
+ In March 2026, Yann LeCun's **AMI Labs** raised **$1.03 billion** to build AI that goes beyond LLMs. The core thesis: real intelligence predicts abstract representations of future states, not pixels or tokens.
73
+
74
+ WorldGuard is a direct local implementation of that thesis applied to CCTV surveillance:
75
+
76
+ > *Train a model to predict what should happen next. When reality deviates from prediction β€” that's an anomaly.*
77
+
78
+ ### Why Not Just Train a Classifier?
79
+
80
+ | | Supervised Classifier | WorldGuard (World Model) |
81
+ |---|---|---|
82
+ | Labels needed | Hundreds per class | **Zero** |
83
+ | Detects novel anomalies | No β€” known classes only | **Yes β€” any deviation** |
84
+ | Generalizes to new cameras | Poor | **Better β€” learns scene structure** |
85
+ | Failure mode | Unknown unknowns invisible | All deviations flagged |
86
+
87
+ ---
88
+
89
+ ## βš™οΈ How It Works
90
+
91
+ ```
92
+ CCTV Video Stream
93
+ β”‚
94
+ β–Ό
95
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
96
+ β”‚ Frame Extractor β”‚ 16 frames @ 224Γ—224, stride 2 (~1s @ 30fps)
97
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
98
+ β”‚
99
+ β–Ό
100
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
101
+ β”‚ JEPA WORLD MODEL β”‚
102
+ β”‚ β”‚
103
+ β”‚ Context Encoder (ViT-S/16) β”‚
104
+ β”‚ β†’ z_ctx [B, T_ctx, D] β”‚
105
+ β”‚ β”‚
106
+ β”‚ Predictor (Transformer, 4 layers) β”‚
107
+ β”‚ β†’ z_pred [B, T_future, D] β”‚
108
+ β”‚ β”‚
109
+ β”‚ Target Encoder (EMA β€” no gradients) β”‚
110
+ β”‚ β†’ z_tgt [B, T_future, D] β”‚
111
+ β”‚ β”‚
112
+ β”‚ Loss = L2(z_pred, z_tgt) β”‚
113
+ β”‚ ↑ latent space only β€” never pixels β”‚
114
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
115
+ β”‚
116
+ β–Ό
117
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
118
+ β”‚ Anomaly Scorer β”‚ mean prediction error per clip
119
+ β”‚ + Spatial Heatmap β”‚ per-patch error β†’ 224Γ—224 overlay
120
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
121
+ ```
122
+
123
+ The model **never trains on anomalies**. It learns the statistical structure of normality. At inference, anything that breaks that structure produces a spike in latent prediction error.
124
+
125
+ ---
126
+
127
+ ## πŸ—οΈ Architecture
128
+
129
+ | Component | Design | VRAM |
130
+ |---|---|---|
131
+ | **Context Encoder** | VideoMAE-pretrained ViT-S/16 (21M params) | ~1.8 GB |
132
+ | **Target Encoder** | EMA copy β€” frozen, no gradients | ~1.8 GB |
133
+ | **Predictor** | 4-layer Transformer (D=384, heads=6) | ~0.4 GB |
134
+ | **Activations** | Batch=16, 16 frames @ 224Γ—224 | ~8–10 GB |
135
+ | **Total** | | **~13–14 GB** |
136
+
137
+ ---
138
+
139
+ ## πŸš€ Quickstart
140
+
141
+ ### 1. Install dependencies
142
+
143
+ ```bash
144
+ pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
145
+ pip install -r requirements.txt
146
+ ```
147
+
148
+ ### 2. Download checkpoint
149
+
150
+ ```python
151
+ from huggingface_hub import hf_hub_download
152
+
153
+ path = hf_hub_download(
154
+ repo_id="irfanalii/worldguard",
155
+ filename="checkpoints/train_default_epoch050_val0.0191.pt"
156
+ )
157
+ ```
158
+
159
+ ### 3. Score a video
160
+
161
+ ```bash
162
+ python inference/score_video.py \
163
+ --video /path/to/footage.mp4 \
164
+ --checkpoint checkpoints/train_default_epoch050_val0.0191.pt \
165
+ --camera-id cam01
166
+ ```
167
+
168
+ ### 4. Evaluate on UCSD Ped2
169
+
170
+ ```bash
171
+ python eval/eval_roc.py \
172
+ --checkpoint checkpoints/train_default_epoch050_val0.0191.pt \
173
+ --test-dir data/ucsd_ped2/ \
174
+ --output outputs/eval/
175
+ ```
176
+
177
+ ### 5. Retrain on your own footage
178
+
179
+ ```bash
180
+ # Extract clips from your normal CCTV recordings
181
+ python data/extract_clips.py \
182
+ --video /path/to/cam01.mp4 \
183
+ --output-dir data/train \
184
+ --camera-id cam01
185
+
186
+ # Train
187
+ python training/train.py --config configs/train_default.yaml
188
+
189
+ # Calibrate threshold
190
+ python training/calibrate.py \
191
+ --checkpoint checkpoints/best.pt \
192
+ --val-dir data/val \
193
+ --camera-id cam01
194
+ ```
195
+
196
+ ---
197
+
198
+ ## πŸ“š References
199
+
200
+ - Assran et al. (2025). [V-JEPA 2: Self-Supervised Video Models Enable Understanding, Prediction and Planning](https://arxiv.org/abs/2506.09985)
201
+ - Bardes et al. (2024). [V-JEPA: Revisiting Feature Prediction for Learning Visual Representations from Video](https://ai.meta.com/blog/v-jepa-yann-lecun-ai-model-video-joint-embedding-predictive-architecture/)
202
+ - LeCun, Y. (2022). [A Path Towards Autonomous Machine Intelligence](https://openreview.net/pdf?id=BZ5a1r-kVsf)
203
+
204
+ ---
205
+
206
+ ## πŸ“„ License
207
+
208
+ MIT
209
+
210
+ ---
211
+
212
+ <div align="center">
213
+
214
+ Built by [@Irfanalee](https://github.com/Irfanalee) Β· [Source Code](https://github.com/Irfanalee/LLM-Trainings/tree/main/WorldGuard) Β· Inspired by LeCun's world model thesis Β· Runs entirely on local hardware
215
+
216
+ </div>