A deep learning-based web application for detecting whether an image is Real or AI-Generated. The project compares multiple CNN and transfer-learning architectures and integrates the best-performing model into a Django web application.
The rapid development of generative AI has made it increasingly difficult to distinguish AI-generated images from real images. This project investigates deep learning approaches for binary image classification and develops a web-based detection system.
The project follows a complete machine learning workflow:
Dataset Acquisition β Data Cleaning β Duplicate Detection β Leakage-Aware Splitting β Preprocessing β Augmentation β Model Training β Fine-Tuning β Evaluation β Django Deployment
- Detect AI-generated images using deep learning.
- Investigate and reduce potential data leakage.
- Compare different CNN and transfer-learning architectures.
- Evaluate models using multiple performance metrics.
- Deploy the best-performing model through a Django web application.
- Provide users with prediction results and prediction history.
The project uses the CIFAKE (Real and AI-Generated Synthetic Images) dataset.
Initial dataset:
- 120,000 images
- 60,000 Real
- 60,000 AI-Generated
- Image size: 32 Γ 32
- RGB images
Duplicate and leakage analysis was performed before model training.
After cleaning and removing duplicate-related leakage:
- 118,664 images
- Training: 78,961
- Validation: 19,741
- Test: 19,962
Exact hash overlap between the final train, validation and test splits was verified to be zero.
The dataset was inspected for:
- Corrupted images
- Duplicate images
- Image dimensions and colour modes
- Class distribution
- Potential train-test leakage
MD5 hashing was used to identify exact duplicate images.
Images were:
- Converted to RGB
- Resized to 224 Γ 224
- Converted to
float32 - Normalized to the range 0β1
Training images were augmented using:
- Random horizontal flipping
- Random rotation
- Random translation
- Random zoom
This was used to reduce overfitting and improve model generalisation.
The TensorFlow pipeline performs:
Load Image
β
Decode JPEG
β
Convert to RGB
β
Resize to 224 Γ 224
β
Normalize to 0β1
β
Apply Augmentation
β
Batch (32)
β
Prefetch
β
Model
Four architectures were investigated:
A custom CNN was developed as a baseline reference model.
Architecture includes:
- Conv2D layers
- MaxPooling
- GlobalAveragePooling
- Dense layer
- Dropout
- Sigmoid output
ResNet50 with ImageNet pretrained weights was used for transfer learning.
A two-stage training strategy was applied:
Stage 1
- Freeze the pretrained backbone
- Train the classification head
Stage 2
- Unfreeze deeper layers
- Fine-tune using a low learning rate
MobileNetV2 was included as a lightweight transfer-learning alternative with significantly fewer parameters than ResNet50.
EfficientNetB0 was evaluated as another efficient transfer-learning architecture. In the tested configuration, it remained close to chance-level performance and therefore was not selected for deployment.
| Model | Accuracy | ROC-AUC | Result |
|---|---|---|---|
| Baseline CNN | 95.01% | 98.80% | Baseline |
| ResNet50 Stage 1 | 96.78% | 99.52% | Improved |
| ResNet50 Fine-Tuned | 98.17% | 99.76% | Selected |
| MobileNetV2 Stage 1 | 91.41% | 97.39% | Lower |
| MobileNetV2 Fine-Tuned | 96.98% | 99.60% | Lightweight alternative |
| EfficientNetB0 Stage 1 | ~50.66% | 50.00% | Not selected |
Fine-tuned ResNet50 achieved the best observed validation performance:
- Accuracy: 98.17%
- Precision: 98.14%
- Recall: 98.14%
- F1 Score: 98.14%
- ROC-AUC: 99.76%
The fine-tuned ResNet50 model was therefore selected for integration into the Django application.
The trained models are integrated into a Django web application.
- User registration
- User login/logout
- Image upload
- AI-generated image detection
- Multiple model selection
- Confidence score
- Prediction history
- Model comparison
- About/project information
User Uploads Image
β
Select Detection Model
β
Django Backend
β
Image Preprocessing
β
Trained CNN Model
β
Prediction Probability
β
0.5 Threshold
β
Real / AI-Generated
β
Confidence Score
β
Prediction History
- Python
- TensorFlow
- Keras
- NumPy
- PIL
- Scikit-learn
- CNN
- Transfer Learning
- ResNet50
- MobileNetV2
- EfficientNetB0
- ImageNet pretrained models
- Django
- HTML
- CSS
- SQLite
- Django ORM
- Jupyter Notebook
- Google Colab
- Visual Studio Code
- Git
- GitHub
AI-Image-Detection-Deep-Learning/
β
βββ aiImageDetector/
β βββ Django project configuration
β
βββ detector/
β βββ ai_models.py
β βββ forms.py
β βββ models.py
β βββ urls.py
β βββ views.py
β
βββ ml_models/
β βββ baseline_cnn_best.keras
β βββ mobilenetv2_finetuned_best.keras
β βββ mobilenetv2_stage1_best.keras
β βββ resnet50_finetuned_best.h5
β βββ resnet50_finetuned_best.keras
β βββ resnet50_stage1_best.keras
β
βββ notebook/
β βββ 3190951_AI Image Detection.ipynb
β
βββ static/
βββ templates/
βββ manage.py
βββ requirements.txt
βββ .gitignore
git clone https://github.com/19jenil/AI-Image-Detection-Deep-Learning.git
cd AI-Image-Detection-Deep-LearningWindows:
python -m venv venv
venv\Scripts\activatemacOS/Linux:
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtpython manage.py migratepython manage.py runserverThen open:
http://127.0.0.1:8000/
The complete machine learning experimentation workflow is available in:
notebook/3190951_AI Image Detection.ipynb
The notebook contains dataset preparation, leakage analysis, preprocessing, augmentation, model training, fine-tuning and evaluation.
The reported performance represents the results obtained on the selected CIFAKE dataset and experimental configuration. It should not be interpreted as guaranteed performance on all real-world images or unseen generative AI systems.
Jenil Patel