Transfer learning pipeline for classifying brain MRI scans into four tumor categories using a two-phase training strategy on Google Colab.
- Dataset
- Pipeline Overview
- Model Architecture
- Training Strategy
- Results
- Explainable AI (XAI)
- Inference
- Project Structure
The dataset contains brain MRI images split into Training and Testing folders, each with four class subdirectories:
| Class | Description |
|---|---|
glioma |
Malignant glial cell tumor |
meningioma |
Tumor arising from meninges |
notumor |
Healthy brain scan |
pituitary |
Pituitary gland tumor |
- Training split: 80% train / 20% validation (stratified by class)
- Test set: 1,616 images (400 glioma, 400 meningioma, 416 no-tumor, 400 pituitary)
- Input resolution: 224 × 224 pixels (RGB)
Dataset (Training/)
│
├── 80% Train ──► Augmentation (flip, rotate, zoom, contrast)
│ │
└── 20% Val ──► ▼
ResNet-50 preprocess_input
│
▼
┌─────────────┐
│ ResNet-50 │ (ImageNet weights, frozen in Phase 1)
│ (no top) │
└─────────────┘
│
GlobalAveragePooling2D
BatchNormalization
Dense(512, relu) → Dropout(0.5)
Dense(256, relu) → Dropout(0.25)
Dense(4, softmax)
│
┌──────┴──────┐
│ Phase 1 │ Head only, LR = 1e-4, 15 epochs
│ Phase 2 │ Last 30 ResNet layers + head, LR = 1e-5, 30 epochs
└─────────────┘
│
Evaluation on Testing/
│
┌──────┴──────┐
│ Metrics │ Confusion matrix, classification report,
│ + XAI │ ROC curves, Grad-CAM, saliency maps
└─────────────┘
Backbone: ResNet-50 pretrained on ImageNet (include_top=False)
Custom Head:
ResNet-50 output (7 × 7 × 2048)
→ GlobalAveragePooling2D (2048,)
→ BatchNormalization
→ Dense(512, ReLU)
→ Dropout(0.50)
→ Dense(256, ReLU)
→ Dropout(0.25)
→ Dense(4, Softmax)
Data Augmentation (training only):
- Horizontal flip
- Random rotation ± 15°
- Random zoom ± 15%
- Random contrast ± 10%
| Phase | Layers trained | Learning rate | Epochs |
|---|---|---|---|
| 1 — Head training | Custom head only (ResNet-50 frozen) | 1e-4 | 15 |
| 2 — Fine-tuning | Last 30 ResNet-50 layers + head | 1e-5 | 30 |
Callbacks:
ModelCheckpoint— saves best model byval_accuracyCSVLogger— logs metrics per epoch tometrics_history.csvReduceLROnPlateau— halves LR ifval_lossstagnates for 4 epochsEarlyStopping— stops ifval_accuracydoes not improve for 10 epochsDriveVerifyCallback— write-tests Google Drive before each checkpoint save to prevent silent data loss after Colab disconnects
Training curves:
| Metric | Value |
|---|---|
| Test Accuracy | 94.55% |
| Macro Avg Precision | 94.77% |
| Macro Avg Recall | 94.50% |
| Macro Avg F1-Score | 94.43% |
| Test Images | 1,616 |
| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Glioma | 98.52% | 83.25% | 90.24% | 400 |
| Meningioma | 89.02% | 95.25% | 92.03% | 400 |
| No Tumor | 95.17% | 99.52% | 97.30% | 416 |
| Pituitary | 96.39% | 100.00% | 98.16% | 400 |
Note: Pituitary achieves perfect recall (0 missed cases). Glioma has the lowest recall (83.25%) — the most likely source of misclassification errors, typically confused with meningioma.
Green border = correct prediction · Red border = incorrect prediction
Three XAI techniques are applied to interpret model decisions:
| Technique | What it shows |
|---|---|
| Grad-CAM | Which spatial regions of the MRI most influence the prediction (overlaid heatmap) |
| Saliency Maps | Pixel-level gradients — which individual pixels drive the output |
| Activation Patterns | Top-activated ResNet-50 channels per class — class-specific learned features |
| Correct vs. Wrong | Side-by-side Grad-CAM comparison to reveal attention misalignment in failure cases |
Red/yellow regions indicate where the model focuses. Clinically valid results show attention concentrated on tumor tissue rather than background or scanner artifacts.
Run resnet50_testing.ipynb top-to-bottom. All outputs are saved to result/.
pred_class, conf = predict_single_image(
inference_model,
'/path/to/your/scan.jpg',
CLASS_NAMES,
)Outputs a confidence bar chart and prints per-class probabilities.
Brain Tumor/
├── resnet50_brain_tumor.ipynb # Training notebook (two-phase pipeline)
├── resnet50_testing.ipynb # Standalone evaluation & XAI notebook
├── Dataset/
│ ├── Training/
│ │ ├── glioma/
│ │ ├── meningioma/
│ │ ├── notumor/
│ │ └── pituitary/
│ └── Testing/
│ └── (same structure)
├── results/
│ ├── confusion_matrix.png
│ ├── per_class_accuracy.png
│ ├── roc_curves.png
│ ├── training_history_combined.png
│ ├── training_loss.png
│ ├── validation_accuracy.png
│ ├── classification_report.txt
│ ├── metrics_history.csv
│ ├── xai_gradcam.png
│ ├── xai_saliency_maps.png
│ ├── xai_activation_patterns.png
│ └── xai_correct_vs_wrong.png
└── predictions/
├── sample_predictions.png
└── wrong_predictions.png











