Skip to content

Abdelhady-22/brain_tumor_classification

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 AI-Powered Brain Tumor Classification

Python TensorFlow License Kaggle

Leveraging Convolutional Neural Networks (CNNs) and Transfer Learning to classify brain MRI images into four categories: Glioma, Meningioma, Pituitary Tumor, and No Tumor. This project achieves up to 97% accuracy using ResNet50V2, supporting early diagnosis and clinical decision-making.


πŸ“‹ Table of Contents


✨ Features

  • Multi-Model Architecture: Implements and compares 5 different deep learning models

    • Custom CNN
    • VGG16 (Transfer Learning)
    • VGG19 (Transfer Learning)
    • ResNet50V2 (Transfer Learning)
    • MobileNetV2 (Transfer Learning)
  • High Accuracy: Achieves up to 97% accuracy on test data with ResNet50V2

  • Comprehensive Evaluation:

    • Accuracy, Precision, Recall, F1-Score metrics
    • Confusion matrices for detailed performance analysis
    • Training/validation loss and accuracy curves
  • Data Augmentation: Robust preprocessing pipeline for better generalization

  • Class Balancing: Automatic downsampling to handle class imbalance

  • Visualization Tools:

    • Sample image visualization from training/testing sets
    • Model comparison charts
    • Training history plots

🎯 Model Performance

Model Test Accuracy Precision Recall F1-Score Loss
ResNet50V2 96.98% 97.03% 96.91% 96.95% 0.1647
MobileNetV2 96.47% 96.49% 96.47% 96.45% 0.2659
VGG19 94.76% 94.85% 94.76% 94.67% 0.3124
VGG16 94.49% 94.70% 94.27% 94.30% 0.3187
Custom CNN 88.61% 88.86% 88.53% 87.63% 0.7931

Best Model: ResNet50V2 with 96.98% test accuracy πŸ†


πŸ“Š Dataset

Sources

The project uses two Kaggle datasets:

  1. Brain Tumor Classification (MRI) by Sartaj Bhuvaji

  2. Brain Tumor MRI Dataset by Masoud Nickparvar

Dataset Structure

Training/
β”œβ”€β”€ glioma_tumor/
β”œβ”€β”€ meningioma_tumor/
β”œβ”€β”€ no_tumor/
└── pituitary_tumor/

Testing/
β”œβ”€β”€ glioma_tumor/
β”œβ”€β”€ meningioma_tumor/
β”œβ”€β”€ no_tumor/
└── pituitary_tumor/

Classes

  • Glioma Tumor: Malignant brain tumors arising from glial cells
  • Meningioma Tumor: Tumors arising from meninges (brain coverings)
  • Pituitary Tumor: Tumors in the pituitary gland
  • No Tumor: Normal brain MRI scans

Preprocessing

  • Images resized to 224x224 pixels
  • Normalization: pixel values scaled to [0, 1]
  • Class balancing through downsampling
  • Batch size: 32

πŸ“ Project Structure

