--- license: apache-2.0 language: - en metrics: - accuracy - recall - precision - IoU - dice coefficient pipeline_tag: image-segmentation tags: - brain-tumor - tensorflow - image-segmentation library_name: keras --- # Model Card for Brain Tumor MRI Segmentation Model A deep-learning model for **brain tumor segmentation** in MRI scans using a U-Net-based convolutional neural network. The model outputs a binary mask identifying tumor regions and is intended for research and educational use only. ## Model Details ### Model Description This model performs **pixel-wise segmentation of brain tumors** from MRI slices. It was developed as part of an academic computer vision project focusing on medical image segmentation. The architecture is a U-Net implemented in **TensorFlow/Keras**, trained on the Brain Tumor MRI dataset by Jun Cheng (2017), available on Figshare (DOI: 10.6084/m9.figshare.1512427.v8). The dataset contains MRI scans labeled for tumor segmentation and is commonly used in medical image analysis research. - **Developed by:** Yara Elshehawi - **Model type:** U-Net convolutional neural network for image segmentation - **License:** Apache-2.0 ### Model Sources - [Repository (training code)](https://github.com/yaraeslamm/Medical_Segmentation) - [Hugging Face Model]( https://huggingface.co/yaraa11/brain-tumor-segmentation) - [Demo](https://huggingface.co/spaces/yaraa11/brain-tumor-segmentation-app) --- ## Uses ### Direct Use The model can be used to: - Segment tumor regions in MRI slices - Support research in medical image processing - Serve as a reference implementation for U-Net segmentation tasks - Be integrated into pipelines requiring binary mask prediction ### Downstream Use (Optional) - Fine-tuning on other medical segmentation datasets - Adaptation to multi-class tumor segmentation - Integration into educational tools or visualization dashboards ### Out-of-Scope Use The model **must not** be used for: - Real clinical diagnosis or medical decision-making - Automated medical treatment recommendations - High-stakes deployment on real patient data without proper validation and certification --- ## Bias, Risks, and Limitations - The model is trained on a **limited dataset**, which may not generalize to all MRI scanners, protocols, or patient populations. - Segmentation quality may vary depending on image contrast, noise, or MRI orientation. - Not suitable for real-world clinical environments without extensive validation. ### Recommendations Users should: - Avoid using the model on out-of-domain MRI scans - Conduct their own evaluation before using it in any research pipeline - Be aware that predictions may include false positives/negatives --- ## How to Get Started with the Model You can use this model in two ways: 1. Load it directly from Hugging Face ```python from tensorflow.keras.models import load_model from keras import backend as K import tensorflow as tf import numpy as np from tensorflow.keras.preprocessing import image # Dice Coefficient (required for custom_objects) def dice_coef(y_true, y_pred): y_true = K.cast(y_true, 'float32') y_pred = K.cast(y_pred, 'float32') y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersect = K.sum(y_true_f * y_pred_f) return (2. * intersect + K.epsilon()) / ( K.sum(y_true_f) + K.sum(y_pred_f) + K.epsilon() ) # Load the model from Hugging Face model = load_model( "yaraa11/brain-tumor-segmentation", custom_objects={"dice_coef": dice_coef}, compile=False ) # (Optional) Recompile model.compile( optimizer="adam", loss="binary_crossentropy", metrics=[ "accuracy", tf.keras.metrics.Precision(), tf.keras.metrics.Recall(), dice_coef ] ) # Load and preprocess input image img = image.load_img( "path_to_image.png", target_size=(224, 224), color_mode="grayscale" ) img = image.img_to_array(img) / 255.0 img = np.expand_dims(img, axis=0) # Predict mask prediction = model.predict(img) # Convert to binary mask binary_mask = (prediction > 0.5).astype(np.float32) ``` 2. Or load it locally if you downloaded the .keras file ```python from tensorflow.keras.models import load_model import numpy as np from tensorflow.keras.preprocessing import image model = load_model( "./model.keras", custom_objects={"dice_coef": dice_coef}, compile=False ) # Load and preprocess input image img = image.load_img( "path_to_image.png", target_size=(224, 224), color_mode="grayscale" ) img = image.img_to_array(img) / 255.0 img = np.expand_dims(img, axis=0) # Predict mask prediction = model.predict(img) # Convert to binary mask binary_mask = (prediction > 0.5).astype(np.float32) ``` ## Training Details ### Training Data The model was trained on a publicly available **brain MRI dataset**. All images were preprocessed to ensure consistency across the dataset, including: - Resizing to **256 × 256** - Normalizing intensities to **[0, 1]** - Converting images to **single-channel (grayscale)** format - Splitting the dataset into **training, validation, and testing subsets** ### Training Procedure #### Preprocessing Before training, each MRI slice was: - Normalized to [0, 1] - Resized to 256 × 256 - Converted to grayscale (single-channel) format #### Training Hyperparameters - **Loss function:** Binary Cross-Entropy - **Optimizer:** Adam - **Learning rate:** 0.001 - **Batch size:** 16 - **Epochs:** 40 The model was trained end-to-end using TensorFlow/Keras. #### Speeds, Sizes, Times - **Model size:** 6 MB - **Training time:** Approximately 1–3 hours on a single Colab GPU (e.g., T4) - **Number of parameters:** 487,009 (~1.86 MB) --- ## Evaluation ### Testing Data, Factors & Metrics #### Testing Data Evaluation was conducted on a held-out portion of the MRI dataset that was not used during training. All preprocessing steps were identical to those used during training. #### Factors Model performance may vary depending on: - Tumor size and location - MRI contrast variations - Image noise, artifacts, or motion - Differences in scanning equipment or acquisition protocols #### Metrics The model was evaluated using standard medical image segmentation metrics: - **Accuracy** - **Precision** - **Recall** - **Intersection over Union (IoU)** - **Dice Coefficient** (primary metric for tumor segmentation) ### Results - Dice: 0.7019 - IoU: 0.4996 - Precision: 0.7860 - Recall: 0.7714 #### Summary The model performs strongly on the provided dataset and reliably segments tumor regions. However, performance may degrade on MRI scans from different sources or with significantly different characteristics. --- ## Model Examination The U-Net architecture uses skip connections that allow fine-grained spatial information to be preserved, improving boundary definition and tumor localization. Performance is influenced heavily by input image quality and consistent preprocessing. --- ## Environmental Impact - **Hardware Type:** Single NVIDIA T4 GPU (Google Colab) - **Training Duration:** Approximately 8 hours - **Cloud Provider:** Google Colab - **Estimated Carbon Emissions:** <0.2 kg CO₂ (low-impact training) --- ## Technical Specifications ### Model Architecture and Objective - **Architecture:** U-Net encoder–decoder CNN - **Objective:** Binary semantic segmentation (tumor vs. non-tumor) - **Activation:** Sigmoid output for pixel-wise classification - **Framework:** TensorFlow/Keras ### Compute Infrastructure #### Hardware - Google Colab GPU (T4 / RTX class) #### Software - Python 3.10 - TensorFlow / Keras - NumPy, OpenCV, SciPy --- ## Citation () ```bibtex @article{Cheng2017, author = "Jun Cheng", title = "{brain tumor dataset}", year = "2017", month = "4", url = "https://figshare.com/articles/dataset/brain_tumor_dataset/1512427", doi = "10.6084/m9.figshare.1512427.v8" } ``` ## Glossary **Dice Coefficient:** A segmentation metric measuring overlap between predicted and ground-truth masks. Ranges from 0 (no overlap) to 1 (perfect overlap). Commonly used in medical imaging. **IoU (Intersection over Union):** A metric evaluating how well predicted regions match the true regions by dividing the intersection area by the union area. **Segmentation Mask:** A binary image where each pixel is labeled as either tumor (1) or background (0). **U-Net:** A convolutional neural network architecture with an encoder–decoder structure and skip connections, widely used for biomedical image segmentation. --- ## More Information For additional details, sample predictions, or troubleshooting, please refer to the repository’s README or open a discussion on the Hugging Face model page. --- ## Model Card Authors Created by **Yara Elshehawi**, as part of a medical image segmentation project focused on brain tumor detection using MRI data. --- ## Contact Information If you have any questions, encounter issues, or are interested in collaboration, feel free to reach out through any of the following channels: - **Hugging Face Repository Discussion Board**: Visit the "Community" section of the model page to start a discussion or post an issue. - **Portfolio Website**: You can also contact me directly through my [portfolio website](https://yaraeslamm.github.io) for additional inquiries. Looking forward to hearing from you!