BrainVision / app.py
Tada-tot's picture
Update app.py
5d459b6 verified
Raw History Blame Contribute Delete
7 kB
import os
import cv2
import json
import torch
import numpy as np
import gradio as gr
import safetensors.torch
import albumentations as A
import segmentation_models_pytorch as smp
from albumentations.pytorch import ToTensorV2
from huggingface_hub import hf_hub_download
# Configuration - embed directly in code or load from repo
CONFIG = {
"IMG_SIZE": 256,
"FILTER_SIZE": 3,
"CLIP_LIMIT": 3.0,
"GRID_SIZE": (8, 8),
"NORMALIZE_MEAN": [0.485, 0.456, 0.406],
"NORMALIZE_STD": [0.229, 0.224, 0.225]
}
BEST_MODEL = None
MODEL_NAME = "BrainVision" # Set your model name
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
def load_model():
"""Load model from Hugging Face Hub or local files."""
global BEST_MODEL
try:
model_path = "best_model_raw_resnet50.safetensors"
if not os.path.exists(model_path):
return "Error: Model file not found."
encoder_name = "resnet50"
model = smp.Unet(
encoder_name=encoder_name,
encoder_weights='imagenet',
in_channels=3,
classes=1,
activation=None)
state_dict = safetensors.torch.load_file(model_path, device=DEVICE)
model.load_state_dict(state_dict)
model.to(DEVICE)
model.eval()
BEST_MODEL = model
return f"Model loaded successfully on {DEVICE}"
except Exception as e:
return f"Error loading model: {str(e)}"
def apply_clahe_and_median_filter(image_np):
"""Applies Median Filter and CLAHE to a numpy image."""
image = cv2.medianBlur(image_np, CONFIG['FILTER_SIZE'])
lab_img = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
l, a, b = cv2.split(lab_img)
clahe = cv2.createCLAHE(clipLimit=CONFIG['CLIP_LIMIT'], tileGridSize=tuple(CONFIG['GRID_SIZE']))
updated_l = clahe.apply(l)
return cv2.cvtColor(cv2.merge((updated_l, a, b)), cv2.COLOR_LAB2RGB)
def dice_score(pred_mask, gt_mask):
"""Calculates the Dice score between two binary masks."""
intersection = np.sum(pred_mask & gt_mask)
union = np.sum(pred_mask) + np.sum(gt_mask)
return (2.0 * intersection) / union if union > 0 else np.nan
def iou_score(pred_mask, gt_mask):
"""Calculates the Intersection over Union score."""
intersection = np.logical_and(pred_mask, gt_mask).sum()
union = np.logical_or(pred_mask, gt_mask).sum()
return intersection / union if union > 0 else np.nan
def predict_and_compare(mri_image, ground_truth_mask):
"""Predicts a tumor mask and compares it to a ground truth mask."""
mri_image_np = np.array(mri_image.convert('RGB')) if mri_image else None
ground_truth_mask_np = np.array(ground_truth_mask.convert('L')) if ground_truth_mask else None
if BEST_MODEL is None:
return None, None, "Error: Model not loaded. Please check model path."
if mri_image_np is None:
return None, None, "Error: Please upload a brain MRI image."
processed_image = mri_image_np.copy()
val_transform = A.Compose([
A.Resize(CONFIG['IMG_SIZE'], CONFIG['IMG_SIZE']),
A.Normalize(mean=CONFIG['NORMALIZE_MEAN'], std=CONFIG['NORMALIZE_STD']),
ToTensorV2()])
augmented = val_transform(image=processed_image)
input_tensor = augmented['image'].unsqueeze(0).to(DEVICE)
with torch.no_grad():
output = BEST_MODEL(input_tensor)
pred_mask = (torch.sigmoid(output).squeeze().cpu().numpy() > 0.5).astype(np.uint8) * 255
pred_mask_resized = cv2.resize(pred_mask, (mri_image_np.shape[1], mri_image_np.shape[0]))
pred_overlay = mri_image_np.copy()
contours, _ = cv2.findContours(pred_mask_resized, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(pred_overlay, contours, -1, (255, 0, 0), 2) # Red for Prediction
status_msg = "Prediction Completed!"
dice = iou = np.nan
comparison_output = None
if ground_truth_mask_np is not None:
gt_mask_resized = cv2.resize(ground_truth_mask_np, (mri_image_np.shape[1], mri_image_np.shape[0]))
dice = dice_score(pred_mask_resized, gt_mask_resized)
iou = iou_score(pred_mask_resized, gt_mask_resized)
comparison_output = mri_image_np.copy()
gt_contours, _ = cv2.findContours(gt_mask_resized, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(comparison_output, gt_contours, -1, (0, 255, 0), 2) # Green for Ground Truth
cv2.drawContours(comparison_output, contours, -1, (255, 0, 0), 2) # Red for Prediction
return pred_overlay, comparison_output, status_msg, dice, iou
def clear_interface():
"""Resets the interface."""
return None, None, "", np.nan, np.nan
# Load model on startup
try:
status_text = load_model()
except Exception as e:
print(f"Error loading model: {e}")
# Create Gradio interface
with gr.Blocks(theme=gr.themes.Soft(), title="🧠👁️ BrainVision") as demo:
gr.Markdown("# Brain Tumor Segmentation")
gr.Markdown("Upload a brain MRI scan and an optional ground truth mask. The model will predict the tumor segmentation.")
with gr.Accordion("📖 How to Use", open=True):
gr.Markdown("""
1. **Upload MRI Image** (required): Upload a brain MRI scan
2. **Upload Ground Truth** (optional): For performance evaluation
3. **Click Predict**: Run the segmentation model
4. **View Results**: See the predicted tumor mask overlaid on the original image
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Upload Images")
mri_input = gr.Image(type="pil", label="Brain MRI (Required) - 3 Channel RGB")
gt_mask_input = gr.Image(type="pil", label="Ground Truth Mask (Optional) - Binary Image")
with gr.Row():
predict_btn = gr.Button("🔮 Predict", variant="primary")
clear_btn = gr.Button("🗑️ Clear")
with gr.Column(scale=2):
gr.Markdown("### Results")
with gr.Row():
pred_output = gr.Image(label="Predicted Segmentation")
comparison_output = gr.Image(label="Comparison (GT vs Prediction)")
with gr.Row():
dice_output = gr.Number(label="Dice Score", precision=4)
iou_output = gr.Number(label="IoU Score", precision=4)
status_output = gr.Textbox(label="Status", value=status_text)
gr.Markdown(f"**Model Name:** {MODEL_NAME} | **Device:** {DEVICE} | **Status:** {'Ready' if BEST_MODEL is not None else 'Stall'}")
# Event handlers
predict_btn.click(
fn=predict_and_compare,
inputs=[mri_input, gt_mask_input],
outputs=[pred_output, comparison_output, status_output, dice_output, iou_output])
clear_btn.click(
fn=clear_interface,
outputs=[pred_output, comparison_output, status_output, dice_output, iou_output])
if __name__ == "__main__":
demo.launch(share=True)