innvestigate.tools

Pattern Computation

class innvestigate.tools.pattern.PatternComputer(model, pattern_type='linear', compute_layers_in_parallel=True, gpus=None)

Pattern computer.

Computes a pattern for each layer with a kernel of a given model.

Parameters:
  • model – A Keras model.
  • pattern_type – A string or a tuple of strings. Valid types are ‘linear’, ‘relu’, ‘relu.positive’, ‘relu.negative’.
  • compute_layers_in_parallel – Not supported yet. Compute all patterns at once. Otherwise computer layer after layer.
  • gpus – Not supported yet. Gpus to use.
compute(X, batch_size=32, verbose=0)

Compute and return the patterns for the model and the data X.

Parameters:
  • X – Data to compute patterns.
  • batch_size – Batch size to use.
  • verbose – As for keras model.fit.
compute_generator(generator, **kwargs)

Compute and return the patterns for the model and the data X.

Parameters:
  • generator – Data to compute patterns.
  • kwargs – Same as for keras model.fit_generator.

Perturbation Analysis

class innvestigate.tools.perturbate.Perturbation(perturbation_function, num_perturbed_regions=0, region_shape=(9, 9), reduce_function=<function mean>, aggregation_function=<function mean>, pad_mode='reflect', in_place=False, value_range=None)

Perturbation of pixels based on analysis result.

Parameters:
  • perturbation_function (function or callable or str) – Defines the function with which the samples are perturbated. Can be a function or a string that defines a predefined perturbation function.
  • num_perturbed_regions (int) – Number of regions to be perturbed.
  • reduce_function (function or callable) – Function to reduce the analysis result to one channel, e.g. mean or max function.
  • aggregation_function (function or callable) – Function to aggregate the analysis over subregions.
  • pad_mode (str or function or callable) – How to pad if the image cannot be subdivided into an integer number of regions. As in numpy.pad.
  • in_place (bool) – If true, the perturbations are performed in place, i.e. the input samples are modified.
  • value_range (tuple) – Minimal and maximal value after perturbation as a tuple: (min_val, max_val). The input is clipped to this range
aggregate_regions(analysis)
static compute_perturbation_mask(ranks, num_perturbated_regions)
static compute_region_ordering(aggregated_regions)
expand_regions_to_pixels(regions)
pad(analysis)
perturbate_on_batch(x, analysis)
Parameters:
  • x (numpy.ndarray) – Batch of images.
  • analysis (numpy.ndarray) – Analysis of this batch.
Returns:

Batch of perturbated images

Return type:

numpy.ndarray

perturbate_regions(x, perturbation_mask_regions)
reshape_region_pixels(region_pixels, target_shape)
reshape_to_regions(analysis)
class innvestigate.tools.perturbate.PerturbationAnalysis(analyzer, model, generator, perturbation, steps=1, regions_per_step=1, recompute_analysis=False, verbose=False)

Performs the perturbation analysis.

Parameters:
  • analyzer (innvestigate.analyzer.base.AnalyzerBase) – Analyzer.
  • model (keras.engine.training.Model) – Trained Keras model.
  • generator (innvestigate.utils.BatchSequence) – Data generator.
  • perturbation (innvestigate.tools.Perturbation) – Instance of Perturbation class that performs the perturbation.
  • steps (int) – Number of perturbation steps.
  • regions_per_step (float) – Number of regions that are perturbed per step.
  • recompute_analysis (bool) – If true, the analysis is recomputed after each perturbation step.
  • verbose – If true, print some useful information, e.g. timing, progress etc.
compute_on_batch(x, analysis=None, return_analysis=False)

Computes the analysis and perturbes the input batch accordingly.

Parameters:
  • x (numpy.ndarray) – Samples.
  • analysis – Analysis of x. If None, it is recomputed.
compute_perturbation_analysis()
evaluate_generator(generator, steps=None, max_queue_size=10, workers=1, use_multiprocessing=False)

Evaluates the model on a data generator.

The generator should return the same kind of data as accepted by test_on_batch. For documentation, refer to keras.engine.training.evaluate_generator (https://keras.io/models/model/)

evaluate_on_batch(x, y, analysis=None, sample_weight=None)

Perturbs the input batch and scores the model on the perturbed batch.

Parameters:
  • x (numpy.ndarray) – Samples.
  • y (numpy.ndarray) – Labels.
  • analysis (numpy.ndarray) – Analysis of x.
  • sample_weight (None) – Sample weights.
Returns:

List of test scores.

Return type:

list