File size: 4,283 Bytes
d3776ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# πŸ” Deepfake Image Detection
**Binary Classifier β€” Real vs Fake**

This project implements a high-performance deepfake detection system using the `InceptionResnetV1` architecture (pretrained on VGGFace2). It is designed to distinguish between real human faces and AI-generated/deepfake images, supporting modern AI sources like Midjourney, Stable Diffusion, and DALL-E.

---

## πŸš€ Key Features
- **Modern AI Support**: Custom data loaders for Midjourney, SD, DALL-E, and Flux images.
- **HuggingFace Integration**: Seamlessly merges the `riandika/AI-vs-Deepfake-vs-Real-Resized-Aug` dataset.
- **GPU Acceleration**: Fully optimized for NVIDIA GPUs (RTX 40 series) with CUDA 12.1.
- **Pause & Resume**: Training state is automatically saved to `checkpoint.pt`, allowing you to interrupt and resume anytime.
- **Diffusion-Aware Augmentation**: Specialized transforms (JPEG compression, Gaussian Blur) to catch subtle AI artifacts.
- **Rich Visualization**: Automated plotting of training loss and validation accuracy.

---

## πŸ“‚ Project Structure
| File/Folder | Description |
|:---|:---|
| `Deepfake_Detection.ipynb` | Main Jupyter Notebook for training and inference. |
| `Deepfake_Detection.py` | Python script version of the detection pipeline. |
| `requirements.txt` | List of Python dependencies. |
| `checkpoint.pt` | Saved training state (epoch, optimizer, model weights). |
| `models/` | Directory where final trained models are saved. |
| `training_curves.png` | Visualization of the latest training run. |

---

## πŸ› οΈ Prerequisites
- **Python**: 3.12 (Recommended via Conda)
- **Conda**: For environment management (Miniconda or Anaconda)
- **GPU**: NVIDIA GPU with CUDA 12.1 support (Standard training runs on CPU but is much slower)

---

## βš™οΈ Installation & Setup

### 1. Clone the Project
```bash
# Navigate to your project folder
cd C:\Users\Downloads\deepfake_main
```

### 2. Create a Virtual Environment (Conda)
It is highly recommended to use a dedicated environment to avoid package conflicts.
```bash
conda create -n deepfake_env python=3.12 -y
conda activate deepfake_env
```

### 3. Install Standard Dependencies
```bash
pip install -r requirements.txt
```

---

## ⚑ Dedicated GPU Installation (NVIDIA)
To leverage your **RTX 40-series GPU (e.g., RTX 4050)**, follow these specific steps to install PyTorch with CUDA 12.1 support.

### 1. Install PyTorch with CUDA 12.1
```bash
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
```

### 2. Verify GPU Recognition
Run the following Python snippet in your terminal:
```python
import torch
print(f"CUDA Available: {torch.cuda.is_available()}")
print(f"GPU Name: {torch.cuda.get_device_name(0)}")
```
*If it returns `True`, your GPU is ready.*

### 3. Troubleshooting
- Ensure you have the latest **NVIDIA Drivers** installed from [nvidia.com](https://www.nvidia.com/Download/index.aspx).
- If you encounter a `DLL load failed` error, try installing the [Microsoft Visual C++ Redistributable](https://aka.ms/vs/17/release/vc_redist.x64.exe).

---

## 🎯 Usage Guide

### 1. Configure Paths
Open `Deepfake_Detection.py` or the Notebook and adjust the `CONFIG` dictionary:
```python
CONFIG = {
    'train_dir' : r'C:\path\to\your\train_data',
    'valid_dir' : r'C:\path\to\your\valid_data',
    'test_dir'  : r'C:\path\to\your\test_data',
    # ... other settings
}
```

### 2. Run Training
**Using Jupyter Notebook:**
Simply open `Deepfake_Detection.ipynb` in VS Code or Jupyter Lab and run all cells.

**Using Terminal:**
```bash
python Deepfake_Detection.py
```

### 3. Pause & Resume
- **To Pause**: Simply interrupt the execution (Ctrl+C in terminal or Stop in Notebook).
- **To Resume**: Re-run the script or training cell. It will detect `checkpoint.pt` and pick up where it left off.

---

## πŸ”Ž Single Image Inference
To test an image, use the provided `predict_image` function in the notebook:
```python
result = predict_image("path/to/test_image.jpg", model, device)
print(f"Verdict: {result['label']} ({result['confidence']*100:.2f}%)")
```

---

## πŸ“Š Results
After training, check `training_curves.png` to see how the model's loss and accuracy improved over time. 

Final models are stored in the `/models` directory with a `.pt` extension.