Flask + PyTorch web application for automated dementia severity classification from brain MRI scans. Built for the Hack4Health AI for Alzheimer's challenge featuring ResNet50 transfer learning and Grad-CAM explainability.
BrainGuard classifies brain MRI images into four dementia severity levels: Non-Demented, Very Mild, Mild, and Moderate Dementia. The system uses ResNet50 transfer learning with Grad-CAM visual explanations, making AI predictions transparent and clinically interpretable.
- Flask Web Interface — Drag-and-drop MRI upload with real-time predictions
- ResNet50 Transfer Learning — High-accuracy ImageNet-pretrained model adapted for medical imaging
- Flexible Input Formats — PNG/JPG images and 3D NIfTI files (
.nii,.nii.gz) with automatic slice extraction - Probability Breakdown — Detailed confidence scores across all four dementia classes
- Grad-CAM Visualization — Highlights influential brain regions driving each prediction
- Complete Pipeline — Training scripts, validation metrics, and comprehensive documentation
BrainGuard/
├── app/
│ └── app.py # Flask server & prediction API
├── models/
│ ├── cnn_model.py # ResNet50 architecture
│ ├── preprocessing.py # Image normalization & augmentation
│ ├── grad_cam.py # Saliency map generation
│ └── best_resnet.pth # Pre-trained model weights
├── templates/
│ └── index.html # Web UI
├── static/
│ ├── css/style.css # Styling
│ ├── js/script.js # Client logic
│ └── uploads/ # Uploaded MRI storage
├── notebooks/
│ └── alz_mri_classification.ipynb # Training notebook
├── train.py # Training script
├── train_colab.ipynb # Google Colab notebook
├── requirements.txt # Python dependencies
├── MODEL_CARD.md # Model documentation
└── README.md # This file
# Navigate to project directory
cd BrainGuard
# Create virtual environment (recommended)
python -m venv venv
# On Windows: venv\Scripts\activate
# On macOS/Linux: source venv/bin/activate
# Install dependencies
pip install -r requirements.txtNote for Windows users: You may need to install Visual C++ Redistributable:
winget install --id=Microsoft.VCRedist.2015+.x64 -epython app/app.pyThen open your browser to http://localhost:8000
- Upload an MRI scan (PNG, JPG, or NIfTI format)
- Click Analyze to run the prediction
- View:
- Predicted dementia severity class
- Confidence score
- Probability distribution across all classes
- Grad-CAM attention heatmap showing influential brain regions
- Architecture: ImageNet-pretrained ResNet50 with adapted first convolutional layer for single-channel (grayscale) MRI input
- Classes: 4 (Non-Demented, Very Mild, Mild, Moderate)
- Input: 224×224 grayscale images
- Performance: High accuracy with strong feature representations
- Grayscale conversion for single-channel input
- Resize to 224×224 pixels
- Normalization using ImageNet statistics (mean=0.485, std=0.229)
- NIfTI handling: Middle axial slice extraction from 3D volumes
- Augmentation (training): Rotation (±15°), horizontal flips, color jitter
- Loss: CrossEntropyLoss
- Optimizer: Adam (lr=1e-3, weight_decay=1e-4)
- Scheduler: ReduceLROnPlateau (factor=0.5, patience=5)
- Batch Size: 32
- Epochs: Up to 50 with early stopping
Grad-CAM (Gradient-weighted Class Activation Mapping) generates visual explanations by:
- Computing gradients of the predicted class with respect to the final convolutional layer
- Creating heatmaps that highlight influential brain regions
- Overlaying attention maps on original MRI images
- Enabling verification that the model focuses on clinically relevant areas (e.g., hippocampal regions)
# Basic training
python train.py --model resnet --epochs 30 --batch-size 32 --lr 1e-3
# Or use Google Colab
# Open train_colab.ipynb in Colab for cloud-based trainingModel checkpoints save to models/best_resnet.pth
| File | Purpose |
|---|---|
app/app.py |
Flask API for inference & visualization |
models/cnn_model.py |
ResNet50 architecture definition |
models/grad_cam.py |
Saliency map generation |
train.py |
Full training pipeline with validation |
MODEL_CARD.md |
Model specifications & ethical considerations |
DEVPOST.md |
Project description for hackathon submission |
Import errors for PyTorch/dependencies:
pip install -r requirements.txtCUDA/GPU not detected:
App automatically falls back to CPU. For GPU support, ensure CUDA Toolkit is installed.
Port 8000 already in use:
Modify the port in app.py:
app.run(debug=True, host='0.0.0.0', port=5000) # Change port numberOpenCV not available warning:
Install opencv-python for full Grad-CAM visualization:
pip install opencv-python- Technical Report: See
BrainGuard_Report.html(2-3 pages covering methods & evaluation) - Model Card: See
MODEL_CARD.mdfor data sources, preprocessing, metrics, and limitations - Devpost: See
DEVPOST.mdfor hackathon submission text
- Primary: Kaggle Alzheimer MRI Disease Classification Dataset
- Optional: ALZ_Variant dataset in
Assets/Datasets/ - Framework: PyTorch with torchvision, scikit-learn, NumPy, Pillow
This system is a research prototype for educational purposes only. It is:
- NOT approved for clinical use
- NOT a substitute for professional medical diagnosis
- Intended for the Hack4Health AI challenge and learning purposes
Built for Hack4Health AI for Alzheimer's Challenge (Intermediate Level)
Need help? Check MODEL_CARD.md for detailed specifications or open an issue in the repository.