Welcome to the documentation of IBA!

IBA.pytorch

to_saliency_map(capacity, shape=None)[source]

Converts the layer capacity (in nats) to a saliency map (in bits) of the given shape .

Parameters
  • capacity (np.ndarray) – Capacity in nats.

  • shape (tuple) – (height, width) of the image.

insert_into_sequential(sequential, layer, idx)[source]

Returns a nn.Sequential with layer inserted in sequential at position idx.

tensor_to_np_img(img_t)[source]

Convert a torch tensor of shape (c, h, w) to a numpy array of shape (h, w, c) and reverse the torchvision prepocessing.

imagenet_transform(resize=256, crop_size=224)[source]

Returns the default torchvision imagenet transform.

get_imagenet_folder(path, image_size=224, transform='default')[source]

Returns a torchvision.datasets.ImageFolder with the default torchvision preprocessing.

class TorchWelfordEstimator[source]

Bases: torch.nn.modules.module.Module

Estimates the mean and standard derivation. For the algorithm see https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance.

Example

Given a batch of images imgs with shape (10, 3, 64, 64), the mean and std could be estimated as follows:

# exemplary data source: 5 batches of size 10, filled with random data
batch_generator = (torch.randn(10, 3, 64, 64) for _ in range(5))

estim = WelfordEstimator(3, 64, 64)
for batch in batch_generator:
    estim(batch)

# returns the estimated mean
estim.mean()

# returns the estimated std
estim.std()

# returns the number of samples, here 10
estim.n_samples()

# returns a mask with active neurons
estim.active_neurons()
forward(x)[source]

Update estimates without altering x

n_samples()[source]

Returns the number of seen samples.

mean()[source]

Returns the estimate of the mean.

std()[source]

returns the estimate of the standard derivation.

active_neurons(threshold=0.01)[source]

Returns a mask of all active neurons. A neuron is considered active if n_nonzero / n_samples  > threshold

class IBA(layer=None, sigma=1.0, beta=10, min_std=0.01, optimization_steps=10, lr=1, batch_size=10, initial_alpha=5.0, active_neurons_threshold=0.01, feature_mean=None, feature_std=None, estimator=None, progbar=False, input_or_output='output', relu=False)[source]

Bases: torch.nn.modules.module.Module

IBA finds relevant features of your model by applying noise to intermediate features.

Example:

model = Net()
# Create the Per-Sample Bottleneck:
iba = IBA(model.conv4)

# Estimate the mean and variance.
iba.estimate(model, datagen)

img, target = next(iter(datagen(batch_size=1)))

