OmniXAI Documentation

repository·main·Indexed 21 days ago

https://github.com/salesforce/omnixai

A comprehensive Python library for Explainable AI (XAI) providing a unified interface for global and local explanations across tabular, image, text, and time-series data. It supports Scikit-learn, PyTorch, and TensorFlow models, featuring a wide range of methods including SHAP, LIME, Grad-CAM, and an experimental GPT Explainer. The library includes core abstractions like TabularExplainer and VisionExplainer, a PredictionAnalyzer for performance metrics, a Dash-based visualization dashboard for "what-if" analysis, and built-in deployment support via BentoML.

Tokens
24.1K
Snippets
52
Records
72
Agent score
71%

What's inside OmniXAI

  1. Overview of OmniXAI capabilities

    main

    OmniXAI is a comprehensive Python library for Explainable AI (XAI). It provides a unified interface to generate explanations for various data types and machine learning models.

    Key Features:

    • Multi-modal Support: Handles Tabular, Image, Text, and Time-series data.
    • Model Support: Works with traditional ML (Scikit-learn) and deep learning (PyTorch/TensorFlow).
    • Diverse Explanation Methods: Includes feature-attribution, counterfactual, gradient-based (e.g., Integrated Gradients, Grad-CAM), and model-specific methods.
    • Unified Interface: Generate complex explanations with minimal code.
    • GUI Dashboard: A built-in dashboard for visualizing and comparing explanations.
    • Extensibility: New algorithms can be added by implementing a class derived from the explainer base class.
  2. Understand the Explainer Factory pattern

    main

    OmniXAI uses a factory pattern to manage its diverse range of explainers. Instead of interacting with individual low-level explainer classes directly, you should use the domain-specific factory classes which inherit from omnixai.explainers.base.AutoExplainerBase.

    These factories act as entry points to generate the appropriate explainers for your data type:

    • omnixai.explainers.tabular.TabularExplainer
    • omnixai.explainers.vision.VisionExplainer
    • omnixai.explainers.nlp.NLPExplainer
    • omnixai.explainers.timeseries.TimeseriesExplainer

    Explainers within these categories are further classified as model-agnostic (black-box), model-specific (requires knowledge of model properties like differentiability), or counterfactual.

  3. How OmniXAI is organized

    main

    OmniXAI is structured into five core subpackages that cover the machine learning lifecycle for explainability:

    1. omnixai.data: Data representation. Provides classes for tabular, image, text, and timeseries data. These classes can be constructed from common formats like numpy arrays, pandas DataFrames, Pillow images, or strings.
    2. omnixai.preprocessing: Feature engineering and transformation. Includes modules for categorical encoding, normalization, and scaling. For tabular data, omnixai.preprocessing.tabular.TabularTransform is the primary tool for preparing raw data for models.
    3. omnixai.explainers: The core engine. Contains various explainer groups (data exploration, prediction metrics, and domain-specific explainers for tabular, vision, NLP, etc.).
    4. omnixai.explanations: Result containers. Stores the output of explainers (e.g., feature_importance) and provides built-in visualization methods like plot (Matplotlib), plotly_plot (Dash), and ipython_plot (IPython).
    5. omnixai.visualization: A GUI dashboard built with Plotly Dash for exploring both global and local explanations.
  4. How OmniXAI explainers and analyzers work together

    main

    OmniXAI uses a factory pattern to provide a simplified interface for generating explanations across different data modalities.

    Core Abstractions

    • Explainers: Use TabularExplainer, VisionExplainer, NLPExplainer, or TimeseriesExplainer to generate local and global explanations for your models. These act as factories for individual algorithms like SHAP, LIME, or Grad-CAM.
    • Analyzers: Use DataAnalyzer for feature analysis and PredictionAnalyzer for analyzing model prediction results and performance metrics.
    • Dashboard: A visualization tool (built on Dash) that consumes explanations from explainers and metrics from analyzers to provide an interactive UI for model inspection and "what-if" analysis.

    Requirements for Explanations

    To generate explanations, you must specify:

    1. The ML model: A scikit-learn, TensorFlow, or PyTorch model, or a black-box prediction function.
    2. The pre-processing function: A function that converts raw input (e.g., a Tabular instance) into the format the model expects.
    3. The post-processing function (optional): A function to convert model outputs (e.g., logits) into a user-friendly format like class probabilities.
    4. The explainers to apply: A list of algorithm names (e.g., ["lime", "shap"]).
  5. How to generate AI explanations in OmniXAI

    main

    To generate explanations using OmniXAI, you need to provide four core components to an explainer (like TabularExplainer):

    1. The ML model to explain: This can be a scikit-learn, TensorFlow, or PyTorch model, or any black-box prediction function.
    2. The pre-processing function: A function that converts raw data (e.g., a Tabular instance) into the format required by the model.
    3. The post-processing function (optional): A function to convert model outputs into a user-specific form, such as class probabilities. The output must be a numpy array.
    4. The explainers to apply: A list of specific algorithms to use, such as SHAP, LIME, MACE, or PDP (Partial Dependence Plot).

    Depending on the task, you should use the appropriate explainer class:

    • Tabular data: omnixai.explainers.tabular.TabularExplainer
    • Vision data: omnixai.explainers.vision.VisionExplainer
    • NLP data: omnixai.explainers.nlp.NLPExplainer
    • Time series data: omnixai.explainers.timeseries.TimeseriesExplainer
  6. Set up pre-commit hooks for OmniXAI development

    main

    Before contributing to the repository, ensure all files are formatted correctly and contain appropriate license headers by setting up pre-commit:

    1. Clone the repository.
    2. Install pre-commit via pip:
      pip install pre-commit
    3. Install the hooks from the root directory:
      pre-commit install
    pip install pre-commit
    pre-commit install
  7. Explore OmniXAI tutorials and application examples

    main

    OmniXAI provides a variety of tutorials and example notebooks categorized by data modality and application type. You can find specialized guidance for:

    • Basics: Miscellaneous fundamental tutorials.
    • Applications: End-to-end workflows including Data Analysis, Tabular Classification, Tabular Regression, Vision, NLP, and Timeseries analysis, as well as integrating OmniXAI into a general ML workflow.
    • Tabular Explainers: Specialized tutorials for explaining tabular data models.
    • Vision Explainers: Tutorials focused on explainability for computer vision tasks.
    • NLP Explainers: Tutorials for Natural Language Processing, including specific examples like IMDB sentiment analysis.
    • Timeseries Explainers: Tutorials for explaining time-series models.
  8. How to implement a new Explainer

    main

    To add a new explanation method to OmniXAI, follow these steps:

    1. Identify Task and Type: Choose a task type (tabular, vision, nlp, or timeseries) and an explainer type (model-agnostic, model-specific, or counterfactual).
    2. Create the Class: Inherit from omnixai.explainers.base.ExplainerBase.
      • For model-agnostic explainers, use __init__(self, predict_function, mode, **kwargs). predict_function takes raw features and returns model outputs.
      • For model-specific explainers, use __init__(self, model, preprocess_function, postprocess_function, mode, **kwargs). postprocess_function is optional.
    3. Define Metadata:
      • Add explanation_type (string: "local", "global", or "both").
      • Add alias (list of strings) to register the name used in the high-level Explainer classes.
    4. Implement Logic:
      • Implement explain(self, **kwargs) for local explanations.
      • Implement explain_global(self, X, **kwargs) for global explanations, where X is a Tabular, Image, Text, or Timeseries instance.
    5. Return Explanations: The explain method must return an instance of a class inheriting from omnixai.explanations.base.ExplanationBase.
    6. Register: Import the class into the __init__.py of the appropriate package (omnixai.explainers.tabular, vision, nlp, or timeseries).
  9. Deploy OmniXAI explainers via BentoML

    main

    OmniXAI provides built-in interfaces for BentoML to facilitate model serving. You can deploy an explainer by saving it to the BentoML local model store and initializing a service that exposes /predict and /explain endpoints.

    1. Save the Explainer

    Use save_model from omnixai.deployment.bentoml.omnixai to persist your TabularExplainer (or other explainer types) to the BentoML store.

    2. Initialize the Service

    Create a service file (e.g., service.py) using init_service from omnixai.deployment.bentoml.omnixai. This function automatically configures the API endpoints for predictions and explanations.

    3. Run the Server

    Use the BentoML CLI to serve your service locally for testing.

    # 1. Save the model
    from omnixai.explainers.tabular import TabularExplainer
    from omnixai.deployment.bentoml.omnixai import save_model
    
    explainer = TabularExplainer(
      explainers=["lime", "shap", "mace", "pdp", "ale"],
      mode="classification",
      data=train_data,
      model=model,
      preprocess=lambda z: transformer.transform(z),
      params={
         "mace": {"ignored_features": ["Sex", "Race", "Relationship", "Capital Loss"]}
      }
    )
    save_model("tabular_explainer", explainer)
    
    # 2. service.py
    from omnixai.deployment.bentoml.omnixai import init_service
    
    svc = init_service(
        model_tag="tabular_explainer:latest",
        task_type="tabular",
        service_name="tabular_explainer"
    )
    # 3. Start the server
    bentoml serve service:svc --reload
  10. How to add a new explanation method to OmniXAI

    main

    To extend OmniXAI with a new explanation method, follow these steps:

    1. Define Task and Explainer Types:
      • Task Type: Choose from tabular, vision, nlp, or timeseries.
      • Explainer Type: Choose from model-agnostic, model-specific, or counterfactual.
    2. File Placement: Create a new Python script in the appropriate directory (e.g., explainers/tabular/agnostic/ for a model-agnostic tabular explainer).
    3. Implement the Explainer Class: Inherit from omnixai.explainers.base.ExplainerBase. Use one of the following constructor patterns:
      • Model-Agnostic: __init__(self, predict_function, mode, **kwargs)
        • predict_function: A function that takes raw input features and returns model outputs.
        • mode: The task type (e.g., classification, regression).
      • Model-Specific: __init__(self, model, preprocess_function, postprocess_function, mode, **kwargs)
        • model: The ML model to explain.
        • preprocess_function: Converts raw features into model inputs (e.g., image resizing).
        • postprocess_function: (Optional) Converts model logits into class probabilities.
        • mode: The task type (e.g., classification, regression).
    4. Set Class Attributes:
      • explanation_type (string): Set to local, global, or both.
      • alias (list): A list of strings specifying the names used to call the explainer.
    5. Implement Explanation Logic:
      • For local explanations: Implement explain(self, **kwargs).
      • For global explanations: Implement explain_global(self, X, **kwargs), where X is an instance of Tabular, Image, Text, or Timeseries.
    6. Register the Explainer: Import the class in the __init__.py of the corresponding package (omnixai.explainers.tabular, omnixai.explainers.vision, omnixai.explainers.nlp, or omnixai.explainers.timeseries).

    Once registered, the explainer can be accessed via the relevant high-level explainer class (e.g., TabularExplainer) by providing one of the names defined in alias.