Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,60 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
-
from keras import
|
5 |
-
from tensorflow.keras.preprocessing
|
6 |
-
from
|
7 |
|
8 |
-
#
|
|
|
|
|
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 |
-
|
|
|
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 |
-
|
|
|
18 |
|
19 |
def f1(y_true, y_pred):
|
20 |
-
|
21 |
-
|
22 |
-
return 2
|
23 |
|
24 |
-
#
|
25 |
-
model
|
26 |
-
|
27 |
-
|
28 |
-
)
|
29 |
|
30 |
-
#
|
|
|
|
|
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)
|
35 |
-
img_array = np.expand_dims(img_array, axis=0)
|
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()
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.preprocessing import image
|
6 |
+
from tensorflow.keras import backend as K
|
7 |
|
8 |
+
# ---------------------------
|
9 |
+
# Custom metrics definitions
|
10 |
+
# ---------------------------
|
11 |
def recall(y_true, y_pred):
|
12 |
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
|
13 |
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
|
14 |
+
recall = true_positives / (possible_positives + K.epsilon())
|
15 |
+
return recall
|
16 |
|
17 |
def precision(y_true, y_pred):
|
18 |
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
|
19 |
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
|
20 |
+
precision = true_positives / (predicted_positives + K.epsilon())
|
21 |
+
return precision
|
22 |
|
23 |
def f1(y_true, y_pred):
|
24 |
+
precision1 = precision(y_true, y_pred)
|
25 |
+
recall1 = recall(y_true, y_pred)
|
26 |
+
return 2*((precision1*recall1)/(precision1+recall1+K.epsilon()))
|
27 |
|
28 |
+
# ---------------------------
|
29 |
+
# Load model with custom objects
|
30 |
+
# ---------------------------
|
31 |
+
model = load_model("model.h5", custom_objects={'f1': f1, 'precision': precision, 'recall': recall})
|
|
|
32 |
|
33 |
+
# ---------------------------
|
34 |
+
# Prediction function
|
35 |
+
# ---------------------------
|
36 |
def predict_pneumonia(img):
|
37 |
img = img.convert('L') # Convert to grayscale
|
38 |
img = img.resize((299, 299))
|
39 |
+
img_array = image.img_to_array(img)
|
40 |
+
img_array = np.expand_dims(img_array, axis=0) / 255.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
prediction = model.predict(img_array)
|
43 |
+
label = "PNEUMONIA" if prediction[0][0] > 0.5 else "NORMAL"
|
44 |
+
confidence = prediction[0][0] if prediction[0][0] > 0.5 else 1 - prediction[0][0]
|
45 |
+
|
46 |
+
return f"Prediction: {label} ({confidence*100:.2f}%)"
|
47 |
+
|
48 |
+
# ---------------------------
|
49 |
+
# Gradio interface
|
50 |
+
# ---------------------------
|
51 |
+
interface = gr.Interface(fn=predict_pneumonia,
|
52 |
+
inputs=gr.Image(type="pil"),
|
53 |
+
outputs="text",
|
54 |
+
title="Pneumonia Detection Model",
|
55 |
+
description="Upload a chest X-ray image to predict whether the patient has Pneumonia or not.")
|
56 |
+
|
57 |
+
# ---------------------------
|
58 |
+
# Launch
|
59 |
+
# ---------------------------
|
60 |
interface.launch()
|