A Shiny-based web application that enables users to upload images and analyze how deep learning models make predictions. The app provides two interpretability techniques:
- Grad-CAM (Gradient-weighted Class Activation Mapping) – Highlights important regions in an image that contributed to a model's prediction.
- SHAP (SHapley Additive exPlanations) – A game-theoretic approach that assigns importance values to each pixel.
This project focuses on XAI (Explainable AI) techniques for deep learning with a clean, scalable architecture.
- ✅ Upload and Analyze Images – Users can upload an image and visualize explanations
- ✅ Multiple Model Support – Select from models like VGG16, ResNet50, MobileNetV2, and EfficientNet
- ✅ Grad-CAM Visualization – See heatmaps overlaying important regions
- ✅ SHAP Interpretability – Generate pixel-based importance explanations
- ✅ Confidence Threshold Slider – Customize the minimum prediction confidence for analysis
- ✅ Interactive UI – Built with Shiny and R, integrating Python via
reticulate - ✅ Modular Architecture – Clean separation of concerns with scalable folder structure
model-explainability/
├── src/
│ ├── python/ # Python explainability modules
│ │ ├── models/ # Model loading and management
│ │ ├── explainability/ # Grad-CAM and SHAP implementations
│ │ ├── utils/ # Utility functions
│ │ └── explainability_api.py # Main API interface
│ └── shiny/ # R Shiny application components
│ ├── ui.R # User interface
│ ├── server.R # Server logic
│ └── modules/ # Modular Shiny components
├── tests/
│ ├── python/ # Python unit tests
│ └── r/ # R tests
├── assets/
│ ├── images/ # Sample images and test data
│ └── docs/ # Documentation and papers
├── config/ # Configuration files
├── outputs/ # Generated explanations (gitignored)
├── requirements.txt # Python dependencies
├── app.R # Main Shiny app entry point
└── README.md
- R (>= 4.0)
- Python (>= 3.8)
- Shiny Server (for deployment)
git clone https://github.com/rodrick-mpofu/model-explainability.git
cd model-explainabilitypip install -r requirements.txt# Install required R packages
install.packages(c("shiny", "shinydashboard", "shinythemes",
"shinycssloaders", "reticulate", "png"))# In R, configure reticulate to use your Python environment
library(reticulate)
use_python("/path/to/your/python") # or use_virtualenv()# Run the Shiny app
shiny::runApp("app.R")- Upload an Image 📷
- Select Model & Technique (Grad-CAM or SHAP) 🏗️
- Adjust Confidence Threshold 🎚️
- View Model Explanation 🔥 (Heatmaps for Grad-CAM / SHAP values for SHAP)
- Interpret Results ✅
You can also use the Python API directly:
from src.python.explainability_api import explain_image
# Generate Grad-CAM explanation
results = explain_image(
img_path="assets/images/dog.jpg",
model_name="vgg16",
technique="gradcam",
confidence_threshold=0.5
)
# Generate SHAP explanation
results = explain_image(
img_path="assets/images/dog.jpg",
model_name="resnet50",
technique="shap",
confidence_threshold=0.3
)Run the test suite:
# Python tests
python -m pytest tests/python/
# R tests (if available)
Rscript -e "testthat::test_dir('tests/r')"The new modular structure provides:
- Separation of Concerns: Clear separation between UI, business logic, and utilities
- Scalability: Easy to add new models and explanation techniques
- Maintainability: Well-organized code with proper imports and dependencies
- Testability: Dedicated test structure for both Python and R components
- Reusability: Python modules can be used independently of the Shiny app
- Framework Conventions: Follows best practices for both Python and R/Shiny
- 👤 Rodrick Mpofu
- 📧 Email: rodrickmpofu@gmail.com
- 🔗 GitHub: @rodrick-mpofu
- 🔗 LinkedIn: Rodrick Mpofu
This project is available under the MIT License. See LICENSE file for details.