Skip to main content

Leap Labs Interpretability Engine

Project description

Leap Interpretability Engine

Congratulations on being a very early adopter of our interpretability engine! Not sure what's going on? Check out the FAQ

Installation

Use the package manager pip to install leap-ie.

pip install leap-ie

Sign in and generate your API key in the leap app - you'll need this to get started.

Usage

Using the interpretability engine is really easy! All you need to do is import leap_ie, and wrap your model in our generate function:

results = engine.generate(project_name="interpretability", model=your_model, class_list=['hotdog', 'not_hotdog'], config= {"leap_api_key": "YOUR_LEAP_API_KEY"})

Results

The generate function returns a pandas dictionary. If you're in a jupyter notebook, you can view these inline using engine.display_results(results), but for the best experience we recommend you head to the leap app to view your prototypes and isolations.

Weights and Biases Integration

We can also log results directly to your WandB projects! To do this, set project_name to the name of the WandB project where you'd like the results to be logged, and add your WandB API key and entity name to the config dictionary:

config = {
    "wandb_api_key": "YOUR_WANDB_API_KEY",
    "wandb_entity": "your_wandb_entity",
    "leap_api_key": "YOUR_LEAP_API_KEY"
}
results = engine.generate(project_name="your_wandb_project_name", model=your_model, class_list=['hotdog', 'not_hotdog'], config=config)

Prototype Generation

Given your model, we generate prototypes and entanglements for each class you specify. What is a prototype? What is entanglement? We also isolate entangled features in your prototypes. What is feature isolation?

from leap_ie import engine
from leap_ie.models import get_model

config = {"leap_api_key": "YOUR_LEAP_API_KEY"}

# Replace this model with your own, or explore any imagenet classifier from torchvision (https://pytorch.org/vision/stable/models.html).
model = preprocessing_fn, model, class_list = get_model('torchvision.resnet18')

# indexes of classes to generate prototypes for. In this case, ['tench', 'goldfish', 'great white shark'].
target_classes = [0, 1, 2]

# generate prototypes
prototypes = engine.generate(project_name="resnet18", model=model, class_list=class_list, config=config,
                             target_classes=target_classes, preprocessing=preprocessing_fn, samples=None, device=None, mode="pt")


# For the best experience, head to https://app.leap-labs.com/ to explore your prototypes and feature isolations in the browser!
# Or, if you're in a jupyter notebook, you can display your results inline:
engine.display_results(prototypes)

Sample Feature Isolation

Given some input image, we can show you which features your model thinks belong to each class. If you specify target classes, we'll isolate features for those, or if not, we'll isolate features for the three highest probability classes.

from torchvision import transforms
from leap_ie import engine
from leap_ie.models import get_model
from PIL import Image

config = {"leap_api_key": "YOUR_LEAP_API_KEY"}

# Replace this model with your own, or explore any imagenet classifier from torchvision (https://pytorch.org/vision/stable/models.html).
model = preprocessing_fn, model, class_list = get_model('torchvision.resnet18')

# load an image
image_path = "tools.jpeg"
tt = transforms.ToTensor()
image = preprocessing_fn[0](tt(Image.open(image_path)).unsqueeze(0))

# to isolate features:
isolations = engine.generate(project_name="resnet18", model=model, class_list=class_list, config=config,
                             target_classes=None, preprocessing=preprocessing_fn, samples=image, mode="pt")

# For the best experience, head to https://app.leap-labs.com/ to explore your prototypes and feature isolations in the browser!
# Or, if you're in a jupyter notebook, you can display your results inline:
engine.display_results(isolations)

config

Leap provides a number of configuration options to fine-tune the interpretability engine's performance with your models. You can provide it as a dictionary or a path to a .json file.

Here are the default values - read on for an explanation of each.

config = {
            "use_alpha": False,
            "alpha_mask": False,
            "alpha_only": False,
            "baseline_init": 0,
            "diversity_weight": 0,
            "isolate_classes": None,
            "isolation_lr": 0.05,
            "hf_weight": 1,
            "isolation_hf_weight": 1,
            "input_dim": [224, 224, 3] if mode == "tf" else [3, 224, 224],
            "isolation": True,
            "logit_scale": 1,
            "log_freq": 100,
            "lr": 0.05,
            "max_isolate_classes": min(3, len(class_list)),
            "max_steps": 500,
            "seed": 0,
            "use_baseline": False,
            "transform": "xl",
            "target_classes": [0] if target_classes is None else target_classes,
            "use_hipe": False,
            "wandb_api_key": None,
            "wandb_entity": None,
        }

