Skip to content

Commit

Permalink
Add full example to README (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrhill authored Oct 11, 2024
1 parent f334705 commit 11ddc0b
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,34 @@ using a pre-trained VGG16 model from [Metalhead.jl](https://github.com/FluxML/Me

```julia
using RelevancePropagation
using VisionHeatmaps # visualization of explanations as heatmaps
using Flux
using Metalhead # pre-trained vision models
using VisionHeatmaps # visualization of explanations as heatmaps
using Flux, Metalhead # pre-trained vision models in Flux
using DataAugmentation # input preprocessing
using HTTP, FileIO, ImageIO # load image from URL
using ImageInTerminal # show heatmap in terminal


# Load & prepare model
model = VGG(16, pretrain=true).layers
model = strip_softmax(model)
model = canonize(model)

# Load input
input = ... # input in WHCN format
url = HTTP.URI("https://github.com/raw/Julia-XAI/ExplainableAI.jl/gh-pages/assets/heatmaps/castle.jpg")
img = load(url)

# Preprocess input
mean = (0.485f0, 0.456f0, 0.406f0)
std = (0.229f0, 0.224f0, 0.225f0)
tfm = CenterResizeCrop((224, 224)) |> ImageToTensor() |> Normalize(mean, std)
input = apply(tfm, Image(img)) # apply DataAugmentation transform
input = reshape(input.data, 224, 224, 3, :) # unpack data and add batch dimension

# Run XAI method
composite = EpsilonPlusFlat()
analyzer = LRP(model, composite)
expl = analyze(input, analyzer) # or: expl = analyzer(input)
heatmap(expl) # show heatmap using VisionHeatmaps.jl

```

We can also get an explanation for the activation of the output neuron
Expand Down

0 comments on commit 11ddc0b

Please sign in to comment.