Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from tensorflow import keras
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2
|
| 6 |
+
|
| 7 |
+
# Load model
|
| 8 |
+
model = keras.models.load_model('my_model (2).h5')
|
| 9 |
+
|
| 10 |
+
# Class labels
|
| 11 |
+
CLASS_NAMES = ['Necrotic Tumor', 'Non-Tumor', 'Viable Tumor']
|
| 12 |
+
|
| 13 |
+
def make_gradcam_heatmap(img_array, model, last_conv_layer_name="last_conv_layer"):
|
| 14 |
+
grad_model = keras.models.Model(
|
| 15 |
+
model.inputs,
|
| 16 |
+
[model.get_layer(last_conv_layer_name).output, model.output]
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
with keras.backend.GradientTape() as tape:
|
| 20 |
+
conv_outputs, predictions = grad_model(img_array)
|
| 21 |
+
pred_index = np.argmax(predictions[0])
|
| 22 |
+
class_channel = predictions[:, pred_index]
|
| 23 |
+
|
| 24 |
+
grads = tape.gradient(class_channel, conv_outputs)
|
| 25 |
+
pooled_grads = np.mean(grads, axis=(0, 1, 2))
|
| 26 |
+
|
| 27 |
+
conv_outputs = conv_outputs[0]
|
| 28 |
+
heatmap = conv_outputs @ pooled_grads[..., np.newaxis]
|
| 29 |
+
heatmap = np.squeeze(heatmap)
|
| 30 |
+
heatmap = np.maximum(heatmap, 0) / (np.max(heatmap) + 1e-8)
|
| 31 |
+
|
| 32 |
+
return heatmap
|
| 33 |
+
|
| 34 |
+
def predict(input_image):
|
| 35 |
+
# Preprocess
|
| 36 |
+
img = Image.fromarray(input_image).convert('RGB')
|
| 37 |
+
img = img.resize((224, 224))
|
| 38 |
+
img_array = np.array(img) / 255.0
|
| 39 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 40 |
+
|
| 41 |
+
# Predict
|
| 42 |
+
predictions = model.predict(img_array)
|
| 43 |
+
pred_class = CLASS_NAMES[np.argmax(predictions[0])]
|
| 44 |
+
confidence = float(np.max(predictions[0])) * 100
|
| 45 |
+
|
| 46 |
+
# Create result text
|
| 47 |
+
result_text = f"Prediction: {pred_class}\nConfidence: {confidence:.2f}%\n\n"
|
| 48 |
+
result_text += "All Probabilities:\n"
|
| 49 |
+
for i, name in enumerate(CLASS_NAMES):
|
| 50 |
+
result_text += f" {name}: {predictions[0][i]*100:.2f}%\n"
|
| 51 |
+
|
| 52 |
+
# Generate Grad-CAM
|
| 53 |
+
try:
|
| 54 |
+
heatmap = make_gradcam_heatmap(img_array, model)
|
| 55 |
+
heatmap = cv2.resize(heatmap, (224, 224))
|
| 56 |
+
heatmap = np.uint8(255 * heatmap)
|
| 57 |
+
heatmap_colored = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
| 58 |
+
|
| 59 |
+
original = np.array(img)
|
| 60 |
+
superimposed = cv2.addWeighted(original, 0.6, heatmap_colored, 0.4, 0)
|
| 61 |
+
output_image = superimposed
|
| 62 |
+
except Exception as e:
|
| 63 |
+
output_image = np.array(img)
|
| 64 |
+
result_text += f"\n(Grad-CAM unavailable: {str(e)})"
|
| 65 |
+
|
| 66 |
+
return output_image, result_text
|
| 67 |
+
|
| 68 |
+
demo = gr.Interface(
|
| 69 |
+
fn=predict,
|
| 70 |
+
inputs=gr.Image(label="Upload Histopathology Image"),
|
| 71 |
+
outputs=[
|
| 72 |
+
gr.Image(label="Grad-CAM Visualization"),
|
| 73 |
+
gr.Textbox(label="Classification Result")
|
| 74 |
+
],
|
| 75 |
+
title="Bone Cancer Detection (Osteosarcoma)",
|
| 76 |
+
description="Upload an H&E stained histopathology image to classify as Non-Tumor, Viable Tumor, or Necrotic Tumor."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
demo.launch()
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
4. Click **Commit new file to main**
|
| 83 |
+
|
| 84 |
+
---
|
| 85 |
+
|
| 86 |
+
## Also update `requirements.txt`
|
| 87 |
+
|
| 88 |
+
You need to add `opencv-python`. Edit your `requirements.txt` to:
|
| 89 |
+
```
|
| 90 |
+
tensorflow
|
| 91 |
+
gradio
|
| 92 |
+
numpy
|
| 93 |
+
opencv-python-headless
|
| 94 |
+
pillow
|