# Closure that returns the loss for one batch
model_loss_closure = lambda x: F.nll_loss(F.log_softmax(model(x), target)

# Explain class target for the given image
saliency_map = iba.analyze(img.to(dev), model_loss_closure)
plot_saliency_map(img.to(dev))
Parameters
  • layer – The layer after which to inject the bottleneck

  • sigma – The standard deviation of the gaussian kernel to smooth the mask, or None for no smoothing

  • beta – Weighting of model loss and mean information loss.

  • min_std – Minimum std of the features

  • lr – Optimizer learning rate. default: 1. As we are optimizing over very few iterations, a relatively high learning rate can be used compared to the training of the model itself.

  • batch_size – Number of samples to use per iteration

  • input_or_output – Select either "output" or "input".

  • initial_alpha – Initial value for the parameter.

detach()[source]

Remove the bottleneck to restore the original model

forward(x)[source]

You don’t need to call this method manually.

The IBA acts as a model layer, passing the information in x along to the next layer either as-is or by restricting the flow of infomration. We use it also to estimate the distribution of x passing through the layer.

interrupt_execution()[source]

Interrupts the execution of the model, once PerSampleBottleneck is called. Useful for estimation when the model has only be executed until the Per-Sample Bottleneck.

Example

Executes the model only until the bottleneck layer:

with bltn.interrupt_execution():
    out = model(x)
    # out will not be defined
    print("this will not be printed")
enable_estimation()[source]

Context manager to enable estimation of the mean and standard derivation. We recommend to use the self.estimate method.

reset_estimate()[source]

Resets the estimator. Useful if the distribution changes. Which can happen if you trained the model more.

estimate(model, dataloader, device=None, n_samples=10000, progbar=None, reset=True)[source]

Estimate mean and variance using the welford estimator. Usually, using 10.000 i.i.d. samples gives decent estimates.

Parameters
  • model – the model containing the bottleneck layer

  • dataloader – yielding batch’s where the first sample batch[0] is the image batch.

  • device – images will be transfered to the device. If None, it uses the device of the first model parameter.

  • n_samples (int) – run the estimate on that many samples

  • progbar (bool) – show a progress bar.

  • reset (bool) – reset the current estimate of the mean and std

restrict_flow()[source]

Context mananger to enable information supression.

Example

To make a prediction, with the information flow being supressed.:

with iba.restrict_flow():
    # now noise is added
    model(x)
analyze(input_t, model_loss_fn, mode='saliency', beta=None, optimization_steps=None, min_std=None, lr=None, batch_size=None, active_neurons_threshold=0.01)[source]

Generates a heatmap for a given sample. Make sure you estimated mean and variance of the input distribution.

Parameters
  • input_t – input image of shape (1, C, H W)

  • model_loss_fn – closure evaluating the model

  • mode – how to post-process the resulting map: ‘saliency’ (default) or ‘capacity’

  • beta – if not None, overrides the bottleneck beta value

  • optimization_steps – if not None, overrides the bottleneck optimization_steps value

  • min_std – if not None, overrides the bottleneck min_std value

  • lr – if not None, overrides the bottleneck lr value

  • batch_size – if not None, overrides the bottleneck batch_size value

  • active_neurons_threshold – used threshold to determine if a neuron is active

Returns

The heatmap of the same shape as the input_t.

capacity()[source]

Returns a tensor with the capacity from the last input, averaged over the redundant batch dimension. Shape is (self.channels, self.height, self.width)

IBA.pytorch_readout

class IBAReadout(attach_layer, readout_layers, model, estimator_type=None, **kwargs)[source]

Bases: IBA.pytorch.IBA

The Readout Bottleneck is an extension to yield the alphas for the IBA bottleneck from a readout network. The readout network is trained on intermediate feature maps which are obtained by performing a nested forward pass on the model and recording activations.

Major differences to the Per-Sample IBA: * an additional context manager for the nested pass * additional hooks to collect the input and the feature maps in the nested pass * a readout network of three 1x1 conv. layers to yield alpha

detach()[source]

Remove the bottleneck to restore the original model

analyze(input_t, model, mode='saliency', **kwargs)[source]

Use the trained Readout IBA to find relevant regions in the input. The input is passed through the network and the Readout Bottleneck restricts the information flow. The capacity at each pixel is then returned as saliency map, similar to the Per-Sample IBA.

Parameters
  • input_t – input image of shape (1, C, H W)

  • model – the model containing the trained bottleneck

  • mode – how to post-process the resulting map: ‘saliency’ (default) or ‘capacity’

Returns

The heatmap of the same shape as the input_t.

Additional arguments are ignored.

reset_estimate()[source]

Equivalent to IBA.reset_estimate, but additionaly resets estimators of bottleneck layers.

forward(x)[source]

You don’t need to call this method manually.

The IBA acts as a model layer, passing the information in x along to the next layer either as-is or by restricting the flow of infomration. We use it also to estimate the distribution of x passing through the layer.

IBA.tensorflow_v1

As IBA restricts the flow of information by adding noise to an intermediate feature map, we have to modify the existing model.

You can add the IBALayer as a layer directly in your model. During training, IBALayer is the identity and only adds noise later to estimate the relevance values.

For some models, you might not be able to add a layer, for example, when using pretrained keras models. In this case, you can use the IBACopy class. It adds the noise operation to the graph coping it partially (using tf.import_graph_def under the hood).

If you have existing code for the innvestigate package, the IBACopyInnvestigate class implements the innvestigate API.

For examples, see also the notebook directory.

Table: Overview over the classes. (Task) type of task (i.e. regression, classification, unsupervised). (Layer) requires you to add a layer to the explained model. (Copy) copies the tensorflow graph.

Class

Task

Layer

Copy

Note

IBALayer

Any

Recommended

IBACopy

Any

Very flexible

IBACopyInnvestigate

Classification

Nice API for classification

class TFWelfordEstimator(feature_name, graph=None)[source]

Bases: IBA.utils.WelfordEstimator

Estimates the mean and standard derivation. For the algorithm see wikipedia.

Parameters
  • feature_name (str) – name of the feature tensor

  • graph (tf.Graph) – graph which holds the feature tensor. If None, uses the default graph.

fit(feed_dict, session=None, run_kwargs={})[source]

Estimates the mean and std given the inputs in feed_dict.

Warning

Ensure that your model is in eval mode. If you use keras, call K.set_learning_phase(0).

Parameters
  • feed_dict (dict) – tensorflow feed dict with model inputs.

  • session (tf.Session) – session to execute the model. If None, uses the default session.

  • run_kwargs (dict) – additional kwargs to session.run.

fit_generator(generator, session=None, progbar=True, run_kwargs={})[source]

Estimates the mean and std from the feed_dict generator.

Warning

Ensure that your model is in eval mode. If you use keras, call K.set_learning_phase(0).

Parameters
  • generator – yield tensorflow ``feed_dict``s.

  • session (tf.Session) – session to execute the model. If None, uses the default session.

  • run_kwargs (dict) – additional kwargs to session.run.

  • progbar (bool) – flag to show progress bar.

state_dict() → dict[source]

Returns the estimator internal state. Can be loaded with load_state_dict().

Example:

state = estimator.state_dict()
with open('estimator_state.pickle', 'wb') as f:
    pickle.dump(state, f)

# load it

estimator = TFWelfordEstimator(feature_name=None)
with open('estimator_state.pickle', 'rb') as f:
    state = pickle.load(f)
    estimator.load_state_dict(state)
load_state_dict(state: dict)[source]

Loads estimator internal state.

to_saliency_map(capacity, shape=None, data_format=None)[source]

Converts the layer capacity (in nats) to a saliency map (in bits) of the given shape .

Parameters
  • capacity (np.ndarray) – Capacity in nats.

  • shape (tuple) – (height, width) of the image.

  • data_format (str) – "channels_first" or "channels_last". If None, the K.image_data_format() of keras is used.

get_imagenet_generator(path, target_size=(256, 256), crop_size=(224, 224), batch_size=50, seed=0, preprocess_input=None, **kwargs)[source]

Yields (image_batch, targets) from the given imagenet directory.

Parameters
  • path (str) – ImageNet directory. Must point to the train or validation directory.

  • image_size (tuple) – Scale image to this size.

  • batch_size (int) – Batch size.

  • seed (int) – Random seed

  • preprocess_input (function) – model precessing function. Default: keras.applications.resnet50 which is used by most keras models.

model_wo_softmax(model: keras.engine.training.Model)[source]

Creates a new model w/o the final softmax activation. model must be a keras model.

class IBALayer(estimator=None, feature_mean=None, feature_std=None, feature_active=None, batch_size=10, steps=10, beta=10, learning_rate=1, min_std=0.01, smooth_std=1.0, normalize_beta=True, **kwargs)[source]

Bases: keras.engine.base_layer.Layer

A keras layer that can be included in your model. This class should work with any model and does not copy the tensorflow graph. Although it is a keras layer, it should be possible to use it from other libaries. If you cannot alter your model definition, you have to copy the graph (use IBACopy or IBACopyInnvestigate).

Example:

model = keras.Sequential()

# add some layer
model.add(Conv2D(64, 3, 3))
model.add(BatchNorm())
model.add(Activation('relu'))
# ... more layers

# add iba in between
iba = IBALayer()
model.add(iba)

# ... more layers
model.add(Conv2D(64, 3, 3))
model.add(Flatten())
model.add(Dense(10))

# set classification cross-entropy loss
target = iba.set_classification_loss(model.output)

# estimate the feature mean and std.
for imgs, _ in data_generator():
    iba.fit({model.input: imgs})

# explain target for image
ex_image, ex_target = get_explained_image()
saliency_map = iba.analyze({model.input: ex_image, target: ex_target})
Hyperparamters Paramters:

The informational bottleneck attribution has a few hyperparameters. They most important parameter is the beta which controls the trade-off between model loss. Generally, beta = 10 works well. Other hyperparameters are the number of optimization steps. The learning_rate of the optimizer. The smoothing of the feature map and the minimum feature standard derviation. All hyperparamters set in the constructure can be overriden in the analyze() method or in the set_default() method.

Parameters
  • estimator (TFWelfordEstimator) – already fitted estimator.

  • feature_mean – estimated feature mean. Do not provide feature_mean_std and estimator.

  • feature_std – estimated feature std.

  • feature_active – estimated active neurons. If feature_active[i] = 0, the i-th neuron will be set to zero and no information is added for this neuron.

  • batch_size (int) – Default number of samples to average the gradient.

  • steps (int) – Default number of iterations to optimize.

  • beta (int) – Default value for trade-off between model loss and information loss.

  • learning_rate (float) – Default learning rate of the Adam optimizer.

  • min_std (float) – Default minimum feature standard derivation.

  • smooth_std (float) – Default smoothing of the lambda parameter. Set to 0 to disable.

  • normalize_beta (bool) – Default flag to devide beta by the nubmer of feature neurons (default: True).

  • **kwargs – keras layer kwargs, see keras.layers.Layer

set_default(batch_size=None, steps=None, beta=None, learning_rate=None, min_std=None, smooth_std=None, normalize_beta=None)[source]

Updates the default hyperparamter values.

get_default()[source]

Returns the default hyperparamter values.

collect(*var_names)[source]

Mark *var_names to be collected for the report. See available_report_variables() for all variable names.

collect_all()[source]

Mark all variables to be collected for the report. If all variables are collected, the optimization can slow down.

available_report_variables()[source]

Returns all variables that can be collected for get_report().

get_report()[source]

Returns the report for the last run.

build(input_shape)[source]

Builds the keras layer given the input shape.

compute_output_shape(input_shape)[source]

Returns the input_shape.

call(inputs) → tensorflow.python.framework.ops.Tensor[source]

Returns the output tensor. You can enable the restriction of the information flow with: restrict_flow().

restrict_flow(session=None)[source]

Context manager to restrict the flow of the layer. Useful to estimate model output when noise is added. If the flow restirction is enabled, you can only call the model with a single sample (batch size = 1).

Example:

capacity = iba.analyze({model.input: x})
# computes logits using all information
logits = model.predict(x)
with iba.restrict_flow():
    # computes logits using only a subset of all information
    logits_restricted = model.predict(x)
set_classification_loss(logits, optimizer_cls=<class 'tensorflow.python.training.adam.AdamOptimizer'>) → tensorflow.python.framework.ops.Tensor[source]

Creates a cross-entropy loss from the logit tensors. Returns the target tensor.

Example:

iba.set_classification_loss(model.output)

You have to ensure that the final layer of model does not applies a softmax. For keras models, you can remove a softmax activation using model_wo_softmax().

set_model_loss(model_loss, optimizer_cls=<class 'tensorflow.python.training.adam.AdamOptimizer'>)[source]

Sets the model loss for the final objective model_loss + beta * capacity_mean. When build the model_loss, ensure you are using the copied graph.

Example:

with iba.copied_session_and_graph_as_default():
    iba.get_copied_outputs()
fit(feed_dict, session=None, run_kwargs={})[source]

Estimate the feature mean and std from the given feed_dict.

Warning

Ensure that your model is in eval mode. If you use keras, call K.set_learning_phase(0).

Parameters
  • generator – Yields feed_dict with all inputs

  • n_samples – Stop after n_samples

  • session – use this session. If None use default session.

  • run_kwargs – additional kwargs to session.run.

Example:

# input is a tensorflow placeholder  of your model
input = tf.placeholder(tf.float32, name='input')

X, y = load_data_batch()
iba.fit({input: X})

Where input is a tensorflow placeholder and X an input numpy array.

fit_generator(generator, n_samples=5000, progbar=True, session=None, run_kwargs={})[source]

Estimates the feature mean and std from the generator.

Warning

Ensure that your model is in eval mode. If you use keras, call K.set_learning_phase(0).

Parameters
  • generator – Yields feed_dict s with inputs to all placeholders.

  • n_samples (int) – Stop after n_samples.

  • session (tf.Session) – tf session to use. If None use default session.

  • run_kwargs (dict) – additional kwargs to session.run.

analyze(feed_dict, batch_size=None, steps=None, beta=None, learning_rate=None, min_std=None, smooth_std=None, normalize_beta=None, session=None, pass_mask=None, progbar=False) → numpy.ndarray[source]

Returns the transmitted information per feature. See to_saliency_map() to convert the intermediate capacites to a visual saliency map.

Parameters
  • feed_dict (dict) – TensorFlow feed_dict providing your model inputs.

  • batch_size (int) – number of samples to average the gradient.

  • steps (int) – number of iterations to optimize.

  • beta (int) – trade-off parameter between model loss and information loss.

  • learning_rate (float) – Learning rate of the Adam optimizer.

  • min_std (float) – Minimum feature standard derivation.

  • smooth_std (float) – Smoothing of the lambda. Set to 0 to disable.

  • normalize_beta (bool) – Devide beta by the nubmer of feature neurons (default: True).

  • session (tf.Session) – TensorFlow session to run the optimization.

  • pass_mask (np.array) – same shape as the feature map. pass_mask masks neurons which are always passed to the next layer. No noise is added if pass_mask == 0. For example, it might be usefull if a variable lenght sequence is zero-padded.

  • progbar (bool) – Flag to display progressbar.

state_dict()[source]

Returns the current layer state.

load_state_dict(state)[source]

Load the given state.

class IBACopy(feature, outputs, estimator=None, feature_mean=None, feature_std=None, feature_active=None, graph=None, session=None, copy_session_config=None, batch_size=10, steps=10, beta=10, learning_rate=1, min_std=0.01, smooth_std=1, normalize_beta=True, **keras_kwargs)[source]

Bases: IBA.tensorflow_v1.IBALayer

Injects an IBALayer into an existing model by partially copying the model. IBACopy is useful for pretrained models which model definition you cannot alter. As tensorflow graphs are immutable, this class copies the original graph partially (using tf.import_graph_def).

Warning

Changes to your model after calling IBACopy have no effect on the explanations. You need to call update_variables() to update the variable values. Coping the graph might also require more memory than adding IBALayer to our model directly. We would recommend to always use IBALayer if you can add it as a layer to your model.

Parameters
  • feature (tf.tensor or str) – tensor or name for the feature tensor to replace.

  • output_names – list of tensors or tensor names for the model outputs. Useful to specify your model loss (see set_model_loss()).

  • estimator (TFWelfordEstimator) – use this estimator.

  • feature_mean_std (tuple) – tuple of estimated feature (mean, std).

  • graph – Graph of the feature and outputs tensor. If None, then the default graph is used.

  • session – TensorFlow session corresponding to the feature tensor. If None, the default session is used.

  • copy_session_config – Session config for the newly created session.

  • batch_size (int) – Default number of samples to average the gradient.

  • steps (int) – Default number of iterations to optimize.

  • beta (int) – Default value for trade-off between model loss and information loss.

  • learning_rate (float) – Default learning rate of the Adam optimizer.

  • min_std (float) – Default minimum feature standard derivation.

  • smooth_std (float) – Default smoothing of the lambda parameter. Set to 0 to disable.

  • normalize_beta (bool) – Default flag to devide beta by the nubmer of feature neurons (default: True).

  • **keras_kwargs – layer kwargs, see keras.layers.Layer.

get_copied_outputs()[source]

Returns the copied model symbolic outputs provided in the the constructor.

assert_variables_equal()[source]

Asserts that all variables in the original graph and the copied graph have the same value.

update_variables()[source]

Copies the variable values from the original graph to the new copied graph. Call this function after you modified your model and want the changes to affect the saliency map.

copied_session_and_graph_as_default()[source]

Context manager that sets the copied gragh and session as default.

set_classification_loss()[source]

Sets a softmax cross entropy loss. Uses the first outputs tensor as logits.

feature_shape()[source]

Returns the shape of the feature map.

get_feature(feed_dict)[source]

Returns feature value given the inputs in feed_dict.

analyze(feature_feed_dict, copy_feed_dict, batch_size=None, steps=None, beta=None, learning_rate=None, min_std=None, smooth_std=None, normalize_beta=None, session=None, pass_mask=None, progbar=False)[source]

Returns the saliency map. This method executes an optimization to remove information while retaining a low model loss.

Parameters
  • feature_feed_dict (dict) – TensorFlow feed_dict with all inputs to compute the feature map. Placeholders must come from the original graph.

  • copy_feed_dict (dict) – TensorFlow feed_dict with all inputs to compute the final model output given the disturbed feature map. Placeholders must correspond to the copied graph.

  • batch_size (int) – number of samples to average the gradient.

  • steps (int) – number of iterations to optimize.

  • beta (int) – trade-off parameter between model loss and information loss.

  • learning_rate (float) – Learning rate of the Adam optimizer.

  • min_std (float) – Minimum feature standard derivation.

  • smooth_std (float) – Smoothing of the lambda

  • normalize_beta (bool) – Devide beta by the nubmer of neurons

  • session (tf.Session) – TensorFlow session to run the optimization

  • pass_mask (np.array) – same shape as the feature map. pass_mask masks neurons which are always passed to the next layer. No noise is added if pass_mask == 0. For example, it might be usefull if a variable lenght sequence is zero-padded.

  • progbar (bool) – Flag to display progressbar.

predict(feed_dict)[source]

Returns the outputs given inputs in feed_dict. The placeholders in feed_dict must correspond to the original graph. Useful to check if the graph was copied correctly.

state_dict()[source]

Returns the current layer state.

class IBACopyInnvestigate(model, neuron_selection_mode='max_activation', feature_name=None, estimator=None, feature_mean=None, feature_std=None, feature_active=None, batch_size=10, steps=10, beta=10.0, learning_rate=1, min_std=0.01, smooth_std=1, normalize_beta=True, session=None, copy_session_config=None, disable_model_checks=False, **keras_kwargs)[source]

Bases: IBA.tensorflow_v1.IBACopy, IBA.tensorflow_v1._InnvestigateAPI

This analyzer implements the innvestigate API. It is handy, if your have existing code written for the innvestigate package. The innvestigate API has some limitations. It assumes your model is a keras.Model and it only works with classification tasks. For more flexibility, see the IBACopy.

Warning

Changes to your model after calling IBACopyInnvestigate have no effect on the explanations. You need to call update_variables() to update the variable values. Coping the graph might also require more memory than adding IBALayer to our model directly. We would recommend to always use IBALayer if you can add it as a layer to your model.

Hyperparamters Paramters:

The innvestigate API requires that the hyperparameters are set in the constructure. They can be overriden using the set_default() method.

Parameters
  • model (keras.Model) – the explained model.

  • neuron_selection_mode (str) – Mode to select the explained neuron. Must be one of "max_activation", "index", "all".

  • estimator (TFWelfordEstimator) – feature mean and std. estimator.

  • feature_mean_std (tuple) – tuple of estimated feature (mean, std).

  • batch_size (int) – Default number of samples to average the gradient.

  • steps (int) – Default number of iterations to optimize.

  • beta (int) – Default value for trade-off between model loss and information loss.

  • learning_rate (float) – Default learning rate of the Adam optimizer.

  • min_std (float) – Default minimum feature standard derivation.

  • smooth_std (float) – Default smoothing of the lambda parameter. Set to 0 to disable.

  • normalize_beta (bool) – Default flag to devide beta by the nubmer of feature neurons (default: True).

  • session – TensorFlow session corresponding to the model. If None, the default session is used.

  • copy_session_config (dict) – Session config for the newly created session.

  • disable_model_checks – Not used by IBA.

  • **keras_kwargs – layer kwargs, see keras.layers.Layer.

fit(X, session=None, run_kwargs={})[source]

Estimates the feature mean and std from the samples X. Generally, we recommend run the estimation on about 5000 samples.

Warning

Ensure that your model is in eval mode. If you use keras, call K.set_learning_phase(0).

fit_generator(generator, steps_per_epoch=None, epochs=1, verbose=1, session=None, progbar=True)[source]

Estimates the feature mean and std from the generator. Generally, we recommend run the estimation on about 5000 samples.

Warning

Ensure that your model is in eval mode. If you use keras, call K.set_learning_phase(0).

Parameters
  • generator – Yields tuples of (samples, targets).

  • steps_per_epoch (int) – number of steps to run per epoch.

  • epochs (int) – number epoch to run.

  • progbar (TODO) – If 1, prints a progressbar.

  • verbose (int) – If 1, prints a progressbar.

  • session (tf.Session, optional) – tf session to evaluate the model.

analyze(X, neuron_selection=None)[source]

Returns the saliency maps for a batch of samples X.

Parameters
  • X – batch of samples

  • neuron_selection – which neuron to explain. Requires neuron_selection_mode == index.

predict(X)[source]

Returns the model output given the input X. Useful to check if the graph was copied correctly.

IBA.utils

class WelfordEstimator[source]

Bases: object

Estimates the mean and standard derivation. For the algorithm see wikipedia.

Example

Given a batch of images imgs with shape (10, 3, 64, 64), the mean and std could be estimated as follows:

# exemplary data source: 5 batches of size 10, filled with random data
batch_generator = (torch.randn(10, 3, 64, 64) for _ in range(5))

estim = WelfordEstimator(3, 64, 64)
for batch in batch_generator:
    estim(batch)

# returns the estimated mean
estim.mean()

# returns the estimated std
estim.std()

# returns the number of seen samples, here 10
estim.n_samples()

# returns a mask with active neurons
estim.active_neurons()
reset()[source]

Resets the estimates.

fit(x)[source]

Update estimates without altering x

n_samples()[source]

Returns the number of seen samples.

mean()[source]

Returns the estimate of the mean.

std()[source]

Returns the estimate of the standard derivation.

active_neurons(threshold=0.01)[source]

Returns a mask of all active neurons. A neuron is considered active if n_nonzero / n_samples  > threshold

state_dict()[source]

Returns internal state. Useful for saving to disk.

load_state_dict(state)[source]

Loads the internal state of the estimator.

get_tqdm()[source]

Tries to import tqdm from tqdm.auto if fails uses cli tqdm.

ifnone(a, b)[source]

If a is None return b.

to_unit_interval(x)[source]

Scales x to be in [0, 1].

load_monkeys(center_crop=True, size=224, pil=False)[source]

Returns the monkey test image.

plot_saliency_map(saliency_map, img=None, ax=None, colorbar_label='Bits / Pixel', colorbar_fontsize=14, min_alpha=0.2, max_alpha=0.7, vmax=None, colorbar_size=0.3, colorbar_pad=0.08)[source]

Plots the heatmap with an bits/pixel colorbar and optionally overlays the image.

Parameters
  • saliency_map (np.ndarray) – the saliency_map.

  • img (np.ndarray) – show this image under the saliency_map.

  • ax – matplotlib axis. If None, a new plot is created.

  • colorbar_label (str) – label for the colorbar.

  • colorbar_fontsize (int) – fontsize of the colorbar label.

  • min_alpha (float) – minimum alpha value for the overlay. only used if img is given.

  • max_alpha (float) – maximum alpha value for the overlay. only used if img is given.

  • vmax – maximum value for colorbar.

  • colorbar_size – width of the colorbar. default: Fixed(0.3).

  • colorbar_pad – width of the colorbar. default: Fixed(0.08).

Returns

The matplotlib axis ax.

This is the documentation of the iba package. Our attribution method is described in our paper “Restricting the Flow: Information Bottlenecks for Attribution”. Please see our README for installation and short overview. We also provide a few examplary jupyter notebooks.

Indices and tables