A novel deep learning approach for Diabetic Retinopathy (DR) detection using Recurrent Attention Mechanisms inspired by Reinforcement Learning. DR-RAM learns to sequentially attend to diagnostically relevant regions of retinal fundus images, mimicking the visual search behavior of expert ophthalmologists.
Our model achieves state-of-the-art performance on the APTOS 2019 dataset, outperforming standard CNN baselines:
| Model | Accuracy | Precision | Recall | F1-Score | Kappa |
|---|---|---|---|---|---|
| DR-RAM (Ours) | 82.13% | 83.28% | 82.13% | 82.48% | 0.8914 |
| EfficientNet-B0 | 79.81% | 81.88% | 79.81% | 80.43% | 0.8702 |
Key Achievement: DR-RAM improves Quadratic Weighted Kappa by +2.12% while providing interpretable attention trajectories.
DR-RAM provides interpretable diagnosis by showing exactly where the model looked:
DR-RAM consists of four interconnected neural network modules:
- Glimpse Sensor: Extracts multi-scale patches (foveal + peripheral vision)
- Glimpse Network: CNN-based feature extraction with location encoding
- Core Network: GRU-based recurrent state aggregation
- Location & Classification Networks: Attention policy and final prediction
Input Image β [Glimpse Sensor] β [Glimpse Network] β [GRU Core] β [Classifier]
β β
βββββββββ [Location Network] βββββββββββ
git clone https://github.com/Ahad690/DR-RAM-Diabetic-Retinopathy-Detection.git
cd DR-RAM-Diabetic-Retinopathy-Detection# Using conda (recommended)
conda create -n drram python=3.10
conda activate drram
# Or using venv
python -m venv drram_env
source drram_env/bin/activate # Linux/Mac
# drram_env\Scripts\activate # Windowspip install -r requirements.txtDownload the APTOS 2019 Blindness Detection dataset from Kaggle and extract it:
# Using Kaggle CLI
kaggle competitions download -c aptos2019-blindness-detection
unzip aptos2019-blindness-detection.zip -d data/# Run the Jupyter notebook
jupyter notebook notebooks/dr-ram-aptos2019.ipynb
# Or run on Kaggle (recommended for GPU)
# Upload notebook to Kaggle and enable GPU acceleratorTo test the model on a sample image and visualize the attention trajectory, use the provided inference script. The project includes a sample_images/ folder with 20 test images from the APTOS dataset.
Copy and paste this single cell into your notebook to run inference immediately:
# 1. Clone & Setup
import os
REPO_NAME = 'DR-RAM-Diabetic-Retinopathy-Detection'
if not os.path.exists(REPO_NAME):
!git clone https://github.com/Ahad690/{REPO_NAME}.git
%cd {REPO_NAME}
!pip install -r requirements.txt -q
# 2. Run Inference (picks a random image from sample_images/)
!python notebooks/inference.py
# 3. Display Result
from IPython.display import Image, display
display(Image('prediction_result.png'))Tip
By default, the script picks the first image from sample_images/. You can change the IMAGE_PATH variable in notebooks/inference.py to test any of the 20 images.
DR-RAM-Diabetic-Retinopathy-Detection/
β
βββ π notebooks/
β βββ dr-ram-aptos2019.ipynb # Main training notebook
β βββ inference.py # Model inference & visualization
β
βββ π results/
β βββ checkpoints/ # Model weights
β β βββ drram_best.pth # Best DR-RAM model
β β βββ baseline_best.pth # Best baseline model
β βββ results/ # Visualization & metrics
β βββ confusion_matrix.png # Confusion matrix
β βββ comparison.png # Model comparison
β βββ attention_0.png # Trajectory sample 1
β βββ attention_1.png # Trajectory sample 2
β βββ attention_2.png # Trajectory sample 3
β βββ attention_3.png # Trajectory sample 4
β βββ results.json # Detailed metrics
β
βββ π LaTeX_project/ # LaTeX source for report
β βββ main.tex # Main LaTeX file
β βββ architecture.png # Architecture diagram
β
βββ π¨ sample_images/ # Sample retinal images for testing
β
βββ π Report.pdf # Final research report
βββ π requirements.txt # Python dependencies
βββ π README.md # This file
βββ π LICENSE.md # MIT License
| Hyperparameter | Value |
|---|---|
| Image Size | 380 Γ 380 |
| Glimpse Size | 64 Γ 64 |
| Number of Glimpses | 6 |
| Number of Scales | 3 |
| Hidden Size | 512 |
| Batch Size | 32 |
| Epochs | 15 |
| Learning Rate | 3 Γ 10β»β΄ |
| Optimizer | AdamW |
| Weight Decay | 10β»β΄ |
| Label Smoothing | 0.1 |
| Backbone | EfficientNet-B0 (pretrained) |
We use the APTOS 2019 Blindness Detection dataset from Kaggle:
| Class | Description | Count | Percentage |
|---|---|---|---|
| 0 | No DR | 1,805 | 49.3% |
| 1 | Mild NPDR | 370 | 10.1% |
| 2 | Moderate NPDR | 999 | 27.3% |
| 3 | Severe NPDR | 193 | 5.3% |
| 4 | Proliferative DR | 295 | 8.1% |
| Total | 3,662 | 100% |
Preprocessing Pipeline:
- Black border cropping (grayscale thresholding)
- Ben Graham's preprocessing for illumination normalization
- Resize to 380 Γ 380 pixels
- Kaggle Notebook: DR-RAM Training Notebook
- Dataset: APTOS 2019 Blindness Detection
- Original RAM Paper: Recurrent Models of Visual Attention (NeurIPS 2014)
If you use this code in your research, please cite:
@misc{drram2025,
author = {Imran, Ahad and Abidin, Zain-Ul and Karim, Hamza Abdul and Aftab, Imaan},
title = {DR-RAM: Diabetic Retinopathy Detection Using Recurrent Attention and Reinforcement Learning},
year = {2025},
publisher = {GitHub},
howpublished = {\url{https://github.com/Ahad690/DR-RAM-Diabetic-Retinopathy-Detection}}
}- V. Mnih et al., "Recurrent Models of Visual Attention," NeurIPS, 2014.
- M. Tan and Q. V. Le, "EfficientNet: Rethinking Model Scaling," ICML, 2019.
- L. Dai et al., "A deep learning system for detecting diabetic retinopathy," Nature Communications, 2021.
- V. Gulshan et al., "Development and Validation of a Deep Learning Algorithm for Detection of Diabetic Retinopathy," JAMA, 2016.
| Name | Roll Number | GitHub |
|---|---|---|
| Ahad Imran | F23607034 | @ahadimran |
| Zain-Ul-Abidin | F23607031 | |
| Hamza Abdul Karim | F23607046 | |
| Imaan Aftab | F23607041 |
Course: CS480 - Reinforcement Learning
Instructor: Lec. Asif Mehmood
Institution: NUTECH
This project is licensed under the MIT License - see the LICENSE.md file for details.
MIT License
Copyright (c) 2025 Ahad Imran, Zain-Ul-Abidin, Hamza Abdul Karim, Imaan Aftab
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
- Kaggle for providing free GPU resources
- APTOS for the dataset
- PyTorch team for the deep learning framework
- Course instructor Lec. Asif Mehmood for guidance
β If you find this project useful, please consider giving it a star! β


