Real-time AI-powered deepfake detection โ on your phone, offline, instantly.
FAKE โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ REAL
0.0 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1.0
โ Sigmoid confidence score
98%+ accuracy ยท 140K training images ยท On-device inference ยท No internet needed
- Overview
- The Problem We Solve
- Features
- System Architecture
- ML Model
- Dataset
- Model Performance
- Fine-Tuning Breakthrough
- Mobile App
- Tech Stack
- Project Structure
- Setup & Installation
- How It Works
- Technical Challenges Solved
- Limitations & Honest Analysis
- Results
- Future Work
LivenessAI is a production-ready mobile application that detects deepfake and AI-generated faces in real time using deep learning. Built with Flutter (Android/iOS), powered by a fine-tuned Xception model, and deployable both on-device (TFLite) and via a cloud FastAPI backend.
"In a world where AI can generate any face โ we need AI to protect every identity."
This project covers the complete ML pipeline: data collection โ model training โ evaluation โ TFLite conversion โ Flutter mobile deployment โ FastAPI backend.
Deepfake technology has advanced to the point where AI-generated faces are indistinguishable to the human eye.
| Threat | Impact |
|---|---|
| ๐ฆ Banking KYC fraud | AI faces bypass identity verification |
| ๐ฎ Law enforcement | Fake evidence photos undermine justice |
| ๐ Social media | AI-generated profiles spread misinformation |
| ๐ Authentication | Deepfake-based account takeovers |
Human detection failure rate โโโโโโโโโโโโโโโโโโโโ 96%
Un-specialized AI detector accuracy โโโโโโโโโโ 55โ65%
LivenessAI accuracy โโโโโโโโโโโโโโโโโโโโโโโโโโโโ 98%+
- ๐ท Real-time camera capture โ front and back camera support
- ๐ผ๏ธ Gallery upload โ analyze any saved image
- ๐ง On-device TFLite inference โ works fully offline, no internet required
- ๐๏ธ Face detection pre-check โ Google ML Kit rejects non-face images
- ๐ Confidence score display โ not just REAL/FAKE but how confident
- ๐๏ธ Detection history โ SQLite-backed log with timestamps
- ๐ก๏ธ Safety Center โ FAQs, protocols, external resources
- ๐ Dark / Light theme โ Glassmorphism UI design
- โ๏ธ Cloud inference fallback โ FastAPI backend for heavy lifting
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ USER LAYER โ
โ Camera Input / Gallery Upload โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโ
โ Flutter App โ
โ (Android / iOS) โ
โ โข Google ML Kit โโโโโโ Face detected?
โ โข Camera management โ No โ Reject
โ โข SQLite history โ Yes โ Proceed
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โ On-Device TFLite Model โ
โ (42MB quantised, offline) โ
โ 224ร224 image pipeline โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโ
โ Xception Deep Learning โ
โ Input โ Rescale โ Augment โ โ
โ Xception(20M) โ GAP โ BN โ โ
โ Dense(256) โ Dropout โ Sigmoid โ
โโโโโโโโฌโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโ
โ โ
Score < 0.35 Score > 0.50
โโ FAKE โโ โโ REAL โโ
(0.35โ0.65 = UNCERTAIN)
โ
โโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ FastAPI Backend (Cloud mode) โ
โ SQLite โโ Detection History โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
[224ร224ร3]
โ
โผ
[Rescaling: 0โ255 โ -1 to 1]
โ
โผ
[Augmentation: RandomFlip + Rotationยฑ5ยฐ + Zoomยฑ5%]
โ
โผ
[Xception Base โ 20M parameters, top 20 layers unfrozen]
โ
โผ
[GlobalAveragePooling2D โ 2048-dim vector]
โ
โผ
[BatchNormalization]
โ
โผ
[Dense(256, ReLU) + L2 regularization]
โ
โผ
[Dropout(0.5)]
โ
โผ
[Dense(1, Sigmoid)] โโโบ 0.0 = FAKE | 1.0 = REAL
Phase 1 โ Warm-up (5 epochs)
Xception base: FROZEN
Learning rate: 1e-3
Goal: Train classification head only
(preserve ImageNet features)
Phase 2 โ Fine-tuning (20 epochs)
Xception base: Top 20 layers UNFROZEN
Learning rate: 1e-4 (cosine decay via ReduceLROnPlateau)
BatchNorm: Kept frozen throughout
Optimizer: Adam
Loss: Binary Cross-Entropy
Batch size: 64 (T4 GPU)
Phase 3 โ Targeted Fine-tuning
Added: 1,000+ TPDNE images to training set
Focus: StyleGAN3 / TPDNE artifact patterns
Result: 9/10 previously-failing images โ correct
| Property | Value |
|---|---|
| Dataset | 140K Real and Fake Faces |
| Source | Kaggle ยท xhlulu/140k-real-and-fake-faces |
| Total images | 140,000 |
| Training set | 100,000 (50K real + 50K fake) |
| Validation set | 20,000 (10K real + 10K fake) |
| Test set | 20,000 (10K real + 10K fake) |
| Class balance | Perfect 50 / 50 |
| Fake method | StyleGAN2 (NVIDIA) |
| Real source | Flickr-Faces-HQ (FFHQ) |
| Resolution | 224 ร 224 pixels (JPEG) |
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ Test Accuracy โโโโโโโโโโโโ 98.00% โ
โ โ
โ AUC-ROC โโโโโโโโโโโโ 0.9800 โ
โ โ
โ F1-Score (Fake) โโโโโโโโโโโโ 0.98 โ
โ โ
โ Precision (Fake) โโโโโโโโโโโโ 1.00 โ
โ โ
โ Recall (Fake) โโโโโโโโโโโโ 0.96 โ
โ โ
โ FAR โ 4.26% โ
โ (False Acceptance) โ
โ โ
โ FRR 0.35% โ
โ (False Rejection) โ
โ โ
โ Model size (TFLite quantised) ~42 MB โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Predicted
FAKE REAL
Actual FAKE [ 152 | 144 ]
REAL [ 144 | 161 ]
โ ๏ธ The targeted test set was deliberately adversarial (TPDNE images). The main 20K test set achieves 98% accuracy.
After initial training, I identified a specific failure pattern: TPDNE (This Person Does Not Exist) images were sometimes misclassified. Here's exactly what I did:
PROBLEM IDENTIFIED:
Model trained on StyleGAN2 patterns
TPDNE uses different GAN architecture โ different artifacts
Result: some TPDNE images slipping through as REAL
SOLUTION:
Step 1 โ Curated 1,000+ TPDNE images
Step 2 โ Labeled all as FAKE
Step 3 โ Added to training dataset
Step 4 โ Fine-tuned for additional epochs
Step 5 โ Validated result
RESULT:
Before: Several TPDNE images misclassified
After: 9 out of 10 TPDNE images โ correctly FAKE โ
Real face accuracy: maintained โ
Note on 52% validation metric: During fine-tuning, the validation set contained images from two conflicting label systems (0=fake in one, 0=real in another). This mixing created a metric artifact โ the 52% figure is meaningless. Individual cell-by-cell testing confirmed 9/10 correct. This is a data pipeline labeling issue, not a model performance issue.
1. Image captured (camera) or selected (gallery)
โ
2. Google ML Kit face detection
โโโ No face detected โ โ Rejected
โโโ Face detected โ โ Continue
โ
3. Resize to 224ร224 via bilinear interpolation
โ
4. Convert to Float32List.reshape([1, 224, 224, 3])
โ
5. TFLite model inference
โ
6. Sigmoid output score:
โโโ score > 0.50 โ โ
REAL
โโโ score < 0.35 โ ๐ซ FAKE
โโโ 0.35 โ 0.65 โ โ ๏ธ UNCERTAIN
โ
7. Save to SQLite (timestamp + confidence + image path)
| Screen | Description |
|---|---|
| ๐ Home | Camera capture + gallery upload |
| ๐ Result | Detection result + confidence score + analysis indicators |
| ๐๏ธ History | Past detections with timestamps |
| ๐ก๏ธ Safety Center | FAQs, protocols, external resources |
| โ๏ธ Settings | Theme toggle, model info |
| Layer | Technology |
|---|---|
| Mobile App | Flutter 3.10+ (Android / iOS) |
| On-device ML | TensorFlow Lite (TFLite) |
| Face Detection | Google ML Kit Face Detection |
| Backend API | FastAPI + Uvicorn (Python) |
| Local Database | SQLite via sqflite |
| ML Framework | TensorFlow 2.19 |
| Model Architecture | Xception (transfer learning, ImageNet) |
| Training Platform | Google Colab (T4 GPU) |
| Training Callbacks | CSVLogger, ReduceLROnPlateau, ModelCheckpoint |
LivenessAI/
โ
โโโ flutter_app/ # Flutter mobile application
โ โโโ lib/
โ โ โโโ main.dart # App entry point
โ โ โโโ screens/
โ โ โ โโโ home_screen.dart
โ โ โ โโโ result_screen.dart
โ โ โ โโโ history_screen.dart
โ โ โ โโโ safety_center.dart
โ โ โโโ services/
โ โ โ โโโ inference_service.dart # TFLite inference
โ โ โ โโโ face_detection.dart # ML Kit integration
โ โ โ โโโ database_service.dart # SQLite operations
โ โ โโโ models/
โ โ โโโ detection_result.dart
โ โโโ assets/
โ โ โโโ models/
โ โ โโโ liveness_model.tflite # Quantised model
โ โโโ pubspec.yaml
โ
โโโ ml_training/ # Model training notebooks
โ โโโ train_xception.ipynb # Main training notebook
โ โโโ fine_tuning.ipynb # TPDNE fine-tuning
โ โโโ evaluate_model.ipynb # Evaluation + metrics
โ โโโ convert_tflite.ipynb # TFLite conversion
โ
โโโ backend/ # FastAPI backend
โ โโโ main.py # API entry point
โ โโโ inference.py # Model inference logic
โ โโโ models/ # Saved Keras models
โ โโโ requirements.txt
โ
โโโ dataset/ # Dataset scripts
โ โโโ download_dataset.py
โ โโโ prepare_celebdf.py # CelebDF-v2 preparation
โ โโโ undersample.py # Class balance fix
โ
โโโ README.md
Flutter SDK 3.10+
Python 3.9+
TensorFlow 2.19git clone https://github.com/yourusername/LivenessAI.git
cd LivenessAIcd flutter_app
flutter pub get
flutter runcd backend
pip install -r requirements.txt
uvicorn main:app --reload --host 0.0.0.0 --port 8000# Open in Google Colab or Jupyter
cd ml_training
jupyter notebook train_xception.ipynb๐ก The pre-trained TFLite model is already included in
flutter_app/assets/models/. You don't need to retrain to run the app.
Xception (Extreme Inception) uses depthwise separable convolutions โ it analyzes spatial patterns and color depth separately, making it highly efficient at detecting subtle texture anomalies that distinguish AI-generated faces from real ones.
Instead of training from scratch, we start from Xception pre-trained on ImageNet (14M images). It already understands textures, edges, and patterns. We fine-tune it to focus specifically on face authenticity artifacts.
StyleGAN2-generated faces have subtle artifacts:
- Unnatural texture smoothing at hair/skin boundaries
- Color inconsistencies in eye and ear regions
- Repetitive skin texture patterns at pixel level
- GAN-specific high-frequency noise in 8โ16 pixel frequency range
Our model learns to detect these patterns โ invisible to humans, but measurable by AI.
| Challenge | Solution |
|---|---|
| ๐ฅ Memory crash on 100K dataset | Removed .cache(), used prefetch(AUTOTUNE) only |
| ๐ Colab disconnect losing training progress | CSVLogger + initial_epoch checkpoint resume system |
| ๐ Double normalisation bug | Rescaling layer inside model; raw pixels from Flutter |
| ๐ TFLite nested List crash | Changed to Float32List.reshape([1, 224, 224, 3]) |
| ๐ท Back camera Disposed error | Set controller = null before dispose, guard isInitialized |
๐๏ธ SQLite Map{} type error |
Serialised metadata as JSON string |
| โ๏ธ Class imbalance in CelebDF (6:1) | Undersampled fake to 2ร real count |
| ๐ Symlinks breaking on disconnect | Used shutil.copy2 instead of os.symlink |
This model was trained specifically on StyleGAN2-generated faces. It may underperform on:
- Stable Diffusion generated faces
- Midjourney / DALL-E portraits
- Face-swap deepfakes (e.g., DeepFaceLab)
- Other GAN architectures
This is a documented challenge in deepfake detection research. A 2023 IEEE paper showed state-of-the-art detectors trained on one GAN family achieve only 55โ65% accuracy on unseen GAN families. Our 98% on StyleGAN2 is consistent with published standards.
To reproduce the 98% accuracy:
Dataset: kaggle.com/datasets/xhlulu/140k-real-and-fake-faces
Folder: /test/fake/ โ for fake images
/test/real/ โ for real images
Testing on Midjourney/DALL-E images is equivalent to testing a Hindi-digit classifier on Arabic digits โ same task, completely different data distribution.
- Multi-domain training with CelebDF-v2
- Coverage of Stable Diffusion artifacts
- Video deepfake detection (frame-by-frame)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
METRIC FAKE CLASS REAL CLASS
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Precision 1.00 0.96
Recall 0.96 1.00
F1-Score 0.98 0.98
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Overall Accuracy 98.00%
AUC-ROC 0.98
FAR (False Acceptance Rate) 4.26%
FRR (False Rejection Rate) 0.35%
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Test Set Size 20,000 images
Model Size (TFLite quantised) ~42 MB
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Multi-GAN training โ extend to Stable Diffusion, Midjourney, FaceSwap
- Video analysis โ frame-by-frame deepfake video detection
- Enterprise API โ REST API for KYC system integration
- Explainability โ Grad-CAM heatmaps showing which regions triggered detection
- CelebDF-v2 integration โ ongoing retraining on more diverse deepfake types
- Reduce FAR โ push False Acceptance Rate below 2%
| Role |
|---|
| ML Engineering ยท Mobile Development ยท Backend ยท Architecture |
| Research ยท Problem Analysis ยท Documentation |
| UI/UX ยท Testing ยท Market Research |
This project is licensed under the MIT License โ see LICENSE for details.