Skip to content

Refactor: Restructure codebase into modular package with API/UI - #1

Open
duclld1709 wants to merge 1 commit into
mainfrom
claude/funny-cannon-y5ye4j
Open

duclld1709 wants to merge 1 commit into
mainfrom
claude/funny-cannon-y5ye4j

Conversation

@duclld1709

Copy link
Copy Markdown
Owner

Summary

This is a major refactoring that transforms the project from a single-file training script into a production-ready Python package with a modular architecture, REST API, Gradio UI, and comprehensive testing.

Key Changes

Package Structure

  • Removed monolithic train.py and app.py
  • Created deepfake_detector/ package with separated concerns:
    • model.py - ResNeXt-50 + LSTM classifier with checkpoint compatibility
    • inference.py - End-to-end video analysis pipeline
    • config.py - Centralized configuration management
    • faces.py - Face detection and tracking abstractions
    • video.py - Video decoding utilities
    • preprocessing.py - Image transforms and tensor conversion
    • explain.py - Grad-CAM visualization
    • schemas.py - Pydantic response models
    • metrics.py - Binary classification metrics

Training & Evaluation

  • Moved training logic to training/train.py with improvements:
    • Device-agnostic code (no hard-coded CUDA tensors)
    • Group-aware leakage-free splits
    • Held-out test set
    • AUC-based model selection
    • Gradient clipping and AMP support
  • Added training/data.py for dataset handling with augmentation
  • Added training/evaluate.py for robustness benchmarking (H.264 CRF, downscale, FPS)
  • Added training/extract_faces.py for preprocessing raw videos

Serving & APIs

  • api.py - FastAPI REST endpoint with upload handling, error codes, concurrency limits
  • ui.py - Gradio interface with timeline visualization and Grad-CAM overlays
  • cli.py - Command-line interface (deepfake-detect command)

Quality & Deployment

  • Added 42 tests covering model compatibility, pipeline, API, and training
  • Added GitHub Actions CI workflow
  • Added Dockerfile for containerized deployment
  • Added pyproject.toml with proper dependency management
  • Added model card documentation

Backward Compatibility

  • Original checkpoint loads without remapping (parameter names preserved)
  • Two temporal modes: legacy (original behavior) and sequence (intended design)
  • Exact per-frame scores available in legacy mode

Notable Implementation Details

  • No train/serve skew: Face extraction uses the same detector/tracker as inference
  • Honest output: Inconclusive verdict band, face coverage reporting, setup mismatch warnings
  • Memory-efficient Grad-CAM: Only selected frames re-encoded with gradients
  • Leakage-free evaluation: Group-aware splits prevent source-video leakage across train/val/test
  • Comprehensive metrics: AUC, EER, ECE, confusion matrices with per-class reports

https://claude.ai/code/session_01TCP7CvAwTrFAUjjDTNnJXT

Restructure the course-project scripts into a `deepfake_detector` package
with a CLI, FastAPI service, Gradio UI and Docker image, plus a rewritten
training/evaluation pipeline.

Model fixes (checkpoint stays loadable, equivalence pinned by tests):
- LSTM was built without batch_first, so inference had no temporal
  modelling and batch>1 training leaked hidden state between videos.
  `legacy` mode reproduces the checkpoint exactly without the leak;
  `sequence` mode adds real recurrence for new training.
- 4th positional LSTM arg was `bias`, not `bidirectional`; made explicit.
- Dropout moved from logits to features.

Pipeline: multi-clip sampling over the whole video, single-subject face
tracking, per-frame/per-clip scores, inconclusive band, Grad-CAM on the
most suspicious frames, typed errors instead of crashes on no-face input.

Training: group-aware splits, held-out test set, AUC/EER/ECE from
probabilities, recompression augmentation, device-agnostic loop, and an
end-to-end robustness benchmark (H.264 CRF, downscale, FPS).

Adds 42 CPU tests, ruff, GitHub Actions CI, model card and a new README.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TCP7CvAwTrFAUjjDTNnJXT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants