An automated deep learning system for detecting and classifying the severity of Diabetic Retinopathy from retinal fundus images using custom-built Convolutional Neural Networks.
Multiclass Classification (5 severity levels) · Binary Classification (DR vs No DR) · Built from Scratch
- About the Project
- Problem Statement
- Dataset
- Data Preprocessing
- Model Architecture
- Results
- Model Variants
- Tech Stack
- Project Structure
- Getting Started
- Future Scope
- References
- Author
Diabetic Retinopathy (DR) is one of the leading causes of blindness worldwide. It occurs when high blood sugar damages the blood vessels in the retina, causing them to swell, leak, or stop blood flow entirely. Early detection is critical — but traditional screening relies on ophthalmologists manually interpreting fundus images, which is time-consuming, subjective, and prone to human error.
This project proposes an automated DR detection system using deep Convolutional Neural Networks (CNNs) built from scratch. Unlike most existing studies that rely on pre-trained transfer learning models, this project trains a custom CNN architecture end-to-end, providing full transparency and control over the learning process.
Two classification approaches were implemented:
- Multiclass Classification — classifying fundus images into 5 severity levels (No DR, Mild, Moderate, Severe, Proliferative DR)
- Binary Classification — detecting whether a patient has DR or not (DR vs No DR)
- The number of expert ophthalmologists is insufficient to handle the growing volume of diabetic patients requiring retinal screening.
- Long waiting lines at hospitals and diagnostic centers delay timely treatment, increasing the risk of irreversible vision loss.
- Fundus images often have subtle, overlapping features across severity levels, making manual diagnosis error-prone.
- There is an urgent need for scalable, automated screening tools that can assist clinicians in early DR detection.
This project uses the APTOS 2019 Blindness Detection Dataset from Kaggle — a large-scale collection of retinal fundus photographs captured under diverse imaging conditions, with clinician-graded severity labels.
| Property | Details |
|---|---|
| Source | Asia Pacific Tele-Ophthalmology Society (APTOS) |
| Total Images | 3,662 training images |
| Image Format | PNG (fundus photography) |
| Labels | Clinician-rated severity (0–4) |
| Split | 80:20 Train/Validation + Hold-out Test Set |
Severity Classes:
| Label | Severity Level | Description |
|---|---|---|
| 0 | No DR | Healthy retina |
| 1 | Mild | Microaneurysms only |
| 2 | Moderate | More than just microaneurysms |
| 3 | Severe | Extensive intraretinal hemorrhages |
| 4 | Proliferative DR | Neovascularization or vitreous/preretinal hemorrhage |
⚠️ Note: The dataset is imbalanced — the "No DR" class significantly outnumbers the severe categories. Strategies like oversampling and class weight distribution were applied to mitigate bias.
A robust preprocessing pipeline was implemented to ensure data quality and consistency before model training:
Steps performed:
- Missing & Duplicate Check — No missing or duplicate values found in the dataset
- Label Type Conversion — Converted diagnosis labels from numeric to string for categorical processing in Keras
- Data Consolidation — Merged image paths with severity labels via the
id_codecolumn - Image Resizing — All images resized to 256 × 256 × 3 (height × width × RGB channels)
- Normalization — Pixel values rescaled to [0, 1] range by dividing by 255
- Class Imbalance Handling — Applied oversampling and class weight distributions
- Image Augmentation — Used Keras
ImageDataGeneratorwith shear, zoom, and horizontal flip
A custom Sequential CNN was designed from scratch using Keras with TensorFlow backend. The architecture prioritizes simplicity and interpretability while maintaining strong feature extraction capability.
Architecture Details:
| Layer | Configuration |
|---|---|
| Input | 256 × 256 × 3 (RGB fundus image) |
| Conv2D Block 1 | 32 filters, 3×3 kernel, stride (1,1), ReLU → MaxPool 2×2 |
| Conv2D Block 2 | 64 filters, 3×3 kernel, stride (1,1), ReLU → MaxPool 2×2 |
| Conv2D Block 3 | 128 filters, 3×3 kernel, stride (1,1), ReLU → MaxPool 2×2 |
| Flatten | Converts 3D feature maps to 1D vector |
| Dense 1 | 128 units, ReLU activation |
| Dense 2 (Output) | 5 units (multiclass) / 1 unit (binary), Softmax / Sigmoid |
Training Configuration:
| Parameter | Value |
|---|---|
| Optimizer | Adam (lr = 0.001) |
| Loss Function | Categorical Cross-Entropy / Binary Cross-Entropy |
| Epochs | 50 |
| Callbacks | EarlyStopping, ReduceLROnPlateau |
| Total Trainable Parameters | 14,839,621 |
Training & Validation Scores:
| Metric | Training | Validation | Test |
|---|---|---|---|
| Accuracy | 85.7% | 75.9% | 74% |
| Loss | 0.384 | 0.805 | — |
Classification Report:
| Class | Precision | Recall | F1-Score |
|---|---|---|---|
| 0 — No DR | 0.93 | 0.97 | 0.95 |
| 1 — Mild | 0.42 | 0.50 | 0.46 |
| 2 — Moderate | 0.69 | 0.68 | 0.68 |
| 3 — Severe | 0.24 | 0.19 | 0.22 |
| 4 — Proliferative DR | 0.33 | 0.25 | 0.28 |
| Overall Accuracy | 0.74 | ||
| Weighted Avg | 0.73 | 0.74 | 0.73 |
Figure 8: Normalized confusion matrix — "No DR" classified at 97% accuracy; minority classes remain challenging due to data imbalance
Training & Validation Scores:
| Metric | Training | Validation | Test |
|---|---|---|---|
| Accuracy | 97.6% | 96% | 95% |
| Loss | 0.06 | 0.10 | 0.17 |
Classification Report:
| Class | Precision | Recall | F1-Score |
|---|---|---|---|
| 0 — No DR | 0.93 | 0.96 | 0.94 |
| 1 — DR | 0.96 | 0.93 | 0.95 |
| Overall Accuracy | 0.95 | ||
| AUC-ROC | 0.986 |
Multiple CNN variants were explored to find the optimal architecture:
| Version | Key Features | Parameters | Train Acc | Val Acc | Test Acc |
|---|---|---|---|---|---|
| V1 ✅ | No BatchNorm, No Dropout, Adam optimizer | 14.8M | 86% | 76% | 75% |
| V2 | BatchNorm + Dropout, SGD optimizer | 14.8M | 81% | 71% | 70% |
| V3 | More hidden layers, Dropout, RMSprop | 167K | 77% | 78% | 74% |
✅ Version 1 was selected as the best-performing model for multiclass classification.
| Category | Technologies |
|---|---|
| Language | Python 3.8+ |
| Deep Learning | TensorFlow 2.x, Keras |
| Data Handling | NumPy, Pandas |
| Visualization | Matplotlib, Seaborn |
| Image Processing | OpenCV, PIL |
| Notebook | Jupyter Notebook |
| Dataset | Kaggle (APTOS 2019) |
Diabetic-Retinopathy-Detection-CNN/
│
├── README.md # Project documentation (you are here)
├── LICENSE # MIT License
│
├── notebooks/
│ └── DR_Detection.ipynb # Main Jupyter Notebook with all code
│
├── docs/
│ ├── PaperSubmission_Ahmad.doc # Research paper (full write-up)
│ └── Ahmad_ProjectPresentationPPT.pptx # Project presentation slides
│
├── images/ # Figures used in README
│ ├── fundus_samples_unscaled.png
│ ├── fundus_samples_rescaled.png
│ ├── image_augmentation.png
│ ├── project_flowchart.jpg
│ ├── cnn_architecture.jpg
│ ├── model_summary.jpg
│ ├── multiclass_accuracy_plot.png
│ ├── multiclass_loss_plot.png
│ ├── multiclass_confusion_matrix.png
│ ├── binary_accuracy_plot.png
│ ├── binary_loss_plot.png
│ └── binary_confusion_matrix.png
│
├── models/ # Saved trained models (optional)
│ └── best_model.h5
│
└── requirements.txt # Python dependencies
- Python 3.8 or higher
- pip package manager
- Jupyter Notebook
- GPU recommended (for faster training)
1. Clone the repository:
git clone https://github.com/AhmadSabbirChowdhury/Diabetic-Retinopathy-Detection-CNN.git
cd Diabetic-Retinopathy-Detection-CNN2. Install dependencies:
pip install -r requirements.txt3. Download the dataset:
Download the APTOS 2019 Blindness Detection Dataset from Kaggle and place the images in a data/ directory.
4. Run the notebook:
jupyter notebook notebooks/DR_Detection.ipynbtensorflow>=2.4.0
keras>=2.4.0
numpy>=1.19.0
pandas>=1.2.0
matplotlib>=3.3.0
seaborn>=0.11.0
opencv-python>=4.5.0
scikit-learn>=0.24.0
Pillow>=8.0.0
- Advanced Architectures — Implement transfer learning with ResNet, EfficientNet, or Vision Transformers for improved accuracy on minority classes
- Ensemble Methods — Combine multiple model predictions for more robust classification
- Transformer-Based Models — Explore attention mechanisms for better feature localization
- Clinical Deployment — Develop a web/mobile application for real-time DR screening in healthcare settings
- Larger Datasets — Train on combined datasets (APTOS + Messidor + EyePACS) for better generalization
- Centers for Disease Control and Prevention, "Diabetes and Vision Loss," U.S. Department of Health & Human Services.
- APTOS, "APTOS 2019 Blindness Detection," Kaggle Competition, 2019.
- Butt et al., "Diabetic Retinopathy Detection from Fundus Images Using Hybrid Deep Learning Features," Diagnostics, 2022.
- Gangwar & Ravi, "Diabetic Retinopathy Detection Using Transfer Learning and Deep Learning," 2021.
- Rakhlin, "Diabetic Retinopathy Detection through Integration of Deep Learning Classification Framework," bioRxiv, 2018.
- Farag et al., "Automatic Severity Classification of DR Based on DenseNet and CBAM," IEEE Access, 2022.
- Zhang et al., "Diabetic Retinopathy Grading by a Source-Free Transfer Learning Approach," Biomed. Signal Process. Control, 2022.
- Gour & Khanna, "Multi-Class Multi-Label Ophthalmological Disease Detection Using Transfer Learning Based CNN," Biomed. Signal Process. Control, 2021.
- Wong et al., "Understanding Data Augmentation for Classification: When to Warp?," DICTA, 2016.
Ahmad Chowdhury
- 🏢 Job: AI Researcher, Nova Scotia Health, Nova Scotia Cancer Society, Halifax, NS, Canada
- 🎓 Education: MSc, Jodrey School of Computer Science, Acadia University, Wolfville, NS, Canada
- 📧 ahmadchowdhury@acadiau.ca
- 🔗 GitHub
If you found this project helpful, please consider giving it a ⭐!










