Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Brain Tumor Classification — ResNet-50 + TensorFlow/Keras

Transfer learning pipeline for classifying brain MRI scans into four tumor categories using a two-phase training strategy on Google Colab.


Table of Contents


Dataset

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)

Pipeline Overview

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
                    └─────────────┘

Model Architecture

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%

Training Strategy

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 by val_accuracy
  • CSVLogger — logs metrics per epoch to metrics_history.csv
  • ReduceLROnPlateau — halves LR if val_loss stagnates for 4 epochs
  • EarlyStopping — stops if val_accuracy does not improve for 10 epochs
  • DriveVerifyCallback — write-tests Google Drive before each checkpoint save to prevent silent data loss after Colab disconnects

Training curves:

Training History

Training Loss

Validation Accuracy


Results

Overall Performance

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

Per-Class Metrics

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.

Per-Class Accuracy

Per-Class Accuracy

Confusion Matrix

Confusion Matrix

ROC Curves (One-vs-Rest)

ROC Curves

Sample Predictions

Green border = correct prediction · Red border = incorrect prediction

Sample Predictions

Wrong Predictions (Error Analysis)

Wrong Predictions


Explainable AI (XAI)

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

Grad-CAM — Model Attention Regions

Grad-CAM

Red/yellow regions indicate where the model focuses. Clinically valid results show attention concentrated on tumor tissue rather than background or scanner artifacts.

Saliency Maps — Pixel Importance

Saliency Maps

Learned Feature Activations by Class

Activation Patterns

Correct vs. Wrong Predictions — Attention Comparison

Correct vs Wrong XAI


Inference

Batch inference on a test directory

Run resnet50_testing.ipynb top-to-bottom. All outputs are saved to result/.

Single image inference

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.


Project Structure

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages