├── SS ├── Example (1).png ├── Example (2).png ├── Screenshot 2025-05-14 194643.png ├── Screenshot 2025-05-14 194747.png ├── Screenshot 2025-05-14 194855.png └── Screenshot_example_result (1).jpeg ├── mymodels ├── AI_or_Realface_imageclassifier.h5 ├── fold_1_phase1_best_model.keras ├── fold_1_phase2_best_model.keras ├── fold_2_phase1_best_model.keras ├── fold_2_phase2_best_model.keras ├── fold_3_phase1_best_model.keras ├── fold_3_phase2_best_model.keras ├── fold_4_phase1_best_model.keras ├── fold_4_phase2_best_model.keras ├── fold_5_phase1_best_model.keras ├── fold_5_phase2_best_model.keras └── AI_or_Realface_imageclassifier.keras ├── .gitattributes ├── LICENSE └── README.md /SS/Example (1).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/indranil143/Real-or-AI-Generated-Face-Detection/HEAD/SS/Example (1).png -------------------------------------------------------------------------------- /SS/Example (2).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/indranil143/Real-or-AI-Generated-Face-Detection/HEAD/SS/Example (2).png -------------------------------------------------------------------------------- /SS/Screenshot 2025-05-14 194643.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/indranil143/Real-or-AI-Generated-Face-Detection/HEAD/SS/Screenshot 2025-05-14 194643.png -------------------------------------------------------------------------------- /SS/Screenshot 2025-05-14 194747.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/indranil143/Real-or-AI-Generated-Face-Detection/HEAD/SS/Screenshot 2025-05-14 194747.png -------------------------------------------------------------------------------- /SS/Screenshot 2025-05-14 194855.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/indranil143/Real-or-AI-Generated-Face-Detection/HEAD/SS/Screenshot 2025-05-14 194855.png -------------------------------------------------------------------------------- /SS/Screenshot_example_result (1).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/indranil143/Real-or-AI-Generated-Face-Detection/HEAD/SS/Screenshot_example_result (1).jpeg -------------------------------------------------------------------------------- /mymodels/AI_or_Realface_imageclassifier.h5: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:b5a02f47b0703ac66734c3f3fc854666eaaffc86846b03c8e1c69afedd550258 3 | size 44394376 4 | -------------------------------------------------------------------------------- /mymodels/fold_1_phase1_best_model.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:a90019f1659ac57165f22242221628413b363bc27d2dfaaa47910f3501343183 3 | size 90682405 4 | -------------------------------------------------------------------------------- /mymodels/fold_1_phase2_best_model.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:c3a3f42a99ae43e2a15d9f2a151a034b6f9d62e48c6147a66a7a4198ad02f7b1 3 | size 90682405 4 | -------------------------------------------------------------------------------- /mymodels/fold_2_phase1_best_model.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:7790f13b4e24f61b222bb457a2fb000ca86391a7b4f5f2053e52219c3573dafc 3 | size 90682325 4 | -------------------------------------------------------------------------------- /mymodels/fold_2_phase2_best_model.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:64fac6ecd0dde2e167f068699ede0a7adb8466145d68963673782f8d7402efb8 3 | size 90682325 4 | -------------------------------------------------------------------------------- /mymodels/fold_3_phase1_best_model.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:3432392921ed69752dc577141622164e0b36fcde99f196f5408c87f265072125 3 | size 90682325 4 | -------------------------------------------------------------------------------- /mymodels/fold_3_phase2_best_model.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:136a5fc964bf4b3271f7fa648a159fa25c60fa1ba7eec239f0295b1968433b81 3 | size 90682325 4 | -------------------------------------------------------------------------------- /mymodels/fold_4_phase1_best_model.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:8fd82ca3393319a0725f8d6bcd84ad07d1718a4a101c5db138ddd8e38e1074c9 3 | size 90682325 4 | -------------------------------------------------------------------------------- /mymodels/fold_4_phase2_best_model.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:e31cdf95af7addf8258bdd4a01e401d1a414677d7bc2f3a1daa940b014c0a739 3 | size 90682325 4 | -------------------------------------------------------------------------------- /mymodels/fold_5_phase1_best_model.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:d6f654809d857326b948e61e6f49374bd32de44c6a837bea713c5db913c4ddaa 3 | size 90682325 4 | -------------------------------------------------------------------------------- /mymodels/fold_5_phase2_best_model.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:3361119e4abd61db7c0881c35f4d8704cffeeec4fafbde6d1dfa27b7a19c55d5 3 | size 90682325 4 | -------------------------------------------------------------------------------- /mymodels/AI_or_Realface_imageclassifier.keras: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:7d4c32c4fdb2b5fd9659c17c4976771819986f0e147823df641dcd678de412a6 3 | size 44398298 4 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.pt filter=lfs diff=lfs merge=lfs -text 2 | *.pkl filter=lfs diff=lfs merge=lfs -text 3 | *.h5 filter=lfs diff=lfs merge=lfs -text 4 | *.keras filter=lfs diff=lfs merge=lfs -text 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Indranil Bandyopadhyay 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deepfake Face Detection using Deep Learning 🤖👤 2 | **(Real-or-AI-Generated-Face-Detection)** 3 | 4 | [![Python](https://img.shields.io/badge/Language-Python-blue?style=flat-square&logo=python&logoColor=white)](https://www.python.org/) 5 | [![Deep Learning](https://img.shields.io/badge/Concept-Deep%20Learning-purple?style=flat-square)](https://en.wikipedia.org/wiki/Deep_learning) 6 | [![TensorFlow](https://img.shields.io/badge/Framework-TensorFlow%20%7C%20Keras-red?style=flat-square&logo=tensorflow)](https://www.tensorflow.org/) 7 | [![NumPy](https://img.shields.io/badge/Data%20Handling-NumPy-informational?style=flat-square&logo=numpy)](https://numpy.org/) 8 | [![Matplotlib](https://img.shields.io/badge/Plotting-Matplotlib-lightgrey?style=flat-square&logo=matplotlib)](https://matplotlib.org/) 9 | [![Seaborn](https://img.shields.io/badge/Visualization-Seaborn-blueviolet?style=flat-square&logo=seaborn)](https://seaborn.pydata.org/) 10 | [![Scikit-learn](https://img.shields.io/badge/ML%20Tools-Scikit--learn-orange?style=flat-square&logo=scikit-learn)](https://scikit-learn.org/stable/) 11 | [![OpenCV](https://img.shields.io/badge/Image%20Processing-OpenCV-darkgreen?style=flat-square&logo=opencv)](https://opencv.org/) 12 | [![Model: Xception](https://img.shields.io/badge/Model-Xception-orange?style=flat-square)](https://keras.io/api/applications/xception/) 13 | [![Evaluation: K-Fold CV](https://img.shields.io/badge/Evaluation-K--Fold%20CV-yellowgreen?style=flat-square)](https://scikit-learn.org/stable/modules/cross_validation.html) 14 | [![Jupyter](https://img.shields.io/badge/Notebook-Jupyter-orange?style=flat-square&logo=jupyter)](https://jupyter.org/) 15 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT) 16 | 17 | ## 📌 Project Overview 18 | AI-generated images are now almost everywhere, making it crucial to detect them before misinformation spreads or you get catfished!! 🎭 19 | There is a critical need to reliably detect AI-generated facial images (deepfakes) to mitigate these risks. This repository showcases a robust primary approach utilizing the **Xception network with transfer learning and K-Fold cross-validation**, alongside an alternative approach using custom Convolutional Neural Network (CNN). 20 | 21 | ## 👤 Problem Statement 22 | The increasing realism of GAN-generated faces makes it hard to distinguish them from real ones, posing significant threats such as the proliferation of disinformation and potential reputational damage. 23 | Therefore, we have developed advanced deep learning models specifically designed to identify AI-generated faces! 24 | 25 | ## 🚀 Main Approach: Xception-based Transfer Learning with K-Fold CV 26 | The core solution, detailed in `Deepfake-Face-Detection-with-Xception.ipynb`, employs: 27 | 28 | * **Data Handling:** Images from 'real' and 'fake' directories are resized to **(224, 224)** pixels, normalized, and augmented (flips, brightness, contrast, etc.). 29 | * **Model Architecture:** **Xception** (ImageNet pre-trained) base with a custom head: Global Average Pooling, Dense layers (**L2 regularization: 0.0001, Dropout: 0.3**), Batch Normalization, and a `sigmoid` output. 30 | * **Two-Phase Training (Adam optimizer):** 31 | 1. Train custom head (Xception frozen): **20 epochs**, initial Learning Rate **0.001**. Callbacks: Early Stopping (patience 10), ReduceLROnPlateau (patience 4). 32 | 2. Fine-tune full model (Xception unfrozen): **30 epochs**, lower Learning Rate **1e-5**. Callbacks: Early Stopping (patience 15), ReduceLROnPlateau (patience 7). 33 | * **Cross-Validation:** **5-Fold Cross-Validation (SEED = 42)** for robust performance estimation and model checkpointing per fold. 34 | * **Evaluation:** Metrics include Accuracy, Precision, Recall, F1-Score, AUC. Generates Classification Reports, Confusion Matrices (from pooled predictions), Training History Plots, and ROC Curves. 35 | * **Prediction:** Loads saved `.keras` models for new image/directory predictions using a **0.5 probability threshold**. 36 | 37 | ## 🗂️ Dataset (Main Approach) 38 | * **Name:** RVF10K 39 | * **Source:** Kaggle - `sachchitkunichetty/rvf10k` 40 | * **Structure:** Expected at `/kaggle/input/rvf10k/rvf10k/train` with `real` and `fake` subdirectories. 41 | * **Size:** Approximately 7000 images. 42 | 43 | **Some example images -** ![some image examples](https://github.com/indranil143/Real-or-AI-Generated-Face-Detection/blob/main/SS/Screenshot%202025-05-14%20194643.png) 44 | 45 | ## ✅ Results (Main Approach) 46 | Based on 5-Fold Cross-Validation on the RVF10K dataset: 47 |
48 | 49 | **K-Fold Cross-Validation Summary:** 50 | 51 | | Metric | Value | 52 | |------------------|-----------------| 53 | | Average Accuracy | 0.7840 ± 0.0102 | 54 | | Average AUC | 0.8609 ± 0.0106 | 55 | 56 | **Overall Metrics (Pooled Predictions Across All Folds):** 57 | 58 | | Metric | Precision | Recall | F1-Score | Support | 59 | |--------------|-----------|--------|----------|---------| 60 | | 0.0 (Real) | 0.79 | 0.77 | 0.78 | 3500 | 61 | | 1.0 (Fake) | 0.77 | 0.80 | 0.79 | 3500 | 62 | | **Accuracy** | | | **0.78** | 7000 | 63 | | Macro Avg | 0.78 | 0.78 | 0.78 | 7000 | 64 | | Weighted Avg | 0.78 | 0.78 | 0.78 | 7000 | 65 |
66 | 67 | * **Overall ROC Curve:** 68 |

69 | Overall ROC Curve 70 |

71 | 72 | **Prediction Examples:** 73 | 74 | * **Example 1:**

Example 1 Prediction

75 | * **Filename:** `LJE2ZPJRWRX.jpg` 76 | * **Prediction:** Fake 77 | * **Probability (Fake):** 0.9294 78 | * *Comment:* A correct prediction with high confidence that the image is Fake. 79 | 80 | * **Example 2:**

Example 2 Prediction

81 | * **Filename:** `46915.jpg` 82 | * **Prediction:** Real 83 | * **Probability (Fake):** 0.0561 84 | * *Comment:* A correct prediction with high confidence that the image is Real. 85 | 86 | --- 87 | 88 | 89 | ## 🛠️ Setup and Installation 90 | 1. **Dependencies:** Install Python (3.10+), TensorFlow, NumPy, Matplotlib, Seaborn, Scikit-learn, OpenCV. 91 | 2. **Datasets:** 92 | * Obtain RVF10K and update the path in `Deepfake-Face-Detection-with-Xception.ipynb`. 93 | * For the custom CNN, prepare your dataset (e.g., in an `imagedata` directory) or use your own and update the path in `Real vs. AI-Generated Face Classification model.ipynb`. 94 | 3. **Run Notebooks:** Open and execute the cells in the respective `.ipynb` files. 95 | 96 | ## ✨ Future Improvements 97 | * **Dataset Expansion:** Train on larger, more diverse datasets. 98 | * **Hyperparameter Tuning:** Optimize for both model architectures. 99 | * **Explore Other Architectures:** Investigate Vision Transformers (ViTs), other advanced CNNs, or specialized deepfake detection methods. 100 | * **Anomaly Detection:** Frame deepfake detection as an anomaly problem. 101 | * **Deployment:** Develop a user-friendly interface for prediction. 102 | 103 | Feel free to contribute by opening issues or submitting pull requests! 104 | 105 | ## 📄 License 106 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 107 | 108 | --- 109 | © 2025 indranil143 110 | --------------------------------------------------------------------------------