Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Make sure to decorate the custom metric class with @register_keras_serializable
|
| 7 |
+
@tf.keras.utils.register_keras_serializable()
|
| 8 |
+
class F1Score(tf.keras.metrics.Metric):
|
| 9 |
+
def __init__(self, name='f1_score', **kwargs):
|
| 10 |
+
super().__init__(name=name, **kwargs)
|
| 11 |
+
self.precision = tf.keras.metrics.Precision()
|
| 12 |
+
self.recall = tf.keras.metrics.Recall()
|
| 13 |
+
|
| 14 |
+
def update_state(self, y_true, y_pred, sample_weight=None):
|
| 15 |
+
y_pred = tf.round(y_pred)
|
| 16 |
+
self.precision.update_state(y_true, y_pred, sample_weight)
|
| 17 |
+
self.recall.update_state(y_true, y_pred, sample_weight)
|
| 18 |
+
|
| 19 |
+
def result(self):
|
| 20 |
+
p = self.precision.result()
|
| 21 |
+
r = self.recall.result()
|
| 22 |
+
return 2 * ((p * r) / (p + r + tf.keras.backend.epsilon()))
|
| 23 |
+
|
| 24 |
+
def reset_states(self):
|
| 25 |
+
self.precision.reset_states()
|
| 26 |
+
self.recall.reset_states()
|
| 27 |
+
|
| 28 |
+
# Load your TensorFlow model
|
| 29 |
+
model_path = './ACC0.9322_valACC0.9148_loss0.3592_Epoch83.keras'
|
| 30 |
+
model = tf.keras.models.load_model(model_path, custom_objects={'F1Score': F1Score})
|
| 31 |
+
|
| 32 |
+
def predict(image):
|
| 33 |
+
# Preprocess the image to match the input shape of the model
|
| 34 |
+
img = Image.fromarray(image.astype('uint8'), 'RGB')
|
| 35 |
+
img = img.resize((224, 224)) # Update the size if different
|
| 36 |
+
img_array = np.array(img)
|
| 37 |
+
img_array = img_array / 255.0 # Normalize if your model expects normalization
|
| 38 |
+
img_array = img_array[np.newaxis, ...] # Model expects a batch dimension
|
| 39 |
+
|
| 40 |
+
# Make prediction
|
| 41 |
+
prediction = model.predict(img_array)
|
| 42 |
+
# Calculate prediction probability
|
| 43 |
+
prediction_probability = float(prediction[0][0])
|
| 44 |
+
|
| 45 |
+
# Check if prediction is 'Normal' or 'Pneumonia'
|
| 46 |
+
if prediction_probability <= 0.5:
|
| 47 |
+
# If 'Normal', add 70% to its probability but cap it at 1.0 (100%)
|
| 48 |
+
adjusted_probability = min(prediction_probability + 0.5, 1.0)
|
| 49 |
+
pred_class = 'Normal'
|
| 50 |
+
else:
|
| 51 |
+
# If 'Pneumonia', use the original probability
|
| 52 |
+
adjusted_probability = prediction_probability
|
| 53 |
+
pred_class = 'Pneumonia'
|
| 54 |
+
|
| 55 |
+
# Return the prediction class and the (adjusted) probability
|
| 56 |
+
return {pred_class: adjusted_probability}
|
| 57 |
+
|
| 58 |
+
iface = gr.Interface(fn=predict,
|
| 59 |
+
inputs=gr.components.Image(image_mode='RGB', label="Upload X-ray Image"),
|
| 60 |
+
outputs=gr.components.Label(num_top_classes=2),
|
| 61 |
+
title="Pneumonia Detection",
|
| 62 |
+
description="Upload an X-ray image to detect pneumonia.")
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
iface.launch(share=True)
|