Skip to content

BertugTas/Brain-MRI-Classification

Repository files navigation

Brain MRI Classification Using Deep Learning

A deep learning project for binary classification of brain MRI images (tumor vs. no tumor) using Convolutional Neural Networks (CNN).

Project Structure

BrainMRIDataSet/
├── data/
│   ├── raw/
│   │   ├── no_tumor/          # Healthy brain MRI images
│   │   └── tumor/             # Brain MRI images with tumors
│   └── processed/             # Preprocessed data
├── src/
│   ├── __init__.py
│   ├── config.py              # Configuration settings
│   ├── data_loader.py         # Data loading and preprocessing
│   ├── model.py               # CNN model architecture
│   ├── train.py               # Training script
│   └── evaluate.py            # Evaluation and visualization
├── models/                    # Trained model weights
├── outputs/                   # Results and visualizations
├── logs/                      # Training logs
├── notebooks/
│   └── EDA.ipynb             # Exploratory Data Analysis
├── main.py                    # Main entry point
├── requirements.txt           # Project dependencies
├── .gitignore                # Git ignore rules
└── README.md                 # This file

Installation

Prerequisites

  • Python 3.8 or higher
  • pip or conda

Setup

  1. Clone the repository

    git clone <repository-url>
    cd BrainMRIDataSet
  2. Create a virtual environment

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt

Data Preparation

Dataset Organization

Place your brain MRI images in the following structure:

data/raw/
├── no_tumor/
│   ├── image1.jpg
│   ├── image2.jpg
│   └── ...
└── tumor/
    ├── image1.jpg
    ├── image2.jpg
    └── ...

Requirements:

  • Images should be in JPG or PNG format
  • Recommended image size: at least 128x128 pixels (will be resized)
  • Ensure balanced classes (similar number of tumor and no_tumor images)

Usage

Training

Run the training script:

python main.py

This will:

  1. Load and preprocess images from data/raw/
  2. Split data into training (80%) and testing (20%) sets
  3. Train the CNN model for 10 epochs
  4. Save the model to models/brain_mri_cnn.keras
  5. Generate evaluation metrics and confusion matrix

Configuration

Modify src/config.py to adjust:

  • Image size: IMG_SIZE
  • Batch size: BATCH_SIZE
  • Number of epochs: EPOCHS
  • Test split ratio: TEST_SIZE
  • And more...

Model Architecture

The CNN model includes:

  • Conv2D Layers: 3 convolutional layers (32, 64, 128 filters)
  • Batch Normalization: For stable training
  • MaxPooling: For dimensionality reduction
  • Dropout: For regularization (0.25 and 0.5)
  • Dense Layers: 2 fully connected layers (128 and 1 units)

Results

After training, results are saved in:

  • Model: models/brain_mri_cnn.keras
  • Confusion Matrix: outputs/confusion_matrix.png
  • Classification Report: Printed to console

Performance Metrics

The model evaluates:

  • Accuracy: Percentage of correct predictions
  • Precision, Recall, F1-Score: Per-class metrics
  • Confusion Matrix: True Positives, True Negatives, False Positives, False Negatives

Dependencies

  • tensorflow: Deep learning framework
  • opencv-python: Image processing
  • numpy: Numerical computations
  • scikit-learn: Machine learning utilities
  • matplotlib: Data visualization
  • seaborn: Statistical visualization

See requirements.txt for exact versions.

Important Notes

⚠️ Data Privacy: Ensure you have proper permissions to use MRI images and comply with HIPAA/GDPR regulations if applicable.

⚠️ Medical Disclaimer: This model is for educational purposes only. Do not use for actual medical diagnosis without proper validation and regulatory approval.

File Descriptions

File Purpose
main.py Entry point - runs training and evaluation
src/config.py Centralized configuration
src/data_loader.py Loads and preprocesses images
src/model.py Defines CNN architecture
src/train.py Training loop and model saving
src/evaluate.py Model evaluation and visualization

Troubleshooting

"No images found" Error

  • Ensure images are in data/raw/no_tumor/ and data/raw/tumor/
  • Check that files have .jpg or .png extension
  • Verify file permissions

Out of Memory Error

  • Reduce BATCH_SIZE in src/config.py
  • Reduce IMG_SIZE in src/config.py
  • Use a machine with more RAM or GPU

Module Import Errors

  • Ensure you're running from the project root directory
  • Verify virtual environment is activated
  • Reinstall dependencies: pip install -r requirements.txt --force-reinstall

Future Improvements

  • Transfer learning with pre-trained models (ResNet, VGG)
  • Data augmentation for better generalization
  • K-fold cross-validation
  • Hyperparameter tuning with Keras Tuner
  • Model explainability (Grad-CAM)
  • Web interface for predictions

License

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

Contact

For questions or suggestions, please open an issue on GitHub.


Last Updated: February 21, 2026

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages