Downloads · 30 days
0
dheeren-tejani/DiabeticRetinpathyClassifier
DiabeticRetinpathyClassifier is a image classification model from dheeren-tejani. Use it when you need a label for an image. It is set up for pytorch. The card lists the license as apache-2.0.
This is a multi-task deep learning model trained to classify the severity of Diabetic Retinopathy (DR) from retinal fundus images. It is based on the EfficientNet-B3 architecture and was specifically optimized to impr…
Downloads · 30 days
0
Access
Public
Updated Sep 13, 2025
Repo size
185 MB
Likes
0
Public
Click a slice to open those files.
.pth165 MB · 89%
From the Hugging Face model README
This is a multi-task deep learning model trained to classify the severity of Diabetic Retinopathy (DR) from retinal fundus images. It is based on the EfficientNet-B3 architecture and was specifically optimized to improve the Quadratic Weighted Kappa (QWK) score, a clinically relevant metric for ordinal classification tasks like DR grading.
This model is the second iteration (V2) of a project focused on building a diagnostically "smarter" classifier that is more sensitive to severe, vision-threatening stages of the disease.
timm/efficientnet_b3 backbone with a custom multi-task head.severity: 5 classes (0: No DR, 1: Mild, 2: Moderate, 3: Severe, 4: Proliferative).lesions: 5 classes (multi-label for various lesion types).regions: 5 classes (multi-label for affected anatomical regions).severity task by setting the loss weights for auxiliary tasks to zero. The auxiliary heads can still produce outputs for interpretability.The model can be easily loaded from Hugging Face Hub for inference.
# Install required libraries
pip install torch torchvision timm albumentations huggingface-hub numpy pillow opencv-python
import torch
import torch.nn as nn
import torch.nn.functional as F
import timm
from PIL import Image
import numpy as np
import albumentations as A
from albumentations.pytorch import ToTensorV2
from huggingface_hub import hf_hub_download
# Define the model architecture
class MultiTaskDRModel(nn.Module):
def __init__(self, model_name='efficientnet_b3', num_classes=5,
num_lesion_types=5, num_regions=5, pretrained=False):
super(MultiTaskDRModel, self).__init__()
self.backbone = timm.create_model(model_name, pretrained=pretrained, num_classes=0)
self.feature_dim = self.backbone.num_features
self.attention = nn.Sequential(
nn.AdaptiveAvgPool2d(1), nn.Flatten(),
nn.Linear(self.feature_dim, self.feature_dim // 8), nn.ReLU(inplace=True),
nn.Linear(self.feature_dim // 8, self.feature_dim), nn.Sigmoid()
)
self.feature_norm = nn.BatchNorm1d(self.feature_dim)
self.dropout = nn.Dropout(0.4)
self.severity_classifier = nn.Sequential(
nn.Linear(self.feature_dim, self.feature_dim // 2), nn.ReLU(inplace=True),
nn.Dropout(0.2), nn.Linear(self.feature_dim // 2, num_classes)
)
self.lesion_detector = nn.Sequential(
nn.Linear(self.feature_dim, self.feature_dim // 4), nn.ReLU(inplace=True),
nn.Dropout(0.2), nn.Linear(self.feature_dim // 4, num_lesion_types)
)
self.region_predictor = nn.Sequential(
nn.Linear(self.feature_dim, self.feature_dim // 4), nn.ReLU(inplace=True),
nn.Dropout(0.2), nn.Linear(self.feature_dim // 4, num_regions)
)
def forward(self, x):
features = self.backbone.forward_features(x)
pooled_features = F.adaptive_avg_pool2d(features, 1).flatten(1)
attention_weights = self.attention(pooled_features.unsqueeze(-1).unsqueeze(-1))
features = pooled_features * attention_weights
features = self.feature_norm(features)
features = self.dropout(features)
severity_logits = self.severity_classifier(features)
lesion_logits = self.lesion_detector(features)
region_logits = self.region_predictor(features)
return {
'severity': severity_logits,
'lesions': lesion_logits,
'regions': region_logits,
'features': features
}
# Load the model
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = MultiTaskDRModel()
# Download and load the checkpoint
model_path = hf_hub_download(
repo_id="dheeren-tejani/DiabeticRetinpathyClassifier",
filename="best_model_v2.pth"
)
checkpoint = torch.load(model_path, map_location=device, weights_only=False)
model.load_state_dict(checkpoint['model_state_dict'])
model.to(device)
model.eval()
print("Model loaded successfully!")
# Preprocessing function
def preprocess_image(image_path):
transforms = A.Compose([
A.Resize(512, 512),
A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
ToTensorV2(),
])
image = np.array(Image.open(image_path).convert("RGB"))
image_tensor = transforms(image=image)['image'].unsqueeze(0)
return image_tensor
# Example inference
def predict_dr_severity(image_path):
image_tensor = preprocess_image(image_path).to(device)
with torch.no_grad():
outputs = model(image_tensor)
# Get severity prediction
severity_probs = torch.softmax(outputs['severity'], dim=1)
predicted_class = torch.argmax(severity_probs, dim=1).item()
confidence = severity_probs[0, predicted_class].item()
severity_labels = {
0: "No DR",
1: "Mild DR",
2: "Moderate DR",
3: "Severe DR",
4: "Proliferative DR"
}
return {
'predicted_severity': severity_labels[predicted_class],
'confidence': confidence,
'all_probabilities': severity_probs[0].cpu().numpy()
}
# Example usage
# result = predict_dr_severity("path/to/your/fundus_image.jpg")
# print(f"Predicted: {result['predicted_severity']} (Confidence: {result['confidence']:.3f})")
This model (V2) was specifically designed to address the shortcomings of a baseline model (V1) that struggled with severe-stage DR detection:
The model was evaluated on a held-out validation set of 735 images:
| Metric | Score |
|---|---|
| Quadratic Weighted Kappa (QWK) | 0.796 |
| Accuracy | 65.0% |
| F1-Score (Weighted) | 66.3% |
| F1-Score (Macro) | 53.5% |
The V2 model achieved a +3.5% improvement in QWK over the V1 baseline (0.761), indicating it makes "smarter" errors that are more aligned with clinical judgment, despite lower overall accuracy. This trade-off prioritizes clinically relevant performance over naive accuracy.
⚠️ Important Disclaimers:
If you use this model in your research, please cite:
@misc{dheerentejani2025dr,
author = {Dheeren Tejani},
title = {Diabetic Retinopathy Grading Model V2},
year = {2025},
publisher = {Hugging Face},
journal = {Hugging Face Model Hub},
howpublished = {\url{https://huggingface.co/dheeren-tejani/DiabeticRetinpathyClassifier}},
}
This model is released under the Apache 2.0 License.