An advanced deep learning model developed to classify brain tumors from MRI scans using state-of-the-art techniques. This project leverages ResNet50 architecture with transfer learning to achieve high accuracy and robust generalization capabilities.
Overview
Brain tumor classification is a critical task in medical imaging that requires high precision and reliability. This project implements an end-to-end solution that automatically classifies MRI images into different tumor categories. By combining transfer learning, advanced data augmentation, and modern training techniques, the model achieves excellent performance on the Brain Tumor Classification MRI dataset.
Key Highlights:
Achieves >90% accuracy on test set
Implements early stopping to prevent overfitting
Provides comprehensive evaluation metrics and visualizations
GPU-accelerated training for faster convergence
Fully reproducible with detailed configuration options
Modify the CONFIG dictionary to adjust training parameters:
python
1CONFIG ={2'model_name':'ResNet50_Improved',3'batch_size':32,# Batch size for training4'lr':0.001,# Initial learning rate5'epochs':25,# Maximum number of epochs6'scheduler_step':7,# Steps before LR reduction7'gamma':0.1,# Learning rate decay factor8'weight_decay':5e-4,# L2 regularization9'dropout_rate':0.6,# Dropout probability10'early_stopping_patience':5,# Epochs to wait before stopping11'early_stopping_min_delta':0.001# Minimum improvement threshold12}
Custom Model Training
python
1# Load a trained model2model = get_model()3# Train with custom parameters4model_ft, logs = train_model(5 model, criterion, optimizer, exp_lr_scheduler,6 num_epochs=307)
ROC-AUC: Area under the Receiver Operating Characteristic curve for each class
Example Output
==================================================
Epoch 5/25
==================================================
TRAIN | Loss: 0.2345 | Acc: 0.9123 (91.23%)
TEST | Loss: 0.2789 | Acc: 0.8945 (89.45%)
✅ NEW RECORD! Test Acc: 0.8945
...
==================================================
SUMMARY REPORT
==================================================
Model: ResNet50_Improved
Total Epochs: 18
Best Test Accuracy: 0.9234 (92.34%)
Final Test Accuracy: 0.9156
Final Train Accuracy: 0.9512
Overfitting Gap: 0.0356
==================================================
DETAILED PERFORMANCE REPORT
==================================================
precision recall f1-score support
Glioma 0.9456 0.9231 0.9342 143
Meningioma 0.9167 0.9375 0.9270 160
Pituitary 0.9545 0.9167 0.9355 120
No Tumor 0.9200 0.9286 0.9243 175
micro avg 0.9287 0.9287 0.9287 598
macro avg 0.9342 0.9265 0.9302 598
weighted avg 0.9298 0.9287 0.9292 598
Data Augmentation
Transformations applied during training to enhance model robustness:
python
1- RandomResizedCrop(224, scale=(0.8,1.0))# Random cropping and resizing2- RandomHorizontalFlip()# Horizontal flip with 50% probability3- RandomRotation(20°)# Random rotation ±20 degrees4- ColorJitter(brightness=0.3, contrast=0.3, saturation=0.2)# Color variations5- RandomAffine(translate=(0.1,0.1))# Random translation6- Normalize(mean=[0.485,0.456,0.406],# ImageNet normalization7 std=[0.229,0.224,0.225])
These augmentations help the model learn invariant features and improve generalization.
Early Stopping Mechanism
Early stopping prevents overfitting by halting training when the model stops improving:
Patience: 5 consecutive epochs without improvement
Min Delta: 0.001 (minimum improvement threshold)
Mode: Maximization (highest test accuracy is the goal)
Saved Model: Best weights are automatically saved to best_model.pth
Advanced Improvements
This project implements cutting-edge techniques for improved performance:
✅ Transfer Learning: Leverage pre-trained ImageNet weights to reduce training time
✅ Strong Augmentation: Diverse augmentation strategies to prevent overfitting
✅ Dropout Regularization: 60% dropout to reduce co-adaptation of neurons
✅ Weight Decay (L2): 5×10⁻⁴ regularization to penalize large weights
✅ Learning Rate Scheduling: Dynamic LR adjustment based on training progress
✅ Early Stopping: Optimal model selection without manual intervention
✅ Comprehensive Evaluation: Multi-metric assessment including ROC-AUC scores
Troubleshooting
CUDA/GPU Issues
If you encounter GPU-related errors, the script automatically falls back to CPU: