An adversarially robust AI system for MRI-based brain tumor classification, featuring a custom hybrid architecture, physics-based attack simulation, and intelligent signal recovery.
NeuroScan β an end-to-end, adversarially resilient brain tumor diagnostic platform powered by a custom deep learning architecture.
Brain tumor classification from MRI scans is a critical medical AI challenge. Most deployed models fail silently when scans are degraded by hardware noise, motion artifacts, or transmission errors β a clinically dangerous gap.
NeuroScan addresses this by building an attack-aware, defense-integrated diagnostic pipeline that:
- Classifies MRI scans into 4 categories: Glioma, Meningioma, Pituitary Tumor, and No Tumor
- Simulates real-world scan corruption via a 7-layer adversarial attack engine (OmniChaos)
- Recovers degraded scans through domain-matched signal healing (VisualHealer + FourierHealer)
- Exposes all diagnostics via a REST API and two interactive UIs (Streamlit + Gradio)
The model was developed through 5 iterative experiments, evolving from a MobileNetV2 baseline to the final HybridChimeraAI β a custom PyTorch architecture combining CNNs, Transformers, and a Mixture-of-Experts (MoE) classifier β achieving 96% shielded recovery accuracy on the final benchmark.
| Layer | Technology |
|---|---|
| Core AI Model | PyTorch β custom HybridChimeraAI (CNN + Transformer + MoE) |
| Transfer Learning | TensorFlow / Keras β MobileNetV2, Xception |
| Attack Simulation | NumPy, SciPy, OpenCV |
| Defense / Pre-processing | OpenCV (CLAHE, Fourier band-stop filter, NL-Means denoising) |
| REST API Backend | Flask, Flask-CORS |
| Streamlit UI | Streamlit, Plotly, Pandas |
| Gradio UI | Gradio |
| Explainability | Custom Class Activation Mapping (CAM) via CNN stem features |
| Dataset | Brain Tumor MRI Dataset β Kaggle |
| Frontend | HTML5, Vanilla CSS, JavaScript (SPA with client-side routing) |
- 4-class MRI classification β Glioma, Meningioma, Pituitary Tumor, No Tumor
- HybridChimeraAI β custom PyTorch architecture combining:
- 3-layer CNN stem for spatial feature extraction
- Transformer encoder (8-head self-attention, 2 layers)
- Mixture-of-Experts (MoE) classifier with 4 expert sub-networks and a learned gating function
- Test-Time Augmentation (TTA) β 3-vote voting ensemble across clean, rotated, and zoomed views for more stable inference
- 5-Layer Visual Attack β elastic pixel warping (Gaussian displacement fields), edge-darkening shadow overlay, and Gaussian noise injection
- 2-Layer Physics / K-Space Attack β Fourier-domain corruption mimicking MRI hardware artifacts (stripe noise caused by frequency-domain spike artifacts β a real failure mode in clinical MRI scanners)
- VisualHealer β NL-Means denoising + CLAHE (Contrast Limited Adaptive Histogram Equalization) on LAB color space to recover edge detail from visual noise
- FourierHealer β Fourier-domain band-stop filtering that surgically removes k-space spike artifacts while preserving anatomical structure
- Diagnostic Pipeline β upload MRI scan β view attack β view recovery β get CAM heatmap + class prediction with confidence scores
- Chaos Sandbox β interactive toggle of individual attack layers with live re-prediction
- Batch Auditor β upload a ZIP of patient scans, get per-image predictions, overall accuracy, and a Plotly confusion matrix
GET /api/healthβ model health + loaded status checkPOST /api/analyzeβ upload MRI image β returns predicted class, confidence, per-class probabilities, alert status, and base64 images (original / attacked / shielded)
- Multi-page Single-Page Application with client-side routing
- Upload MRI β see diagnostic pipeline results in real time
- Connects to Flask API backend
- Lightweight three-panel view: Original β Attacked β Shielded with classification confidence bars and clinical alert status
Brain-Tumor-Detector/
β
βββ apex_master.py # Core PyTorch architecture + attack/defense modules
β βββ HybridChimeraAI # CNN + Transformer + MoE model
β βββ OmniChaosInjector # 7-layer adversarial attack engine
β βββ VisualHealer # NL-Means + CLAHE defense
β βββ FourierHealer # K-Space band-stop defense
β βββ TTAPipeline # Test-Time Augmentation inference
β
βββ api.py # Flask REST API β wraps model with /api/analyze endpoint
βββ app.py # Streamlit Command Center (3-module UI)
βββ app_main.py # Gradio Diagnostic Workstation (TF/Keras)
β
βββ frontend/ # Web SPA Frontend
β βββ index.html # Main entry point
β βββ router.js # Client-side routing
β βββ styles.css # Global CSS
β βββ views/ # Page components
β
βββ train_robust.py # Experiment 1: MobileNetV2 baseline
βββ train_extreme.py # Experiment 2: Xception with heavy augmentation
βββ train_final_98.py # Experiment 3: MobileNetV2 + CLAHE fine-tuning
βββ train_unbeatable.py # Experiment 4-5: Final champion model
β
βββ real_benchmark.py # Full test-set evaluation + classification report
βββ test_security.py # Triple-scenario robustness test (Clean/Attack/Shield)
βββ verify_robustness.py # Noise-level stress test
βββ check_labels.py # Label alignment validation
βββ generate_attack.py # Standalone attack/shield visual demo
βββ save_visuals.py # Generate comparison images for presentation
β
βββ experiment_log.md # Full iterative experiment journal (5 experiments)
β
βββ data/
β βββ Training/ # Kaggle training set (by class folder)
β βββ Testing/ # Kaggle test set (by class folder)
β
βββ models/
β βββ v4_Unbeatable_Final.h5 # TF/Keras champion model (96% accuracy)
β βββ apex_master_weights.pth # PyTorch HybridChimeraAI weights
β
βββ presentation_assets/ # Auto-generated comparison/graph images
- Python 3.9+
- CUDA-capable GPU recommended (CPU inference supported)
- pip
git clone https://github.com/MohdAltamish/Brain-Tumor-Detector.git
cd Brain-Tumor-Detectorpython -m venv .venv
source .venv/bin/activate # macOS/Linux
# .venv\Scripts\activate # Windowspip install torch torchvision tensorflow streamlit gradio \
flask flask-cors \
opencv-python-headless scipy numpy pillow \
pandas plotly scikit-learnGet the dataset from Kaggle: Brain Tumor MRI Dataset
Place the downloaded folders as:
data/
βββ Training/
β βββ glioma/
β βββ meningioma/
β βββ notumor/
β βββ pituitary/
βββ Testing/
βββ glioma/
βββ meningioma/
βββ notumor/
βββ pituitary/
Option A β Train HybridChimeraAI (PyTorch) from scratch:
python apex_master.py
# Trains for 35 epochs, saves: apex_master_weights.pthOption B β Train the TF/Keras champion model:
python train_unbeatable.py
# Trains for 15 epochs with CLAHE + heavy augmentation
# Saves: models/v4_Unbeatable_Final.h5Pre-trained weights (
apex_master_weights.pth,models/v4_Unbeatable_Final.h5) are included in the repository for direct inference without training.
python api.py
# API running at: http://localhost:5050GET http://localhost:5050/api/healthβ health checkPOST http://localhost:5050/api/analyzeβ send MRI image, receive diagnosis
streamlit run app.py
# Navigate to: http://localhost:8501Use the sidebar to switch between:
- Diagnostic Pipeline β upload + analyze scan
- Chaos Sandbox β interactive attack toggle
- Batch Auditor β ZIP upload + confusion matrix
python app_main.py
# Navigate to the local URL printed in the terminalOpen frontend/index.html in a browser (requires Flask API running on port 5050).
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
INPUT MRI SCAN
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββ
β OmniChaos Attack Engine β
β βββββββββββββββββββββββββββ β
β β 5-Layer Visual Attack β β
β β β’ Elastic Pixel Warp β β
β β β’ Shadow Overlay β β
β β β’ Gaussian Noise β β
β βββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββββ β
β β 2-Layer K-Space Attack β β
β β β’ Fourier Transform β β
β β β’ Frequency Spike Inj. β β
β βββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββ
β Defense / Healing Layer β
β βββββββββββββββββββββββββββ β
β β VisualHealer β β
β β β’ NL-Means Denoising β β
β β β’ CLAHE Enhancement β β
β βββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββββ β
β β FourierHealer β β
β β β’ Band-Stop Mask β β
β β β’ Inverse FFT Recon. β β
β βββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββ
β HybridChimeraAI β
β β
β [1] CNN Stem β
β 3 conv blocks β
β (64 β 128 β 256 filters) β
β β β
β [2] Transformer Encoder β
β 8-head self-attention β
β 2 encoder layers β
β β β
β [3] Mixture of Experts β
β 4 expert sub-networks β
β Learned gating function β
βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββ
β Test-Time Augmentation (TTA) β
β 3-vote ensemble inference β
β (clean + rotated + zoomed) β
βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Diagnosis + CAM Heatmap + Confidence Score
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
NeuroScan features a high-performance, single-page application (SPA) built with Vanilla JS and CSS, designed for rapid clinical decision support. The interface connects seamlessly to the Flask REST API to provide real-time diagnostics and system monitoring.
The central hub for clinical operations, providing a high-level overview of engine status, API connectivity, and recent diagnostic activity.

