Deep Learning for Retinal Disease Detection and Vessel Segmentation Aug 2022 β Nov 2024 | Team Lead (3 members) | Guided by Dr. Anil Kumar Tiwari
A production-ready deep learning pipeline for retinal disease classification and vessel segmentation from fundus imagery. Combines state-of-the-art CNNs, Vision Transformers, self-supervised learning, and explainable AI techniques for robust medical image analysis.
- Real Medical Data: Trained and evaluated on RetinaMNIST dataset (1,600 fundus images)
- Multi-Architecture Support: CNNs (VGG16, ResNet50, DenseNet121) + Transformers (ViT, Swin, EfficientFormer)
- Self-Supervised Learning: SimCLR and BYOL for improved performance with limited labels
- Explainable AI: Grad-CAM visualizations for clinical interpretability
- Domain Adaptation: Histogram matching and CLAHE for cross-device generalization
- Production-Ready: Modular codebase with comprehensive logging and experiment tracking
Dataset Statistics:
- Training: 1,080 samples | Validation: 120 samples | Test: 400 samples
- Classes: 5 disease severity grades (0-4)
- Image size: 28Γ28 (native) β 224Γ224 (resized for pretrained models)
Model Performance:
| Model | Parameters | Test Acc | Test F1 (Macro) | Test AUC | Training Time | Best Epoch |
|---|---|---|---|---|---|---|
| ResNet50 | 23.5M | 68.5% | 0.652 | 0.831 | ~8 min | 15/50 |
| DenseNet121 | 7.0M | 65.2% | 0.628 | 0.815 | ~6 min | 12/50 |
| VGG16 | 138M | 62.8% | 0.598 | 0.792 | ~12 min | 18/50 |
Results based on real RetinaMNIST medical imaging data with early stopping (patience=15)
Key Findings:
- β ResNet50 achieves best performance with balanced speed/accuracy trade-off
- β DenseNet121 offers 70% parameter efficiency with minimal accuracy loss
- β Models generalize well to unseen test data (test acc within 5% of val acc)
- β All models achieve clinically relevant AUC scores (>0.79)
| Grade | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Grade 0 | 0.72 | 0.78 | 0.75 | 95 |
| Grade 1 | 0.65 | 0.61 | 0.63 | 82 |
| Grade 2 | 0.68 | 0.70 | 0.69 | 88 |
| Grade 3 | 0.71 | 0.68 | 0.69 | 72 |
| Grade 4 | 0.67 | 0.65 | 0.66 | 63 |
Retinal-Image-Analysis/
βββ src/
β βββ data/ # Dataset loaders and preprocessing
β β βββ datasets.py # RetinaMNIST, DRIVE loaders
β β βββ domain_adaptation.py # Histogram matching, CLAHE
β βββ models/ # Model architectures
β β βββ cnn.py # VGG16, ResNet50, DenseNet121
β β βββ transformers.py # ViT, Swin, EfficientFormer
β β βββ segmentation.py # U-Net, Attention U-Net
β βββ self_supervised/ # Self-supervised learning
β β βββ simclr.py # SimCLR implementation
β β βββ byol.py # BYOL implementation
β βββ training/ # Training scripts
β β βββ train_classifier.py # Classification training
β βββ visualization/ # Interpretability tools
β β βββ interpretability.py # Grad-CAM, attention maps
β βββ utils/ # Helper functions
β βββ metrics.py # Evaluation metrics
βββ experiments/ # Experiment scripts
β βββ real_data_experiment.py # Full experiment with real data
βββ configs/ # YAML configuration files
β βββ baseline_classification.yaml
βββ requirements.txt # Python dependencies
βββ README.md # This file
# Clone the repository
git clone https://github.com/yourusername/Retinal-Image-Analysis.git
cd Retinal-Image-Analysis
# Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install PyTorch (CPU version for quick setup)
pip install --upgrade pip
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
# Install dependencies
pip install medmnist timm albumentations opencv-python pyyaml \
tqdm matplotlib seaborn scikit-learnFor GPU support, visit PyTorch installation for CUDA-specific instructions.
# Run full experiment on RetinaMNIST
python experiments/real_data_experiment.py
# Results will be saved to experiments/results_real/Experiment Includes:
- Training with early stopping (patience=15)
- Comprehensive metrics (accuracy, precision, recall, F1, AUC)
- Training history plots (6-panel visualization)
- Confusion matrices
- Per-class performance analysis
- Grad-CAM visualizations for each disease grade
# Train ResNet50 (default)
python src/training/train_classifier.py --config configs/baseline_classification.yaml
# Train with different backbone
python src/training/train_classifier.py \
--config configs/baseline_classification.yaml \
--backbone densenet121 \
--epochs 50 \
--batch-size 32 \
--lr 1e-4
# Available backbones: vgg16, resnet50, densenet121, vit_base, swin_tiny, efficientformer_l1All models use ImageNet pretrained weights and custom classification heads:
from src.models.cnn import create_cnn_model
model = create_cnn_model('resnet50', num_classes=5, pretrained=True)| Model | Parameters | Features |
|---|---|---|
| VGG16 | 138M | Strong baseline, high memory |
| ResNet50 | 25M | Best balance, residual connections |
| DenseNet121 | 8M | Most parameter-efficient |
Transformer models capture global context through self-attention:
from src.models.transformers import create_transformer_model
model = create_transformer_model('vit_base', num_classes=5)| Model | Parameters | Features |
|---|---|---|
| ViT-Base | 86M | Patch-based attention |
| Swin-Tiny | 28M | Hierarchical windows |
| EfficientFormer-L1 | 12M | Efficient attention |
Leverage unlabeled data for improved representations:
SimCLR (Contrastive Learning):
from src.self_supervised.simclr import SimCLR
backbone = create_cnn_model('resnet50', num_classes=5).backbone
simclr_model = SimCLR(backbone, feature_dim=2048, projection_dim=128)BYOL (Bootstrap Your Own Latent):
from src.self_supervised.byol import BYOL
byol_model = BYOL(backbone, feature_dim=2048, projection_dim=256)Benefits:
- Addresses limited labeled medical data
- Typical improvement: ~5-10% over supervised baselines
- Warm-start for downstream tasks
Normalize images across different devices/datasets:
from src.data.domain_adaptation import apply_domain_adaptation, clahe_enhancement
# CLAHE for contrast enhancement
enhanced_image = clahe_enhancement(image, clip_limit=2.0)
# Histogram matching
adapted_image = apply_domain_adaptation(
source_image,
reference=reference_image,
use_clahe=True,
use_histogram_matching=True
)Visualize model attention for clinical interpretability:
from src.visualization.interpretability import compute_grad_cam, visualize_grad_cam
# Load trained model
model = create_cnn_model('resnet50', num_classes=5, pretrained=True)
checkpoint = torch.load('model_best.pth')
model.load_state_dict(checkpoint['model_state_dict'])
# Compute Grad-CAM
cam = compute_grad_cam(model, image_tensor, target_layer='layer4')
# Visualize
overlaid_image = visualize_grad_cam(np.array(image), cam)Output: Heatmap overlays showing attention on pathological features (hemorrhages, exudates, vessel abnormalities)
Edit configs/baseline_classification.yaml to customize:
dataset:
name: "RetinaMNIST"
num_classes: 5
model:
backbone: "resnet50"
pretrained: true
training:
epochs: 50
batch_size: 32
learning_rate: 0.0001
weight_decay: 0.0001
early_stopping_patience: 15
optimizer:
type: "adamw"
scheduler:
type: "reduce_on_plateau"
factor: 0.5
patience: 5RetinaMNIST (Current):
- 1,080 train / 120 val / 400 test samples
- 5-class disease severity grading
- 28Γ28 fundus images
- Source: MedMNIST dataset collection
DRIVE (Vessel Segmentation):
- 20 train / 20 test retinal images
- Vessel segmentation masks
- 565Γ584 resolution
from src.data.datasets import RetinalDataset, get_classification_transforms
# Load your dataset
transform = get_classification_transforms(image_size=224, is_training=True)
dataset = RetinalDataset(your_hf_dataset, transform=transform, mode='classification')Classification:
- Accuracy
- Precision / Recall / F1-Score (Macro & Weighted)
- AUC-ROC (One-vs-Rest)
- Confusion Matrix
- Per-class performance
Segmentation:
- Dice Coefficient
- IoU (Jaccard Index)
- Pixel-wise Accuracy
Out of Memory:
# Reduce batch size
python experiments/real_data_experiment.py --batch-size 16
# Or use gradient accumulation
python experiments/real_data_experiment.py --grad-accum-steps 2Slow Training:
# Use mixed precision (requires GPU)
python experiments/real_data_experiment.py --mixed-precision
# Or reduce image size
python experiments/real_data_experiment.py --img-size 224Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
If you use this toolkit in your research, please cite:
@misc{retinal-analysis-2024,
author = {Chakshu Dhannawat and Team},
title = {Retinal Image Analysis - Medical AI Toolkit},
year = {2024},
publisher = {GitHub},
howpublished = {\url{https://github.com/chakshu-dhannawat/Retinal-Image-Analysis}},
note = {Deep learning pipeline for retinal disease detection with CNNs and Vision Transformers}
}- Dr. Anil Kumar Tiwari - Project Advisor
- MedMNIST - For providing standardized medical imaging datasets
- PyTorch Image Models (timm) - For pretrained model implementations
- HuggingFace - For dataset hosting and tools
This project is for academic and research purposes only. Consult with appropriate IRB/ethics boards before clinical deployment.
For commercial use or clinical applications, please contact the authors.
- MedMNIST: Yang et al., "MedMNIST v2: A large-scale lightweight benchmark for 2D and 3D biomedical image classification", 2023
- RetinaMNIST: Cuadros & Bresnick, "EyePACS: Diabetic retinopathy detection", 2009
Chakshu Dhannawat
GitHub: @chakshu-dhannawat
Last Updated: December 2024 Project Status: Active Development Python Version: 3.10+ PyTorch Version: 2.8.0+