File size: 4,604 Bytes
957df8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8b021a
 
957df8a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# AI Diabetic Retinopathy Detection

An AI-powered application for detecting diabetic retinopathy (DR) from Optical Coherence Tomography (OCT) images using deep learning and Grad-CAM visualization.

## πŸ₯ What is Diabetic Retinopathy?

Diabetic retinopathy is a diabetes complication that affects the eyes. It's caused by damage to the blood vessels of the light-sensitive tissue at the back of the eye (retina). Early detection is crucial for preventing vision loss.

## πŸš€ Features

- **AI Classification**: Uses a pre-trained ResNet-50 model to classify OCT images as DR (Diabetic Retinopathy) or NoDR (No Diabetic Retinopathy)
- **Grad-CAM Visualization**: Shows which areas of the image the AI focuses on for diagnosis
- **Confidence Scoring**: Provides probability scores for predictions
- **Image Storage**: Automatically saves analyzed images with timestamps
- **Web Interface**: User-friendly Gradio web interface

## πŸ“‹ Requirements

- Python 3.8 or higher
- PyTorch 2.0+
- CUDA-compatible GPU (optional, for faster inference)

## πŸ› οΈ Installation

1. **Clone or download this repository**
   ```bash
   git clone <repository-url>
   cd Deep_Learning_for_Ophthalmologist
   ```

2. **Create a virtual environment (recommended)**
   ```bash
   python -m venv venv
   
   # On Windows:
   venv\Scripts\activate
   
   # On macOS/Linux:
   source venv/bin/activate
   ```

3. **Install dependencies**
   ```bash
   pip install -r requirements.txt
   ```

## 🎯 Usage

1. **Start the application**
   ```bash
   python app.py
   ```

2. **Open your web browser** and navigate to the URL shown in the terminal (usually `http://127.0.0.1:7860`)

3. **Upload an OCT image** by clicking the upload area or dragging and dropping an image file

4. **View results**:
   - The AI will analyze the image and show the classification result
   - A Grad-CAM heatmap will highlight areas of interest
   - The prediction and confidence score will be displayed
   - The analyzed image will be automatically saved to the `saved_predictions` folder

## πŸ“ File Structure

```
Deep_Learning_for_Ophthalmologist/
β”œβ”€β”€ app.py                          # Main application file
β”œβ”€β”€ requirements.txt                # Python dependencies
β”œβ”€β”€ resnet50_dr_classifier.pth     # Pre-trained model weights
β”œβ”€β”€ README.md                      # This file
└── saved_predictions/             # Folder for saved analyzed images
```

## πŸ”¬ How It Works

1. **Image Preprocessing**: OCT images are resized to 224x224 pixels and normalized
2. **AI Analysis**: A ResNet-50 model processes the image to classify DR vs NoDR
3. **Grad-CAM**: Generates a heatmap showing which image regions influenced the AI's decision
4. **Results**: Displays classification, confidence score, and visual heatmap

## πŸ“Š Model Information

- **Architecture**: ResNet-50 with modified final layer for binary classification
- **Training**: Pre-trained on OCT image dataset
- **Classes**: 2 (DR - Diabetic Retinopathy, NoDR - No Diabetic Retinopathy)
- **Input**: 224x224 RGB images
- **Output**: Binary classification with confidence scores

## ⚠️ Important Notes

- **Medical Disclaimer**: This tool is for research and educational purposes only. It should not be used for actual medical diagnosis without proper validation and clinical oversight.
- **Image Quality**: For best results, use high-quality OCT images with good contrast and resolution
- **Model Limitations**: The model's accuracy depends on the quality and characteristics of the training data

## πŸ› Troubleshooting

- **CUDA errors**: The app runs on CPU by default. If you have GPU issues, ensure PyTorch is installed correctly
- **Memory issues**: Large images may cause memory problems. The app automatically resizes images to 224x224
- **Model loading errors**: Ensure `resnet50_dr_classifier.pth` is in the same directory as `app.py`

## 🀝 Contributing

Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.

## πŸ“„ License

This project is for educational and research purposes. Please ensure compliance with relevant regulations when using medical imaging data.

## πŸ”— References

- [PyTorch](https://pytorch.org/)
- [Gradio](https://gradio.app/)
- [Grad-CAM](https://github.com/jacobgil/pytorch-grad-cam)
- [ResNet Paper](https://arxiv.org/abs/1512.03385)

---

**Note**: This application is designed for research and educational purposes in ophthalmology and medical AI. Always consult with qualified healthcare professionals for actual medical diagnosis and treatment decisions.