-
Notifications
You must be signed in to change notification settings - Fork 233
/
readme_code_snippet.py
50 lines (38 loc) · 1.39 KB
/
readme_code_snippet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from __future__ import annotations
import imp
import os
import matplotlib.pyplot as plt
import numpy as np
base_dir = os.path.dirname(__file__)
utils = imp.load_source("utils", os.path.join(base_dir, "utils.py"))
if __name__ == "__main__":
# Load an image.
# Need to download examples images first.
# See script in images directory.
image = utils.load_image(
os.path.join(base_dir, "images", "ILSVRC2012_val_00011670.JPEG"), 224
)
# Code snippet.
plt.imshow(image / 255)
plt.axis("off")
plt.savefig(os.path.join(base_dir, "images", "readme_example_input.png"))
import keras.applications.vgg16 as vgg16
import innvestigate
import innvestigate.utils
# 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))
plt.axis("off")
plt.savefig(os.path.join(base_dir, "images", "readme_example_analysis.png"))