BRAIN-TUMOR-CLASSIFICATION/
β”‚
β”œβ”€β”€ src/
β”‚   └── Models/
β”‚       β”œβ”€β”€ CNN.ipynb              # Custom CNN implementation
β”‚       β”œβ”€β”€ MobileNet.ipynb        # MobileNetV2 transfer learning
β”‚       β”œβ”€β”€ ResNet.ipynb           # ResNet50V2 transfer learning
β”‚       β”œβ”€β”€ VGG_16.ipynb           # VGG16 transfer learning
β”‚       └── VGG_19.ipynb           # VGG19 transfer learning
β”‚
β”œβ”€β”€ results/
β”‚   β”œβ”€β”€ CNN_Result/
β”‚   β”‚   └── CNN.png                # CNN performance visualization
β”‚   β”œβ”€β”€ MobileNet_Result/
β”‚   β”‚   └── MobileNet.png
β”‚   β”œβ”€β”€ ResNet_Result/
β”‚   β”‚   └── ResNet.png
β”‚   β”œβ”€β”€ VGG_16_Result/
β”‚   β”‚   └── VGG_16.png
β”‚   β”œβ”€β”€ VGG_19_Result/
β”‚   β”‚   └── VGG_19.png
β”‚   β”œβ”€β”€ Comparison_Models/
β”‚   β”‚   β”œβ”€β”€ Comparison_1.png      # Model accuracy comparison
β”‚   β”‚   β”œβ”€β”€ Comparison_2.png      # Validation loss comparison
β”‚   β”‚   β”œβ”€β”€ Comparison_3.png      # Training curves
β”‚   β”‚   β”œβ”€β”€ Comparison_4.png      # Bar charts
β”‚   β”‚   β”œβ”€β”€ Comparison_5.png      # Horizontal comparison
β”‚   β”‚   β”œβ”€β”€ Comparison_6.png      # Final rankings
β”‚   β”‚   └── Comparison_7.png      # Detailed metrics
β”‚   └── Data_Visualization/
β”‚       β”œβ”€β”€ Testing_data.png       # Sample test images
β”‚       β”œβ”€β”€ Training_data.png      # Sample training images
β”‚       └── validtion_data.png     # Merged dataset samples
β”‚
β”œβ”€β”€ brain-tumor-classification-cnn-97-acc.py   # Main training script
β”œβ”€β”€ models.txt                     # Links to trained models (.h5 files)
β”œβ”€β”€ dataset.txt                    # Dataset download links
β”œβ”€β”€ kaggle_notebook.txt            # Link to Kaggle notebook
β”œβ”€β”€ requirements.txt               # Python dependencies
β”œβ”€β”€ .env.example                   # Environment variables template
β”œβ”€β”€ .gitignore                     # Git ignore rules
β”œβ”€β”€ LICENSE                        # Project license
└── README.md                      # This file

πŸš€ Installation

Prerequisites

  • Python 3.10 or higher
  • CUDA-compatible GPU (recommended for faster training)
  • 8GB+ RAM
  • 10GB+ free disk space

Installation Steps

1. Install Python using MiniConda

Download and install MiniConda from here

Create a new environment:

conda create -n Brain_Tumor_Classification python=3.10

Activate the environment:

conda activate Brain_Tumor_Classification

2. Clone the Repository

git clone https://github.com/Abdelhady-22/brain_tumor_classification.git
cd brain-tumor-classification

3. Install Dependencies

pip install -r requirements.txt

4. Download Dataset

Option A: Using Kaggle API

pip install kagglehub
python -c "import kagglehub; kagglehub.dataset_download('sartajbhuvaji/brain-tumor-classification-mri')"
python -c "import kagglehub; kagglehub.dataset_download('masoudnickparvar/brain-tumor-mri-dataset')"

Option B: Manual Download

  • Download datasets from links in dataset.txt
  • Extract to project directory

5. Setup Environment Variables

cp .env.example .env

Edit .env file:

PROJECT_VERSION=1.0
IMAGE_SIZE=224
BATCH_SIZE=32
EPOCHS=20
LEARNING_RATE=0.0001

πŸ’» Usage

Training Models

Run the main training script:

python brain-tumor-classification-cnn-97-accuracy.ipynb

Or train individual models using Jupyter notebooks:

jupyter notebook src/Models/CNN.ipynb

Model Inference

from tensorflow.keras.models import load_model
import numpy as np
from PIL import Image

# Load trained model
model = load_model('ResNet_best_model.keras')

# Load and preprocess image
img = Image.open('path/to/mri_scan.jpg')
img = img.resize((224, 224))
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)

# Make prediction
prediction = model.predict(img_array)
classes = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor']
result = classes[np.argmax(prediction)]

print(f"Prediction: {result}")
print(f"Confidence: {np.max(prediction) * 100:.2f}%")

Evaluating Models

# Load test data
from tensorflow.keras.preprocessing.image import ImageDataGenerator

test_datagen = ImageDataGenerator(preprocessing_function=lambda x: x/255.0)
test_generator = test_datagen.flow_from_directory(
    'path/to/Testing',
    target_size=(224, 224),
    batch_size=32,
    class_mode='categorical'
)

# Evaluate model
results = model.evaluate(test_generator)
print(f"Test Accuracy: {results[1]*100:.2f}%")

πŸ”§ Technical Details

Architecture Overview

Custom CNN

Input (224x224x3)
β†’ Conv2D(64) + MaxPool
β†’ Conv2D(64) + MaxPool
β†’ Conv2D(128) + MaxPool
β†’ Flatten + Dropout(0.4)
β†’ Dense(128) + Dense(64) + Dense(64)
β†’ Output (4 classes, softmax)

