engrsakib
5,433 hrs
Live WakaTime Coding Metric

Repository Workspace

Neurological-MRI-XAI-Pipeline

Explore source files, configuration surfaces, README documentation, and commit history.

Jupyter NotebookOpen on GitHub
★ 0 stars⑂ 0 forksDefault · main

Source viewer

Select a file from the tree

Configuration files and source modules stream through the secure API proxy and render with the active theme accent.

README.md

Neurological MRI XAI Pipeline

Python PyTorch Hugging Face Timm Kaggle License

Modular, production-ready PyTorch pipeline for multi-class neurological MRI classification with explainable AI (XAI) and Vision-Language clinical reporting.

Designed for reproducible research and conference publication (BECITHCON 2026). Refactored from the legacy monolithic notebook (legacy/notebook8010ef336b.ipynb) into a pip-installable package with CLI-driven orchestration.

Repository: github.com/engrsakib/Neurological-MRI-XAI-Pipeline


Overview

CapabilityDescription
ClassificationSwin Transformer (swin_base_patch4_window7_224) with partial fine-tuning (last stage + head)
Class balanceInverse-frequency weighted CrossEntropyLoss across 8 disorder categories
Data integrityPatient-level stratified splits — zero slice leakage between train/val/test
XAIGrad-CAM, attention saliency, SAM-constrained ROI overlays
ReportingFlorence-2 natural-language diagnostic narratives embedded in HTML reports
Ensemble (optional)Soft-voting benchmark across Swin, ConvNeXt, and DenseNet

Pipeline Architecture

flowchart LR
  subgraph data [Data Layer]
    DS[ImageFolder Dataset]
    SPL[Patient-Level Splits]
    DS --> SPL
  end
  subgraph train [Training]
    SPL --> Swin[Swin Classifier]
    Swin --> FT[Partial Fine-Tune + Class Weights]
    FT --> CKPT[best_swin.pt]
  end
  subgraph xai [XAI and Reporting]
    CKPT --> Eval[Test Evaluation]
    CKPT --> GC[Grad-CAM]
    GC --> SAM[SAM ROI Overlay]
    SAM --> F2[Florence-2 Caption]
    F2 --> HTML[HTML Diagnostic Report]
  end

Model & Training Specifications

Primary Classifier — Swin Transformer

ParameterValue
Backboneswin_base_patch4_window7_224 (timm, ImageNet pretrained)
Input resolution224 × 224 RGB
Output classes8 neurological disorders
Fine-tuning strategyFreeze early stages; train last Swin stage + classification head
LoRA (PEFT)Disabled by default (use_lora: false) — partial FT preferred for T4 VRAM
Modulesrc/neuro_mri_xai/models/swin_classifier.py

Optimized Training Configuration (configs/default.yaml)

SettingValuePurpose
OptimizerAdamW, lr=1e-4, weight_decay=1e-2Stable convergence on ~9k training images
SchedulerCosineAnnealingLR (T_max=epochs)Smooth LR decay
LossClass-weighted CrossEntropyMitigates AD / tumor class imbalance
AugmentationFlip (p=0.5), Rotation (±15°), ColorJitterImproved generalization
AMPEnabledFaster epochs on T4 GPU
Early stoppingPatience = 5 epochsPrevents overfitting
Split strategypatient (group-level stratified 80/10/10)Prevents data leakage

Auxiliary Models

ModelRoleModule
SAM (sam_vit_b)Brain ROI segmentation for constrained XAI overlaysmodels/sam_roi.py
Florence-2 (microsoft/Florence-2-base)Clinical caption / diagnostic text generationmodels/florence_reporter.py
ConvNeXt / DenseNetOptional ensemble backbones for benchmark comparisonmodels/classifier.py, evaluation/benchmark.py

VRAM Management (Kaggle T4 — 16 GB)

Heavy models are loaded sequentially during XAI/report stages (vram.sequential_models: true). Training runs Swin only; SAM and Florence-2 load afterward and are unloaded to stay under ~12 GB peak VRAM.


Dataset

Primary source: Neurological Disorders MRI Dataset for XAI

Kaggle mount path (verified):

/kaggle/input/datasets/engrsakib02/neurological-disorders-mri-dataset-for-xai/data/

Expected ImageFolder layout:

