WellScan Healthcare β€” Attention ResU-Net Brain Tumor Model

Brain tumor classification + segmentation model trained on MRI scans.
Part of the WellScan Healthcare project β€” BTech Final Year Project

Model Description

A custom Attention ResU-Net with Classifier β€” dual-output architecture that simultaneously:

  • Classifies the tumor type (4 classes)
  • Segments the exact tumor region (pixel-level mask)

Built on U-Net with ResNet skip connections and attention gates integrated into the decoder path.

Model Performance

Metric Training Validation
Classification Accuracy 98.73% 97.18%
Segmentation Accuracy 99.48% 99.25%
Mean IoU 0.7312 0.6397
Dice Coefficient 0.8438 0.7769
Total Loss 0.0523 0.0928
Optimal Threshold 0.38 β€”

Classes

Label Class
0 Meningioma
1 Glioma
2 Pituitary Tumor
3 No Tumor

Training Details

Parameter Value
Framework TensorFlow 2.15 / Keras
Image size 224 Γ— 224 (grayscale)
Training samples 3,727 MRI images
Train/Val/Test split 80/10/10
Optimizer Adam
LR scheduler ReduceLROnPlateau
Epochs 50
Batch size 32
Hardware Kaggle GPU P100 (15GB RAM)

Preprocessing Pipeline

  1. Otsu thresholding β†’ binary mask
  2. Contour detection β†’ crop to brain region (removes background noise)
  3. Resize to 224Γ—224 grayscale
  4. Z-score normalization (mean=0, std=1)
  5. Data augmentation: rotation, flipping, shifting

How to Use

Load the model

from huggingface_hub import hf_hub_download
from keras.models import load_model
import tensorflow as tf

threshold = 0.38

def mean_iou(y_true, y_pred):
    y_true = tf.cast(y_true, tf.float32)
    y_pred = tf.cast(y_pred >= threshold, tf.float32)
    intersection = tf.reduce_sum(tf.abs(y_true * y_pred))
    union = tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) - intersection
    return intersection / union

def dice_coefficient(y_true, y_pred, smooth=1e-5):
    y_true = tf.cast(y_true, tf.float32)
    y_pred = tf.cast(y_pred >= threshold, tf.float32)
    intersection = tf.reduce_sum(y_true * y_pred)
    union = tf.reduce_sum(y_true) + tf.reduce_sum(y_pred)
    return (2. * intersection + smooth) / (union + smooth)

# Download model from Hugging Face
model_path = hf_hub_download(
    repo_id="varad-patil/wellscanhealthcare-brain-tumor",
    filename="AT_RESu_net_all_STD_0.0927_th=0.38.hdf5"
)

# Load with custom metrics
model = load_model(model_path, custom_objects={
    'mean_iou': mean_iou,
    'dice_coefficient': dice_coefficient
})

Run inference

import cv2
import numpy as np

def preprocess(image_path, size=224):
    img = cv2.imread(image_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, binary = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[0]
    x, y, w, h = cv2.boundingRect(contours)
    img = cv2.resize(img[y:y+h, x:x+w], (size, size))
    img = (img - img.mean()) / img.std()
    return np.expand_dims(np.expand_dims(img, axis=-1), axis=0)

class_labels = ['meningioma', 'glioma', 'pituitary tumor', 'noTumor']

img = preprocess("your_mri_scan.jpg")
classification, segmentation_mask = model.predict(img)

predicted_class = class_labels[np.argmax(classification)]
confidence = np.max(classification) * 100
print(f"Prediction: {predicted_class} ({confidence:.2f}%)")

Project Links

Downloads last month
-
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support