jdelgado2002
commited on
Commit
•
d9ead92
1
Parent(s):
c65f442
Update README.md
Browse files
README.md
CHANGED
@@ -60,4 +60,96 @@ Training accuracy - trained for 50 epochs, reaching 83% accuracy within our tra
|
|
60 |
![confusion matrix](https://drive.google.com/file/d/1lI7pps03RXTFKYjY_iv4UPeSOhqQhxQB/view)
|
61 |
|
62 |
We submitted our model for validation to the [APTOS 2019 Blindness Detection Competition](https://www.kaggle.com/competitions/aptos2019-blindness-detection/submissions#),
|
63 |
-
achieving a private score of 0.869345
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
![confusion matrix](https://drive.google.com/file/d/1lI7pps03RXTFKYjY_iv4UPeSOhqQhxQB/view)
|
61 |
|
62 |
We submitted our model for validation to the [APTOS 2019 Blindness Detection Competition](https://www.kaggle.com/competitions/aptos2019-blindness-detection/submissions#),
|
63 |
+
achieving a private score of 0.869345
|
64 |
+
|
65 |
+
## Trying the model
|
66 |
+
|
67 |
+
Note: You can easily try our model [here](https://www.kaggle.com/code/josemauriciodelgado/proliferative-retinopathy)
|
68 |
+
|
69 |
+
This application uses a trained model to detect the severity of diabetic retinopathy from a given retina image taken using fundus photography. The severity levels are:
|
70 |
+
|
71 |
+
- 0 - No DR
|
72 |
+
- 1 - Mild
|
73 |
+
- 2 - Moderate
|
74 |
+
- 3 - Severe
|
75 |
+
- 4 - Proliferative DR
|
76 |
+
|
77 |
+
### How to Use the Model
|
78 |
+
|
79 |
+
To use the model, you need to provide an image of the retina taken using fundus photography. The model will then predict the severity of diabetic retinopathy and return a dictionary where the keys are the severity levels and the values are the corresponding probabilities.
|
80 |
+
|
81 |
+
### Breakdown of the `app.py` File
|
82 |
+
|
83 |
+
Here's a breakdown of what the `app.py` file is doing:
|
84 |
+
|
85 |
+
1. **Import necessary libraries**: The file starts by importing the necessary libraries. This includes `gradio` for creating the UI, `fastai.vision.all` for loading the trained model, and `skimage` for image processing.
|
86 |
+
|
87 |
+
2. **Define helper functions**: The `get_x` and `get_y` functions are defined. These functions are used to get the x and y values from the input dictionary. In this case, the x value is the image and the y value is the diagnosis.
|
88 |
+
|
89 |
+
3. **Load the trained model**: The trained model is loaded from the `model.pkl` file using the `load_learner` function from `fastai`.
|
90 |
+
|
91 |
+
4. **Define label descriptions**: A dictionary is defined to map label numbers to descriptions. This is used to return descriptions instead of numbers in the prediction result.
|
92 |
+
|
93 |
+
5. **Define the prediction function**: The `predict` function is defined. This function takes an image as input, makes a prediction using the trained model, and returns a dictionary where the keys are the severity levels and the values are the corresponding probabilities.
|
94 |
+
|
95 |
+
6. **Define title and description**: The title and description of the application are defined. These will be displayed in the Gradio UI.
|
96 |
+
|
97 |
+
To run the application, you need to create a Gradio interface with the `predict` function as the prediction function, an image as the input, and a label as the output. You can then launch the interface to start the application.
|
98 |
+
|
99 |
+
```import gradio as gr
|
100 |
+
from fastai.vision.all import *
|
101 |
+
import skimage
|
102 |
+
|
103 |
+
# Define the functions to get the x and y values from the input dictionary - in this case, the x value is the image and the y value is the diagnosis
|
104 |
+
# needed to load the model since we defined them during training
|
105 |
+
def get_x(r): return ""
|
106 |
+
|
107 |
+
def get_y(r): return r['diagnosis']
|
108 |
+
|
109 |
+
learn = load_learner('model.pkl')
|
110 |
+
labels = learn.dls.vocab
|
111 |
+
|
112 |
+
# Define the mapping from label numbers to descriptions
|
113 |
+
label_descriptions = {
|
114 |
+
0: "No DR",
|
115 |
+
1: "Mild",
|
116 |
+
2: "Moderate",
|
117 |
+
3: "Severe",
|
118 |
+
4: "Proliferative DR"
|
119 |
+
}
|
120 |
+
|
121 |
+
def predict(img):
|
122 |
+
img = PILImage.create(img)
|
123 |
+
pred, pred_idx, probs = learn.predict(img)
|
124 |
+
# Use the label_descriptions dictionary to return descriptions instead of numbers
|
125 |
+
return {label_descriptions[labels[i]]: float(probs[i]) for i in range(len(labels))}
|
126 |
+
|
127 |
+
title = "Diabetic Retinopathy Detection"
|
128 |
+
description = """Detects severity of diabetic retinopathy from a given retina image taken using fundus photography -
|
129 |
+
|
130 |
+
0 - No DR
|
131 |
+
|
132 |
+
1 - Mild
|
133 |
+
|
134 |
+
2 - Moderate
|
135 |
+
|
136 |
+
3 - Severe
|
137 |
+
|
138 |
+
4 - Proliferative DR
|
139 |
+
"""
|
140 |
+
article = "<p style='text-align: center'><a href='https://www.kaggle.com/code/josemauriciodelgado/proliferative-retinopathy' target='_blank'>Notebook</a></p>"
|
141 |
+
|
142 |
+
# Get a list of all image paths in the test folder
|
143 |
+
test_folder = "test" # replace with the actual path to your test folder
|
144 |
+
image_paths = [os.path.join(test_folder, img) for img in os.listdir(test_folder) if img.endswith(('.png', '.jpg', '.jpeg'))]
|
145 |
+
|
146 |
+
gr.Interface(
|
147 |
+
fn=predict,
|
148 |
+
inputs=gr.Image(),
|
149 |
+
outputs=gr.Label(num_top_classes=5),
|
150 |
+
examples=image_paths, # set the examples parameter to the list of image paths
|
151 |
+
article=article,
|
152 |
+
title=title,
|
153 |
+
description=description,
|
154 |
+
).launch()
|
155 |
+
```
|