Instructions to use starpreeda/BrainTumorTest with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use starpreeda/BrainTumorTest with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://starpreeda/BrainTumorTest") - Notebooks
- Google Colab
- Kaggle
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,98 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import urllib.request
|
| 3 |
-
import numpy as np
|
| 4 |
-
import tensorflow as tf
|
| 5 |
-
from tensorflow.keras.applications import EfficientNetB0
|
| 6 |
-
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout, BatchNormalization
|
| 7 |
-
from tensorflow.keras.models import Model
|
| 8 |
-
from tensorflow.keras.applications.efficientnet import preprocess_input
|
| 9 |
-
import gradio as gr
|
| 10 |
-
import cv2
|
| 11 |
-
import spaces
|
| 12 |
-
|
| 13 |
-
MODEL_PATH = "efficientnetb0_finetuned_brain_mri.keras"
|
| 14 |
-
MODEL_URL = "https://huggingface.co/starpreeda/BrainTumorTest/resolve/main/efficientnetb0_finetuned_brain_mri.keras"
|
| 15 |
-
|
| 16 |
-
def load_brain_mri_model():
|
| 17 |
-
if not os.path.exists(MODEL_PATH):
|
| 18 |
-
print("Downloading model weights from Hugging Face Repository...")
|
| 19 |
-
try:
|
| 20 |
-
urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
|
| 21 |
-
print("Model downloaded successfully!")
|
| 22 |
-
except Exception as e:
|
| 23 |
-
raise RuntimeError(f"ไม่สามารถดาวน์โหลดโมเดลได้: {e}") from e
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
base_model = EfficientNetB0(weights=None, include_top=False, input_shape=(224, 224, 3))
|
| 27 |
-
x = base_model.output
|
| 28 |
-
x = GlobalAveragePooling2D()(x)
|
| 29 |
-
x = BatchNormalization()(x)
|
| 30 |
-
x = Dense(256, activation='relu')(x)
|
| 31 |
-
x = Dropout(0.4)(x)
|
| 32 |
-
outputs = Dense(4, activation='softmax')(x)
|
| 33 |
-
|
| 34 |
-
model = Model(inputs=base_model.input, outputs=outputs)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
model.load_weights(MODEL_PATH)
|
| 38 |
-
return model
|
| 39 |
-
|
| 40 |
-
model = load_brain_mri_model()
|
| 41 |
-
|
| 42 |
-
CLASS_MAPPING = {
|
| 43 |
-
'glioma': {'name': 'Glioma Tumor', 'desc': 'A type of tumor that originates in the glial cells.'},
|
| 44 |
-
'meningioma': {'name': 'Meningioma Tumor', 'desc': 'A tumor arising from the meninges.'},
|
| 45 |
-
'notumor': {'name': 'No Tumor Detected', 'desc': 'No clear evidence of brain tumor tissue.'},
|
| 46 |
-
'pituitary': {'name': 'Pituitary Tumor', 'desc': 'An abnormal growth located in the pituitary gland.'}
|
| 47 |
-
}
|
| 48 |
-
CLASS_NAMES = ['glioma', 'meningioma', 'notumor', 'pituitary']
|
| 49 |
-
@spaces.GPU
|
| 50 |
-
def predict_mri(input_img):
|
| 51 |
-
if input_img is None:
|
| 52 |
-
return "<h3 style='color:#d93025;'>Please upload a valid Brain MRI scan image.</h3>", {}
|
| 53 |
-
|
| 54 |
-
if input_img.ndim == 2:
|
| 55 |
-
input_img = cv2.cvtColor(input_img, cv2.COLOR_GRAY2RGB)
|
| 56 |
-
elif input_img.shape[-1] == 4:
|
| 57 |
-
input_img = cv2.cvtColor(input_img, cv2.COLOR_RGBA2RGB)
|
| 58 |
-
|
| 59 |
-
img_resized = cv2.resize(input_img, (224, 224))
|
| 60 |
-
img_array = img_resized.astype(np.float32)
|
| 61 |
-
img_batch = np.expand_dims(img_array, axis=0)
|
| 62 |
-
img_preprocessed = preprocess_input(img_batch)
|
| 63 |
-
|
| 64 |
-
predictions = model.predict(img_preprocessed)[0]
|
| 65 |
-
|
| 66 |
-
confidences = {}
|
| 67 |
-
for idx, class_key in enumerate(CLASS_NAMES):
|
| 68 |
-
confidences[CLASS_MAPPING[class_key]['name']] = float(predictions[idx])
|
| 69 |
-
|
| 70 |
-
top_idx = int(np.argmax(predictions))
|
| 71 |
-
top_key = CLASS_NAMES[top_idx]
|
| 72 |
-
top_confidence = predictions[top_idx] * 100
|
| 73 |
-
info = CLASS_MAPPING[top_key]
|
| 74 |
-
|
| 75 |
-
summary_html = f"""
|
| 76 |
-
<div style="background-color: #f8f9fa; border-left: 6px solid #1a73e8; padding: 18px; border-radius: 8px;">
|
| 77 |
-
<h3 style="color: #1a73e8; margin-top: 0;">Diagnostic Classification Summary</h3>
|
| 78 |
-
<p style="font-size: 20px; font-weight: bold;">Predicted Class: <span style="color: #d93025;">{info['name']}</span></p>
|
| 79 |
-
<p style="font-size: 16px; font-weight: bold;">Confidence Score: <span style="color: #188038;">{top_confidence:.2f}%</span></p>
|
| 80 |
-
<hr style="border: 0.5px solid #dadce0;">
|
| 81 |
-
<p style="font-size: 14px; color: #5f6368;"><b>Clinical Note:</b> {info['desc']}</p>
|
| 82 |
-
</div>
|
| 83 |
-
"""
|
| 84 |
-
return summary_html, confidences
|
| 85 |
-
|
| 86 |
-
demo = gr.Interface(
|
| 87 |
-
fn=predict_mri,
|
| 88 |
-
inputs=gr.Image(type="numpy", label="Upload Brain MRI Image"),
|
| 89 |
-
outputs=[
|
| 90 |
-
gr.HTML(label="Classification Result"),
|
| 91 |
-
gr.Label(num_top_classes=4, label="Class Probability Distribution")
|
| 92 |
-
],
|
| 93 |
-
title="🧠 Brain Tumor MRI Classification System",
|
| 94 |
-
description="Upload a Brain MRI scan to analyze potential tumor types."
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
if __name__ == "__main__":
|
| 98 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|