use_alpha: if True, adds an alpha channel to the prototype. This results in the prototype generation process returning semi-transparent prototypes, which allow it to express ambivalence about the values of pixels that don't change the model prediction.

alpha_mask: if True, applies a mask during prototype generation which encourages the resulting prototypes to be minimal, centered and concentrated. Experimental.

alpha_only: if True, during the prototype generation process, only an alpha channel is optimised. This results in generation prototypical shapes and textures only, with no colour information.

baseline_init: diversity_weight: isolate_classes: isolation_lr: hf_weight: isolation_hf_weight: input_dim: isolation: logit_scale: log_freq: lr: max_isolate_classes: max_steps: seed: use_baseline: transform: target_classes: use_hipe: wandb_api_key: wandb_entity:

engine.generate()

The generate function is used for both prototype generation directly from the model, and for feature isolation on your input samples.

leap_ie.engine.generate(project_name, model, class_list, config, target_classes=None, preprocessing=None, samples=None, device=None, mode="pt")

project_name: Name of your project. Used for logging.

model: Model for interpretation. Currently we support image classification models only. We expect the model to take a batch of images as input, and return a batch of logits (NOT probabilities). If using pytorch, we expect the model to take images to be in channels first format, e.g. of shape [1, channels, height, width]. If tensorflow, channels last, e.g.[1, height, width, channels].

class_list: List of class names corresponding to your model's output classes, e.g. ['hotdog', 'not hotdog', ...].

config: Configuration dictionary, or path to a json file containing your configuration. At minimim, this must contain {"leap_api_key": "YOUR_LEAP_API_KEY"}

target_classes (optional): List of target class indices to generate prototypes or isolations for, e.g. [0,1]. If None, prototypes will be generated for the class at output index 0 only, e.g. 'hotdog', and feature isolations will be generated for the top 3 classes.

preprocessing (optional): Preprocessing function to be used for generation. This can be None, but for best results, use the preprocessing function used on inputs for inference.

samples (optional): None, or a batch of images to perform feature isolation on. If provided, only feature isolation is performed (not prototype generation). We expect samples to be of shape [num_images, height, width, channels] if using tensorflow, or [1, channels, height, width] if using pytorch.

device (optional): Device to be used for generation. If None, we will try to find a device.

mode (optional): Framework to use, either 'pt' for pyorch or 'tf' for tensorflow. Default is 'pt'.

returns: A pandas dataframe containing the results of the generation process. Also logs more detailed results to the leap app.

FAQ

What is a prototype?

What is entanglement?

What is feature isolation?

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

leap_ie-0.0.3-pp310-pypy310_pp73-win_amd64.whl (525.5 kB view hashes)

Uploaded PyPy Windows x86-64