A deep-dive view into the 3-stage analysis: Original Scan β OmniChaos Attack β Shielded Recovery. Radiologists can verify the AI's robustness before confirming a diagnosis.

Designed for large-scale clinical audits, the Batch Auditor processes ZIP archives of MRI scans, generating comprehensive performance metrics and match-confidence reports.

Real-time tracking of anomaly detection rates, engine efficiency, and dataset metrics. This module ensures the model is performing within clinical safety parameters.

A secure management system for neurological profiles, allowing physicians to search, filter, and review historical scan results.

An immutable ledger of every system interaction, from user logins to parameter overrides, ensuring full clinical accountability and traceability.

Advanced configuration for the Neural Core, allowing real-time adjustment of AI sensitivity profiles and operational protocols.

The system was developed through 5 iterative experiments, each with a hypothesis, architecture change, results, and analysis:
| # | Architecture | Val Acc | Clean Test | Under Attack | Shielded Recovery |
|---|---|---|---|---|---|
| 1 | MobileNetV2 + Median Filter | 80.9% | 66% | 38% | 58% |
| 2 | Xception (overfitting) | 73.3% | 56% | 42% | 42% |
| 3 | MobileNetV2 + CLAHE Fine-tune | 91.8% | β | β | β |
| 4 | v3 Robustness Validation | β | 74% | 42% | 70% |
| 5 β | MobileNetV2 + Heavy Aug (Champion) | 92.7% | 96% | 40% | 96% |
Training Performance - Xception Model

