SDLC-Corp/Medical-Imaging-Brain-tumor-analysis
<div align="center">
π§ Brain Tumor Detection β SDLC Corp
    
An SDLC Corp deep-learning web app that classifies brain MRI scans into four categories (glioma Β· meningioma Β· no-tumor Β· pituitary). Two models run entirely in the browser via ONNX Runtime Web β a ViT for the prediction (~97% on a held-out test set) and a ResNet50 for grad-free explainability β deployable as a free Hugging Face Static Space.
</div>
β οΈ Medical Disclaimer
This tool is for educational and research purposes only. It is not a substitute for professional medical diagnosis. Always consult a qualified radiologist or medical professional for clinical decisions.
π Table of Contents
- About the Project
- How It Works
- Advanced ML features
- Classes & data
- Model Architecture
- Model Performance
- Project Structure
- Getting Started
- Tech Stack
- Credits & Licenses
π¬ About the Project
Brain tumors are among the most critical conditions in medicine β accurate classification helps guide treatment decisions. This SDLC Corp project packages a fine-tuned ResNet50 MRI classifier into a modern, privacy-preserving web app that runs the model entirely in the user's browser (no server, no upload) and layers on real explainability and reliability tooling.
What this project covers:
- Exporting a PyTorch ResNet50 to ONNX (fp16) for in-browser inference via ONNX Runtime Web
- Class Activation Mapping β instant, grad-free, per-class explainability heatmaps
- Test-time augmentation, predictive-entropy uncertainty, and embedding-based out-of-distribution detection
- Content-based retrieval of the most similar reference scans
- One-command deployment to a free Hugging Face Static Space
βοΈ How It Works
User selects an MRI Scan (.jpg / .png) β stays in the browser
β
βββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
βΌ βΌ β
ViT preprocessing ResNet preprocessing β
(resize 224, norm [-1,1]) (resize 256 β crop 224 β ImageNet) β
βΌ βΌ β
ViT-base/16 (ONNX fp16) ResNet50 (ONNX fp16, 2 outputs) β
β logits β softmax β logits + last-conv feature maps β
β β β
βΌ βΌ βΌ
Prediction + confidence Grad-free Class Activation Map GAP embedding β
bars + uncertainty (per-class, instant) OOD + retrieval
β
βΌ
ββββββββββββ¬βββββββββββββ¬βββββββββββ¬βββββββββββββ
β Glioma β Meningioma β No-tumor β Pituitary β
ββββββββββββ΄βββββββββββββ΄βββββββββββ΄βββββββββββββπ§ͺ Advanced ML features (all in-browser)
Beyond a single forward pass, the app runs a small suite of real inference-time ML techniques β entirely client-side, with negligible extra download:
Why OOD matters here: softmax is overconfident on garbage β feed the app pure noise and it will predict a tumor class at ~100% "confidence". The embedding-based OOD check catches exactly this case and warns the user, which softmax confidence alone never would.
These assets are produced by compute_stats.py (run after export_onnx.py).
π Classes & data
The classifier predicts four categories of T1-weighted brain MRI:
The underlying model weights were fine-tuned on a public Brain Tumor MRI image-classification dataset (the four-class glioma / meningioma / no-tumor / pituitary set). See Credits & Licenses for provenance.
β οΈ SDLC Corp has not independently re-benchmarked these third-party weights; accuracy on new data is not claimed here (see Model Performance).
ποΈ Model Architecture
The app runs two complementary models in the browser, each doing what it's best at:
βββββββββββββββββββββββββββββββββββββββββββ
MRI ββ¬β resize 224 βββββΊ ViT-base/16 (higher accuracy) β
β norm [-1,1] β β prediction Β· confidence Β· uncertaintyβ β displayed
β βββββββββββββββββββββββββββββββββββββββββββ
β βββββββββββββββββββββββββββββββββββββββββββ
ββ resize 256 ββββββΊ ResNet50 (GAP β Linear head) β
crop 224 β β CAM heatmap (grad-free) β β explain
ImageNet norm β β 2048-d embedding β OOD + retrieval β
βββββββββββββββββββββββββββββββββββββββββββThe ResNet is kept specifically because its global-average-pool β Linear head makes grad-free CAM possible (a ViT has no such head). Both are exported to ONNX float16 (export_onnx.py): ResNet 47 MB, ViT 172 MB β parity with their PyTorch sources ~1e-4 / 1e-6 (verify_pipeline.py). Each falls back to fp32 if a browser can't run fp16 in WASM.
π Model Performance
Measured with benchmark.py / benchmark_ensemble.py on a held-out test set of 1,311 labelled MRIs (the Kaggle "Brain Tumor MRI" Testing split; label order matches the models).
The app therefore uses the ViT for prediction and the ResNet50 for the CAM heatmap + feature embeddings (OOD / retrieval).
ViT per-class F1 (deployed model): glioma 0.967, meningioma 0.948, notumor 0.985, pituitary 0.980 β and glioma recall 0.963 (vs the ResNet's 0.713). Run python benchmark_ensemble.py for the full breakdown.
Read this honestly:
- Averaging the two models is slightly worse than the ViT alone β the ResNet (86%) is too weak to help, so a naive ensemble drags the ViT down. The real win was switching the predictor to the ViT (+11 points; glioma recall 0.71 β 0.96).
- This is in-distribution generalization; it assumes the checkpoints trained on the Training split and did not see this Testing split. If they overlapped, 97% is optimistic, and real-world accuracy on other scanners/institutions is lower.
- The deployed fp16 ONNX models match their PyTorch sources (parity ~1e-4 / 1e-6).
- Still not a medical device β even at 97% test accuracy, ~1 in 30 is wrong.
π Project Structure
BRAIN TUMOR DETECTION [END 2 END]/
β
β ββ Static Space (deployed, in-browser) ββββββββββββββββββββββββββ
βββ index.html # Space entry point (sdk: static)
βββ style.css # UI styles
βββ app.js # onnxruntime-web inference + CAM + TTA + uncertainty + OOD + retrieval
βββ cam_weights.json # classifier weights for in-browser CAM (generated)
βββ reference.json # gallery feature embeddings for kNN retrieval (generated)
βββ advanced.json # OOD threshold + uncertainty config (generated)
βββ labels.json # labels + preprocessing config (generated)
βββ π model/
β βββ vit.fp16.onnx # prediction model, ~172 MB (git-lfs)
β βββ vit.onnx # fp32 fallback, ~343 MB (git-lfs)
β βββ bt_resnet50.fp16.onnx # CAM/embedding model, ~47 MB (git-lfs)
β βββ bt_resnet50.onnx # fp32 fallback, ~94 MB (git-lfs)
βββ π examples/ # example MRIs shown in the UI
β
β ββ Tooling & PyTorch reference ββββββββββββββββββββββββββββββββββ
βββ export_onnx.py # PyTorch β ONNX (fp32 + fp16) + labels.json + cam_weights.json
βββ compute_stats.py # gallery embeddings + OOD threshold (reference.json, advanced.json)
βββ benchmark.py # accuracy/F1/confusion matrix on a labelled held-out test set
βββ benchmark_ensemble.py # compares ResNet vs ViT vs ensemble on the test set
βββ verify_pipeline.py # checks browser preprocessing == PyTorch
βββ app_gradio.py # full Gradio app w/ real Grad-CAM (local)
βββ π src/ # config Β· model Β· inference Β· gradcam
βββ π Brain-Tumor-Test-Images/ # source images copied into examples/
β
βββ requirements.txt # deps for the Gradio reference app
βββ requirements-export.txt # deps for export_onnx.py (one-time)
βββ .gitattributes # git-lfs rules for *.onnx
βββ .gitignore
βββ README.md # you are hereπ Getting Started
1. Get the repository
git clone <your-sdlc-corp-repo-url>
cd brain-tumor-detection2. Set up environment
python -m venv venv
source venv/bin/activate # Linux / macOS
venv\Scripts\activate # Windows
pip install -r requirements.txtThis project ships two front-ends that share the same model:
Option A β Run the static web app locally
First export the model to ONNX (one-time; needs the export deps):
pip install -r requirements-export.txt
python export_onnx.py # downloads weights, writes model/*.onnx + labels.json + cam_weights.json
python compute_stats.py # writes reference.json + advanced.json (embeddings + OOD threshold)Then serve the folder as static files (any static server works):
python -m http.server 8000
# open http://localhost:8000Upload an MRI scan β you get a prediction, per-class confidence bars, and an instant Class Activation Map (click any class to see where the model looks for it) β all computed in the browser in ~1 second.
Option B β Run the full Gradio app (real Grad-CAM)
pip install -r requirements.txt
python app_gradio.py # opens http://127.0.0.1:7860Weights (bt_resnet50_model.pt) auto-download from Google Drive on first run. Override with the BT_MODEL_URL / BT_GDRIVE_ID environment variables.
βοΈ Deploy to Hugging Face Spaces (free Static Space)
On the free plan, HF Spaces only offers Static Spaces (Gradio/Docker require PRO). This repo targets exactly that: the YAML block at the top of this README sets sdk: static, and the models run in the browser β no server needed.
This repo is already built and committed (git + LFS initialised, fp16 models tracked, main branch). To deploy, just create a Static Space and push:
# 1. Create a new Space β SDK: Static β https://huggingface.co/new-space
# 2. Add it as a remote and push (LFS uploads the fp16 .onnx models):
git remote add space https://huggingface.co/spaces/<your-username>/<space-name>
git push space mainHF then serves index.html at the Space URL; the browser fetches the two fp16 ONNX models and runs inference locally. Because everything is static, the Space is free and always-on (no cold starts).
Sizes: the fp16 models (~172 MB ViT + ~47 MB ResNet β 220 MB) download to each visitor once, then are cached. The fp32 fallbacks are.gitignored to keep the repo lean; add them back (edit.gitignore) for older browsers that can't run fp16 in WASM.
Rebuilding the assets from scratch (optional)
The .onnx models and JSON assets are already committed. To regenerate them (e.g. after changing models):
pip install -r requirements-export.txt
python export_onnx.py # ViT + ResNet β ONNX (fp16 + fp32) + labels.json + cam_weights.json
python compute_stats.py # reference.json + advanced.json (embeddings + OOD threshold)
python benchmark.py # (optional) accuracy on a held-out test setπ₯οΈ App Preview
βββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββ
β 1 Β· Choose an MRI scan β 2 Β· Result β
β β β
β [ Click to upload / drag ] β π‘ Meningioma β
β β Confidence: 35.7% β
β Or try an example: β β
β [βͺ][βͺ][βͺ][βͺ][βͺ] β Meningioma ββββββββββ 35.7% β
β β Glioma ββββββββββ 25.7% β
β βββββββββββ¬ββββββββββ β No tumor ββββββββββ 22.1% β
β β Input β CAM β β Pituitary ββββββββββ 16.5% β
β β (MRI) β(heatmap)β β β click a class to switch CAM β
β βββββββββββ΄ββββββββββ β [ Analyze ] opacity βββββ β
β β β οΈ Educational use only. β
βββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββ
Everything runs in the browser β the image never leaves the device.π οΈ Tech Stack
π Credits & Licenses
This application β the in-browser inference pipeline, Class Activation Mapping, test-time augmentation, uncertainty/OOD tooling, retrieval, UI, and export/verification scripts β is Β© 2026 SDLC Corp, released under the Apache License 2.0.
It builds on the following third-party components, used under their licenses (this section serves as the attribution NOTICE required by Apache-2.0 Β§4):
The model weights are third-party pretrained artifacts used under the Apache-2.0 license; SDLC Corp does not claim to have trained them and has not independently validated them for clinical use.
Further reading:
- Deep Residual Learning for Image Recognition β He et al. (2015)
- Grad-CAM / CAM β Zhou et al. (2016)
- Energy-based / feature-space OOD detection β Lee et al. (2018)
<div align="center">
Β© 2026 SDLC Corp Β· For research & educational use only β not a medical device.
</div>
