11# # [Heatmapping](@id docs-heatmapping)
22# 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+ #
47# This page showcases different options and preset for heatmapping,
58# building on the basics shown in the [*Getting started*](@ref docs-getting-started) section.
69#
710# We start out by loading the same pre-trained LeNet5 model and MNIST input data:
811using ExplainableAI
12+ using VisionHeatmaps
913using Flux
1014
1115using BSON # hide
@@ -19,10 +23,10 @@ index = 10
1923x, y = MNIST (Float32, :test )[10 ]
2024input = reshape (x, 28 , 28 , 1 , :)
2125
22- convert2image (MNIST, x)
26+ img = convert2image (MNIST, x)
2327
2428# ## 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.
2630#
2731# Since [`InputTimesGradient`](@ref) computes attributions,
2832# heatmaps are shown in a blue-white-red color scheme.
@@ -35,7 +39,7 @@ heatmap(input, analyzer)
3539
3640# ## Custom heatmap settings
3741# ### 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`.
3943# For example, we can use a custom color scheme from ColorSchemes.jl using the keyword argument `colorscheme`:
4044using ColorSchemes
4145
@@ -89,20 +93,32 @@ heatmap(expl; rangescale=:centered, colorscheme=:inferno)
8993# -
9094heatmap (expl; rangescale= :extrema , colorscheme= :inferno )
9195
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 )
93109
94110# ## [Heatmapping batches](@id docs-heatmapping-batches)
95111# 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 ]
98114batch = reshape (xs, 28 , 28 , 1 , :); # reshape to WHCN format
99115
100- # The [ `heatmap`](@ref) function automatically recognizes
116+ # The `heatmap` function automatically recognizes
101117# that the explanation is batched and returns a `Vector` of images:
102118heatmaps = heatmap (batch, analyzer)
103119
104120# Image.jl's `mosaic` function can used to display them in a grid:
105- mosaic (heatmaps; nrow= 10 )
121+ mosaic (heatmaps; nrow= 5 )
106122
107123# When heatmapping batches, the mapping to the color scheme is applied per sample.
108124# For example, `rangescale=:extrema` will normalize each heatmap
@@ -113,12 +129,12 @@ mosaic(heatmaps; nrow=10)
113129# `heatmap` can be called with the keyword-argument `process_batch=true`:
114130expl = analyze (batch, analyzer)
115131heatmaps = heatmap (expl; process_batch= true )
116- mosaic (heatmaps; nrow= 10 )
132+ mosaic (heatmaps; nrow= 5 )
117133
118134# This can be useful when comparing heatmaps for fixed output neurons:
119135expl = analyze (batch, analyzer, 7 ) # explain digit "6"
120136heatmaps = heatmap (expl; process_batch= true )
121- mosaic (heatmaps; nrow= 10 )
137+ mosaic (heatmaps; nrow= 5 )
122138
123139# md # !!! note "Output type consistency"
124140# md #
0 commit comments