File size: 9,585 Bytes
a0d92b3
928b74f
 
 
 
a0d92b3
 
 
928b74f
a0d92b3
928b74f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
---
title: Image-Forensic
emoji: πŸ”
colorFrom: indigo
colorTo: purple
sdk: docker
pinned: false
---
# ImageForensics-Detect

> **Research-grade multi-branch image forensics platform for detecting Real vs. AI-Generated images.**  
> B.Tech Final Year Project Β· IEEE-Style Research System

---

## 🧠 What This Does

ImageForensics-Detect analyzes uploaded images through **5 independent forensic detection branches** and fuses their outputs using **certainty-weighted probabilistic fusion** to decide whether an image is:

- **Real** β€” captured by a physical camera
- **AI-Generated** β€” created by GANs or diffusion models (Stable Diffusion, DALL-E, Midjourney, etc.)

---

## πŸ—οΈ Architecture

```
Input Image
    β”‚
    β”œβ”€β”€β–Ί Spectral Branch   (FFT/DCT analysis)         [no training needed]
    β”œβ”€β”€β–Ί Edge Branch       (Sobel/Laplacian forensics) [no training needed]
    β”œβ”€β”€β–Ί CNN Branch        (EfficientNet-B0 / TF)      [train_cnn.py]
    β”œβ”€β”€β–Ί ViT Branch        (ViT-B/16 / PyTorch+timm)   [train_vit.py]
    └──► Diffusion Branch  (residual noise analysis)   [no training needed]
             β”‚
    Certainty-Weighted Probabilistic Fusion
             β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚  Prediction: Real / AI-Gen     β”‚
    β”‚  Confidence: 97.1%             β”‚
    β”‚  Grad-CAM heatmap              β”‚
    β”‚  Spectral anomaly map          β”‚
    β”‚  Noise residual map            β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

---

## πŸ“ Folder Structure

```
ImageForensics-Detect/
β”œβ”€β”€ data/raw/{real,fake}/          ← Your dataset goes here
β”œβ”€β”€ models/                        ← Saved .h5 / .pth weights
β”œβ”€β”€ branches/
β”‚   β”œβ”€β”€ spectral_branch.py         βœ… COMPLETE (signal processing)
β”‚   β”œβ”€β”€ edge_branch.py             βœ… COMPLETE (signal processing)
β”‚   β”œβ”€β”€ cnn_branch.py              πŸ”΅ BASELINE (needs training)
β”‚   β”œβ”€β”€ vit_branch.py              πŸ”΅ BASELINE (needs training)
β”‚   └── diffusion_branch.py        βœ… COMPLETE (signal processing)
β”œβ”€β”€ fusion/fusion.py               βœ… COMPLETE
β”œβ”€β”€ explainability/
β”‚   β”œβ”€β”€ gradcam.py                 βœ… COMPLETE
β”‚   └── spectral_heatmap.py        βœ… COMPLETE
β”œβ”€β”€ training/
β”‚   β”œβ”€β”€ dataset_loader.py          βœ… COMPLETE
β”‚   β”œβ”€β”€ train_cnn.py               βœ… COMPLETE
β”‚   β”œβ”€β”€ train_vit.py               βœ… COMPLETE
β”‚   └── evaluate.py                βœ… COMPLETE
β”œβ”€β”€ backend/app.py                 βœ… COMPLETE (FastAPI)
β”œβ”€β”€ frontend/{index.html,style.css,app.js}  βœ… COMPLETE
β”œβ”€β”€ utils/{image_utils.py,logger.py}        βœ… COMPLETE
└── outputs/                       ← Logs, heatmaps, eval results
```

---

## βš™οΈ Installation

### Prerequisites
- Python 3.9+
- pip / conda

### 1. Create Virtual Environment
```bash
cd "ImageForensics-Detect"
python -m venv venv
source venv/bin/activate          # macOS/Linux
# venv\Scripts\activate           # Windows
```

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

> **Note**: On Apple Silicon (M1/M2/M3), use `pip install tensorflow-macos tensorflow-metal` instead of `tensorflow`.

---

## πŸ—‚οΈ Dataset Setup

Populate the dataset folders before training:
```
data/raw/
β”œβ”€β”€ real/    ← Real camera photos (.jpg, .png)
└── fake/    ← AI-generated images (.jpg, .png)
```

**Recommended datasets:**
| Type | Dataset | Source |
|---|---|---|
| Real | RAISE-1K / VISION / MIT-5k | Kaggle / research groups |
| AI-Gen | ThisPersonDoesNotExist / SDXL outputs | Collected/scraped |
| Mixed | ArtiFact / CNNDetection | GitHub papers |

The loader auto-splits: **70% train / 15% val / 15% test** (stratified).

---

## πŸ‹οΈ Training

### Train CNN Branch (EfficientNet-B0 / TensorFlow)
```bash
python training/train_cnn.py --epochs 30 --batch_size 32 --lr 1e-4
# Saves: models/cnn_branch.h5
```

### Train ViT Branch (ViT-B/16 / PyTorch)
```bash
python training/train_vit.py --epochs 20 --batch_size 16 --lr 1e-4
# Saves: models/vit_branch.pth
```

> **Without training:** The system is still functional β€” the 3 handcrafted branches (Spectral, Edge, Diffusion) produce real forensic outputs immediately. CNN/ViT branches return neutral `0.5` confidence and are flagged as "untrained" in the API response.

---

## πŸ“Š Evaluation

```bash
# Evaluate entire fusion system
python training/evaluate.py

# Evaluate individual branches
python training/evaluate.py --branch spectral
python training/evaluate.py --branch edge
python training/evaluate.py --branch cnn
python training/evaluate.py --branch vit
python training/evaluate.py --branch diffusion
```

Reports saved to `outputs/`:
- `confusion_matrix_<branch>.png`
- `roc_curve_<branch>.png`
- `evaluation_<branch>.csv`

---

## πŸš€ Running the System

### Step 1: Start Backend API
```bash
uvicorn backend.app:app --reload --host 0.0.0.0 --port 8000
```

### Step 2: Open Frontend
Open `frontend/index.html` in your browser (double-click, or use Live Server in VS Code).

### Step 3: Upload and Analyze
Drag-and-drop any image β†’ Click **Analyze Image** β†’ View results.

---

## 🌐 API Reference

### `POST /predict`
Upload an image and receive full forensic analysis.

**Request:**
```bash
curl -X POST "http://localhost:8000/predict" \
     -F "file=@your_image.jpg"
```

**Response:**
```json
{
  "prediction": "AI-Generated",
  "confidence": 97.1,
  "prob_fake": 0.9855,
  "branches": {
    "spectral":  { "prob_fake": 0.9420, "confidence": 0.8800, "label": "AI-Generated" },
    "edge":      { "prob_fake": 0.8100, "confidence": 0.7200, "label": "AI-Generated" },
    "cnn":       { "prob_fake": 0.9820, "confidence": 0.9640, "label": "AI-Generated" },
    "vit":       { "prob_fake": 0.9600, "confidence": 0.9200, "label": "AI-Generated" },
    "diffusion": { "prob_fake": 0.8900, "confidence": 0.8300, "label": "AI-Generated" }
  },
  "gradcam_b64":   "<base64-encoded JPEG>",
  "spectrum_b64":  "<base64-encoded JPEG>",
  "noise_map_b64": "<base64-encoded JPEG>",
  "edge_map_b64":  "<base64-encoded JPEG>",
  "low_certainty": false
}
```

### `GET /health`
```json
{ "status": "ok", "service": "ImageForensics-Detect", "version": "1.0.0" }
```

### `GET /logs`
```json
{ "total": 42, "real": 18, "ai_generated": 24 }
```

---

## πŸ“ Research Methodology

### Title (Suggested)
> **"Multi-Branch Certainty-Weighted Forensic Detection of AI-Generated Images Using Spectral Analysis, Edge Statistics, CNN, and Vision Transformers"**

### Abstract
This work presents ImageForensics-Detect, a multi-branch forensic analysis framework for distinguishing real camera photographs from AI-generated images produced by GANs and diffusion models. The system integrates five complementary detection branches: (1) spectral analysis using FFT and DCT to capture frequency-domain artifacts; (2) edge analysis using Sobel/Laplacian operators and gradient distribution statistics; (3) a CNN branch (EfficientNet-B0) for local texture and patch-level artifact detection; (4) a ViT branch (ViT-B/16) for global semantic inconsistency detection; and (5) a diffusion residual branch analyzing noise kurtosis and spatial uniformity. Branch predictions are combined using certainty-weighted probabilistic fusion, ensuring that uncertain or untrained branches contribute proportionally less to the final decision.

### Key Design Decisions

| Decision | Rationale |
|---|---|
| Multi-branch ensemble | No single signal catches all generator types |
| Certainty-weighted fusion | Prevents weak/untrained branches from degrading accuracy |
| FFT + DCT (spectral) | GAN checkerboard artifacts are frequency-domain detectable |
| EfficientNet-B0 | Best accuracy-efficiency trade-off for the CNN branch |
| ViT-B/16 (timm) | Global receptive field catches semantic incoherence CNNs miss |
| Noise uniformity (diffusion) | Diffusion models produce spatially uniform noise fields |

### References (Key Papers)
1. Wang et al. (2020). *CNN-generated images are surprisingly easy to spot... for now.* CVPR.
2. Frank et al. (2020). *Leveraging Frequency Analysis for Deep Fake Image Recognition.* ICML.
3. Corvi et al. (2023). *On the detection of synthetic images generated by diffusion models.* ICASSP.
4. Ojha et al. (2023). *Towards Universal Fake Image Detection.* CVPR.
5. Selvaraju et al. (2017). *Grad-CAM: Visual Explanations from Deep Networks.* ICCV.

---

## πŸ› οΈ Tech Stack

| Layer | Technology |
|---|---|
| CNN Branch | TensorFlow 2.13+, EfficientNet-B0 |
| ViT Branch | PyTorch 2.0+, timm, ViT-B/16 |
| Signal Branches | OpenCV, NumPy, SciPy |
| Backend | FastAPI, Uvicorn |
| Frontend | HTML5, CSS3, Vanilla JavaScript |
| Evaluation | Scikit-learn, Matplotlib, Seaborn |

---

## πŸ“Œ Status Summary

| Module | Status | Result |
|---|---|---|
| Spectral Branch | βœ… Complete | Signal Forensics |
| Edge Branch | βœ… Complete | Signal Forensics |
| Diffusion Branch | βœ… Complete | Signal Forensics |
| CNN Branch | βœ… Trained | EfficientNet-B0 |
| ViT Branch | βœ… Fully Trained | **99.30% Accuracy** |
| Fusion Module | βœ… Complete | Certainty-Weighted |
| Grad-CAM | βœ… Complete | (with Saliency Fallback) |
| Frontend UI | βœ… Enhanced | Stats Hero + Prob Bar |
| FastAPI Backend | βœ… Complete | Port 8000 |

---

*Built for IEEE-style research deployment. B.Tech Final Year Project.*