Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,50 @@
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
import tensorflow as tf
|
4 |
-
|
5 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
'recall': recall
|
12 |
-
})
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def predict_pneumonia(img):
|
16 |
-
img = img.convert('L') #
|
17 |
img = img.resize((299, 299))
|
18 |
-
img_array =
|
19 |
-
img_array = img_array
|
20 |
-
img_array = np.expand_dims(img_array, axis=0) # batch dimension
|
21 |
prediction = model.predict(img_array)[0][0]
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
outputs="text",
|
32 |
-
title="Pneumonia Detection from Chest X-rays",
|
33 |
-
description="Upload a chest X-ray image to detect Pneumonia using a CNN model.")
|
34 |
|
35 |
-
|
36 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from keras import backend as K
|
5 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# --- Custom Metrics ---
|
9 |
+
def recall(y_true, y_pred):
|
10 |
+
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
|
11 |
+
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
|
12 |
+
return true_positives / (possible_positives + K.epsilon())
|
13 |
|
14 |
+
def precision(y_true, y_pred):
|
15 |
+
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
|
16 |
+
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
|
17 |
+
return true_positives / (predicted_positives + K.epsilon())
|
|
|
|
|
18 |
|
19 |
+
def f1(y_true, y_pred):
|
20 |
+
prec = precision(y_true, y_pred)
|
21 |
+
rec = recall(y_true, y_pred)
|
22 |
+
return 2 * ((prec * rec) / (prec + rec + K.epsilon()))
|
23 |
+
|
24 |
+
# --- Load the Model ---
|
25 |
+
model = tf.keras.models.load_model(
|
26 |
+
"pneumonia_cnn_model.h5",
|
27 |
+
custom_objects={'precision': precision, 'recall': recall, 'f1': f1}
|
28 |
+
)
|
29 |
+
|
30 |
+
# --- Prediction Function ---
|
31 |
def predict_pneumonia(img):
|
32 |
+
img = img.convert('L') # Convert to grayscale
|
33 |
img = img.resize((299, 299))
|
34 |
+
img_array = img_to_array(img) / 255.0
|
35 |
+
img_array = np.expand_dims(img_array, axis=0) # Shape: (1, 299, 299, 1)
|
|
|
36 |
prediction = model.predict(img_array)[0][0]
|
37 |
+
label = "PNEUMONIA" if prediction >= 0.5 else "NORMAL"
|
38 |
+
confidence = prediction if prediction >= 0.5 else 1 - prediction
|
39 |
+
return f"{label} ({confidence * 100:.2f}% confidence)"
|
40 |
|
41 |
+
# --- Gradio Interface ---
|
42 |
+
interface = gr.Interface(
|
43 |
+
fn=predict_pneumonia,
|
44 |
+
inputs=gr.Image(type="pil"),
|
45 |
+
outputs=gr.Text(),
|
46 |
+
title="Pneumonia Detection from Chest X-ray",
|
47 |
+
description="Upload a chest X-ray image to detect Pneumonia using a CNN model trained on grayscale 299x299 images."
|
48 |
+
)
|
|
|
|
|
|
|
49 |
|
50 |
+
interface.launch()
|
|