In the recent years neural networks furthered the state of the art in many domains like, e.g., object detection and speech recognition. Despite the success neural networks are typically still treated as black boxes. Their internal workings are not fully understood and the basis for their predictions is unclear. In the attempt to understand neural networks better several methods were proposed, e.g., Saliency, Deconvnet, GuidedBackprop, SmoothGrad, IntergratedGradients, LRP, PatternNet&-Attribution. Due to the lack of a reference implementations comparing them is a major effort. This library addresses this by providing a common interface and out-of-the-box implementation for many analysis methods. Our goal is to make analyzing neural networks' predictions easy!
"iNNvestigate neural networks!"(http://arxiv.org/abs/1808.04260) by Maximilian Alber, Sebastian Lapuschkin, Philipp Seegerer, Miriam Hägele, Kristof T. Schütt, Grégoire Montavon, Wojciech Samek, Klaus-Robert Müller, Sven Dähne, Pieter-Jan Kindermans
iNNvestigate can be installed with the following commands. The library is based on Keras and therefore requires a supported Keras-backend o(Currently only the Tensorflow backend is supported. We test with Python 3.6, Tensorflow 1.12 and Cuda 9.x.):
pip install innvestigate
# Installing Keras backend
pip install [tensorflow | theano | cntk]
To use the example scripts and notebooks one additionally needs to install the package matplotlib:
pip install matplotlib
The library's tests can be executed via:
git clone https://github.com/albermax/innvestigate.git
cd innvestigate
python setup.py test
The iNNvestigate library contains implementations for the following methods:
- function:
- gradient: The gradient of the output neuron with respect to the input.
- smoothgrad: SmoothGrad averages the gradient over number of inputs with added noise.
- signal:
- deconvnet: DeConvNet applies a ReLU in the gradient computation instead of the gradient of a ReLU.
- guided: Guided BackProp applies a ReLU in the gradient computation additionally to the gradient of a ReLU.
- pattern.net: PatternNet estimates the input signal of the output neuron.
- attribution:
- input_t_gradient: Input * Gradient
- deep_taylor[.bounded]: DeepTaylor computes for each neuron a rootpoint, that is close to the input, but which's output value is 0, and uses this difference to estimate the attribution of each neuron recursively.
- pattern.attribution: PatternAttribution applies Deep Taylor by searching rootpoints along the singal direction of each neuron.
- lrp.*: LRP attributes recursively to each neuron's input relevance proportional to its contribution of the neuron output.
- integrated_gradients: IntegratedGradients integrates the gradient along a path from the input to a reference.
- deeplift.wrapper: DeepLIFT (wrapper around original code, slower) computes a backpropagation based on "finite" gradients.
- miscellaneous:
- input: Returns the input.
- random: Returns random Gaussian noise.
The intention behind iNNvestigate is to make it easy to use analysis methods, but it is not to explain the underlying concepts and assumptions. Please, read the according publication(s) when using a certain method and when publishing please cite the according paper(s) (as well as the iNNvestigate paper). Thank you!
All the available methods have in common that they try to analyze the output of a specific neuron with respect to input to the neural network. Typically one analyses the neuron with the largest activation in the output layer. For example, given a Keras model, one can create a 'gradient' analyzer:
import innvestigate
model = create_keras_model()
analyzer = innvestigate.create_analyzer("gradient", model)
and analyze the influence of the neural network's input on the output neuron by:
analysis = analyzer.analyze(inputs)
To analyze a neuron with the index i, one can use the following scheme:
analyzer = innvestigate.create_analyzer("gradient",
model,
neuron_selection_mode="index")
analysis = analyzer.analyze(inputs, i)
Let's look at an example (code) with VGG16 and this image:
import innvestigate
import innvestigate.utils
import keras.applications.vgg16 as vgg16
# Get model
model, preprocess = vgg16.VGG16(), vgg16.preprocess_input
# Strip softmax layer
model = innvestigate.utils.model_wo_softmax(model)
# Create analyzer
analyzer = innvestigate.create_analyzer("deep_taylor", model)
# Add batch axis and preprocess
x = preprocess(image[None])
# Apply analyzer w.r.t. maximum activated output-neuron
a = analyzer.analyze(x)
# Aggregate along color channels and normalize to [-1, 1]
a = a.sum(axis=np.argmax(np.asarray(a.shape) == 3))
a /= np.max(np.abs(a))
# Plot
plt.imshow(a[0], cmap="seismic", clim=(-1, 1))
Some methods like PatternNet and PatternAttribution are data-specific and need to be trained. Given a data set with train and test data, this can be done in the following way:
import innvestigate
analyzer = innvestigate.create_analyzer("pattern.net", model)
analyzer.fit(X_train)
analysis = analyzer.analyze(X_test)
In the directory examples one can find different examples as Python scripts and as Jupyter notebooks:
- Introduction to iNNvestigate: shows how to use iNNvestigate.
- Comparing methods on MNIST: shows how to train and compare analyzers on MNIST.
- Comparing output neurons on MNIST: shows how to analyze the prediction of different classes on MNIST.
- Comparing methods on ImageNet: shows how to compare analyzers on ImageNet.
- Comparing networks on ImageNet: shows how to compare analyzes for different networks on ImageNet.
- Sentiment Analysis.
- Development with iNNvestigate: shows how to develop with iNNvestigate.
- Perturbation Analysis.
To use the ImageNet examples please download the example images first (script).
... can be found here: https://innvestigate.readthedocs.io/en/latest/
If you would like to contribute or add your analysis method please get in touch with us!