
Brain Tumor Classification using CNN
Deep learning project that classifies brain MRI scans into four categories using Convolutional Neural Networks
Timeline
Completed
Role
Deep Learning Engineer
Status
CompletedTechnology Stack
Brain Tumor Classification using CNN
A deep learning project that classifies brain MRI scans into four categories: Glioma, Meningioma, Pituitary Tumor, and No Tumor using Convolutional Neural Networks (CNN).
Project Overview
This project implements a CNN-based classification model to detect and classify brain tumors from MRI images. The model achieves 96.34% validation accuracy after 10 epochs of training.
Key Features
- Multi-class classification (4 tumor types)
- Custom CNN architecture with dropout regularization
- Data augmentation and preprocessing
- Model training with visualization
- Single image prediction capability
Dataset Structure
brain_tumor_project/
├── dataset/
│ ├── Training/
│ │ ├── glioma/ # Glioma tumor images
│ │ ├── meningioma/ # Meningioma tumor images
│ │ ├── notumor/ # No tumor images
│ │ └── pituitary/ # Pituitary tumor images
│ └── Testing/
│ ├── glioma/
│ ├── meningioma/
│ ├── notumor/
│ └── pituitary/
├── model/
│ └── brain_tumor_classifier.h5 # Trained model
├── test.ipynb # Jupyter notebook
├── requirements.txt # Project dependencies
└── README.md
Dataset Statistics
- Training Images: 5,712 images
- Testing Images: 1,311 images
- Image Size: 150x150 pixels
- Classes: 4 (glioma, meningioma, notumor, pituitary)
Model Architecture
Sequential Model
Input Layer: (150, 150, 3)
↓
Conv2D (32 filters) → ReLU → MaxPooling
↓
Conv2D (64 filters) → ReLU → MaxPooling
↓
Conv2D (128 filters) → ReLU → MaxPooling
↓
Flatten
↓
Dense (128 units) → ReLU
↓
Dropout (0.5)
↓
Dense (4 units) → Softmax
Training Configuration
- Optimizer: Adam
- Loss Function: Categorical Crossentropy
- Metrics: Accuracy
- Batch Size: 32
- Epochs: 10
- Image Augmentation: Rescaling (1./255)
Installation
Prerequisites
- Python 3.7+
- pip package manager
Setup Steps
- Clone the repository
git clone https://github.com/SHREY275/brain_tumor_project.git
cd brain_tumor_project- Create a virtual environment (recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install -r requirements.txtUsage
Training the Model
Open the Jupyter notebook:
jupyter notebook test.ipynbRun all cells to:
- Load and preprocess the dataset
- Build and compile the model
- Train the model
- Save the trained model
- Visualize training metrics
Making Predictions
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
# Load the trained model
model = load_model("model/brain_tumor_classifier.h5")
# Define class names
class_names = ['glioma', 'meningioma', 'notumor', 'pituitary']
# Load and preprocess image
img_path = "path/to/your/image.jpg"
img = image.load_img(img_path, target_size=(150, 150))
img_array = image.img_to_array(img)
img_array = img_array / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Make prediction
predictions = model.predict(img_array)
predicted_class = class_names[np.argmax(predictions)]
print(f"Predicted Tumor Type: {predicted_class}")Results
Final Training Metrics (Epoch 10)
- Training Accuracy: 97.93%
- Validation Accuracy: 96.34%
- Training Loss: 0.0568
- Validation Loss: 0.1089
Training Progress
The model shows excellent convergence with minimal overfitting:
- Consistent improvement across all epochs
- Validation accuracy closely follows training accuracy
- Achieved 96% validation accuracy
Requirements
numpy>=1.24.0
pandas>=2.0.0
matplotlib>=3.7.0
seaborn>=0.12.0
scikit-learn>=1.3.0
tensorflow>=2.13.0
jupyter>=1.0.0
ipykernel>=6.25.0
python-dateutil>=2.8.0
Tech Stack
Key Learnings
- Data Preprocessing: Importance of proper image normalization and augmentation
- Model Architecture: Balancing model complexity with overfitting prevention
- Regularization: Effective use of dropout to improve generalization
- Performance Optimization: Achieving high accuracy with efficient training
Notes
- The model is saved in HDF5 format (.h5)
- Images are automatically resized to 150x150 pixels
- The model expects RGB images (3 channels)
- Normalization is applied (pixel values divided by 255)
Repository: github.com/SHREY275/brain_tumor_project