Training Performance - v3 Booster Model

The Fourier-based K-Space attack intentionally drops accuracy to ~40% by targeting the frequency domain directly. The FourierHealer restores performance to 96% baseline by applying a surgical band-stop mask β proving that domain-aware defenses are essential for real-world medical AI robustness.
| Clean MRI | Signal Attack | Shielded Recovery |
|---|---|---|
| Original diagnostic scan | OmniChaos 7-layer corruption | Defense pipeline restored |

Pituitary Tumor β 99.5% confidence after adversarial recovery

Meningioma β 93.4% confidence with visible tumor mass in CAM focus area

No Tumor β 99.9% confidence on clean healthy scan
-
Custom architecture from scratch β HybridChimeraAI is not a fine-tuned pretrained model; it combines spatial feature extraction (CNN), global context (Transformer), and specialized decision-making (MoE) in a single unified forward pass.
-
Physics-grounded attack simulation β The K-Space attack is based on actual MRI hardware failure modes (frequency-domain spike artifacts), not random pixel noise. This makes it a clinically meaningful robustness test.
-
Domain-matched defenses β VisualHealer and FourierHealer are paired to match the specific attack type, demonstrating that defense mechanisms must be designed based on the signal degradation domain.
-
Iterative scientific methodology β The experiment log documents 5 complete training runs with hypothesis, architecture change, results, and analysis β following standard ML research practice.
-
Full-stack deployment β The project ships with a Flask REST API, a Streamlit multi-module diagnostic workstation, a Gradio app, and a Vanilla JS SPA frontend β ready for end-to-end evaluation.
-
Production-ready Batch Auditor β the Streamlit Batch Auditor accepts ZIP uploads of patient scans, produces per-image predictions and a Plotly confusion matrix β suitable for radiologist-style workflow evaluation.
This project is a research prototype developed for academic and hackathon purposes. It is not intended for clinical diagnosis. All predictions should be reviewed by a qualified medical professional. The adversarial attack simulation is included for robustness research only.
Mohd Altamish
B.Tech Computer Science Engineering, GL Bajaj Institute of Technology and Management (2025β2029)
LinkedIn: Mohd-Altamish
GitHub: @MohdAltamish
This project is licensed under the MIT License. See LICENSE for details.
