Spaces:
Sleeping
Sleeping
| sdk: gradio | |
| sdk_version: "4.19.0" | |
| app_file: "app.py" | |
| pinned: false | |
| <div align="center"> | |
| <img src="assets/dashboard.png" alt="Dashboard Preview" width="800"/> | |
|  | |
|  | |
|  | |
|  | |
|  | |
| **A Comprehensive Explainability and Validation Dashboard for Vision Transformers** | |
| [π Live Demo](#live-demo) | [π Features](#features) | [π‘ Usage Guide](#usage-guide) | [π€ Contributing](#contributing) | |
| </div> | |
| ## π Overview | |
| The **ViT Auditing Toolkit** is an advanced, interactive dashboard designed to help researchers, ML practitioners, and AI auditors understand, validate, and improve Vision Transformer (ViT) models. It provides a comprehensive suite of explainability techniques and auditing tools through an intuitive web interface. | |
| ### οΏ½ Purpose & Scope | |
| This toolkit is designed as an **Explainable AI (XAI) and Human-Centered AI (HCAI) analysis tool** to help you: | |
| - **Understand model decisions** through visualization and interpretation | |
| - **Identify potential issues** in model behavior before deployment | |
| - **Explore model robustness** through systematic testing | |
| - **Analyze fairness** across different data characteristics | |
| - **Build trust** in AI systems through transparency | |
| **Important**: This is an **exploratory and educational tool** for model analysis and research. For production-level auditing: | |
| - Use comprehensive, representative validation datasets (not single images) | |
| - Conduct systematic bias testing with diverse demographic groups | |
| - Combine automated analysis with domain expert review | |
| - Follow established AI fairness and auditing frameworks | |
| We encourage researchers and practitioners to use this toolkit as a **starting point** for deeper investigation into model behavior, complementing it with rigorous testing protocols and domain expertise. | |
| ### οΏ½π Why This Toolkit? | |
| - **π Transparency**: Understand what your ViT models actually "see" and learn | |
| - **β Validation**: Verify model reliability through systematic testing | |
| - **βοΈ Fairness**: Detect potential biases across different data subgroups | |
| - **π‘οΈ Robustness**: Test prediction stability under various perturbations | |
| - **π Calibration**: Ensure confidence scores reflect true prediction accuracy | |
| --- | |
| <a id="features"></a> | |
| ## β¨ Features | |
| ### π¬ Basic Explainability | |
| Visualize and understand model predictions through multiple state-of-the-art techniques: | |
| - **π¨ Attention Visualization**: See which image patches the transformer focuses on at each layer and head | |
| - **π₯ GradCAM**: Gradient-weighted Class Activation Mapping for highlighting discriminative regions | |
| - **π« GradientSHAP**: Shapley value-based attribution for pixel-level importance | |
| ### π Counterfactual Analysis | |
| Test model robustness by systematically perturbing image regions: | |
| - **Patch Perturbation**: Apply blur, blackout, grayscale, or noise to image patches | |
| - **Sensitivity Mapping**: Identify which regions are critical for predictions | |
| - **Prediction Stability**: Measure confidence changes and prediction flip rates | |
| ### π Confidence Calibration | |
| Evaluate whether model confidence scores accurately reflect prediction reliability: | |
| - **Calibration Curves**: Visual assessment of confidence vs accuracy alignment | |
| - **Reliability Diagrams**: Binned analysis of prediction calibration | |
| - **Metrics Dashboard**: Mean confidence, overconfidence rate, and underconfidence rate | |
| ### βοΈ Bias Detection | |
| Identify performance disparities across different data subgroups: | |
| - **Subgroup Analysis**: Compare performance across demographic or environmental variations | |
| - **Fairness Metrics**: Detect systematic biases in model predictions | |
| - **Comparative Visualization**: Side-by-side analysis of confidence distributions | |
| --- | |
| <a id="live-demo"></a> | |
| ## π Live Demo | |
| Try the toolkit instantly on Hugging Face Spaces: | |
| ### π [Launch Interactive Demo](https://huggingface.co/spaces/Dyra1204/ViT-Auditing-Toolkit) | |
| *No installation required! Upload an image and start exploring.* | |
| --- | |
| ## οΏ½οΈ Test Images Included | |
| The project includes **20 curated test images** organized by analysis type: | |
| ```bash | |
| examples/ | |
| βββ basic_explainability/ # 5 images - Clear objects for explanation testing | |
| βββ counterfactual/ # 4 images - Centered subjects for robustness testing | |
| βββ calibration/ # 3 images - Varied quality for confidence testing | |
| βββ bias_detection/ # 4 images - Different conditions for fairness testing | |
| βββ general/ # 4 images - Miscellaneous testing | |
| ``` | |
| **Quick Download**: Run `python examples/download_samples.py` to get all test images instantly! | |
| See [examples/README.md](examples/README.md) for detailed image descriptions and testing guidelines. | |
| --- | |
| ## οΏ½πΈ Screenshots | |
| <div align="center"> | |
| ### Basic Explainability Interface | |
| <img src="assets/basic-explainability-interface.png" alt="Basic Explainability" width="700"/> | |
| ### Counterfactual Analysis | |
| <img src="assets/counterfactual-analysis.png" alt="Counterfactual Analysis" width="700"/> | |
| ### Confidence Calibration | |
| <img src="assets/confidence-calibration.png" alt="Confidence Calibration" width="700"/> | |
| ### Bias Detection | |
| <img src="assets/bias-detection.png" alt="Bias Detection" width="700"/> | |
| </div> | |
| --- | |
| <a id="usage-guide"></a> | |
| ## π― Usage Guide | |
| ### Quick Start (3 Steps) | |
| 1. **Select a Model**: Choose between ViT-Base or ViT-Large from the dropdown | |
| 2. **Upload Your Image**: Any image you want to analyze (JPG, PNG, etc.) or use provided examples | |
| 3. **Choose Analysis Type**: Select from 4 tabs based on your needs | |
| **π‘ Tip**: Use images from the `examples/` directory for quick testing! | |
| ### Detailed Workflow | |
| #### π For Understanding Predictions: | |
| ``` | |
| 1. Go to "Basic Explainability" tab | |
| 2. Upload your image (try: examples/basic_explainability/cat_portrait.jpg) | |
| 3. Select explanation method (Attention/GradCAM/SHAP) | |
| 4. Adjust layer/head indices if needed | |
| 5. Click "Analyze Image" | |
| 6. View predictions and visual explanations | |
| ``` | |
| **Example Images to Try**: | |
| - `cat_portrait.jpg` - Clear subject for attention visualization | |
| - `sports_car.jpg` - Distinct features for GradCAM | |
| - `bird_flying.jpg` - Dynamic action for SHAP analysis | |
| #### π For Testing Robustness: | |
| ``` | |
| 1. Go to "Counterfactual Analysis" tab | |
| 2. Upload your image (try: examples/counterfactual/flower.jpg) | |
| 3. Set patch size (16-64 pixels) | |
| 4. Choose perturbation type (blur/blackout/gray/noise) | |
| 5. Click "Run Analysis" | |
| 6. Review sensitivity maps and metrics | |
| ``` | |
| **Example Images to Try**: | |
| - `face_portrait.jpg` - Test facial feature importance | |
| - `car_side.jpg` - Identify critical vehicle components | |
| - `flower.jpg` - Simple object for baseline testing | |
| #### π For Validating Confidence: | |
| ``` | |
| 1. Go to "Confidence Calibration" tab | |
| 2. Upload a sample image (try: examples/calibration/clear_panda.jpg) | |
| 3. Adjust number of bins for analysis | |
| 4. Click "Analyze Calibration" | |
| 5. Review calibration curves and metrics | |
| ``` | |
| **Example Images to Try**: | |
| - `clear_panda.jpg` - High-quality image (high confidence expected) | |
| - `workspace.jpg` - Complex scene (varied confidence) | |
| - `outdoor_scene.jpg` - Medium difficulty | |
| #### βοΈ For Detecting Bias: | |
| ``` | |
| 1. Go to "Bias Detection" tab | |
| 2. Upload a sample image (try: examples/bias_detection/dog_daylight.jpg) | |
| 3. Click "Detect Bias" | |
| 4. Compare performance across generated subgroups | |
| 5. Review fairness metrics | |
| ``` | |
| **Example Images to Try**: | |
| - `dog_daylight.jpg` - Test lighting variations | |
| - `cat_indoor.jpg` - Indoor vs outdoor performance | |
| - `urban_scene.jpg` - Environmental bias detection | |
| --- | |
| ## π» Local Installation | |
| ### Prerequisites | |
| - Python 3.8 or higher | |
| - CUDA-compatible GPU (optional, but recommended for faster inference) | |
| - 8GB+ RAM | |
| ### Step 1: Clone the Repository | |
| ```bash | |
| git clone https://github.com/dyra-12/ViT-XAI-Dashboard.git | |
| cd ViT-XAI-Dashboard | |
| ``` | |
| ### Step 2: Create Virtual Environment (Recommended) | |
| ```bash | |
| # Using venv | |
| python -m venv venv | |
| source venv/bin/activate # On Windows: venv\Scripts\activate | |
| # OR using conda | |
| conda create -n vit-audit python=3.10 | |
| conda activate vit-audit | |
| ``` | |
| ### Step 3: Install Dependencies | |
| ```bash | |
| pip install -r requirements.txt | |
| ``` | |
| ### Step 4: Download Test Images (Optional but Recommended) | |
| ```bash | |
| # Download 20 curated test images for all tabs | |
| python examples/download_samples.py | |
| # Or use the bash script | |
| chmod +x examples/download_samples.sh | |
| ./examples/download_samples.sh | |
| ``` | |
| This creates an `examples/` directory with images organized by tab. | |
| ### Step 5: Run the Application | |
| ```bash | |
| python app.py | |
| ``` | |
| The dashboard will be available at `http://localhost:7860` | |
| ### π³ Docker Installation (Alternative) | |
| ```bash | |
| # Build the Docker image | |
| docker build -t vit-auditing-toolkit . | |
| # Run the container | |
| docker run -p 7860:7860 vit-auditing-toolkit | |
| ``` | |
| --- | |
| ## ποΈ Project Structure | |
| ``` | |
| ViT-XAI-Dashboard/ | |
| β | |
| βββ app.py # Main Gradio application | |
| βββ requirements.txt # Python dependencies | |
| βββ README.md # This file | |
| βββ examples/download_samples.py # Script to download test images | |
| β | |
| βββ src/ | |
| β βββ __init__.py | |
| β βββ model_loader.py # ViT model loading from Hugging Face | |
| β βββ predictor.py # Prediction and classification logic | |
| β βββ explainer.py # XAI methods (Attention, GradCAM, SHAP) | |
| β βββ auditor.py # Advanced auditing tools | |
| β βββ utils.py # Helper functions and preprocessing | |
| β | |
| βββ examples/ # 20 curated test images | |
| β βββ basic_explainability/ # Images for Tab 1 testing | |
| β βββ counterfactual/ # Images for Tab 2 testing | |
| β βββ calibration/ # Images for Tab 3 testing | |
| β βββ bias_detection/ # Images for Tab 4 testing | |
| β βββ general/ # General purpose test images | |
| β | |
| βββ tests/ | |
| βββ test_phase1_complete.py # Basic functionality tests | |
| βββ test_advanced_features.py # Advanced auditing tests | |
| ``` | |
| --- | |
| ## π§ Technical Details | |
| ### Vision Transformers (ViT) | |
| Vision Transformers apply the transformer architecture (originally designed for NLP) to computer vision tasks. Key concepts: | |
| - **Patch Embedding**: Images are split into fixed-size patches (e.g., 16Γ16 pixels) | |
| - **Self-Attention**: Each patch attends to all other patches to capture global context | |
| - **Layer Hierarchy**: Multiple transformer layers progressively refine representations | |
| - **Classification Token**: A special [CLS] token aggregates information for final prediction | |
| **Advantages:** | |
| - Strong performance on large-scale datasets | |
| - Captures long-range dependencies better than CNNs | |
| - More interpretable through attention mechanisms | |
| ### Explainability Techniques | |
| #### 1. Attention Visualization | |
| **Method**: Direct visualization of transformer attention weights | |
| **Purpose**: Shows which image patches the model focuses on | |
| **Implementation**: Extracts attention matrices from specified layers/heads | |
| ```python | |
| # Example: Layer 6, Head 0 typically captures semantic patterns | |
| attention_map = model.encoder.layer[6].attention.self.attention_weights | |
| ``` | |
| #### 2. GradCAM (Gradient-weighted Class Activation Mapping) | |
| **Method**: Uses gradients flowing into the final conv layer | |
| **Purpose**: Highlights discriminative regions for target class | |
| **Implementation**: Via Captum's `LayerGradCam` | |
| ```python | |
| # Generates heatmap showing which regions support the prediction | |
| gradcam = LayerGradCam(model, target_layer) | |
| attribution = gradcam.attribute(input, target=predicted_class) | |
| ``` | |
| #### 3. GradientSHAP (Gradient-based Shapley Values) | |
| **Method**: Combines Shapley values with gradient information | |
| **Purpose**: Pixel-level attribution with theoretical guarantees | |
| **Implementation**: Via Captum's `GradientShap` | |
| ```python | |
| # Computes fair attribution using random baselines | |
| gradient_shap = GradientShap(model) | |
| attributions = gradient_shap.attribute(input, baselines=random_baselines) | |
| ``` | |
| ### Auditing Methodologies | |
| #### Counterfactual Analysis | |
| Systematically modifies image regions to test: | |
| - **Robustness**: Does the prediction remain stable? | |
| - **Feature Importance**: Which regions matter most? | |
| - **Adversarial Vulnerability**: How easy is it to fool the model? | |
| #### Confidence Calibration | |
| Measures alignment between predicted confidence and actual accuracy: | |
| - **Well-calibrated**: 80% confidence β 80% correct | |
| - **Overconfident**: 90% confidence β 60% correct (problem!) | |
| - **Underconfident**: 50% confidence β 80% correct (less critical) | |
| #### Bias Detection | |
| Compares performance across subgroups to identify: | |
| - **Demographic bias**: Different accuracy for different groups | |
| - **Environmental bias**: Performance varies with lighting, quality, etc. | |
| - **Systematic patterns**: Consistent over/under-performance | |
| --- | |
| ### π§ Supported Models | |
| The dashboard now supports multiple architectures (ViT family and others). The models currently exposed in the UI are: | |
| | Display name | Hugging Face ID | Notes | | |
| |--------------:|-----------------|-------| | |
| | ViT-Base | `google/vit-base-patch16-224` | ViT β attention visualizations and GradCAM supported | | |
| | ViT-Large | `google/vit-large-patch16-224` | ViT β attention visualizations and GradCAM supported | | |
| | ResNet-50 | `microsoft/resnet-50` | CNN β GradCAM supported; attention visualization not applicable | | |
| | Swin Transformer | `microsoft/swin-base-patch4-window7-224` | Swin β GradCAM supported; attention visualization limited to ViT-style models | | |
| | DeiT | `facebook/deit-base-patch16-224` | ViT-like β attention visualizations and GradCAM supported | | |
| | EfficientNet-B7 | `google/efficientnet-b7` | CNN β loaded via Hugging Face when possible; if HF loading triggers a torch.load restriction, the app falls back to `timm` (no torch upgrade required). GradCAM supported; attention visualization not applicable | | |
| Notes: | |
| - Attention visualizations (patch-level attention maps) are meaningful for ViT-style models (ViT, DeiT). For CNNs (ResNet, EfficientNet) and some hierarchical transformers (Swin), the dashboard will use GradCAM or a last-conv fallback instead of patch attention. | |
| - EfficientNet on the Hugging Face hub can trigger a torch.load security restriction in older torch versions. The toolkit will transparently fall back to a `timm`-based loader to avoid requiring a torch upgrade; this is handled automatically in `src/model_loader.py`. | |
| **Easy to extend**: Add more models to `src/model_loader.py` under `SUPPORTED_MODELS` and they will appear in the app dropdown. | |
| --- | |
| ## π¦ Dependencies | |
| ### Core Libraries | |
| - **PyTorch** (β₯2.2.0): Deep learning framework | |
| - **Transformers** (β₯4.35.0): Hugging Face model hub | |
| - **Gradio** (β₯4.19.0): Web interface framework | |
| - **Captum** (β₯0.7.0): Model interpretability library | |
| ### Supporting Libraries | |
| - **Pillow**: Image processing | |
| - **Matplotlib**: Visualization | |
| - **NumPy**: Numerical computations | |
| See `requirements.txt` for complete list with version constraints. | |
| --- | |
| ## π Use Cases | |
| ### Research | |
| - **Interpretability Studies**: Analyze transformer attention patterns | |
| - **Benchmark Explainability**: Compare XAI methods systematically | |
| - **Model Auditing**: Validate models before deployment | |
| ### Industry | |
| - **Model Validation**: Ensure reliability before production | |
| - **Bias Auditing**: Detect and mitigate fairness issues | |
| - **Regulatory Compliance**: Document model decision-making | |
| ### Education | |
| - **Teaching Tool**: Demonstrate XAI concepts interactively | |
| - **Student Projects**: Foundation for ML course assignments | |
| - **Research Training**: Hands-on experience with modern techniques | |
| --- | |
| <a id="contributing"></a> | |
| ## π€ Contributing | |
| Contributions are welcome! Here's how you can help: | |
| ### Ways to Contribute | |
| 1. **π Bug Reports**: Open an issue with detailed reproduction steps | |
| 2. **β¨ Feature Requests**: Suggest new explainability methods or auditing tools | |
| 3. **π Documentation**: Improve guides, add examples, fix typos | |
| 4. **π» Code**: Submit pull requests for new features or fixes | |
| 5. **π¨ UI/UX**: Enhance the dashboard design and user experience | |
| ### Development Setup | |
| ```bash | |
| # Fork and clone the repository | |
| git clone https://github.com/YOUR-USERNAME/ViT-XAI-Dashboard.git | |
| cd ViT-XAI-Dashboard | |
| # Create a feature branch | |
| git checkout -b feature/your-feature-name | |
| # Make changes and test | |
| python -m pytest tests/ | |
| # Commit and push | |
| git commit -m "Add: your feature description" | |
| git push origin feature/your-feature-name | |
| # Open a pull request | |
| ``` | |
| ### Code Style | |
| - Follow PEP 8 guidelines | |
| - Add docstrings to all functions | |
| - Include type hints where applicable | |
| - Write unit tests for new features | |
| --- | |
| ## οΏ½ Additional Resources | |
| - **[QUICKSTART.md](QUICKSTART.md)** - Get started in 5 minutes | |
| - **[TESTING.md](TESTING.md)** - Comprehensive testing guide with 22 test cases | |
| - **[CONTRIBUTING.md](CONTRIBUTING.md)** - Guidelines for contributors | |
| - **[CHEATSHEET.md](CHEATSHEET.md)** - Quick reference for common tasks | |
| - **[examples/README.md](examples/README.md)** - Detailed test image guide | |
| --- | |
| ## οΏ½π License | |
| This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details. | |
| ``` | |
| MIT License | |
| Copyright (c) 2024 ViT Auditing Toolkit Contributors | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| [Full license text...] | |
| ``` | |
| --- | |
| ## Contact & Support | |
| - **GitHub Issues**: [Report bugs or request features](https://github.com/dyra-12/ViT-XAI-Dashboard/issues) | |
| - **Discussions**: [Ask questions or share ideas](https://github.com/dyra-12/ViT-XAI-Dashboard/discussions) | |
| - **Email**: dyra12@example.com | |
| --- | |
| <div align="center"> | |
| **Built with β€οΈ by the community** | |
| [β¬ Back to Top](#-vit-auditing-toolkit) | |
| </div> | |
| ======= | |
| title: ViT Auditing Toolkit | |
| emoji: π» | |
| colorFrom: gray | |
| colorTo: red | |
| sdk: gradio | |
| sdk_version: 5.49.1 | |
| app_file: app.py | |
| pinned: false | |
| --- | |
| Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference | |
| >>>>>>> hf/main | |