Notebooks gerador com bom nome + README

This commit is contained in:
DiogoCosta18
2026-05-14 20:20:46 +01:00
parent f46320f81e
commit 3bff7eefb0
9 changed files with 1127 additions and 504 deletions
+148 -107
View File
@@ -1,125 +1,166 @@
# DRL_PROJ — DeepFake Detection
# Deep Learning Face Project
Deep learning project for binary deepfake detection on the DeepFakeFace dataset.
This repository contains a two-part deep learning project on the
DeepFakeFace (DFF) dataset:
## Project structure
1. **Classifier:** detect whether a face image is real or fake.
2. **Generator:** train generative models that produce new fake face images.
```
The project is written as an experimental report. The notebooks are the main
deliverable: they show the pipeline, the intermediate failures, the ablations,
the decisions, and the final models. Read them in order.
## Project Story
The work follows the same principle in both parts: start with a simple
baseline, inspect what fails, change one important factor at a time, and keep
the evidence tied to saved logs and saved artifacts.
For the **classifier**, the story moves from dataset understanding to
preprocessing, baseline models, controlled ablations, Grad-CAM inspection,
stronger model families, and data scaling. The final practical classifier is a
ResNet50-style pipeline using face crops, 224x224 inputs, ImageNet/default
normalization, and no stochastic augmentation at validation/test time.
For the **generator**, the story starts with raw baseline failures, then locks
the data pipeline before comparing three parallel model-family branches:
GAN, VAE, and DDPM. The final comparison keeps quality versus speed central:
DDPM gives the best saved FID and visual quality, GAN is the best
quality-speed compromise, and VAE is the fastest but smoothest option.
## How To Read The Project
Start with the classifier notebooks, then read the generator notebooks. The
generator has one linear setup stage followed by three parallel branches:
GAN, VAE, and DDPM. Those branches are numbered in reading order, but they are
conceptually parallel experiments after the pipeline is selected.
### Classifier Notebooks
Read these first:
1. `classifier/notebooks/01_eda.ipynb`
Dataset composition, real/fake source mapping, image statistics, and
shortcut risks.
2. `classifier/notebooks/02_preprocessing.ipynb`
Deterministic preprocessing, train-only augmentation, face crops, and
normalization.
3. `classifier/notebooks/03_phase1_analysis.ipynb`
SimpleCNN and ResNet18 controlled baselines.
4. `classifier/notebooks/04_phase2_analysis.ipynb`
Resolution, normalization, source holdouts, facecrop, and augmentation
ablations.
5. `classifier/notebooks/05_gradcam_analysis.ipynb`
Qualitative localization analysis across the classifier pipeline.
6. `classifier/notebooks/06_phase3_model_family_analysis.ipynb`
Stronger pretrained model families and the ResNet50 practical choice.
7. `classifier/notebooks/07_phase4_data_scaling_analysis.ipynb`
Data scaling for strong backbones and the final classifier decision.
### Generator Notebooks
Read these after the classifier:
1. `generator/notebooks/01_baseline_sanity_check.ipynb`
Raw baseline failures and why the data pipeline must be fixed first.
2. `generator/notebooks/02_pipeline_selection.ipynb`
Controlled pipeline ablations: resolution, alignment, augmentation, and
raw/aligned mixing.
3. `generator/notebooks/03_gan_stability_progression.ipynb`
GAN branch: DCGAN -> WGAN-GP -> spectral normalization + GroupNorm +
self-attention -> 128x128 check.
4. `generator/notebooks/04_vae_loss_progression.ipynb`
VAE branch: MSE + KL -> perceptual loss -> PatchGAN adversarial loss.
5. `generator/notebooks/05_ddpm_recipe_progression.ipynb`
DDPM branch: linear schedule -> cosine schedule -> v-prediction -> wider
backbone.
6. `generator/notebooks/06_final_family_comparison.ipynb`
Final comparison of the selected GAN, VAE, and DDPM recipes under saved
Phase 5 conditions.
7. `generator/notebooks/07_final_sample_showcase.ipynb`
Curated final sample examples from saved outputs. This is qualitative
showcase material, not a replacement for FID.
## What The Notebooks Do
The notebooks are analysis/report chapters. They load existing configs, logs,
figures, saved sample grids, checkpoints, and prediction summaries. They are
not intended to launch new training runs.
When a notebook shows a plot or image grid, the surrounding markdown explains:
- what the artifact shows;
- why it is needed;
- how it supports the phase decision;
- what limitation remains.
This is important because the project is evaluated not only by final
performance, but by the documented evolution of the solution.
## Repository Layout
```text
DRL_PROJ/
classifier/ ← discriminative model (real vs. fake classifier)
src/ ← model definitions, training, evaluation, preprocessing
configs/ ← experiment configs organised by phase
phase1/ ← baseline models (SimpleCNN, ResNet18)
phase2/ ← architecture sweep (ResNet variants, face-crop)
phase3/ ← EfficientNet, ViT, frequency-aware training
phase4/ ← ensemble strategies
tools/ ← analyse.py, ensemble.py, inference.py, facecrop.py
notebooks/ ← EDA, preprocessing, evaluation, GradCAM
outputs/ ← models, logs, figures (gitignored except .pt/.json)
run.py ← main training entry point
generator/ generative model (GAN / VAE / diffusion) — in progress
pipeline/ ← Vast.ai ephemeral GPU orchestration
data/ ← dataset root (gitignored)
cropped/ ← MTCNN pre-cropped faces (gitignored)
classifier/ ← bbox crops for the classifier
generator/ ← landmark-aligned crops for the generator
classifier/
configs/ experiment configs by phase
notebooks/ classifier report notebooks
outputs/ saved logs, figures, Grad-CAM panels, checkpoints
src/ classifier data, models, training, evaluation
tools/ facecrop, Grad-CAM, inference, reevaluation helpers
generator/
configs/ generator configs by phase/family
notebooks/ generator report notebooks and notebook builder
outputs/ saved logs, sample grids, final showcase artifacts
src/ generator data, models, training, metrics
tools/ sampling and utility scripts
data/ original DFF dataset root, not committed
cropped/ preprocessed face crops, not committed
docs/ project statement and supporting documents
pipeline/ optional remote/GPU orchestration helpers
```
## Setup
## Rebuilding The Generator Notebooks
Create a local environment when you want to run the code directly on a machine you control:
The generator notebooks are generated from a single source file:
```bash
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip setuptools wheel
python -m pip install -r requirements.txt
```powershell
cd generator/notebooks
python _build.py
```
## Local Training
That builder writes the numbered generator notebooks listed above. It uses
existing saved logs and artifacts; it does not train models.
```bash
python3 classifier/run.py classifier/configs/phase2/p2_resnet18_facecrop.json
python3 classifier/run.py classifier/configs/phase3/p3_efficientnet_b0.json
## Running The Code
Create an environment and install the project requirements:
```powershell
python -m venv .venv
.\.venv\Scripts\python.exe -m pip install --upgrade pip setuptools wheel
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
```
## Ephemeral Vast.ai Pipeline
The raw dataset should be placed under `data/`. Preprocessed crops are stored
under `cropped/`. These folders are intentionally not committed.
The deployment/orchestration path now lives under [`pipeline/`](/run/host/mnt/shared/UP/DRL/DRL_PROJ/pipeline/README.md).
Execution entry points exist in `classifier/run.py` and `generator/run.py` for
reproducibility, but the report notebooks should be read as analysis over
already saved results.
One-time setup:
## Final Takeaway
```bash
cat > pipeline/.env <<'EOF'
VAST_API_KEY=<your-api-key>
VAST_SSH_PRIVATE_KEY=/home/your-user/.ssh/id_ed25519
EOF
```
The project is best understood as a sequence of controlled decisions:
End-to-end ephemeral run:
1. cleanly define the data and preprocessing;
2. establish simple baselines;
3. improve one factor at a time;
4. compare model families using saved evidence;
5. report both performance and limitations.
```bash
python3 -m pipeline run classifier/configs/phase2/p2_resnet18_facecrop.json --upload-data
```
Interactive offer selection:
```bash
python3 -m pipeline offers --select-offer
```
You can override the ranking mode per run:
```bash
python3 -m pipeline offers --sort price
python3 -m pipeline offers --sort performance
python3 -m pipeline offers --sort performance --price 0.14
```
You can also filter by region:
```bash
python3 -m pipeline offers --select-offer --region europe
python3 -m pipeline offers --select-offer --region Portugal
python3 -m pipeline offers --select-offer --region US
python3 -m pipeline offers --select-offer --region europe --price 0.14
```
To inspect which region strings are currently available from the search results:
```bash
python3 -m pipeline offers --list-regions
```
That command:
- ensures your SSH public key is registered with Vast.ai
- searches offers using the filters in `pipeline/defaults/vast.json`
- creates an instance
- waits for SSH readiness
- syncs the repo
- uploads `data/` when `--upload-data` is set
- runs `python3 classifier/run.py ...`
- downloads `classifier/outputs/`
- for generator runs, rsyncs `generator/outputs/` back every 25 epochs and again at completion
- destroys the instance automatically unless `--keep-on-failure` is set
Useful commands:
```bash
python3 -m pipeline up
python3 -m pipeline status <instance_id>
python3 -m pipeline down <instance_id>
```
To override the default Vast search/runtime settings, copy `pipeline/defaults/vast.json`, edit it, and pass:
```bash
python3 -m pipeline run classifier/configs/phase3/p3_efficientnet_b0.json --pipeline-config /path/to/vast.override.json
```
The default policy in `pipeline/defaults/vast.json` now targets:
- `1x` GPU
- `RTX 3090` or `RTX 3090 Ti`
- `<= $0.20/hour`
- sorted by `dlperf` descending
- uses `vastai/pytorch:latest` as the default image
The classifier becomes reliable through source-aware preprocessing, stronger
pretrained backbones, and scaling. The generator improves by first locking the
face-aligned pipeline and then selecting the best recipe inside each model
family before the final GAN/VAE/DDPM comparison.