# snarayan43/mls_final_project2025 architecture

> Architecture diagram of the GitHub repository snarayan43/mls_final_project2025, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/snarayan43/mls_final_project2025
- Repository: https://github.com/snarayan43/mls_final_project2025

## Overview

1. **Repository purpose**
   - This is an end-to-end deepfake detection experiment, not a general application.
   - The architecture is split into two major phases: **offline video preprocessing** and **online model training/evaluation**.

2. **External data boundary**
   - The source data comes from **FaceForensics++ (C23)**, specifically `original` vs `Face2Face`.
   - Raw inputs are **video files**; the project does not train directly from videos during the main experiment.

3. **Preprocessing subsystem: `preprocess.py`**
   - This is the data-engine layer.
   - It uses **OpenCV face detection** with `haarcascade_frontalface_default.xml` to locate facial regions.
   - For each video, it samples **32 frames** and extracts three region views in one pass:
     - **Full face**
     - **Eyes**
     - **Mouth**
   - Frames are resized to **224x224** and written out as **NumPy `.npy` arrays** for fast later loading.

4. **Preprocessed data boundary**
   - Preprocessing creates three separate datasets on disk:
     - `Processed_Data_Full_HighRes`
     - `Processed_Data_Eyes_HighRes`
     - `Processed_Data_Mouth_HighRes`
   - This cleanly separates expensive video decoding/face cropping from model training.

5. **Training data ingestion**
   - `run.py` loads the preprocessed `.npy` files through a custom `VideoDataGenerator`.
   - The generator is the interface between stored frame tensors and the TensorFlow training loop.
   - This means training consumes **frame sequences**, not raw images or videos.

6. **Model subsystem: `models.py`**
   - Both experiment variants share the same temporal feature extractor:
     - **TimeDistributed EfficientNetB1** as the per-frame CNN backbone
     - **Bidirectional LSTM** to model temporal dynamics across the 32-frame sequence
   - The key architectural difference is only in the final temporal aggregation layer.

7. **Two model heads**
   - **Control model:** uses **GlobalAveragePooling1D** to average temporal features across frames.
   - **Attention model:** uses a learned **temporal soft-attention** mechanism (`Dense` → `Softmax` → `Multiply`) to weight important frames more heavily.
   - This is the main experimental comparison in the repo.

8. **Experiment orchestration: `run.py`**
   - `run.py` is the top-level orchestrator for training.
   - It selects the region to train on (`full`, `eyes`, or `mouth`) and the model type.
   - It also handles **GPU memory safety**, training setup, and **early stopping**.

9. **Evaluation path**
   - After training, the pipeline produces a **confusion matrix** for final evaluation.
   - The architecture therefore includes a standard supervised classification loop: load preprocessed sequences → train model → evaluate predictions against labels.

10. **Utility/support files**
   - `inspect_img.py` is a helper for visually checking preprocessed outputs.
   - `post_cropping_images/` appears to be auxiliary experiment/output storage rather than part of the core training path.
   - `requirements.txt` defines the Python runtime dependencies, with the main stack centered on **Python 3.10**, **TensorFlow/Keras**, **OpenCV**, and **NumPy**.

11. **Overall system diagram perspective**
   - The cleanest architecture view is:
     - **Raw videos + face cascade** → **offline preprocessing** → **region-specific `.npy` datasets** → **generator-based training** → **shared CNN+BiLSTM model** → **control vs attention head** → **classification metrics**
   - The main architectural boundary is between **data preparation** and **model experimentation**.

Main entry points: [`preprocess.py`](https://github.com/snarayan43/mls_final_project2025/blob/main/preprocess.py) (Preprocess), [`models.py`](https://github.com/snarayan43/mls_final_project2025/blob/main/models.py) (BiLSTM), [`post_cropping_images`](https://github.com/snarayan43/mls_final_project2025/tree/main/post_cropping_images) (Full Dataset), [`run.py`](https://github.com/snarayan43/mls_final_project2025/blob/main/run.py) (Run)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_external_data["External Data"]
  node_videos[("FaceForensics++<br/>raw videos")]
  node_cascade["Face Cascade<br/>cv detector"]
end

subgraph group_preprocessing["Preprocessing"]
  node_preprocess["Preprocess<br/>offline pipeline<br/>[preprocess.py]"]
  node_face_crop["Full Face<br/>region crop"]
  node_eye_crop["Eyes<br/>region crop"]
  node_mouth_crop["Mouth<br/>region crop"]
end

subgraph group_datasets["Stored Datasets"]
  node_full_ds[("Full Dataset<br/>npy sequences")]
  node_eyes_ds[("Eyes Dataset<br/>npy sequences")]
  node_mouth_ds[("Mouth Dataset<br/>npy sequences")]
end

subgraph group_training["Training"]
  node_run["Run<br/>trainer orchestrator<br/>[run.py]"]
  node_generator["VideoDataGenerator<br/>data loader"]
end

subgraph group_modeling["Modeling"]
  node_effnet["EfficientNetB1<br/>frame encoder<br/>[models.py]"]
  node_bilstm["BiLSTM<br/>temporal encoder<br/>[models.py]"]
  node_control_head(("Average Head<br/>baseline head<br/>[models.py]"))
  node_attention_head(("Attention Head<br/>soft attention<br/>[models.py]"))
end

subgraph group_evaluation["Evaluation"]
  node_eval["Confusion Matrix<br/>classification eval"]
end

subgraph group_support["Support"]
  node_inspect["Inspect Img<br/>visual check<br/>[inspect_img.py]"]
end

node_videos -->|"video input"| node_preprocess
node_cascade -->|"face detect"| node_preprocess
node_preprocess -->|"extract"| node_face_crop
node_preprocess -->|"extract"| node_eye_crop
node_preprocess -->|"extract"| node_mouth_crop
node_face_crop -->|"write .npy"| node_full_ds
node_eye_crop -->|"write .npy"| node_eyes_ds
node_mouth_crop -->|"write .npy"| node_mouth_ds
node_full_ds -->|"load"| node_generator
node_eyes_ds -->|"load"| node_generator
node_mouth_ds -->|"load"| node_generator
node_run -->|"orchestrate"| node_generator
node_generator -->|"frame tensors"| node_effnet
node_effnet -->|"features"| node_bilstm
node_bilstm -->|"control path"| node_control_head
node_bilstm -->|"attention path"| node_attention_head
node_control_head -->|"predict"| node_eval
node_attention_head -->|"predict"| node_eval
node_inspect -.->|"check output"| node_full_ds
node_run -->|"report"| node_eval

click node_cascade "https://github.com/snarayan43/mls_final_project2025/blob/main/haarcascade_frontalface_default.xml"
click node_preprocess "https://github.com/snarayan43/mls_final_project2025/blob/main/preprocess.py"
click node_full_ds "https://github.com/snarayan43/mls_final_project2025/tree/main/post_cropping_images"
click node_eyes_ds "https://github.com/snarayan43/mls_final_project2025/tree/main/post_cropping_images"
click node_mouth_ds "https://github.com/snarayan43/mls_final_project2025/tree/main/post_cropping_images"
click node_run "https://github.com/snarayan43/mls_final_project2025/blob/main/run.py"
click node_effnet "https://github.com/snarayan43/mls_final_project2025/blob/main/models.py"
click node_bilstm "https://github.com/snarayan43/mls_final_project2025/blob/main/models.py"
click node_control_head "https://github.com/snarayan43/mls_final_project2025/blob/main/models.py"
click node_attention_head "https://github.com/snarayan43/mls_final_project2025/blob/main/models.py"
click node_inspect "https://github.com/snarayan43/mls_final_project2025/blob/main/inspect_img.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_videos,node_cascade,node_inspect toneBlue
class node_preprocess,node_face_crop,node_eye_crop,node_mouth_crop toneAmber
class node_full_ds,node_eyes_ds,node_mouth_ds toneMint
class node_run,node_generator toneRose
class node_effnet,node_bilstm,node_control_head,node_attention_head toneIndigo
class node_eval toneTeal
```

## Components

### External Data

Raw FaceForensics++ video inputs and face detector asset boundary.

- **FaceForensics++**: raw videos · Source video data for original vs Face2Face C23 experiments.
- [Face Cascade](https://github.com/snarayan43/mls_final_project2025/blob/main/haarcascade_frontalface_default.xml): cv detector · OpenCV Haar cascade used to locate faces during preprocessing. · `haarcascade_frontalface_default.xml`

### Preprocessing

Offline video decoding, face localization, region cropping, and .npy export.

- [Preprocess](https://github.com/snarayan43/mls_final_project2025/blob/main/preprocess.py): offline pipeline · Samples frames, crops regions, resizes to 224x224, and writes .npy arrays. · `preprocess.py`
- **Full Face**: region crop · Full-face frame sequence extracted during preprocessing.
- **Eyes**: region crop · Eye-region frame sequence extracted during preprocessing.
- **Mouth**: region crop · Mouth-region frame sequence extracted during preprocessing.

### Stored Datasets

Region-specific preprocessed sequence datasets used for training.

- [Full Dataset](https://github.com/snarayan43/mls_final_project2025/tree/main/post_cropping_images): npy sequences · Processed_Data_Full_HighRes sequence store. · `post_cropping_images`
- [Eyes Dataset](https://github.com/snarayan43/mls_final_project2025/tree/main/post_cropping_images): npy sequences · Processed_Data_Eyes_HighRes sequence store. · `post_cropping_images`
- [Mouth Dataset](https://github.com/snarayan43/mls_final_project2025/tree/main/post_cropping_images): npy sequences · Processed_Data_Mouth_HighRes sequence store. · `post_cropping_images`

### Training

Generator-based TensorFlow training orchestration and runtime safeguards.

- [Run](https://github.com/snarayan43/mls_final_project2025/blob/main/run.py): trainer orchestrator · Selects region and model variant, configures GPU safety, and launches training. · `run.py`
- **VideoDataGenerator**: data loader · Loads preprocessed frame sequences into the TensorFlow training loop.

### Modeling

Shared temporal backbone plus alternative aggregation heads.

- [EfficientNetB1](https://github.com/snarayan43/mls_final_project2025/blob/main/models.py): frame encoder · TimeDistributed per-frame CNN backbone. · `models.py`
- [BiLSTM](https://github.com/snarayan43/mls_final_project2025/blob/main/models.py): temporal encoder · Models temporal dynamics across the 32-frame sequence. · `models.py`
- [Average Head](https://github.com/snarayan43/mls_final_project2025/blob/main/models.py): baseline head · GlobalAveragePooling1D temporal aggregation for the control model. · `models.py`
- [Attention Head](https://github.com/snarayan43/mls_final_project2025/blob/main/models.py): soft attention · Dense→Softmax→Multiply temporal weighting for the attention model. · `models.py`

### Evaluation

Final classification assessment and confusion-matrix reporting.

- **Confusion Matrix**: classification eval · Final prediction-vs-label evaluation artifact.

### Support

Auxiliary inspection and experiment-output files.

- [Inspect Img](https://github.com/snarayan43/mls_final_project2025/blob/main/inspect_img.py): visual check · Helper for visually validating preprocessed outputs. · `inspect_img.py`

## Connections

- FaceForensics++ → Preprocess: video input · Raw videos are decoded and sampled offline.
- Face Cascade → Preprocess: face detect · Haar cascade provides face localization for cropping.
- Preprocess → Full Face: extract · Produces full-face frame sequences.
- Preprocess → Eyes: extract · Produces eye-region frame sequences.
- Preprocess → Mouth: extract · Produces mouth-region frame sequences.
- Full Face → Full Dataset: write .npy · Stores processed full-face sequences.
- Eyes → Eyes Dataset: write .npy · Stores processed eye-region sequences.
- Mouth → Mouth Dataset: write .npy · Stores processed mouth-region sequences.
- Full Dataset → VideoDataGenerator: load · Training can consume the full-face dataset.
- Eyes Dataset → VideoDataGenerator: load · Training can consume the eye-region dataset.
- Mouth Dataset → VideoDataGenerator: load · Training can consume the mouth-region dataset.
- Run → VideoDataGenerator: orchestrate · Run.py configures data loading and experiment setup.
- VideoDataGenerator → EfficientNetB1: frame tensors · Sequence batches enter the TimeDistributed CNN backbone.
- EfficientNetB1 → BiLSTM: features · Per-frame embeddings are passed to temporal modeling.
- BiLSTM → Average Head: control path · Baseline model aggregates temporal features with averaging.
- BiLSTM → Attention Head: attention path · Attention model reweights temporal features before classification.
- Average Head → Confusion Matrix: predict · Control model predictions are evaluated.
- Attention Head → Confusion Matrix: predict · Attention model predictions are evaluated.
- Inspect Img → Full Dataset: check output · Visual inspection supports preprocessing validation.
- Run → Confusion Matrix: report · Training run produces the final confusion matrix.