leap_ie-0.0.3-pp310-pypy310_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (626.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

leap_ie-0.0.3-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (655.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-pp310-pypy310_pp73-macosx_10_9_x86_64.whl (561.4 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

leap_ie-0.0.3-pp39-pypy39_pp73-win_amd64.whl (525.7 kB view hashes)

Uploaded PyPy Windows x86-64

leap_ie-0.0.3-pp39-pypy39_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (625.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

leap_ie-0.0.3-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (654.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (561.0 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

leap_ie-0.0.3-pp38-pypy38_pp73-win_amd64.whl (519.0 kB view hashes)

Uploaded PyPy Windows x86-64

leap_ie-0.0.3-pp38-pypy38_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (624.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

leap_ie-0.0.3-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (658.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (551.6 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

leap_ie-0.0.3-pp37-pypy37_pp73-win_amd64.whl (518.9 kB view hashes)

Uploaded PyPy Windows x86-64

leap_ie-0.0.3-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (624.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

leap_ie-0.0.3-pp37-pypy37_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (658.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (551.2 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

leap_ie-0.0.3-cp312-cp312-win_amd64.whl (626.2 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

leap_ie-0.0.3-cp312-cp312-win32.whl (559.5 kB view hashes)

Uploaded CPython 3.12 Windows x86

leap_ie-0.0.3-cp312-cp312-musllinux_1_1_x86_64.whl (4.7 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

leap_ie-0.0.3-cp312-cp312-musllinux_1_1_i686.whl (4.4 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

leap_ie-0.0.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.7 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

leap_ie-0.0.3-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (4.4 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-cp312-cp312-macosx_10_9_x86_64.whl (732.9 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

leap_ie-0.0.3-cp311-cp311-win_amd64.whl (634.5 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

leap_ie-0.0.3-cp311-cp311-win32.whl (572.0 kB view hashes)

Uploaded CPython 3.11 Windows x86

leap_ie-0.0.3-cp311-cp311-musllinux_1_1_x86_64.whl (4.5 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

leap_ie-0.0.3-cp311-cp311-musllinux_1_1_i686.whl (4.3 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

leap_ie-0.0.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.5 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

leap_ie-0.0.3-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (4.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-cp311-cp311-macosx_10_9_x86_64.whl (758.0 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

leap_ie-0.0.3-cp310-cp310-win_amd64.whl (629.9 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

leap_ie-0.0.3-cp310-cp310-win32.whl (571.9 kB view hashes)

Uploaded CPython 3.10 Windows x86

leap_ie-0.0.3-cp310-cp310-musllinux_1_1_x86_64.whl (4.1 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

leap_ie-0.0.3-cp310-cp310-musllinux_1_1_i686.whl (3.9 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

leap_ie-0.0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

leap_ie-0.0.3-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (3.8 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-cp310-cp310-macosx_10_9_x86_64.whl (753.0 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

leap_ie-0.0.3-cp39-cp39-win_amd64.whl (631.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

leap_ie-0.0.3-cp39-cp39-win32.whl (573.6 kB view hashes)

Uploaded CPython 3.9 Windows x86

leap_ie-0.0.3-cp39-cp39-musllinux_1_1_x86_64.whl (4.1 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

leap_ie-0.0.3-cp39-cp39-musllinux_1_1_i686.whl (3.9 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

leap_ie-0.0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

leap_ie-0.0.3-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (3.9 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-cp39-cp39-macosx_10_9_x86_64.whl (755.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

leap_ie-0.0.3-cp38-cp38-win_amd64.whl (643.8 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

leap_ie-0.0.3-cp38-cp38-win32.whl (582.2 kB view hashes)

Uploaded CPython 3.8 Windows x86

leap_ie-0.0.3-cp38-cp38-musllinux_1_1_x86_64.whl (5.0 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

leap_ie-0.0.3-cp38-cp38-musllinux_1_1_i686.whl (4.4 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

leap_ie-0.0.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

leap_ie-0.0.3-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (4.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-cp38-cp38-macosx_10_9_x86_64.whl (745.8 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

leap_ie-0.0.3-cp37-cp37m-win_amd64.whl (620.3 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

leap_ie-0.0.3-cp37-cp37m-win32.whl (562.0 kB view hashes)

Uploaded CPython 3.7m Windows x86

leap_ie-0.0.3-cp37-cp37m-musllinux_1_1_x86_64.whl (3.7 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

leap_ie-0.0.3-cp37-cp37m-musllinux_1_1_i686.whl (3.5 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

leap_ie-0.0.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.6 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

leap_ie-0.0.3-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (3.5 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-cp37-cp37m-macosx_10_9_x86_64.whl (741.2 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

leap_ie-0.0.3-cp36-cp36m-win_amd64.whl (670.1 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

leap_ie-0.0.3-cp36-cp36m-win32.whl (587.7 kB view hashes)

Uploaded CPython 3.6m Windows x86

leap_ie-0.0.3-cp36-cp36m-musllinux_1_1_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

leap_ie-0.0.3-cp36-cp36m-musllinux_1_1_i686.whl (3.2 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

leap_ie-0.0.3-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

leap_ie-0.0.3-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (3.1 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

leap_ie-0.0.3-cp36-cp36m-macosx_10_9_x86_64.whl (713.1 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page