1
1
# # [Heatmapping](@id docs-heatmapping)
2
2
# Since numerical explanations are not very informative at first sight,
3
- # we can visualize them by computing a [`heatmap`](@ref).
3
+ # we can visualize them by computing a `heatmap`, using either
4
+ # [VisionHeatmaps.jl](https://julia-xai.github.io/XAIDocs/VisionHeatmaps/stable/) or
5
+ # [TextHeatmaps.jl](https://julia-xai.github.io/XAIDocs/TextHeatmaps/stable/).
6
+ #
4
7
# This page showcases different options and preset for heatmapping,
5
8
# building on the basics shown in the [*Getting started*](@ref docs-getting-started) section.
6
9
#
7
10
# We start out by loading the same pre-trained LeNet5 model and MNIST input data:
8
11
using ExplainableAI
12
+ using VisionHeatmaps
9
13
using Flux
10
14
11
15
using BSON # hide
@@ -19,10 +23,10 @@ index = 10
19
23
x, y = MNIST (Float32, :test )[10 ]
20
24
input = reshape (x, 28 , 28 , 1 , :)
21
25
22
- convert2image (MNIST, x)
26
+ img = convert2image (MNIST, x)
23
27
24
28
# ## Automatic heatmap presets
25
- # The function [ `heatmap`](@ref) automatically applies common presets for each method.
29
+ # The function `heatmap` automatically applies common presets for each method.
26
30
#
27
31
# Since [`InputTimesGradient`](@ref) computes attributions,
28
32
# heatmaps are shown in a blue-white-red color scheme.
@@ -35,7 +39,7 @@ heatmap(input, analyzer)
35
39
36
40
# ## Custom heatmap settings
37
41
# ### Color schemes
38
- # We can partially or fully override presets by passing keyword arguments to [ `heatmap`](@ref) .
42
+ # We can partially or fully override presets by passing keyword arguments to `heatmap`.
39
43
# For example, we can use a custom color scheme from ColorSchemes.jl using the keyword argument `colorscheme`:
40
44
using ColorSchemes
41
45
@@ -89,20 +93,32 @@ heatmap(expl; rangescale=:centered, colorscheme=:inferno)
89
93
# -
90
94
heatmap (expl; rangescale= :extrema , colorscheme= :inferno )
91
95
92
- # For the full list of `heatmap` keyword arguments, refer to the [`heatmap`](@ref) documentation.
96
+ # For the full list of `heatmap` keyword arguments, refer to the `heatmap` documentation.
97
+
98
+ # ## [Heatmap overlays](@id overlay)
99
+ # Heatmaps can be overlaid onto the input image using the `heatmap_overlay` function
100
+ # from VisionHeatmaps.jl.
101
+ # This can be useful for visualizing the relevance of specific regions of the input:
102
+ heatmap_overlay (expl, img)
103
+
104
+ # The alpha value of the heatmap can be adjusted using the `alpha` keyword argument:
105
+ heatmap_overlay (expl, img; alpha= 0.3 )
106
+
107
+ # All previously discussed keyword arguments for `heatmap` can also be used with `heatmap_overlay`:
108
+ heatmap_overlay (expl, img; alpha= 0.7 , colorscheme= :inferno , rangescale= :extrema )
93
109
94
110
# ## [Heatmapping batches](@id docs-heatmapping-batches)
95
111
# Heatmapping also works with input batches.
96
- # Let's demonstrate this by using a batch of 100 images from the MNIST dataset:
97
- xs, ys = MNIST (Float32, :test )[1 : 100 ]
112
+ # Let's demonstrate this by using a batch of 25 images from the MNIST dataset:
113
+ xs, ys = MNIST (Float32, :test )[1 : 25 ]
98
114
batch = reshape (xs, 28 , 28 , 1 , :); # reshape to WHCN format
99
115
100
- # The [ `heatmap`](@ref) function automatically recognizes
116
+ # The `heatmap` function automatically recognizes
101
117
# that the explanation is batched and returns a `Vector` of images:
102
118
heatmaps = heatmap (batch, analyzer)
103
119
104
120
# Image.jl's `mosaic` function can used to display them in a grid:
105
- mosaic (heatmaps; nrow= 10 )
121
+ mosaic (heatmaps; nrow= 5 )
106
122
107
123
# When heatmapping batches, the mapping to the color scheme is applied per sample.
108
124
# For example, `rangescale=:extrema` will normalize each heatmap
@@ -113,12 +129,12 @@ mosaic(heatmaps; nrow=10)
113
129
# `heatmap` can be called with the keyword-argument `process_batch=true`:
114
130
expl = analyze (batch, analyzer)
115
131
heatmaps = heatmap (expl; process_batch= true )
116
- mosaic (heatmaps; nrow= 10 )
132
+ mosaic (heatmaps; nrow= 5 )
117
133
118
134
# This can be useful when comparing heatmaps for fixed output neurons:
119
135
expl = analyze (batch, analyzer, 7 ) # explain digit "6"
120
136
heatmaps = heatmap (expl; process_batch= true )
121
- mosaic (heatmaps; nrow= 10 )
137
+ mosaic (heatmaps; nrow= 5 )
122
138
123
139
# md # !!! note "Output type consistency"
124
140
# md #
0 commit comments