trw.train.meaningful_perturbation

Module Contents

Classes

MeaningfulPerturbation

Implementation of "Interpretable Explanations of Black Boxes by Meaningful Perturbation", arXiv:1704.03296

Functions

default_optimizer(params, nb_iters, learning_rate=0.5)

Create a default optimizer for trw.train.MeaningfulPerturbation

create_inputs(batch, modified_input_name, modified_input)

Create the model inputs depending on whether the input is a dictionary or tensor

default_information_removal_smoothing(image, blurring_sigma=5, blurring_kernel_size=23, explanation_for=None)

Default information removal (smoothing).

Attributes

logger

trw.train.meaningful_perturbation.logger
trw.train.meaningful_perturbation.default_optimizer(params, nb_iters, learning_rate=0.5)

Create a default optimizer for trw.train.MeaningfulPerturbation

Parameters
  • params – the parameters to optimize

  • nb_iters – the number of iterations

  • learning_rate – the default learning rate

Returns

a tuple (torch.optim.Optimizer, torch.optim.lr_scheduler._LRScheduler)

trw.train.meaningful_perturbation.create_inputs(batch, modified_input_name, modified_input)

Create the model inputs depending on whether the input is a dictionary or tensor

trw.train.meaningful_perturbation.default_information_removal_smoothing(image, blurring_sigma=5, blurring_kernel_size=23, explanation_for=None)

Default information removal (smoothing).

Parameters
  • image – an image

  • blurring_sigma – the sigma of the blurring kernel used to “remove” information from the image

  • blurring_kernel_size – the size of the kernel to be used. This is an internal parameter to approximate the gaussian kernel. This is exposed since in 3D case, the memory consumption may be high and having a truthful gaussian blurring is not crucial.

  • explanation_for – the class to explain

Returns

a smoothed image

class trw.train.meaningful_perturbation.MeaningfulPerturbation(model, iterations=150, l1_coeff=0.1, tv_coeff=0.2, tv_beta=3, noise=0.2, model_output_postprocessing=functools.partial(F.softmax, dim=1), mask_reduction_factor=8, optimizer_fn=default_optimizer, information_removal_fn=default_information_removal_smoothing, export_fn=None)

Implementation of “Interpretable Explanations of Black Boxes by Meaningful Perturbation”, arXiv:1704.03296

Handle only 2D and 3D inputs. Other inputs will be discarded.

Deviations: - use a global smoothed image to speed up the processing

__call__(self, inputs, target_class_name, target_class=None)
Parameters
  • inputs – a tensor or dictionary of tensors. Must have require_grads for the inputs to be explained

  • target_class – the index of the class to explain the decision. If None, the class output will be used

  • target_class_name

    the output node to be used. If None: * if model output is a single tensor then use this as target output

    • else it will use the first OutputClassification output

Returns

a tuple (output_name, dictionary (input, explanation mask))

static _get_output(target_class_name, outputs, postprocessing)