Skip to content

Repository files navigation

Robust LIME & SHAP: Defending Adversarial Attacks

Python 3.8+ Scikit-Learn SHAP LIME

This repository contains the complete implementation of a state-of-the-art Generative Defense Mechanism against adversarial attacks on Explainable AI (XAI) models. It demonstrates how popular explainers like LIME and SHAP can be systematically fooled using Out-of-Distribution (OOD) Watchdogs, and introduces a High-Resolution CTGAN pipeline to break the disguise and reveal the underlying model bias.

Project Overview

Machine Learning models in high-stakes domains (like criminal justice and finance) are increasingly required to provide explanations for their decisions. However, adversarial actors can deploy "Two-Faced" scaffolding models that hide discriminatory biases (e.g., Racism or Sexism) by feeding benign, synthetic explanations to LIME and SHAP.

Our Defense: We introduce a manifold-cleansing defense using Generative Adversarial Networks (CTGAN). By generating a massive pool of realistic, on-manifold synthetic profiles and mapping perturbed samples back to their nearest Euclidean neighbors, we blind the adversarial watchdog and force the explainers to reveal the true, biased nature of the underlying model.

Key Capabilities:

  • Dual-Explainer Vulnerability: Replicates scaffolding attacks against both LIME (Gaussian perturbation) and SHAP (K-Means background perturbation).
  • High-Res CTGAN Defense: Utilizes 1,000-epoch, dynamically encoded Generative Adversarial Networks to map the true data manifold.
  • Integer Cleansing & Standardization: Mitigates decimal-hunting watchdogs and scales high-dimensional data (e.g., German Credit) to prevent mathematical collapse in surrogate Ridge regression.
  • Modular Architecture: Fully abstracted Python backend for rapid testing across different datasets.

Repository Structure

Robust-LIME-SHAP/
│
├── data/                               # Datasets
│   ├── compas.csv                      # ProPublica Recidivism Data
│   └── german_processed.csv            # German Credit Data
│
├── poster/                      
│   ├── Robust_LIME_SHAP_poster.pdf     # Final poster presented in UG research showcase at IITGN.
│   
├── src/                                # Core Python Engine
│   ├── __init__.py             
│   ├── data_loader.py                  # Data ingestion, preprocessing and Decoy generation
│   ├── models.py                       # Two-Faced Classifiers & Random Forest Watchdogs
│   ├── adversarial.py                  # LIME/SHAP adversarial execution logic
│   ├── defense.py                      # CTGAN pooling and Ridge Surrogate revealers
│   └── visualization.py                # t-SNE dimensionality reduction mapping
│
├── demo_compas.ipynb                   # Interactive Master Demo (COMPAS)
├── demo_german.ipynb                   # Interactive Master Demo (German Credit)
├── requirements.txt                    # Project dependencies
└── README.md                           # Project documentation

Installation & Setup

It is highly recommended to run this project inside a virtual environment to manage dependencies like sdv and shap.

1. Clone the repository:

git clone [https://github.com/16niraj/Robust-LIME-SHAP.git](https://github.com/16niraj/Robust-LIME-SHAP.git)
cd Robust-LIME-SHAP

2. Create and activate a virtual environment:

# Windows
python -m venv venv
.\venv\Scripts\activate

# Mac/Linux
python3 -m venv venv
source venv/bin/activate

3. Install the dependencies:

pip install -r requirements.txt

Usage

The core logic has been fully abstracted into the src/ directory. To witness the pipeline in action, simply launch Jupyter Notebook or VS Code and open one of the demo files:

  • Run demo_compas.ipynb to see the defense deployed against racial bias (African-American feature) in the 7-dimensional COMPAS dataset.

  • Run demo_german.ipynb to see the defense deployed against gender bias in the 24-dimensional German Credit dataset.

Both notebooks execute a four-phase pipeline:

  1. The Trap Setup: Builds the biased base model and trains the OOD Watchdogs.

  2. The Attack (The Lie): LIME and SHAP are fooled into highlighting a harmless Synthetic_Decoy feature.

  3. The Defense: A 5,000-sample CTGAN pool is generated and integer-cleansed.

  4. The Master Revealer (The Truth): The perturbed instances are mapped to the on-manifold defense pool, blinding the Watchdog and forcing LIME/SHAP to expose the true biased feature.

Visual Proof & Manifold Mapping

As part of the execution pipeline, the codebase generates high-resolution t-SNE Visualizations that mathematically prove why the standard explainers fail and why the CTGAN defense succeeds.

  • Blue (LIME Perturbations): Falls entirely off the data manifold, instantly triggering the adversarial Watchdog.

  • Gray (True Data): The true shape of the dataset.

  • Red (CTGAN Defense): Successfully populates the exact shape of the true manifold, bypassing the Watchdog entirely.


(All the output plots of attack, defense and t-sne are automatically saved to your root directory upon running the demo notebooks).

Acknowledgments

This repository contains research and codebase developed as part of a project course(CS 299) at IIT Gandhinagar under the supervision of Prof. Manisha Padala.

I would like to express my sincere gratitude to Professor Manisha Padala for her invaluable guidance, feedback, and mentorship throughout the duration of this project. Her insights into explainable AI and generative modeling were instrumental in shaping this defense architecture.

About

A generative defense mechanism using High-Res CTGANs to expose and break adversarial attacks on XAI models (LIME & SHAP).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages