LGN-Net Video Anomaly Detector
A Gradio GUI for LGN-Net (Local-Global Normality Network, Zhao et al.) β a video future-frame-prediction anomaly detector. It predicts frame N from frames N-4 .. N-1; frames it fails to predict well (vs. its learned memory of "normal" patterns) are flagged as anomalies.
β οΈ This model needs TWO files, not one
The original training script saves two separate files per epoch:
torch.save(model.state_dict(), 'Nmodel.pth') # network weights
torch.save(m_items, 'Nkeys.pt') # the memory bank ("normal" prototypes)
You need both. model.pth alone will load without error, but the
memory bank will be an untrained random tensor and every anomaly score
will be meaningless. Locate the matching Nkeys.pt from your training
run and rename it keys.pt next to model.pth in this folder.
Files
app.pyβ Gradio GUI (Video tab + 5-frame Image Sequence tab)model.pyβ faithful port of the LGN-Net architecture (Encoder/Decoder/ Predictor/ConvLSTM/Memory) plus CPU-safe inference helpersmodel.pthβ you must add this (network weights / state_dict)keys.ptβ you must add this (memory bank, see above)requirements.txt
Why "grayscale" still means a 3-channel network
The network's Encoder/Decoder are hardcoded to n_channel=3. The
original UCSD Ped2 dataset is grayscale but loaded via cv2.imread,
which replicates it into 3 identical channels. model.py does the same
replication automatically for grayscale input β there is no literal
single-channel mode.
Important limitation: this is a video model
A single still image has no temporal context, so it cannot produce a
meaningful anomaly score on its own. The Video tab is the intended
use case: it slides a 5-frame window across the whole clip and computes
a calibrated 0β1 anomaly score per frame (normalized using that video's
own PSNR / feature-distance statistics, exactly as in the paper's
Evaluate.py). The Image Sequence tab accepts exactly 5 consecutive
frames for quick testing, but can only report raw, uncalibrated metrics
(PSNR, feature distance) since normalization needs a full video.
Deploying to Hugging Face Spaces
- Create a new Space β SDK: Gradio.
- Upload all files in this folder, including
model.pthandkeys.pt. - The Space will auto-build and launch
app.py.
Local testing
pip install -r requirements.txt
python app.py