A deep learning project for binary classification of brain MRI images (tumor vs. no tumor) using Convolutional Neural Networks (CNN).
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
- Python 3.8 or higher
- pip or conda
-
Clone the repository
git clone <repository-url> cd BrainMRIDataSet
-
Create a virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
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)
Run the training script:
python main.pyThis will:
- Load and preprocess images from
data/raw/ - Split data into training (80%) and testing (20%) sets
- Train the CNN model for 10 epochs
- Save the model to
models/brain_mri_cnn.keras - Generate evaluation metrics and confusion matrix
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...
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)
After training, results are saved in:
- Model:
models/brain_mri_cnn.keras - Confusion Matrix:
outputs/confusion_matrix.png - Classification Report: Printed to console
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
- 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.
| 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 |
- Ensure images are in
data/raw/no_tumor/anddata/raw/tumor/ - Check that files have
.jpgor.pngextension - Verify file permissions
- Reduce
BATCH_SIZEinsrc/config.py - Reduce
IMG_SIZEinsrc/config.py - Use a machine with more RAM or GPU
- Ensure you're running from the project root directory
- Verify virtual environment is activated
- Reinstall dependencies:
pip install -r requirements.txt --force-reinstall
- 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
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or suggestions, please open an issue on GitHub.
Last Updated: February 21, 2026