A deep learning project for classifying brain MRI scans into three categories: glioma, meningioma, and other brain tumors using Convolutional Neural Networks (CNN) with PyTorch.
This project implements a custom CNN architecture to classify brain MRI images into three distinct categories:
- Brain Glioma - A type of tumor that occurs in the brain and spinal cord
- Brain Meningioma - A tumor that arises from the meninges
- Brain Tumor - Other types of brain tumors
The model achieves 86.9% accuracy on the test set with strong performance across all three classes.
| Activation | Final Epoch | Train Loss | Val Loss | Train Acc (%) | Val Acc (%) |
|---|---|---|---|---|---|
| LeakyReLU | 50 | 0.450 | 0.356 | 81.67 | 86.47 |
| SiLU | 50 | 0.477 | 0.375 | 80.37 | 85.48 |
| ReLU | 50 | 0.459 | 0.416 | 81.48 | 84.71 |
| ELU | 50 | 0.502 | 0.749 | 79.38 | 63.70 |
The LeakyReLU activation function achieved the best validation accuracy of 86.47%, closely followed by SiLU and ReLU. ELU showed signs of overfitting with significantly lower validation accuracy.
Custom CNN architecture (MRIClassifier) with the following layers:
- Convolutional Block 1: Conv2d (3→32) → ReLU → MaxPool
- Convolutional Block 2: Conv2d (32→64) → ReLU → MaxPool
- Convolutional Block 3: Conv2d (64→128) → ReLU → MaxPool
- Flatten Layer
- Fully Connected Block 1: Linear → ReLU → Dropout(0.5)
- Fully Connected Block 2: Linear → ReLU → Dropout(0.5)
- Output Layer: Linear (→3 classes)
Input: 224×224×3 RGB images (grayscale converted to 3 channels)
Output: 3-class softmax predictions
- Python 3.x
- PyTorch - Deep learning framework
- torchvision - Image transformations and datasets
- NumPy - Numerical computations
- Pandas - Data manipulation and analysis
- Matplotlib - Visualization
- PIL (Pillow) - Image processing
- Kagglehub - Dataset management
- Total Images: 6,056 MRI scans
- Classes: 3 (brain_glioma, brain_menin, brain_tumor)
- Train/Val/Test Split: Stratified split for balanced evaluation
- Test Set Size: 909 images
transforms = v2.Compose([
v2.Resize((224, 224), antialias=True),
v2.Grayscale(num_output_channels=3),
v2.ToImage(),
v2.ToDtype(torch.float32, scale=True),
v2.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])The project includes experiments with different activation functions:
| Activation | Final Val Accuracy | Model File |
|---|---|---|
| SiLU | 85.48% | ClassyModel_SiLU.pth |
| LeakyReLU | ~86% | ClassyModel_LeakyReLU.pth |
| ELU | 63.70% | ClassyModel_ELU.pth |
See BrainCancerMRI_diffacfunc.ipynb for detailed comparisons.
- Loss Function: CrossEntropyLoss
- Optimizer: Adam
- Epochs: Multiple training runs with different configurations
- Dropout: 0.5 (applied in fully connected layers)
- Device: CPU/CUDA (automatic detection)
This project was developed as part of a Data Mining and Machine Learning coursework at Heriot-Watt University.