--- license: mit datasets: - masoudnickparvar/brain-tumor-mri-dataset metrics: - accuracy pipeline_tag: image-classification library_name: keras tags: - cnn - keras - brain-tumor - medical-imaging - tensor-flow - image-classification language: - en --- Brain Tumor Detection CNN Model This model was trained using a Convolutional Neural Network (CNN) to classify brain MRI images as either having a tumor or not. It uses Keras with TensorFlow backend and was trained on the publicly available [Brain Tumor MRI Dataset](https://www.kaggle.com/datasets/masoudnickparvar/brain-tumor-mri-dataset) from Kaggle. Dataset The dataset contains 3,762 T1-weighted contrast-enhanced MRI images, labeled as: - **Yes** – Images with a brain tumor - **No** – Images without a brain tumor The data is balanced and preprocessed into two folders: `yes/` and `no/`. Train Accuracy: ~98% Validation Accuracy: ~96% ## 🧠 Model Architecture - Type: CNN - Framework: Keras (TensorFlow backend) - Input shape: `(150, 150, 3)` - Final Activation: `sigmoid` - Loss: `binary_crossentropy` - Optimizer: `Adam` Code to Run: ```python import streamlit as st import tensorflow as tf import numpy as np from tensorflow.keras.preprocessing import image from PIL import Image # Load the trained model @st.cache_resource def load_model(): return tf.keras.models.load_model('models/brain_tumor_model.h5') # Update path if needed model = load_model() # Define class labels class_names = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor'] # UI st.title("🧠 Brain Tumor Detection from MRI") st.write("Upload an MRI image to detect the type of brain tumor.") # Upload image uploaded_file = st.file_uploader("Choose an MRI image", type=["jpg", "jpeg", "png"]) if uploaded_file is not None: # Show image img = Image.open(uploaded_file) st.image(img, caption="🖼️ Uploaded Image", use_container_width=True) # Preprocessing img = img.resize((224, 224)) # ✅ Make sure it matches your model's input size img_array = image.img_to_array(img) img_array = np.expand_dims(img_array, axis=0) / 255.0 # Prediction predictions = model.predict(img_array) confidence = float(np.max(predictions)) * 100 predicted_class = class_names[np.argmax(predictions)] # Output st.success(f"🎯 Predicted Tumor Type: **{predicted_class}**") st.info(f"📊 Model Confidence: **{confidence:.2f}%**")