Transfer Learning Models

  • Base Models: Pre-trained on ImageNet
  • Frozen Layers: All base layers non-trainable
  • Custom Head:
    • GlobalAveragePooling2D / Flatten
    • Dense(256, relu)
    • Output(4, softmax)

Training Configuration

Parameter Value
Image Size 224 Γ— 224
Batch Size 32
Epochs 20 (with early stopping)
Optimizer Adam
Learning Rate 0.0001
Loss Function Categorical Crossentropy

Callbacks

  • EarlyStopping: Patience=5, monitors val_loss
  • ModelCheckpoint: Saves best model based on val_loss
  • ReduceLROnPlateau: Reduces LR by 0.5 when val_loss plateaus

Hardware Requirements

  • GPU: NVIDIA GPU with CUDA support (recommended)
  • RAM: 8GB minimum, 16GB recommended
  • Storage: 10GB for datasets and models

πŸ“ˆ Results

Model Comparison

Model Accuracy Comparison

Training History

All models were trained for up to 20 epochs with early stopping:

  • ResNet50V2: Best validation loss at epoch 12
  • MobileNetV2: Best validation loss at epoch 15
  • VGG19: Best validation loss at epoch 18
  • VGG16: Best validation loss at epoch 16
  • Custom CNN: Best validation loss at epoch 19

Key Findings

  1. Transfer Learning Superiority: Pre-trained models significantly outperform custom CNN
  2. ResNet50V2 Excellence: Achieved best overall performance with 97% accuracy
  3. Convergence Speed: Transfer learning models converge faster (fewer epochs)
  4. Generalization: All models show good generalization on merged dataset

🌐 Environment Variables

Create a .env file with the following variables:

# Project Configuration
PROJECT_NAME=Brain_Tumor_Classification
PROJECT_VERSION=1.0

# Model Parameters
IMAGE_SIZE=224
BATCH_SIZE=32
EPOCHS=20
LEARNING_RATE=0.0001

# Paths
TRAIN_PATH=/kaggle/input/brain-tumor-mri-dataset/Training
TEST_PATH=/kaggle/input/brain-tumor-mri-dataset/Testing
MODEL_SAVE_PATH=/kaggle/working/

# Training Configuration
EARLY_STOPPING_PATIENCE=5
REDUCE_LR_PATIENCE=3
REDUCE_LR_FACTOR=0.5
MIN_LEARNING_RATE=1e-7

# Callbacks
MONITOR_METRIC=val_loss
SAVE_BEST_ONLY=True

πŸ“š Resources

Trained Models

All trained models (.h5 and .keras formats) are available in models.txt:

  • Custom CNN Model
  • VGG16 Model
  • VGG19 Model
  • ResNet50V2 Model
  • MobileNetV2 Model

Kaggle Notebook

Complete interactive notebook: See kaggle_notebook.txt for link

Datasets

Download links available in dataset.txt

Documentation


🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Development Guidelines

  • Follow PEP 8 style guide for Python code
  • Add docstrings to functions and classes
  • Update README.md for significant changes
  • Test code before submitting PR

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ‘₯ Authors

  • Abdelhady Ali - Initial work - MYGitHub

πŸ™ Acknowledgments

  • Datasets: Thanks to Sartaj Bhuvaji and Masoud Nickparvar for providing the datasets
  • Kaggle: For providing the platform and computational resources
  • TensorFlow/Keras: For the deep learning framework
  • Medical Community: For inspiring this work to assist in early diagnosis

πŸ“ž Contact

For questions, suggestions, or collaborations:


⚠️ Disclaimer

This project is for educational and research purposes only. The models should not be used as a substitute for professional medical diagnosis. Always consult qualified healthcare professionals for medical advice and diagnosis.


πŸ“Š Citations

If you use this project in your research, please cite:

@misc{brain_tumor_classification_2025,
  title={AI-Powered Brain Tumor Classification using Deep Learning},
  author={abdelhady ali},
  year={2025},
  publisher={GitHub},
  url={https://github.com/Abdelhady-22/brain_tumor_classification}
}

Made with ❀️ for advancing medical AI

⭐ Star this repo if you find it helpful!

About

Leveraging Convolutional Neural Networks (CNNs) and Transfer Learning to classify brain MRI images into four categories: Glioma, Meningioma, Pituitary Tumor, and No Tumor. This project achieves up to 97% accuracy using ResNet50V2, supporting early diagnosis and clinical decision-making.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors