Keras¶
Keras is “a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano”.
Keras can be used for many Machine Learning tasks, and it has support for both popular and experimental neural network architectures.
Note: only TensorFlow 1.x is supported, recommended Keras version is 2.3.1 or earlier.
explain_prediction¶
Currently ELI5 supports eli5.explain_prediction()
for Keras image classifiers.
eli5.explain_prediction()
explains image classifications through Grad-CAM.
The returned eli5.base.Explanation
instance contains some important objects:
image
represents the image input into the model. A Pillow image.targets
represents the explanation values for each target class (currently only 1 target is supported). A list ofeli5.base.TargetExplanation
objects with the following attributes set:heatmap
a grayscale “localization map” (rank 2 (2D) numpy array, with float values in the interval [0, 1]). The numbers indicate how important the region in the image is for the target class (even if the target class was not the predicted class). Higher numbers mean that the region tends to increase the predicted value for a class. Lower numbers mean that the region has smaller effect on the predicted class score.target
the integer ID of the class (same as the argument totargets
if one was passed, or the predicted class ID if no argument was passed).score
the output of the network for the predicted class.
Important arguments to eli5.explain_prediction()
for Model
and Sequential
:
doc
is an image as a tensor that can be inputted to the model.- The tensor must be an instance of
numpy.ndarray
. - Usually the tensor has the format (batch, dims, …, channels) (channels last format, dims=(height, width), batch=1, one image), i.e. BHWC.
- Check
model.input_shape
to confirm the required dimensions of the input tensor.
- The tensor must be an instance of
image
Pillow image, corresponds to doc input.- Image over which to overlay the heatmap.
- If not given, the image will be derived from
doc
where possible. - Useful if ELI5 fails in case you have a custom image model or image input.
targets
are the output classes to focus on. Possible values include:- A list of integers (class ID’s). Only the first prediction from the list is currently taken. The list must be length one.
- None for automatically taking the top prediction of the model.
layer
is the layer in the model from which the heatmap will be generated. Possible values are:- An instance of
Layer
, a name (str), or an index (int) - None for automatically getting a suitable layer, if possible.
- An instance of
All other arguments are ignored.
Note
Top-level eli5.explain_prediction()
calls are dispatched
to eli5.keras.explain_prediction_keras()
for
keras.models.Model
and keras.models.Sequential
.
show_prediction¶
ELI5 supports eli5.show_prediction()
to conveniently
invoke explain_prediction
with format_as_image
, and display the explanation in an
IPython cell.
Grad-CAM¶
ELI5 contains eli5.keras.gradcam.gradcam()
and eli5.keras.gradcam.gradcam_backend()
.
These functions can be used to obtain finer details of a Grad-CAM explanation.