Distance explainer quick start
In this notebook we show how one typically would use the distance_explainer package. We show how to explain how a model embeds two images into an embedded space.
import numpy as np
from distance_explainer import DistanceExplainer
Let’s assume we have some model that embeds an input image and into some embedded space. This model can be a neural net or deep network of any kind. For the purpose of this tutorial we are using a dummy model that outputs a random embedding.
DUMMY_EMBEDDING_DIMENSIONALITY = 10
dummy_model = lambda x: np.random.randn(x.shape[0], DUMMY_EMBEDDING_DIMENSIONALITY)
We now have 2 images whose relationship we want to understand in the embedded space. In other words, we want to understand how the model embeds these two images relative to each other.
Note that we are using images with 3 channels (RGB) that have their channels on axis 2.
image1 = np.random.random((100, 100, 3))
image2 = np.random.random((100, 100, 3))
axis_labels = {2: 'channels'}
We are going to embed the second image, and use its position in the embedded space as a reference point to compare to. We are then going to perturb our first image, by masking some of its features, and then embed the masked image, many times. This results in a list of points in the embedded space, representing each masked image. We then calculate the distance of those embedded points to the reference point representing image2. Finally, we combine the masks with the resulting distances to form a attribution map.
batch = image2[None, ...] # We create a batch of 1 because we expect the model to run on batches
embedded_batch = dummy_model(batch)
image2_embedded = embedded_batch[0]
explainer = DistanceExplainer(axis_labels=axis_labels) # Make a distance explainer object, and set the channel axis.
attribution_map = explainer.explain_image_distance(dummy_model, image1, image2_embedded)[0] # Multiple maps can be returned, we take the first (and in this case the only) map
Explaining: 100%|██████████| 100/100 [00:00<00:00, 248.55it/s]
ref.shape=(1, 10)
ref.shape=(1, 10)
(100, 100, 1)
The attribution_map shows us which parts of image1 brings it closer to image2 in the embedded space, and what parts bring it further away. The map can easily be visualized like any other greyscale image. Because we used random data and model, we don’t visualize the attribution map in this notebook.