forked from IRIS-DaSJ/saliency_maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradient_based.py
executable file
·45 lines (35 loc) · 1.28 KB
/
gradient_based.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
import numpy as np
import tensorflow as tf
def gradient_saliency(model, data, index=None):
data = tf.convert_to_tensor(data)
with tf.GradientTape() as tape:
tape.watch(data)
pred = model(data)
n_classes = get_num_classes(model)
if index is None:
loss = pred
elif n_classes > 1:
o = tf.one_hot([index], n_classes)
loss = tf.keras.losses.categorical_crossentropy(o, pred)
else:
o = tf.ones_like(pred) * index
loss = tf.keras.losses.binary_crossentropy(o, pred)
return tape.gradient(loss, data).numpy()
def smooth_grad(model, data, index=None, noise_level=0.2, n=32):
# SmoothGrad: https://arxiv.org/pdf/1706.03825.pdf
if len(model.input_shape) == len(data.shape) + 1:
data = np.expand_dims(data, 0)
elif data.shape[0] == 1:
pass
else:
raise ValueError('')
max_min = np.max(data) - np.min(data)
data = np.repeat(data, n, axis=0)
noise = np.random.random(data.shape).astype('float32')
noise = noise * noise_level * max_min
data += noise
return np.mean(gradient_saliency(model, data, index), axis=0)
def get_num_classes(model):
if len(model.output_shape) == 1:
return 1
return model.output_shape[-1]