data/
├── AD_MildDemented/
├── AD_ModerateDemented/
├── AD_VeryMildDemented/
├── BT_glioma/
├── BT_meningioma/
├── BT_pituitary/
├── MS/
└── Normal/
ClassClinical Category
AD_MildDemented / AD_ModerateDemented / AD_VeryMildDementedAlzheimer's disease (severity stages)
BT_glioma / BT_meningioma / BT_pituitaryBrain tumors
MSMultiple sclerosis
NormalHealthy control

~16,400 axial MRI slices · 8 classes · patient-level stratified 80/10/10 split.


Directory Structure

model/
├── configs/default.yaml              # Central hyperparameter & path configuration
├── Colab_Runner.ipynb                # Kaggle 10-cell orchestrator (subprocess-based)
├── plan/
│   ├── MODEL_PLAN.md                 # Publication roadmap & feasibility report
│   └── index.html                    # HTML version of the publication plan
├── scripts/                          # Data/weight download, CI helpers
├── src/neuro_mri_xai/
│   ├── config.py                     # Typed YAML loader
│   ├── data/
│   │   ├── dataset.py                # ImageFolder + stratified splits
│   │   ├── splits.py                 # Patient/group ID extraction (zero leakage)
│   │   └── transforms.py             # Train/val/test augmentations
│   ├── models/
│   │   ├── classifier.py             # timm factory (Swin / ConvNeXt / DenseNet)
│   │   ├── ensemble.py               # Soft-voting ensemble
│   │   ├── swin_classifier.py        # Swin backbone + partial freeze
│   │   ├── florence_reporter.py      # Florence-2 captioning (task-prompt validated)
│   │   ├── sam_roi.py                # SAM brain ROI extraction
│   │   └── lora.py                   # Optional PEFT LoRA adapters
│   ├── training/
│   │   ├── trainer.py                # AMP, early stopping, checkpointing
│   │   ├── train_cli.py              # Training entry point
│   │   └── kfold_cli.py              # Patient-level k-fold CV
│   ├── evaluation/
│   │   ├── test_eval.py              # Held-out test metrics + batch XAI
│   │   ├── benchmark.py              # Multi-backbone benchmark
│   │   ├── ensemble_eval.py          # Soft-voting ensemble evaluation
│   │   └── metrics.py                # Accuracy, P/R/F1, AUC-ROC, per-class JSON
│   ├── explainability/
│   │   ├── gradcam.py                # Grad-CAM heatmaps
│   │   ├── attention_rollout.py      # Attention saliency maps
│   │   ├── sam_overlay.py            # SAM-constrained overlays
│   │   ├── batch_export.py           # Batch XAI export for test set
│   │   └── xai_cli.py                # Single-image / batch XAI CLI
│   └── report.py                     # HTML diagnostic report generator
├── tests/                            # 80+ unit & smoke tests
└── outputs/                          # Checkpoints, figures, reports (gitignored)
    ├── checkpoints/
    ├── figures/
    ├── logs/
    └── reports/

Configuration & Environment Variables

Edit configs/default.yaml or override at runtime:

Variable / FlagPurpose
--data-dir PATHOverride dataset ImageFolder root (all CLIs)
NEURO_MRI_DATA_DIREnvironment override for dataset path
NEURO_MRI_SAM_ENABLEDtrue / false — enable SAM during XAI/report
NEURO_MRI_SEQUENTIAL_VRAMSequential model load/unload (recommended on T4)
NEURO_MRI_USE_LORAEnable LoRA adapters (default: off)

🚀 Run on Kaggle (Quick Start)

The fastest way to run the full pipeline is Kaggle Notebooks with a free GPU (T4).

1. Create a Kaggle notebook

StepAction
GPUSettings → Accelerator → GPU T4 x2 (or T4)
DatasetAdd Input → Datasets → engrsakib02/neurological-disorders-mri-dataset-for-xai
RunnerUpload or copy cells from Colab_Runner.ipynb

2. Run cells in order

