File size: 2,445 Bytes
0d89ca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12a087d
0d89ca2
 
554025f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---
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}%**")