### Setup Development Environment Source: https://github.com/salesforce/omnixai/blob/main/docs/index.md Commands to prepare the repository for development and ensure proper formatting and license headers. ```bash pip install pre-commit ``` ```bash pre-commit install ``` -------------------------------- ### Start BentoML API Server Source: https://github.com/salesforce/omnixai/blob/main/README.md Run the local API server to test the deployed service. ```bash bentoml serve service:svc --reload ``` -------------------------------- ### Setup Visualization Dashboard Source: https://context7.com/salesforce/omnixai/llms.txt Prepare data and models for use with the interactive Dashboard visualization tool. ```python import numpy as np import pandas as pd import xgboost from sklearn.model_selection import train_test_split from omnixai.data.tabular import Tabular from omnixai.preprocessing.tabular import TabularTransform from omnixai.explainers.tabular import TabularExplainer from omnixai.explainers.prediction import PredictionAnalyzer from omnixai.visualization.dashboard import Dashboard # Prepare data and train model (same as TabularExplainer example) df = pd.DataFrame({ "Age": np.random.randint(20, 60, 1000), "Education_Num": np.random.randint(8, 16, 1000), "Hours_Per_Week": np.random.randint(20, 60, 1000), "Workclass": np.random.choice(["Private", "Govt", "Self-emp"], 1000), "Income": np.random.choice([0, 1], 1000) }) tabular_data = Tabular( data=df, categorical_columns=["Workclass"], target_column="Income" ) transformer = TabularTransform().fit(tabular_data) X = transformer.transform(tabular_data) train_data, test_data, train_labels, test_labels = train_test_split( X[:, :-1], X[:, -1], train_size=0.8 ) model = xgboost.XGBClassifier(n_estimators=100, max_depth=5) model.fit(train_data, train_labels) train_tabular = transformer.invert(train_data) test_tabular = transformer.invert(test_data) ``` -------------------------------- ### Install OmniXAI from Source Source: https://github.com/salesforce/omnixai/blob/main/docs/omnixai.md Install OmniXAI from its source code repository. Use `pip install .` for a standard installation or `pip install -e .` for an editable installation. ```bash pip install . ``` ```bash pip install -e . ``` -------------------------------- ### Initialize L2XImage Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/l2x.ipynb Initializes the L2XImage explainer with training data and the prediction function. This setup is required before generating explanations. ```python explainer = L2XImage( training_data=train_imgs, predict_function=predict_function, ) ``` -------------------------------- ### Initialize and show the OmniXAI dashboard Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp_imdb.ipynb Use this to start the visualization server. Requires pre-computed local explanations and class names. ```python dashboard = Dashboard( instances=x, local_explanations=local_explanations, class_names=class_names ) dashboard.show() ``` -------------------------------- ### Initialize PartialDependenceTabular Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/pdp.ipynb Initializes the PartialDependenceTabular explainer with the training data and the prediction function. This setup is required before generating explanations. ```python explainer = PartialDependenceTabular( training_data=tabular_data, predict_function=predict_function ) ``` -------------------------------- ### Install OmniXAI from PyPI Source: https://github.com/salesforce/omnixai/blob/main/docs/omnixai.md Install the OmniXAI library using pip. For specific task dependencies, append the relevant extra, e.g., `[vision]`, `[nlp]`, or `[plot]`. ```bash pip install omnixai ``` ```bash pip install omnixai[vision] ``` ```bash pip install omnixai[nlp] ``` ```bash pip install omnixai[plot] ``` -------------------------------- ### Prepare Text Input Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp.ipynb Creates a Text instance to be used as input for the NLP explainer. This example includes two sentences for sentiment analysis. ```python x = Text([ "What a great movie!", "The Interview was neither that funny nor that witty. " "Even if there are words like funny and witty, the overall structure is a negative type." ]) ``` -------------------------------- ### Predict with Sequence Examples Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/ranking.ipynb Processes a DataFrame into sequence example protos and generates ranking scores using the loaded model. ```python from ml4ir.base.data.tfrecord_helper import get_sequence_example_proto def predict(features_df): features_df["query_text"] = features_df["query_text"].fillna("") features_df = (features_df.copy() .rename(columns={ feature["serving_info"]["name"]: feature["name"] for feature in feature_config.context_features + feature_config.sequence_features })) #print(features_df) context_feature_names = [feature["name"] for feature in feature_config.context_features] protos = features_df.groupby(["query_id","query_text"]).apply(lambda g: get_sequence_example_proto( group=g, context_features=feature_config.context_features, sequence_features=feature_config.sequence_features, )) # Score the proto with the model ranking_scores = protos.apply(lambda se: infer_fn( tf.expand_dims( tf.constant(se.SerializeToString()), axis=-1))["ranking_score"].numpy()[0]) # Check parity of scores predicted_scores = (ranking_scores.reset_index(name="ranking_score") .set_index("query_id") .squeeze()) return predicted_scores["ranking_score"] ``` -------------------------------- ### FFT Preconditioned Feature Visualization Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/feature_visualization_torch.ipynb Performs feature visualization using FFT preconditioning for potentially faster convergence. This example uses a different target layer and a larger image shape, and enables the use_fft parameter. ```python target_layer = model.features[-6] optimizer = FeatureVisualizer( model=model, objectives=[ {"layer": target_layer, "type": "channel", "index": list(range(6))} ] ) explanations = optimizer.explain( num_iterations=300, image_shape=(512, 512), use_fft=True ) explanations.ipython_plot() ``` -------------------------------- ### Initialize ShapImage Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/shap.ipynb Set up the explainer with the trained model and preprocessing function. ```python explainer = ShapImage( model=model, preprocess_function=preprocess_func ) ``` -------------------------------- ### Initialize L2X Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/l2x.ipynb Sets up the L2X explainer with the training data and the model's prediction function. ```python from omnixai.explainers.tabular.agnostic.L2X.l2x import L2XTabular explainer = L2XTabular( training_data=tabular_data, predict_function=predict_function, ) ``` -------------------------------- ### Plot Second Counterfactual Explanation Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp/ce_qa.ipynb Plots the counterfactual explanation for the second example in the generated explanations. ```python explanations.ipython_plot(index=1) ``` -------------------------------- ### Get Feature Names Source: https://github.com/salesforce/omnixai/blob/main/PiML_explainable_models.ipynb Retrieves the names of the features available in the experiment. This is useful for understanding the dataset's columns. ```python exp.get_feature_names() ``` -------------------------------- ### Initialize BentoML Service Source: https://github.com/salesforce/omnixai/blob/main/README.md Create a service definition for the deployed explainer. ```python from omnixai.deployment.bentoml.omnixai import init_service svc = init_service( model_tag="tabular_explainer:latest", task_type="tabular", service_name="tabular_explainer" ) ``` -------------------------------- ### Initialize SHAP Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/shap.ipynb Configures the SHAP explainer with training data and the prediction function, then prepares test instances. ```python explainer = ShapTabular( training_data=tabular_data, predict_function=predict_function, nsamples=100 ) # Apply an inverse transform, i.e., converting the numpy array back to `Tabular` test_instances = transformer.invert(test) test_x = test_instances[1653:1655] ``` -------------------------------- ### Initialize Image Datasets Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/shap.ipynb Create Image objects for training and test datasets. ```python train_imgs, train_labels = Image(x_train.astype('float32'), batched=True), y_train test_imgs, test_labels = Image(x_test.astype('float32'), batched=True), y_test ``` -------------------------------- ### Initialize ContrastiveExplainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/cem_tf.ipynb Sets up the explainer with the target model and a preprocessing function for raw data transformation. ```python explainer = ContrastiveExplainer( model=model, preprocess_function=preprocess_func ) ``` -------------------------------- ### Import necessary libraries for L2X text classification Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp/l2x.ipynb Imports required for using the L2X explainer with text data. Ensure all libraries are installed. ```python import numpy as np import sklearn.ensemble from sklearn.datasets import fetch_20newsgroups from omnixai.data.text import Text from omnixai.preprocessing.text import Tfidf from omnixai.explainers.nlp.agnostic.l2x import L2XText ``` -------------------------------- ### Initialize Experiment Source: https://github.com/salesforce/omnixai/blob/main/PiML_explainable_models.ipynb Instantiate the Experiment class to begin a new machine learning experiment. ```python exp = Experiment() ``` -------------------------------- ### Configure Features and Logger Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/ranking.ipynb Initializes the logger and sets up feature configuration from a YAML file. ```python logger = logging.getLogger() tf.get_logger().setLevel("INFO") tf.autograph.set_verbosity(3) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' feature_config: SequenceExampleFeatureConfig = FeatureConfig.get_instance( tfrecord_type=TFRecordTypeKey.SEQUENCE_EXAMPLE, feature_config_dict=file_io.read_yaml("configs/activate_2020/feature_config.yaml"), logger=logger) print("Training features\n-----------------") print("\n".join(feature_config.get_train_features(key="name"))) ``` -------------------------------- ### Import Necessary Libraries for OmnixAI Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/ce.ipynb Imports essential libraries for data manipulation, machine learning, and the OmnixAI explainer functionalities. Ensure these are installed before running. ```python import tensorflow as tf import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from omnixai.data.tabular import Tabular from omnixai.explainers.tabular import CounterfactualExplainer ``` -------------------------------- ### Client Code to Call BentoML Service Source: https://context7.com/salesforce/omnixai/llms.txt Example client code using requests to interact with the deployed OmniXAI explainer service for predictions and explanations. ```python import requests import json from requests_toolbelt.multipart.encoder import MultipartEncoder data = json.dumps([25, "Private", 12, 40]) prediction = requests.post( "http://127.0.0.1:3000/predict", headers={"content-type": "application/json"}, data=data ).text m = MultipartEncoder( fields= { "data": data, "params": json.dumps({"lime": {"num_features": 5}}) } ) result = requests.post( "http://127.0.0.1:3000/explain", headers={"Content-Type": m.content_type}, data=m ).text from omnixai.explainers.base import AutoExplainerBase explanations = AutoExplainerBase.parse_explanations_from_json(result) for name, explanation in explanations.items(): print(f"{name}: {explanation}") ``` -------------------------------- ### Generate Explanations Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/ranking.ipynb Run the explainer on the sample query to identify top features. ```python explanation = ranking_explainer.explain(sample_query, # The tabular instance to be explained k=3 # The maximum number of features to consider as explanation ) ``` -------------------------------- ### VisionExplainer Initialization Source: https://context7.com/salesforce/omnixai/llms.txt Shows the initialization of VisionExplainer for image classification models, loading a pretrained ResNet50 model. This setup is for generating explanations for image data. ```python import torch import torch.nn as nn import torchvision.models as models import torchvision.transforms as transforms import numpy as np from PIL import Image as PilImage from omnixai.data.image import Image from omnixai.explainers.vision import VisionExplainer # Load pretrained model model = models.resnet50(pretrained=True) model.eval() ``` -------------------------------- ### Initialize and run L2X explainer for text classification Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp/l2x.ipynb Initializes the L2XText explainer with training data and the prediction function, then generates explanations for a given test instance and plots them. The explainer requires `training_data`, `predict_function`, and `mode`. ```python idx = 83 explainer = L2XText( training_data=x_train, predict_function=predict_function ) explanations = explainer.explain(x_test[idx:idx+9]) explanations.ipython_plot(class_names=class_names) ``` -------------------------------- ### Import PiML Experiment and Matplotlib Source: https://github.com/salesforce/omnixai/blob/main/PiML_explainable_models.ipynb This snippet shows how to import the main Experiment class from the piml library and matplotlib for plotting. Ensure these libraries are installed in your environment. ```python from piml import Experiment # Experiment is main class of PiML from matplotlib import pyplot as plt %matplotlib inline import pandas as pd import numpy as np ``` -------------------------------- ### Launch Visualization Dashboard Source: https://github.com/salesforce/omnixai/blob/main/tutorials/omnixai_in_ml_workflow.ipynb Launches an interactive dashboard for visualizing explanations. Requires instances, data explanations, local explanations, global explanations, prediction explanations, and class names. ```python from omnixai.visualization.dashboard import Dashboard # Launch a dashboard for visualization dashboard = Dashboard( instances=test_instances, data_explanations=data_explanations, local_explanations=local_explanations, global_explanations=global_explanations, prediction_explanations=prediction_explanations, class_names=class_names ) dashboard.show() ``` -------------------------------- ### Load Question Answering Model Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp/ce_qa.ipynb Loads a pre-trained question answering model from Hugging Face's transformers library. The 'deepset/roberta-base-squad2' model is used in this example. ```python # Load the pretrained model for question answering model_name = "deepset/roberta-base-squad2" model = pipeline('question-answering', model=model_name, tokenizer=model_name) ``` -------------------------------- ### Initializing and Using LimeText Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp/lime.ipynb Initializing the LimeText explainer and generating explanations for specific text samples. ```python idx = 83 explainer = LimeText(predict_function=predict_function) explanations = explainer.explain(x_test[idx:idx+4]) explanations.ipython_plot(index=0, class_names=class_names) ``` ```python explanations.ipython_plot(index=1, class_names=class_names) ``` -------------------------------- ### Generate and Plot Counterfactual Explanations Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp/ce_qa.ipynb Generates counterfactual explanations for a given set of question-answering text inputs using the initialized explainer. The explanations are then plotted for the first example. ```python x = Text([ "The option to convert models between FARM and transformers gives freedom to the user and let people easily switch between frameworks. [SEP] " "What can people do with model coversion?", "Electric vehicles emit much less harmful pollutants than conventional vehicles and ultimately, create a cleaner environment for human beings. [SEP] " "what is the result of using eletric vehicles?" ]) explanations = explainer.explain(x) explanations.ipython_plot(index=0) ``` -------------------------------- ### Generate and Print Explanations Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/gpt.ipynb Generates explanations for the selected test instance using the initialized explainer and prints the text-based explanation. This step requires the explainer to be set up with valid data and API key. ```python explanations = explainer.explain(test_x) print(explanations.get_explanations(index=0)["text"]) ``` -------------------------------- ### Import Libraries for Decision Tree Explanations Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/tree.ipynb Imports necessary libraries for data manipulation, machine learning, and OmniXAI's tabular explanation capabilities. Ensure these are installed before use. ```python import os import sklearn import numpy as np import pandas as pd from omnixai.data.tabular import Tabular from omnixai.explainers.tabular.specific.decision_tree import TreeClassifier ``` -------------------------------- ### Create a Tabular Sample Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/ranking.ipynb Initialize a Tabular instance from a subset of training data for explanation. ```python sample_query = Tabular( df_train[df_train["query_id"]=="query_5"], target_column='clicked', ) sample_query.to_pd() ``` -------------------------------- ### Load Data, Train Model, and Print Shapes Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/ce.ipynb This snippet orchestrates the data loading and model training process. It calls the `diabetes_data` function to get the processed data and then uses `train_tf_model` to train a TensorFlow model. Finally, it prints the shapes of the training and testing feature sets. ```python file_path = '../data/diabetes.csv' x_train, y_train, x_test, y_test, feature_names = diabetes_data(file_path) print('x_train shape: {}'.format(x_train.shape)) print('x_test shape: {}'.format(x_test.shape)) model = train_tf_model(x_train, y_train, x_test, y_test) ``` -------------------------------- ### Initialize Model and Preprocessing Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/ig_torch.ipynb Sets up a pretrained InceptionV3 model and defines a preprocessing function to transform images into tensors. ```python device = "cuda" if torch.cuda.is_available() else "cpu" model = models.inception_v3(pretrained=True).to(device) transform = transforms.Compose([ transforms.Resize((256, 256)), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) preprocess = lambda ims: torch.stack([transform(im.to_pil()) for im in ims]) ``` -------------------------------- ### Initialize and Visualize Timeseries Explainer Source: https://github.com/salesforce/omnixai/blob/main/docs/omnixai.md Configures the TimeseriesExplainer with SHAP and MACE methods for anomaly detection and launches an interactive dashboard. ```python # Initialize a TimeseriesExplainer explainers = TimeseriesExplainer( explainers=["shap", "mace"], # Apply SHAP and MACE explainers mode="anomaly_detection", # An anomaly detection task data=Timeseries.from_pd(train_df), # Set data for initializing the explainers model=detector, # Set the black-box anomaly detector preprocess=None, postprocess=None, params={"mace": {"threshold": 0.001}} # Additional parameters for MACE ) # Generate local explanations test_instance = Timeseries.from_pd(test_df) local_explanations = explainers.explain(test_instance) # Launch a dashboard for visualization dashboard = Dashboard(instances=test_instance, local_explanations=local_explanations) dashboard.show() ``` -------------------------------- ### Initialize Ranking Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/ranking.ipynb Set up the ValidityRankingExplainer with training data and a prediction function. ```python from omnixai.explainers.ranking.agnostic.validity import ValidityRankingExplainer ranking_explainer = ValidityRankingExplainer(training_data=training_data, ignored_features=ignored_features, predict_function=lambda x: predict(x.to_pd())) ``` -------------------------------- ### Initialize Grad-CAM Explainer for Vision Models Source: https://github.com/salesforce/omnixai/blob/main/docs/omnixai.explainers.md Initialize a Grad-CAM explainer for vision models. Requires a pre-trained model, a preprocessing function, and the target layer. ```python # A ResNet model to explain model = models.resnet50(pretrained=True) # Construct the prediction function transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) preprocess = lambda images: torch.stack([transform(im.to_pil()) for im in images]) # Initialize the Grad-CAM explainer explainer = GradCAM( model=model, preprocess_function=preprocess, target_layer=model.layer4[-1] ) ``` -------------------------------- ### Launch Visualization Dashboard Source: https://github.com/salesforce/omnixai/blob/main/docs/index.md Initializes and displays a Dash-based dashboard for visualizing explanations and performing what-if analysis. ```python # Launch a dashboard for visualization dashboard = Dashboard( instances=test_instances, # The instances to explain local_explanations=local_explanations, # Set the generated local explanations global_explanations=global_explanations, # Set the generated global explanations prediction_explanations=prediction_explanations, # Set the prediction metrics class_names=class_names, # Set class names explainer=explainer # The created TabularExplainer for what if analysis ) dashboard.show() # Launch the dashboard ``` -------------------------------- ### Initialize CounterfactualExplainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/ce_tf.ipynb Sets up the explainer with the target model and a preprocessing function required to format input data. ```python explainer = CounterfactualExplainer( model=model, preprocess_function=preprocess_func ) ``` -------------------------------- ### Launch OmniXAI Explanation Dashboard Source: https://github.com/salesforce/omnixai/blob/main/tutorials/timeseries.ipynb Launches an interactive Dash application for visualizing time series explanations. Requires test instances and local explanations objects. ```python from omnixai.visualization.dashboard import Dashboard dashboard = Dashboard(instances=test_instances, local_explanations=local_explanations) dashboard.show() ``` -------------------------------- ### Initialize DataAnalyzer and Generate Explanations Source: https://github.com/salesforce/omnixai/blob/main/tutorials/data_analysis.ipynb Initializes the DataAnalyzer with specified analysis types and generates global explanations. The `params` argument customizes analyzers like 'imbalance' with specific features. ```python # Initialize a `DataAnalyzer` explainer. # We can choose multiple explainers/analyzers by specifying analyzer names. # In this example, the first explainer is for feature correlation analysis and # the last two explainers are for feature selection and # the others are for feature imbalance analysis (the same explainer with different parameters). explainer = DataAnalyzer( explainers=["correlation", "imbalance#0", "imbalance#1", "imbalance#2", "imbalance#3", "imbalance#4", "mutual", "chi2"], mode="classification", data=tabular_data ) # Generate explanations by calling `explain_global`. explanations = explainer.explain_global( params={"imbalance#0": {"features": ["Sex"]}, "imbalance#1": {"features": ["Race"]}, "imbalance#2": {"features": ["Sex", "Race"]}, "imbalance#3": {"features": ["Marital Status", "Age"]}, "imbalance#4": {"features": ["Workclass"]}} ) print("Correlation:") explanations["correlation"].ipython_plot() print("Imbalance#0:") explanations["imbalance#0"].ipython_plot() print("Imbalance#4:") explanations["imbalance#4"].ipython_plot() print("Mutual information:") explanations["mutual"].ipython_plot() print("Chi square:") explanations["chi2"].ipython_plot() ``` -------------------------------- ### Initialize and Use VisionExplainer for Classification Source: https://context7.com/salesforce/omnixai/llms.txt Demonstrates initializing VisionExplainer with multiple explanation methods for a classification model and generating local explanations. It also shows how to set up a global explainer for feature visualization. ```python transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) def preprocess(images): tensors = [] for img in images.to_pil(): tensors.append(transform(img)) return torch.stack(tensors) def postprocess(outputs): return torch.nn.functional.softmax(outputs, dim=1).detach().numpy() # Load test image pil_img = PilImage.open("test_image.jpg").convert("RGB") test_image = Image(data=pil_img) # Initialize VisionExplainer explainer = VisionExplainer( explainers=["gradcam", "lime", "ig", "shap"], mode="classification", model=model, preprocess=preprocess, postprocess=postprocess, params={ "gradcam": {"target_layer": model.layer4[-1]}, "lime": {"kernel_size": 4}, "ig": {"steps": 50} } ) # Generate local explanations local_explanations = explainer.explain(test_image) # Access Grad-CAM explanation gradcam_exp = local_explanations["gradcam"] # Generate global explanations (feature visualization) explainer_global = VisionExplainer( explainers=["feature_visualization"], mode="classification", model=model, preprocess=preprocess, postprocess=postprocess, params={ "feature_visualization": { "objectives": [ {"layer": model.layer4[-1], "type": "channel", "index": list(range(6))} ] } } ) global_explanations = explainer_global.explain_global() # List available explainers VisionExplainer.list_explainers() ``` -------------------------------- ### Initialize PredictionAnalyzer Source: https://github.com/salesforce/omnixai/blob/main/docs/index.md Sets up the PredictionAnalyzer to compute performance metrics for classification or regression tasks. ```python from omnixai.explainers.prediction import PredictionAnalyzer analyzer = PredictionAnalyzer( mode="classification", test_data=test_data, # The test dataset (a `Tabular` instance) test_targets=test_labels, # The test labels (a numpy array) model=model, # The ML model preprocess=lambda z: transformer.transform(z) # Converts raw features into the model inputs ) prediction_explanations = analyzer.explain() ``` -------------------------------- ### Launch the OmniXAI Dashboard Source: https://github.com/salesforce/omnixai/blob/main/README.md Initialize and display the interactive dashboard for model explanations. ```python dashboard = Dashboard( instances=test_img, local_explanations=local_explanations, global_explanations=global_explanations ) dashboard.show() ``` -------------------------------- ### Tabular Explainer Initialization and Usage Source: https://context7.com/salesforce/omnixai/llms.txt Demonstrates initializing TabularExplainer with multiple explanation methods (lime, shap, mace, pdp, ale) for classification tasks. It covers data preparation, model training, preprocessing, generating local and global explanations, and accessing individual explanation results. ```python import numpy as np import pandas as pd import xgboost from sklearn.model_selection import train_test_split from omnixai.data.tabular import Tabular from omnixai.preprocessing.tabular import TabularTransform from omnixai.explainers.tabular import TabularExplainer df = pd.DataFrame({ "Age": np.random.randint(20, 60, 1000), "Education_Num": np.random.randint(8, 16, 1000), "Hours_Per_Week": np.random.randint(20, 60, 1000), "Workclass": np.random.choice(["Private", "Govt", "Self-emp"], 1000), "Income": np.random.choice([0, 1], 1000) }) tabular_data = Tabular( data=df, categorical_columns=["Workclass"], target_column="Income" ) # Preprocess and train model transformer = TabularTransform().fit(tabular_data) X = transformer.transform(tabular_data) train_data, test_data, train_labels, test_labels = train_test_split( X[:, :-1], X[:, -1], train_size=0.8 ) model = xgboost.XGBClassifier(n_estimators=100, max_depth=5) model.fit(train_data, train_labels) # Convert back to Tabular for explainer train_tabular = transformer.invert(train_data) test_tabular = transformer.invert(test_data) # Initialize TabularExplainer with multiple methods explainer = TabularExplainer( explainers=["lime", "shap", "mace", "pdp", "ale"], mode="classification", data=train_tabular, model=model, preprocess=lambda z: transformer.transform(z), params={ "lime": {"kernel_width": 3}, "shap": {"nsamples": 100}, "mace": {"ignored_features": ["Workclass"]} } ) # Generate local explanations for test instances test_instances = test_tabular[:5] local_explanations = explainer.explain(X=test_instances) # Access individual explanations lime_explanation = local_explanations["lime"] shap_explanation = local_explanations["shap"] # Generate global explanations global_explanations = explainer.explain_global( params={"pdp": {"features": ["Age", "Education_Num", "Hours_Per_Week"]}} ) # List available explainers TabularExplainer.list_explainers() ``` -------------------------------- ### Initialize LIME Explainer and Generate Explanations Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/lime.ipynb Initializes the LimeImage explainer with the batch prediction function and generates explanations for the test image. It then plots the explanations for the first class. ```python explainer = LimeImage(predict_function=batch_predict) # Explain the top labels explanations = explainer.explain(img, hide_color=0, num_samples=1000) explanations.ipython_plot(index=0, class_names=idx2label) ``` -------------------------------- ### Launch Visualization Dashboard Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp.ipynb Launches a Dash-based dashboard to visualize the generated local explanations along with the original instances. This provides an interactive way to explore the model's behavior. ```python # Launch a dashboard for visualization dashboard = Dashboard( instances=x, local_explanations=local_explanations ) dashboard.show() ``` -------------------------------- ### Initialize and Use Grad-CAM Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/gradcam_torch.ipynb Initializes the GradCAM explainer with the model, target layer, and preprocessing function. It then generates explanations for the top predicted label and plots them. ```python explainer = GradCAM( model=model, target_layer=model.layer4[-1], preprocess_function=preprocess ) # Explain the top label explanations = explainer.explain(img) explanations.ipython_plot(index=0, class_names=idx2label) ``` -------------------------------- ### Initialize and Run CounterfactualExplainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/ce.ipynb Initializes the explainer with training data and a prediction function, then generates and visualizes explanations for test data. ```python explainer = CounterfactualExplainer( training_data=tabular_data, predict_function=model ) explanations = explainer.explain(x_test[:1]) explanations.ipython_plot() ``` -------------------------------- ### Explain Model with IntegratedGradientText Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp/ig_tf.ipynb Initializes the explainer and generates visual explanations for text inputs. ```python explainer = IntegratedGradientText( model=model, embedding_layer=model.embedding, preprocess_function=preprocess, id2token=transform.id_to_word ) x = Text([ "What a great movie! if you have no taste.", "it was a fantastic performance!", "best film ever", "such a great show!", "it was a horrible movie", "i've never watched something as bad" ]) explanations = explainer.explain(x) explanations.ipython_plot(class_names=class_names) ``` -------------------------------- ### Initialize Text Data Source: https://github.com/salesforce/omnixai/blob/main/tutorials/misc/preprocessing.ipynb Demonstrates how to create a Text object from a list of strings. This is the initial step before applying any transformations. ```python from omnixai.data.text import Text from omnixai.preprocessing.text import Tfidf, Word2Id text = Text( data=["Hello I'm a single sentence.", "And another sentence.", "And the very very last one."] ) print(text) ``` -------------------------------- ### Initialize TabularExplainer and Generate Explanations Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular_classification.ipynb Initialize a TabularExplainer with specified explainers, mode, training data, model, preprocessing function, and explainer-specific parameters. Then, generate local and global explanations for test instances. ```python # Initialize a TabularExplainer explainers = TabularExplainer( explainers=["lime", "shap", "mace", "pdp", "ale"], mode="classification", data=train_data, model=gbtree, preprocess=preprocess, params={ "lime": {"kernel_width": 3}, "shap": {"nsamples": 100}, "mace": {"ignored_features": ["Sex", "Race", "Relationship", "Capital Loss"]} } ) # Generate explanations test_instances = test_data[1653:1658] local_explanations = explainers.explain(X=test_instances) global_explanations = explainers.explain_global( params={"pdp": {"features": ["Age", "Education-Num", "Capital Gain", "Capital Loss", "Hours per week", "Education", "Marital Status", "Occupation"]}} ) ``` -------------------------------- ### Initialize GPT Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/gpt.ipynb Initializes the GPTExplainer with training data, the prediction function, and an OpenAI API key. The training data can be a subset if the full dataset is too large. ```python explainer = GPTExplainer( training_data=tabular_data, predict_function=predict_function, apikey="sk-xxx" ) ``` -------------------------------- ### Initialize and Use FeatureMapVisualizer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/feature_map_tf.ipynb Initializes the FeatureMapVisualizer with the VGG16 model, a target layer (the 11th layer, index 10), and the defined preprocessing function. It then generates explanations for the input image and plots them. ```python explainer = FeatureMapVisualizer( model=model, target_layer=model.layers[10], preprocess_function=preprocess ) explanations = explainer.explain(img) explanations.ipython_plot() ``` -------------------------------- ### Initialize TabularExplainer and Generate Explanations Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular_regression.ipynb Configures the TabularExplainer with specific explainers and parameters, then generates local and global explanations. ```python # Initialize a TabularExplainer explainers = TabularExplainer( explainers=["lime", "shap", "sensitivity", "pdp", "ale"], mode="regression", data=train_data, model=rf, preprocess=preprocess, params={ "lime": {"kernel_width": 3}, "shap": {"nsamples": 100} } ) # Generate explanations test_instances = test_data[0:5] local_explanations = explainers.explain(X=test_instances) global_explanations = explainers.explain_global( params={"pdp": {"features": ["MedInc", "HouseAge", "AveRooms", "AveBedrms", "Population", "AveOccup", "Latitude", "Longitude"]}} ) ``` -------------------------------- ### Initialize Relevance Model Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/ranking.ipynb Loads a relevance model with specific feature configurations and logging. ```python relevance_model = RelevanceModel( feature_config=feature_config, tfrecord_type=TFRecordTypeKey.EXAMPLE, model_file=os.path.join(MODEL_DIR, 'final/default/'), logger=logger, output_name="relevance_score", file_io=file_io ) logger.info("Is Keras model? {}".format(isinstance(relevance_model.model, tf.keras.Model))) logger.info("Is compiled? {}".format(relevance_model.is_compiled)) ``` -------------------------------- ### Initialize BentoML Service for OmniXAI Explainer Source: https://context7.com/salesforce/omnixai/llms.txt Creates a BentoML service from a saved OmniXAI explainer. Specify the task type ('tabular', 'vision', or 'nlp'). ```python from omnixai.deployment.bentoml.omnixai import init_service svc = init_service( model_tag="tabular_explainer:latest", task_type="tabular", service_name="tabular_explainer" ) # Start service: bentoml serve service:svc --reload ``` -------------------------------- ### Initialize IntegratedGradient Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/ig_vlm.ipynb Initializes the `IntegratedGradient` explainer for vision-language tasks. It requires the model, embedding layer, preprocessing function, tokenizer, and a loss function. ```python explainer = IntegratedGradient( model=model, embedding_layer=model.text_encoder.embeddings.word_embeddings, preprocess_function=preprocess, tokenizer=tokenizer, loss_function=lambda outputs: outputs[:, 1].sum() ) ``` -------------------------------- ### Initialize VisionExplainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision.ipynb Configures the VisionExplainer with multiple explanation methods and specific layer parameters. ```python # Initialize a VisionExplainer explainer = VisionExplainer( explainers=["gradcam", "lime", "ig", "ce", "scorecam", "smoothgrad", "guidedbp", "layercam"], mode="classification", model=model, preprocess=preprocess, postprocess=postprocess, params={ "gradcam": {"target_layer": model.layer4[-1]}, "ce": {"binary_search_steps": 2, "num_iterations": 100}, "scorecam": {"target_layer": model.layer4[-1]}, "layercam": {"target_layer": model.layer3[-3]}, } ) ``` -------------------------------- ### Initialize LIME Tabular Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/lime.ipynb Initializes a LIME explainer for tabular data. Requires training data, the prediction function, and the task mode (classification or regression). The training data can be a subset if the original dataset is too large. ```python explainer = LimeTabular( training_data=tabular_data, predict_function=predict_function ) ``` -------------------------------- ### Launch Dashboard for Visualization Source: https://github.com/salesforce/omnixai/blob/main/tutorials/data_analysis.ipynb Launches an interactive Dash dashboard to visualize the generated global explanations. This provides a user-friendly interface for exploring the analysis results. ```python # Launch a dashboard for visualization. dashboard = Dashboard(global_explanations=explanations) dashboard.show() ``` -------------------------------- ### Launch Visualization Dashboard Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular_classification.ipynb Initializes and displays a Dash application for visualizing various explanation types and prediction results. ```python # Launch a dashboard for visualization dashboard = Dashboard( instances=test_instances, local_explanations=local_explanations, global_explanations=global_explanations, prediction_explanations=prediction_explanations, class_names=class_names, explainer=explainers ) dashboard.show() ``` -------------------------------- ### Initialize ContrastiveExplainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/cem_torch.ipynb Initializes the explainer with the trained model and the required preprocessing function. ```python from omnixai.explainers.vision import ContrastiveExplainer explainer = ContrastiveExplainer( model=model, preprocess_function=preprocess ) ``` -------------------------------- ### Initialize SHAP Time Series Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/timeseries/shap.ipynb Initializes the ShapTimeseries explainer with training data, a prediction function (detector), and specifies the task mode as 'anomaly_detection'. ```python explainer = ShapTimeseries( training_data=Timeseries.from_pd(train_df), predict_function=detector, mode="anomaly_detection" ) test_x = Timeseries.from_pd(test_df) ``` -------------------------------- ### Launch an OmniXAI Dashboard Source: https://github.com/salesforce/omnixai/blob/main/docs/omnixai.md Initializes and displays a dashboard using pre-computed local and global explanations. ```python dashboard = Dashboard( instances=test_instances, # The instances to explain local_explanations=local_explanations, # Set the generated local explanations global_explanations=global_explanations, # Set the generated global explanations class_names=class_names # Set class names ) dashboard.show() # Launch the dashboard ``` -------------------------------- ### Generate and Plot Grad-CAM Explanations Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/gradcam_vlm.ipynb Generates explanations for the given inputs using the initialized GradCAM explainer and then plots the explanations using the `ipython_plot` method. ```python explanations = explainer.explain(inputs) explanations.ipython_plot() ``` -------------------------------- ### Initialize SHAP Explainer for Tabular Data Source: https://github.com/salesforce/omnixai/blob/main/docs/omnixai.explainers.md Use ShapTabular explainer for tabular data. Requires training data and a prediction function. ```python explainer = ShapTabular( training_data=tabular_data, predict_function=predict_function ) ``` -------------------------------- ### Configure Model-Agnostic Explainer Source: https://github.com/salesforce/omnixai/blob/main/docs/omnixai.explainers.md Demonstrates setting up a model-agnostic explainer by defining a prediction function that wraps an XGBoost model and its data transformer. ```python # Load dataset data = np.genfromtxt('adult.data', delimiter=', ', dtype=str) feature_names = [ "Age", "Workclass", "fnlwgt", "Education", "Education-Num", "Marital Status", "Occupation", "Relationship", "Race", "Sex", "Capital Gain", "Capital Loss", "Hours per week", "Country", "label" ] tabular_data = Tabular( data, feature_columns=feature_names, categorical_columns=[feature_names[i] for i in [1, 3, 5, 6, 7, 8, 9, 13]], target_column='label' ) # Preprocessing transformer = TabularTransform().fit(tabular_data) x = transformer.transform(tabular_data) # Train an XGBoost model gbtree = xgboost.XGBClassifier(n_estimators=300, max_depth=5) gbtree.fit(x[:, :-1], x[:, -1]) # The last column in `x` is the label column # Construct the prediction function predict_function=lambda z: gbtree.predict_proba(transformer.transform(z)) ``` -------------------------------- ### Launch OmniXAI Dashboard Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular_regression.ipynb Launch an interactive Dash dashboard for visualizing explanations. Provide instances, local explanations, global explanations, and prediction explanations. ```python # Launch a dashboard for visualization dashboard = Dashboard( instances=test_instances, local_explanations=local_explanations, global_explanations=global_explanations, prediction_explanations=prediction_explanations ) dashboard.show() ``` -------------------------------- ### Initialize MACEExplainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/timeseries/mace.ipynb Configures the MACEExplainer with training data, the prediction function, and an anomaly threshold. ```python explainer = MACEExplainer( training_data=Timeseries.from_pd(train_df), predict_function=detector, mode="anomaly_detection", threshold=0.001 ) test_x = Timeseries.from_pd(test_df) ``` -------------------------------- ### Initialize Polyjuice Explainer for QA Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp/ce_qa.ipynb Initializes the Polyjuice explainer for question answering tasks. It requires the prediction function and sets the mode to 'qa'. ```python # Initialize the counterfactual explainer based on Polyjuice explainer = Polyjuice(predict_function=_predict, mode="qa") ``` -------------------------------- ### Initialize Grad-CAM Explainer and Plot Explanation for Top Label Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/gradcam_tf.ipynb Initializes the Grad-CAM explainer with the model, target layer, and preprocessing function. It then generates and plots the explanation for the top predicted label. ```python explainer = GradCAM( model=model, target_layer=model.layers[-5], preprocess_function=preprocess ) # Explain the top label explanations = explainer.explain(img) explanations.ipython_plot(index=0, class_names=idx2label) ``` -------------------------------- ### Initialize Polyjuice Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/nlp/ce_classification.ipynb Initializes the Polyjuice counterfactual explainer with the defined prediction function. ```python # Initialize the counterfactual explainer based on Polyjuice explainer = Polyjuice(predict_function=_predict) ``` -------------------------------- ### Initialize GradCAM Explainer for Vision-Language Tasks Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/gradcam_vlm.ipynb Initializes the GradCAM explainer with the specified model, target layer, preprocessing function, tokenizer, and a custom loss function. The target layer is set to a specific cross-attention layer. ```python explainer = GradCAM( model=model, target_layer=model.text_encoder.base_model.base_model.encoder.layer[6]. crossattention.self.attention_probs_layer, preprocess_function=preprocess, tokenizer=tokenizer, loss_function=lambda outputs: outputs[:, 1].sum() ) ``` -------------------------------- ### Launch OmniXAI Dashboard Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision.ipynb Launch an interactive Dash application for visualizing explanations. Provide the instances, local explanations, and class names to the Dashboard constructor. ```python # Launch a dashboard for visualization dashboard = Dashboard( instances=img, local_explanations=local_explanations, class_names=idx2label, ) dashboard.show() ``` -------------------------------- ### Initialize TimeseriesExplainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/timeseries.ipynb Initializes the TimeseriesExplainer with specified explainers ('shap', 'mace'), mode ('anomaly_detection'), training data, the anomaly detection model, and parameters for the 'mace' explainer. ```python # Initialize a TimeseriesExplainer explainers = TimeseriesExplainer( explainers=["shap", "mace"], mode="anomaly_detection", data=Timeseries.from_pd(train_df), model=detector, preprocess=None, postprocess=None, params={"mace": {"threshold": 0.001}} ) ``` -------------------------------- ### Initialize MACE Explainer Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/mace.ipynb Initializes the MACEExplainer with training data, the prediction function, and a list of features to ignore during counterfactual generation. Ignored features will remain unchanged in the generated explanations. ```python explainer = MACEExplainer( training_data=tabular_data, predict_function=predict_function, ignored_features=["Sex", "Race", "Relationship", "Capital Loss"] ) test_instances = tabular_data.remove_target_column()[0:5] ``` -------------------------------- ### Initialize Model and Preprocessing Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/feature_map_torch.ipynb Loads a pre-trained ResNet50 model and defines the transformation pipeline for input images. ```python model = models.resnet50(pretrained=True) transform = transforms.Compose( [ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ] ) preprocess = lambda ims: torch.stack([transform(im.to_pil()) for im in ims]) ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/feature_visualization_torch.ipynb Imports PyTorch, torchvision models, and the FeatureVisualizer from OmniXAI for vision model explanation. ```python import torch from torchvision import models from omnixai.explainers.vision.specific.feature_visualization.visualizer import FeatureVisualizer ``` -------------------------------- ### Generate Local Explanations Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision.ipynb Use the explainer.explain() method to generate local explanations for a given image instance. This is a core step before visualization. ```python local_explanations = explainer.explain(img) ``` -------------------------------- ### Initialize GPT Explainer for Tabular Data Source: https://github.com/salesforce/omnixai/blob/main/README.md Initializes a TabularExplainer using the GPT explainer for classification tasks. Requires API key for OpenAI. ```python explainer = TabularExplainer( explainers=["gpt"], # The GPT explainer to apply mode="classification", # The task type data=train_data, # The data for initializing the explainers model=model, # The ML model to explain preprocess=lambda z: transformer.transform(z), # Converts raw features into the model inputs params={ "gpt": {"apikey": "xxxx"} } # Set the OpenAI API KEY ) local_explanations = explainer.explain(X=test_instances) ``` -------------------------------- ### Load and Preprocess Test Image Source: https://github.com/salesforce/omnixai/blob/main/tutorials/vision/feature_map_tf.ipynb Loads a test image using PIL, converts it to RGB, and then resizes it to 224x224 pixels using OmniXAI's Resize transformer. The image is wrapped in an OmniXAI Image object. ```python img = img = Resize((224, 224)).transform( Image(PilImage.open("../data/images/dog_cat_2.png").convert("RGB"))) ``` -------------------------------- ### Prepare Test Instances for Explanation Source: https://github.com/salesforce/omnixai/blob/main/tutorials/tabular/gpt.ipynb Applies an inverse transform to convert numpy arrays back to a Tabular format and selects a specific test instance for explanation generation. Ensure `transformer` and `test` are defined. ```python # Apply an inverse transform, i.e., converting the numpy array back to `Tabular` test_instances = transformer.invert(test) test_x = test_instances[1653] ```