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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision.models as models
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Define class labels for Diabetic Retinopathy
|
| 8 |
+
class_labels = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative DR']
|
| 9 |
+
|
| 10 |
+
# Define image preprocessing function
|
| 11 |
+
transform = transforms.Compose([
|
| 12 |
+
transforms.Resize((224, 224)),
|
| 13 |
+
transforms.ToTensor(),
|
| 14 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
|
| 15 |
+
])
|
| 16 |
+
|
| 17 |
+
def load_model():
|
| 18 |
+
model = models.vit_b_16(weights=None)
|
| 19 |
+
if isinstance(model.heads, torch.nn.Sequential):
|
| 20 |
+
in_features = model.heads[0].in_features
|
| 21 |
+
else:
|
| 22 |
+
in_features = model.heads.in_features
|
| 23 |
+
model.heads = torch.nn.Linear(in_features, len(class_labels))
|
| 24 |
+
state_dict = torch.load("best_model.pt", map_location=torch.device('cpu'))
|
| 25 |
+
model.load_state_dict(state_dict)
|
| 26 |
+
model.eval()
|
| 27 |
+
return model
|
| 28 |
+
|
| 29 |
+
model = load_model()
|
| 30 |
+
|
| 31 |
+
def predict_retinopathy(image):
|
| 32 |
+
if image is None:
|
| 33 |
+
return "Please upload an image for analysis."
|
| 34 |
+
|
| 35 |
+
image = transform(image).unsqueeze(0)
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
output = model(image)
|
| 38 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 39 |
+
prediction = torch.argmax(probabilities).item()
|
| 40 |
+
|
| 41 |
+
result_text = "<div style='text-align: center; padding: 20px; background-color: #46a7f7; border-radius: 10px; margin: 20px 0;'>"
|
| 42 |
+
result_text += "<h2>👁️ Analysis Results</h2>"
|
| 43 |
+
result_text += f"<h3>Primary Classification: {class_labels[prediction]}</h3>"
|
| 44 |
+
result_text += "</div>"
|
| 45 |
+
|
| 46 |
+
result_text += "<table style='width: 100%; border-collapse: collapse; margin: 20px 0;'>"
|
| 47 |
+
result_text += "<tr><th style='padding: 10px; text-align: left; background-color: #46a7f7;'>Classification</th>"
|
| 48 |
+
result_text += "<th style='padding: 10px; text-align: right; background-color: #46a7f7;'>Confidence</th></tr>"
|
| 49 |
+
|
| 50 |
+
for i, label in enumerate(class_labels):
|
| 51 |
+
prob = probabilities[i] * 100
|
| 52 |
+
color = f"hsl({120 - prob}, 70%, 50%)"
|
| 53 |
+
result_text += f"<tr style='border-bottom: 1px solid #dee2e6;'>"
|
| 54 |
+
result_text += f"<td style='padding: 10px;'>{label}</td>"
|
| 55 |
+
result_text += f"<td style='padding: 10px; text-align: right;'>"
|
| 56 |
+
result_text += f"<span style='color: {color}; font-weight: bold;'>{prob:.2f}%</span></td></tr>"
|
| 57 |
+
|
| 58 |
+
result_text += "</table>"
|
| 59 |
+
return result_text
|
| 60 |
+
|
| 61 |
+
# Custom CSS for enhanced styling
|
| 62 |
+
custom_css = """
|
| 63 |
+
footer {display: none !important;}
|
| 64 |
+
.container {max-width: 1000px; margin: auto; padding: 20px;}
|
| 65 |
+
.header {text-align: center; margin-bottom: 40px;}
|
| 66 |
+
.main-title {color: #2c3e50; font-size: 2.5em; margin-bottom: 20px;}
|
| 67 |
+
.subtitle {color: #34495e; font-size: 1.2em; line-height: 1.6;}
|
| 68 |
+
.input-section {background: #f8f9fa; padding: 30px; border-radius: 15px; margin: 20px 0;}
|
| 69 |
+
.disclaimer {background: #fff3cd; color: #856404; padding: 15px; border-radius: 8px; margin-top: 30px;}
|
| 70 |
+
.info-box {background: #e3f2fd; padding: 20px; border-radius: 10px; margin: 15px 0;}
|
| 71 |
+
.warning-box {background: #ffebee; padding: 20px; border-radius: 10px; margin: 15px 0;}
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
# Create Gradio UI with enhanced styling
|
| 75 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 76 |
+
# Header Section
|
| 77 |
+
gr.HTML("""
|
| 78 |
+
<div class="header">
|
| 79 |
+
<h1 class="main-title">👁️ Welcome to EarlyMed Diabetic Retinopathy Scanner</h1>
|
| 80 |
+
</div>
|
| 81 |
+
""")
|
| 82 |
+
|
| 83 |
+
# Introduction
|
| 84 |
+
gr.Markdown("""
|
| 85 |
+
Welcome to EarlyMed—an initiative by our team at VIT-AP University dedicated to empowering you with early health insights.
|
| 86 |
+
Leveraging AI for early detection, our mission is simple: "Early Detection, Smarter Decision."
|
| 87 |
+
Our Diabetic Retinopathy detection project is one of our key efforts to help you stay informed before visiting a doctor.
|
| 88 |
+
""")
|
| 89 |
+
|
| 90 |
+
# What is Diabetic Retinopathy Section
|
| 91 |
+
gr.Markdown("""
|
| 92 |
+
## 🔍 Understanding Diabetic Retinopathy (DR)
|
| 93 |
+
|
| 94 |
+
Diabetic Retinopathy is a diabetes complication that affects the eyes. It's caused by damage to the blood vessels
|
| 95 |
+
in the tissue at the back of the eye (retina). Poorly controlled blood sugar is a risk factor.
|
| 96 |
+
Early detection can prevent vision loss.
|
| 97 |
+
|
| 98 |
+
### Primary Symptoms of DR:
|
| 99 |
+
|
| 100 |
+
* **Blurred or fluctuating vision**
|
| 101 |
+
* **Impaired color vision**
|
| 102 |
+
* **Dark or empty areas in your vision**
|
| 103 |
+
* **Floaters (spots or dark strings floating in your vision)**
|
| 104 |
+
* **Vision loss**
|
| 105 |
+
* **Difficulty seeing at night**
|
| 106 |
+
|
| 107 |
+
### Risk Factors:
|
| 108 |
+
|
| 109 |
+
* Duration of diabetes
|
| 110 |
+
* Poor control of blood sugar level
|
| 111 |
+
* High blood pressure
|
| 112 |
+
* High cholesterol
|
| 113 |
+
* Pregnancy
|
| 114 |
+
* Tobacco use
|
| 115 |
+
* Being of Hispanic, Native American, or African heritage
|
| 116 |
+
""")
|
| 117 |
+
|
| 118 |
+
# Image Upload Section
|
| 119 |
+
with gr.Row():
|
| 120 |
+
with gr.Column():
|
| 121 |
+
gr.Markdown("### 📤 Upload Retinal Image")
|
| 122 |
+
image_input = gr.Image(
|
| 123 |
+
type="pil",
|
| 124 |
+
label="Upload a Fundus (Retinal) Image",
|
| 125 |
+
elem_classes="input-section"
|
| 126 |
+
)
|
| 127 |
+
submit_btn = gr.Button(
|
| 128 |
+
"🔍 Analyze Image",
|
| 129 |
+
variant="primary",
|
| 130 |
+
size="lg"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# Results Section
|
| 134 |
+
diagnosis_output = gr.HTML()
|
| 135 |
+
|
| 136 |
+
submit_btn.click(
|
| 137 |
+
fn=predict_retinopathy,
|
| 138 |
+
inputs=image_input,
|
| 139 |
+
outputs=diagnosis_output
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Understanding Stages
|
| 143 |
+
gr.Markdown("""
|
| 144 |
+
## 📊 Understanding DR Stages and Required Actions
|
| 145 |
+
|
| 146 |
+
### No DR Stage
|
| 147 |
+
**What it means:**
|
| 148 |
+
* No visible signs of diabetic retinopathy
|
| 149 |
+
* Normal retinal appearance
|
| 150 |
+
* Blood vessels appear healthy
|
| 151 |
+
|
| 152 |
+
**Required Actions:**
|
| 153 |
+
* Continue regular eye check-ups (yearly for diabetic patients)
|
| 154 |
+
* Maintain blood sugar control
|
| 155 |
+
* Follow healthy lifestyle recommendations
|
| 156 |
+
|
| 157 |
+
### Mild Stage
|
| 158 |
+
**What it means:**
|
| 159 |
+
* Small areas of balloon-like swelling in the retina's tiny blood vessels
|
| 160 |
+
* Microaneurysms detected
|
| 161 |
+
* Early changes that don't typically affect vision
|
| 162 |
+
|
| 163 |
+
**Required Actions:**
|
| 164 |
+
* Schedule follow-up with an ophthalmologist within 6-12 months
|
| 165 |
+
* Improve blood sugar control
|
| 166 |
+
* Monitor blood pressure and cholesterol
|
| 167 |
+
* Consider more frequent eye screenings
|
| 168 |
+
|
| 169 |
+
### Moderate Stage
|
| 170 |
+
**What it means:**
|
| 171 |
+
* More extensive damage to blood vessels
|
| 172 |
+
* Blood vessels may begin to leak
|
| 173 |
+
* Some retinal swelling might be present
|
| 174 |
+
|
| 175 |
+
**Required Actions:**
|
| 176 |
+
* Consult with an ophthalmologist within 3-6 months
|
| 177 |
+
* Possible referral to a retina specialist
|
| 178 |
+
* Strict blood sugar, blood pressure, and cholesterol management
|
| 179 |
+
* Discuss potential early interventions
|
| 180 |
+
|
| 181 |
+
### Severe Stage
|
| 182 |
+
**What it means:**
|
| 183 |
+
* Significant blockage of blood vessels
|
| 184 |
+
* Retina being deprived of proper blood supply
|
| 185 |
+
* Increased risk for vision-threatening complications
|
| 186 |
+
|
| 187 |
+
**Required Actions:**
|
| 188 |
+
* Urgent consultation with a retina specialist
|
| 189 |
+
* Possible laser treatment consideration
|
| 190 |
+
* Intensive monitoring of diabetes management
|
| 191 |
+
* Prepare for possible treatments
|
| 192 |
+
|
| 193 |
+
### Proliferative DR Stage
|
| 194 |
+
**What it means:**
|
| 195 |
+
* Advanced disease with new abnormal blood vessels growing
|
| 196 |
+
* High risk of vitreous hemorrhage
|
| 197 |
+
* Potential retinal detachment
|
| 198 |
+
* Significant threat to vision
|
| 199 |
+
|
| 200 |
+
**Required Actions:**
|
| 201 |
+
* Immediate specialist intervention
|
| 202 |
+
* Likely need for laser treatment or surgery
|
| 203 |
+
* Aggressive management of all contributing factors
|
| 204 |
+
* Regular monitoring post-treatment
|
| 205 |
+
""")
|
| 206 |
+
|
| 207 |
+
# Technical Details
|
| 208 |
+
gr.Markdown("""
|
| 209 |
+
## 🔬 How Our AI Works
|
| 210 |
+
|
| 211 |
+
* **Image Processing:** Fundus (retinal) images are resized and normalized to match the model's input specifications.
|
| 212 |
+
* **Feature Extraction:** Our advanced Vision Transformer scans the image to detect subtle changes in blood vessels, microaneurysms, exudates, and other retinal features.
|
| 213 |
+
* **Classification:** Based on the extracted features, the AI classifies the image into one of five categories:
|
| 214 |
+
* **No DR**
|
| 215 |
+
* **Mild**
|
| 216 |
+
* **Moderate**
|
| 217 |
+
* **Severe**
|
| 218 |
+
* **Proliferative DR**
|
| 219 |
+
|
| 220 |
+
## 🎯 Why It Is Reliable
|
| 221 |
+
|
| 222 |
+
* **Robust Training:** Trained on thousands of diverse retinal images, our model has learned to recognize even the faintest indicators of DR.
|
| 223 |
+
* **Rigorous Validation:** Extensive testing and validation protocols ensure the model's predictions are accurate and trustworthy.
|
| 224 |
+
* **Continuous Improvement:** Regular updates and refinements keep the model up-to-date with the latest ophthalmological insights and technological advancements.
|
| 225 |
+
* **Cutting-Edge Technology:** Utilizing state-of-the-art Vision Transformer architecture, our AI leverages modern deep learning techniques to provide early and reliable health insights.
|
| 226 |
+
""")
|
| 227 |
+
|
| 228 |
+
# Treatment and Support Information
|
| 229 |
+
gr.Markdown("""
|
| 230 |
+
## 💉 Treatment Approaches
|
| 231 |
+
|
| 232 |
+
### Standard Treatment Options:
|
| 233 |
+
* **Laser Treatment (Photocoagulation):** Seals or destroys leaking blood vessels
|
| 234 |
+
* **Anti-VEGF Injections:** Reduces growth of abnormal blood vessels
|
| 235 |
+
* **Vitrectomy:** Surgical removal of vitreous gel and blood
|
| 236 |
+
* **Corticosteroid Injections:** Reduces inflammation and swelling
|
| 237 |
+
|
| 238 |
+
### Supportive Care:
|
| 239 |
+
* Blood sugar control
|
| 240 |
+
* Blood pressure management
|
| 241 |
+
* Regular eye examinations
|
| 242 |
+
* Vision rehabilitation if needed
|
| 243 |
+
* Psychological support
|
| 244 |
+
|
| 245 |
+
### Long-term Considerations:
|
| 246 |
+
* Lifestyle modifications
|
| 247 |
+
* Regular monitoring of diabetes
|
| 248 |
+
* Adherence to medication regimens
|
| 249 |
+
* Regular ophthalmologic follow-ups
|
| 250 |
+
""")
|
| 251 |
+
|
| 252 |
+
# Prevention and Early Detection
|
| 253 |
+
gr.Markdown("""
|
| 254 |
+
## 🛡️ Prevention and Early Detection
|
| 255 |
+
|
| 256 |
+
### Risk Reduction Strategies:
|
| 257 |
+
* Maintaining optimal blood sugar levels
|
| 258 |
+
* Controlling blood pressure
|
| 259 |
+
* Managing cholesterol levels
|
| 260 |
+
* Regular exercise
|
| 261 |
+
* Healthy diet low in refined carbohydrates
|
| 262 |
+
* Avoiding tobacco use
|
| 263 |
+
|
| 264 |
+
### When to Seek Medical Attention:
|
| 265 |
+
* Sudden vision changes
|
| 266 |
+
* Eye pain or redness
|
| 267 |
+
* Floaters or spots in vision
|
| 268 |
+
* Blurred vision
|
| 269 |
+
* Any noticeable change in visual acuity
|
| 270 |
+
""")
|
| 271 |
+
# Disclaimer
|
| 272 |
+
gr.Markdown("""
|
| 273 |
+
---
|
| 274 |
+
### ⚠️ Important Medical Disclaimer
|
| 275 |
+
|
| 276 |
+
This AI-powered tool is designed to assist in the early detection of Diabetic Retinopathy (DR) through
|
| 277 |
+
retinal image analysis. We strongly urge users to consult an ophthalmologist for appropriate medical
|
| 278 |
+
guidance after getting the diagnosis.
|
| 279 |
+
|
| 280 |
+
**Please Note:**
|
| 281 |
+
* Results should be verified by healthcare professionals
|
| 282 |
+
* Consult a qualified ophthalmologist for proper treatment
|
| 283 |
+
|
| 284 |
+
This initiative is developed by our team at VIT-AP University with the goal of empowering individuals to be more
|
| 285 |
+
aware of their eye health before visiting a doctor. Our mission is to leverage AI for early detection and better
|
| 286 |
+
healthcare awareness.
|
| 287 |
+
""")
|
| 288 |
+
|
| 289 |
+
if __name__ == "__main__":
|
| 290 |
+
demo.launch()
|
best_model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d424673cafa6140356e6102e60c037107f7d7a6c64e34e44bf7824f5eea900ba
|
| 3 |
+
size 94386110
|
requirements.txt
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Acute Lymphoblastic Leukemia (ALL) Classifier - Requirements
|
| 2 |
+
# Usage: pip install -r requirements.txt
|
| 3 |
+
|
| 4 |
+
# Base ----------------------------------------
|
| 5 |
+
torch>=2.0.0 # For loading and running the ViT-based PyTorch model
|
| 6 |
+
torchvision>=0.15.0 # For image transformations and model utilities
|
| 7 |
+
gradio>=4.0.0 # For building the interactive UI
|
| 8 |
+
Pillow>=8.0.0 # For handling image input and resizing
|
| 9 |
+
|
| 10 |
+
# Logging -------------------------------------
|
| 11 |
+
# tensorboard>=2.4.1 # Uncomment if training visualization is needed
|
| 12 |
+
|
| 13 |
+
# Plotting (For Notebook, Not Needed for Deployment) ----------------------------
|
| 14 |
+
# matplotlib>=3.2.2 # For plotting results in training
|
| 15 |
+
# pandas>=1.1.4 # For handling tabular data in training analysis
|
| 16 |
+
# seaborn>=0.11.0 # For enhanced plots (optional)
|
| 17 |
+
|
| 18 |
+
# Export (Not Needed for Gradio Deployment) --------------------------------------
|
| 19 |
+
# torchscript # If exporting the model to TorchScript
|
| 20 |
+
|
| 21 |
+
# Extras --------------------------------------
|
| 22 |
+
ipython # For interactive notebook executions
|
| 23 |
+
psutil # For system utilization monitoring
|