CellWhat it doesOutput
1Check Python / PyTorch / CUDAGPU name printed
2Clone repo → /kaggle/working/Neurological-MRI-XAI-PipelinePROJECT_DIR
3pip install -e . + Segment AnythingPackage importable
4Resolve Kaggle dataset path + env varsDATA_DIR
5Download SAM weightsweights/sam_vit_b_01ec64.pth
6Train Swin (partial fine-tune, ~20 epochs)outputs/checkpoints/best_swin.pt
7Test evaluation (accuracy, F1, ROC, confusion matrix)outputs/figures/metrics.json
8Single-sample XAI (Grad-CAM, attention, SAM overlay)outputs/figures/*_gradcam.png
9HTML diagnostic report (Florence-2 + XAI)outputs/reports/*_report.html
10List saved artifactsFile counts under outputs/

Time: Cell 6 (training) is the bottleneck (~30–90 min on T4 depending on session). Cells 7–9 need the checkpoint from Cell 6.

3. Download results

Kaggle persists /kaggle/working/ automatically. Before the session ends:

  • Open the notebook Output tab, or
  • Save Version → Save & Run All (Commit)

Download outputs/checkpoints/, outputs/figures/, outputs/reports/, and outputs/logs/.

4. One-line equivalent (after Cells 1–5)

cd /kaggle/working/Neurological-MRI-XAI-Pipeline
export DATA_DIR="/kaggle/input/datasets/engrsakib02/neurological-disorders-mri-dataset-for-xai/data"
export NEURO_MRI_SAM_ENABLED=false
export NEURO_MRI_SEQUENTIAL_VRAM=true

python -m neuro_mri_xai.training.train_cli --config configs/default.yaml --data-dir "$DATA_DIR"
python -m neuro_mri_xai.evaluation.test_eval --config configs/default.yaml --checkpoint outputs/checkpoints/best_swin.pt --data-dir "$DATA_DIR"

5. Updating code in an existing Kaggle session

find /kaggle/working/Neurological-MRI-XAI-Pipeline -type d -name "__pycache__" -exec rm -rf {} +
cd /kaggle/working/Neurological-MRI-XAI-Pipeline && git pull

Then Kernel → Restart Session and re-run from Cell 1 (or Cell 3 onward).


Publication Experiments (from plan/MODEL_PLAN.md)

Journal-grade experiments beyond the default Kaggle quickstart:

# 5-fold patient CV with mean ± std (primary metric: macro-F1)
python -m neuro_mri_xai.training.kfold_cli --config configs/default.yaml --folds 5 --data-dir "$DATA_DIR"

# Multi-seed reproducibility (3 seeds × hold-out test + bootstrap CI)
python -m neuro_mri_xai.training.seed_sweep_cli --config configs/default.yaml --seeds 42 43 44 --data-dir "$DATA_DIR"

# External dataset generalization (zero-shot, no fine-tuning)
python -m neuro_mri_xai.evaluation.external_eval \
  --config configs/default.yaml \
  --checkpoint outputs/checkpoints/best_swin.pt \
  --data-dir "$DATA_DIR"

# Test evaluation with bootstrap confidence intervals
python -m neuro_mri_xai.evaluation.test_eval \
  --config configs/default.yaml \
  --checkpoint outputs/checkpoints/best_swin.pt \
  --data-dir "$DATA_DIR" \
  --bootstrap
CommandSummary output
kfold_clioutputs/logs/kfold/kfold_summary.json
seed_sweep_clioutputs/logs/seed_sweep/seed_sweep_summary.json
external_evaloutputs/figures/external/external_eval_summary.json

Full publication roadmap: plan/MODEL_PLAN.md · HTML report: plan/index.html


🚀 Kaggle Quickstart & Execution Guide (Detailed)

Run the full pipeline on Kaggle Notebooks using the official orchestrator: Colab_Runner.ipynb.

The notebook contains 11 cells total — one Markdown intro plus 10 executable Code cells (Cell 1 → Cell 10). Paste each block below into a matching Kaggle Code cell and run in order (later cells depend on PROJECT_DIR, DATA_DIR, and sample_image from earlier steps).

Prerequisites (Markdown Cell — run first)

  1. Settings → Accelerator → GPU (T4 recommended, 16 GB VRAM).
  2. Add Input → Datasets → attach engrsakib02/neurological-disorders-mri-dataset-for-xai.
  3. Run all Code cells sequentially.

Updating an Existing Kaggle Session (cache + git pull)

If you already cloned this repo in a previous Kaggle run and need fresh Python code (e.g., Florence-2 fixes), run the cell below before re-running downstream cells.

Important: Kaggle keeps imported modules in memory. Deleting __pycache__ and pulling git does not reload already-imported packages. After this cell completes, you must manually restart the kernel:

Kernel / Runtime → Restart Session (then re-run from Cell 1 or Cell 3 onward).

# Refresh repo: clear bytecode cache, then pull latest main
import subprocess
from pathlib import Path

PROJECT_DIR = Path("/kaggle/working/Neurological-MRI-XAI-Pipeline")
assert PROJECT_DIR.exists(), "Clone the repo first (Cell 2) before updating."

# Step 1: Clean compiled Python cache & temporary bytecode
subprocess.run(
    [
        "find",
        str(PROJECT_DIR),
        "-type",
        "d",
        "-name",
        "__pycache__",
        "-exec",
        "rm",
        "-rf",
        "{}",
        "+",
    ],
    check=False,
)
subprocess.run(
    ["find", str(PROJECT_DIR), "-name", "*.pyc", "-delete"],
    check=False,
)

# Step 2: Pull the latest updates from Git
subprocess.run(["git", "-C", str(PROJECT_DIR), "pull"], check=True)

print("Cache cleared and git pull complete.")
print("ACTION REQUIRED: Kernel / Runtime -> Restart Session, then re-run cells.")

Equivalent notebook shell one-liners (paste into a Code cell if you prefer ! syntax):

# Step 1: Clean compiled Python cache & temporary bytecode
!find /kaggle/working/Neurological-MRI-XAI-Pipeline -type d -name "__pycache__" -exec rm -rf {} +
!find /kaggle/working/Neurological-MRI-XAI-Pipeline -name "*.pyc" -delete

# Step 2: Pull the latest updates from Git
!cd /kaggle/working/Neurological-MRI-XAI-Pipeline && git pull

Optional — Florence-2 smoke test after restart (Cell 3+ installed deps):

python scripts/smoke_test_florence.py --config configs/default.yaml

Or use importlib.reload(neuro_mri_xai.models.florence_reporter) inside a dedicated test cell (see scripts/smoke_test_florence.py).


StepCellPurposeKey Outputs
0MarkdownPrerequisites & dataset attachment—
1CodeGPU / Python / PyTorch environment checkCUDA status, GPU name
2CodeClone repository to /kaggle/working/PROJECT_DIR
3CodeInstall dependencies (pip install -e .)neuro_mri_xai importable
4CodeResolve dataset path + set env varsDATA_DIR, config summary
5CodeDownload SAM weightsweights/sam_vit_b_01ec64.pth
6CodeTrain Swin classifieroutputs/checkpoints/best_swin.pt
7CodeEvaluate on held-out test setoutputs/figures/metrics.json, confusion matrix
8CodeSingle-sample XAI (Grad-CAM, attention, SAM)outputs/figures/*_gradcam.png, etc.
9CodeHTML diagnostic report (Florence-2 + XAI)outputs/reports/*.html
10CodeList persisted output artifactsFile counts under outputs/

Note: configs/default.yaml uses partial Swin fine-tuning (use_lora: false, freeze_early_backbone: true). The notebook header still references “Swin + LoRA” for historical compatibility; LoRA can be re-enabled via config if needed.


Cell 1 — Environment Check

Verify Python, PyTorch, and GPU availability before installing anything.

# Cell 1: Environment check
import sys

import torch

print(f"Python: {sys.version}")
print(f"PyTorch: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
    print(f"GPU: {torch.cuda.get_device_name(0)}")

Expected output: CUDA available: True and a T4 (or similar) GPU name.


Cell 2 — Clone Repository

Clone the pipeline into Kaggle working storage. Re-running this cell skips clone if the folder already exists.

# Cell 2: Clone repository
import os
import subprocess
from pathlib import Path

REPO_URL = "https://github.com/engrsakib/Neurological-MRI-XAI-Pipeline.git"
PROJECT_DIR = Path("/kaggle/working/Neurological-MRI-XAI-Pipeline")

if not PROJECT_DIR.exists():
    subprocess.run(["git", "clone", REPO_URL, str(PROJECT_DIR)], check=True)
else:
    print(f"Repo already exists at {PROJECT_DIR}")
    print("Tip: For code updates, run the 'Updating an Existing Kaggle Session' cache+pull cell,")
    print("     then Kernel / Runtime -> Restart Session before continuing.")

os.chdir(PROJECT_DIR)
print(f"Working directory: {os.getcwd()}")

Expected output: Working directory: /kaggle/working/Neurological-MRI-XAI-Pipeline


Cell 3 — Install Dependencies

Install project requirements, editable package, and Segment Anything.

# Cell 3: Install dependencies
import subprocess
import sys

import torch

print(f"Kaggle torch version: {torch.__version__}")

subprocess.run(["pip", "install", "-q", "-r", "requirements.txt"], check=True)
subprocess.run(["pip", "install", "-q", "-e", "."], check=True)
subprocess.run(
    ["pip", "install", "-q", "git+https://github.com/facebookresearch/segment-anything.git"],
    check=True,
)

sys.path.insert(0, str(PROJECT_DIR / "src"))
print("Dependencies installed.")

Expected output: Dependencies installed.


Cell 4 — Kaggle Dataset Path & Environment

Verify the Kaggle input mount, resolve the ImageFolder root via load_config, and print the active model settings. SAM is disabled during training to save VRAM.

# Cell 4: Kaggle dataset path + environment
import os
from pathlib import Path

from neuro_mri_xai.config import load_config

DATA_DIR = "/kaggle/input/datasets/engrsakib02/neurological-disorders-mri-dataset-for-xai/data"

print("Kaggle inputs:", os.listdir("/kaggle/input"))
data_path = Path(DATA_DIR)
assert data_path.exists() or data_path.parent.exists(), (
    "Attach engrsakib02/neurological-disorders-mri-dataset-for-xai via Add Input"
)

os.environ["NEURO_MRI_PROJECT_ROOT"] = str(PROJECT_DIR)
os.environ["NEURO_MRI_SAM_ENABLED"] = "false"
os.environ["NEURO_MRI_SEQUENTIAL_VRAM"] = "true"

cfg = load_config("configs/default.yaml", data_dir=DATA_DIR)
DATA_DIR = str(cfg.dataset.data_dir)
print(f"Resolved data dir: {DATA_DIR}")
print(f"Backbone: {cfg.model.backbone}, LoRA: {cfg.model.use_lora}, SAM: {cfg.sam.enabled}")
print(f"Split strategy: {cfg.dataset.split_strategy}, freeze backbone: {cfg.training.freeze_early_backbone}")

Optional — per-class distribution check (append to Cell 4 or run as a separate cell):

from collections import Counter

EXPECTED = cfg.get_class_names()
counts = {}
for cls in EXPECTED:
    folder = Path(DATA_DIR) / cls
    counts[cls] = len(list(folder.glob("*.*"))) if folder.is_dir() else 0

print(f"Total images: {sum(counts.values()):,}")
for cls, n in sorted(counts.items(), key=lambda x: -x[1]):
    print(f"  {cls:25s} {n:6,d}")

Expected output: Resolved path under /kaggle/input/.../data, 8 classes, ~16,400 total images.


Cell 5 — Download SAM Weights

Download sam_vit_b_01ec64.pth into the configured weights directory (required for Cells 8–9).

# Cell 5: Download SAM weights
import subprocess

subprocess.run(["python", "scripts/download_weights.py", "--config", "configs/default.yaml"], check=True)

Expected output: SAM checkpoint saved to weights/sam_vit_b_01ec64.pth.


Cell 6 — Train Swin Classifier

Run the training CLI with optimized defaults: partial backbone freeze, class-weighted loss, augmentation, AdamW + CosineAnnealingLR, patient-level splits.

# Cell 6: Train Swin classifier
import subprocess

subprocess.run(
    [
        "python",
        "-m",
        "neuro_mri_xai.training.train_cli",
        "--config",
        "configs/default.yaml",
        "--data-dir",
        DATA_DIR,
    ],
    check=True,
)
DeliverablePath
Best checkpointoutputs/checkpoints/best_swin.pt
Training curvesoutputs/logs/training_curves.png
LoRA adapter (if enabled)outputs/checkpoints/lora_adapter/

Expected console output:

Using dataset: /kaggle/input/.../data
Partial fine-tune: ... trainable parameters
Using class-weighted CrossEntropyLoss (8 classes)
Epoch 1/20 — train_loss=... val_acc=...
  Saved best checkpoint (val_acc=...)
Best checkpoint: outputs/checkpoints/best_swin.pt (val_acc=...)

Equivalent terminal command:

python -m neuro_mri_xai.training.train_cli \
  --config configs/default.yaml \
  --data-dir "${DATA_DIR}"

Cell 7 — Evaluate on Test Set

Run held-out test evaluation: accuracy, macro P/R/F1, AUC-ROC, confusion matrix, and per-class metrics.

# Cell 7: Evaluate on test set
import subprocess

subprocess.run(
    [
        "python",
        "-m",
        "neuro_mri_xai.evaluation.test_eval",
        "--config",
        "configs/default.yaml",
        "--checkpoint",
        "outputs/checkpoints/best_swin.pt",
        "--data-dir",
        DATA_DIR,
    ],
    check=True,
)

Optional — include batch XAI export during evaluation:

subprocess.run(
    [
        "python", "-m", "neuro_mri_xai.evaluation.test_eval",
        "--config", "configs/default.yaml",
        "--checkpoint", "outputs/checkpoints/best_swin.pt",
        "--data-dir", DATA_DIR,
        "--export-xai",
        "--xai-max-samples", "16",
    ],
    check=True,
)
DeliverablePath
Confusion matrixoutputs/figures/confusion_matrix.png
Per-class metricsoutputs/figures/per_class_metrics.json
ROC curvesoutputs/figures/roc_curves.png
Test metricsoutputs/figures/metrics.json
Classification reportoutputs/figures/classification_report.txt

Equivalent terminal command:

python -m neuro_mri_xai.evaluation.test_eval \
  --config configs/default.yaml \
  --checkpoint outputs/checkpoints/best_swin.pt \
  --data-dir "${DATA_DIR}"

Cell 8 — XAI Visualizations (Single Sample)

Enable SAM, pick a sample MRI slice, and export Grad-CAM, attention saliency, and SAM-constrained overlay figures.

# Cell 8: XAI visualizations (single sample)
import os
import subprocess
from pathlib import Path

os.environ["NEURO_MRI_SAM_ENABLED"] = "true"
sample_image = next(Path(DATA_DIR).rglob("*.jpg"))
print(f"Sample: {sample_image}")

subprocess.run(
    [
        "python",
        "-m",
        "neuro_mri_xai.explainability.xai_cli",
        "--config",
        "configs/default.yaml",
        "--checkpoint",
        "outputs/checkpoints/best_swin.pt",
        "--image",
        str(sample_image),
        "--data-dir",
        DATA_DIR,
        "--output-dir",
        "outputs/figures",
    ],
    check=True,
)
DeliverablePath
Grad-CAM overlayoutputs/figures/<stem>_gradcam.png
Attention saliencyoutputs/figures/<stem>_attention.png
SAM-constrained overlayoutputs/figures/<stem>_sam_overlay.png
SAM ROI maskoutputs/figures/<stem>_sam_mask.png

Equivalent terminal command:

python -m neuro_mri_xai.explainability.xai_cli \
  --config configs/default.yaml \
  --checkpoint outputs/checkpoints/best_swin.pt \
  --image "${SAMPLE_IMAGE}" \
  --data-dir "${DATA_DIR}" \
  --output-dir outputs/figures

Cell 9 — Full HTML Diagnostic Report

Generate an inline-viewable HTML report combining classifier prediction, XAI figures, and Florence-2 clinical narrative.

# Cell 9: Full HTML diagnostic report
import os
from pathlib import Path

from IPython.display import HTML, display

from neuro_mri_xai.report import generate_report

os.environ["NEURO_MRI_SAM_ENABLED"] = "true"
os.environ["NEURO_MRI_SEQUENTIAL_VRAM"] = "true"

sample_image = next(Path(DATA_DIR).rglob("*.jpg"))
print(f"Sample: {sample_image}")

report_path = generate_report(
    checkpoint="outputs/checkpoints/best_swin.pt",
    image=str(sample_image),
    config_path="configs/default.yaml",
    data_dir=DATA_DIR,
)
display(HTML(report_path.read_text()))

Pass skip_florence=True to generate_report(...) or add --skip-florence on the CLI if Florence-2 exceeds VRAM.

Equivalent terminal command:

python -m neuro_mri_xai.report \
  --config configs/default.yaml \
  --checkpoint outputs/checkpoints/best_swin.pt \
  --image "${SAMPLE_IMAGE}" \
  --data-dir "${DATA_DIR}"
DeliverablePath
HTML reportoutputs/reports/<stem>_report.html
Embedded figuresGrad-CAM, attention saliency, SAM overlay (base64 in HTML)
Clinical narrativeFlorence-2 caption + predicted class + confidence

Cell 10 — Persist Outputs

Kaggle automatically persists /kaggle/working/. This cell lists artifact counts before the session ends.

# Cell 10: Persist outputs (Kaggle saves /kaggle/working automatically)
from pathlib import Path

output_root = Path("outputs")
for folder in ["checkpoints", "figures", "reports", "logs"]:
    path = output_root / folder
    if path.exists():
        files = list(path.rglob("*"))
        print(f"{folder}: {len(files)} file(s) under {path.resolve()}")

print("\nDownload outputs from the Kaggle notebook 'Output' tab before the session ends.")

Download outputs/checkpoints/, outputs/figures/, outputs/reports/, and outputs/logs/ via the Kaggle Output tab or Save Version → Save & Run All (Commit).


Optional: Multi-Backbone Benchmark & Ensemble

For comparative analysis (Swin vs ConvNeXt vs DenseNet) and soft-voting ensemble evaluation:

# Train and evaluate all three backbones
python -m neuro_mri_xai.evaluation.benchmark \
  --config configs/default.yaml \
  --data-dir "${DATA_DIR}"

# Soft-voting ensemble on saved checkpoints
python -m neuro_mri_xai.evaluation.ensemble_eval \
  --config configs/default.yaml \
  --checkpoints \
    outputs/checkpoints/benchmark/swin_base_patch4_window7_224/best_swin.pt \
    outputs/checkpoints/benchmark/convnext_base_fb_in22k_ft_in1k/best_swin.pt \
    outputs/checkpoints/benchmark/densenet121/best_swin.pt \
  --data-dir "${DATA_DIR}" \
  --export-xai
Modeltimm IdentifierTypical Role
Swin Transformerswin_base_patch4_window7_224Primary classifier (global + local context)
ConvNeXtconvnext_base.fb_in22k_ft_in1kCNN-style hierarchical features
DenseNet-121densenet121Dense feature reuse baseline
EnsembleSoft-voting average of softmax probabilitiesImproved robustness

Local Development

git clone https://github.com/engrsakib/Neurological-MRI-XAI-Pipeline.git
cd Neurological-MRI-XAI-Pipeline

pip install -r requirements.txt -r requirements-dev.txt
pip install -e .

python -m pytest tests/ -q
python -m ruff check src tests

CLI Reference

CommandPurpose
python -m neuro_mri_xai.training.train_cliTrain Swin classifier
python -m neuro_mri_xai.training.kfold_cli --folds 5Patient-level k-fold CV with val eval + mean ± std
python -m neuro_mri_xai.training.seed_sweep_cli --seeds 42 43 44Multi-seed train + test + bootstrap CI
python -m neuro_mri_xai.evaluation.external_eval --checkpoint ...Zero-shot external dataset test
python -m neuro_mri_xai.evaluation.test_eval --checkpoint ...Test-set metrics + optional batch XAI
python -m neuro_mri_xai.explainability.xai_cli --image ...Single-image XAI
python -m neuro_mri_xai.explainability.xai_cli --batchBatch test-set XAI export
python -m neuro_mri_xai.report --checkpoint ... --image ...HTML diagnostic report
python -m neuro_mri_xai.evaluation.benchmarkMulti-backbone benchmark
python -m neuro_mri_xai.evaluation.ensemble_eval --checkpoints ...Soft-voting ensemble eval

All commands accept --data-dir and --config configs/default.yaml.


Citation & License

If you use this pipeline in academic work (e.g., BECITHCON 2026), please cite the repository:

@software{sakib2026neuromrixai,
  author  = {Md. Nazmus Sakib},
  title   = {Neurological MRI XAI Pipeline: Swin Transformer Classification with SAM and Florence-2 Reporting},
  year    = {2026},
  url     = {https://github.com/engrsakib/Neurological-MRI-XAI-Pipeline}
}

License: GNU General Public License v3.0 — Copyright (C) 2026 Md. Nazmus Sakib.

Medical disclaimer: All AI-generated reports and visualizations are for research and interpretability purposes only. They are not a substitute for professional medical diagnosis.

Back online.