We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 52a6b83 commit 2ce2816Copy full SHA for 2ce2816
5 files changed
xplique/attributions/grad_cam_pp.py
@@ -40,10 +40,6 @@ class GradCAMPP(GradCAM):
40
If a string is provided it will look for the layer name.
41
"""
42
43
- # Avoid zero division during procedure. (the value is not important, as if the denominator is
44
- # zero, then the nominator will also be zero).
45
- EPSILON = tf.constant(1e-4)
46
-
47
@staticmethod
48
@tf.function
49
def _compute_weights(feature_maps_gradients: tf.Tensor,
@@ -71,7 +67,11 @@ def _compute_weights(feature_maps_gradients: tf.Tensor,
71
67
nominator = feature_maps_gradients_square
72
68
denominator = 2.0 * feature_maps_gradients_square + \
73
69
feature_maps_gradients_cube * feature_map_avg
74
- denominator += tf.cast(denominator == 0, tf.float32) * GradCAMPP.EPSILON
70
+
+ # Avoid zero division during procedure. (the value is not important, as if the denominator is
+ # zero, then the nominator will also be zero).
+ EPSILON = tf.constant(1e-4)
+ denominator += tf.cast(denominator == 0, tf.float32) * EPSILON
75
76
feature_map_alphas = nominator / denominator * tf.nn.relu(feature_maps_gradients)
77
weights = tf.reduce_mean(feature_map_alphas, axis=(1, 2))
xplique/attributions/rise.py
@@ -42,10 +42,6 @@ class Rise(BlackBoxExplainer):
Value used as when applying masks.
def __init__(self,
50
model: Callable,
51
batch_size: Optional[int] = 32,
@@ -113,7 +109,10 @@ def explain(self,
113
109
rise_nominator += tf.reduce_sum(predictions * masks_upsampled, 0)
114
110
rise_denominator += tf.reduce_sum(masks_upsampled, 0)
115
111
116
- rise_map = rise_nominator / (rise_denominator + Rise.EPSILON)
112
+ rise_map = rise_nominator / (rise_denominator + EPSILON)
117
rise_map = rise_map[tf.newaxis]
118
119
rise_maps = rise_map if rise_maps is None else tf.concat([rise_maps, rise_map], axis=0)
xplique/commons/operators.py
@@ -7,7 +7,7 @@
7
import tensorflow as tf
8
9
from ..types import Callable, Optional
10
-from ..utils_functions.object_detection import _box_iou, _format_objects, _EPSILON
+from ..utils_functions.object_detection import _box_iou, _format_objects
11
12
13
@@ -197,6 +197,7 @@ def batch_loop(args):
197
class_refs = tf.repeat(tf.expand_dims(class_refs, axis=1), repeats=size, axis=1)
198
199
# (nb_box_ref, nb_box_pred)
200
+ _EPSILON = tf.constant(1e-4)
201
classification_score = tf.reduce_sum(class_refs * classification, axis=-1) \
202
/ (tf.norm(classification, axis=-1) * tf.norm(class_refs, axis=-1)+ _EPSILON)
203
xplique/features_visualizations/preconditioning.py
@@ -16,13 +16,6 @@
16
IMAGENET_SPECTRUM_URL = "https://storage.googleapis.com/serrelab/loupe/"\
17
"spectrums/imagenet_decorrelated.npy"
18
19
-imagenet_color_correlation = tf.cast(
20
- [[0.56282854, 0.58447580, 0.58447580],
21
- [0.19482528, 0.00000000,-0.19482528],
22
- [0.04329450,-0.10823626, 0.06494176]], tf.float32
23
-)
24
25
26
def recorrelate_colors(images: tf.Tensor) -> tf.Tensor:
27
28
Map uncorrelated colors to 'normal colors' by using empirical color
@@ -39,6 +32,11 @@ def recorrelate_colors(images: tf.Tensor) -> tf.Tensor:
39
32
images
33
Images recorrelated.
34
35
+ imagenet_color_correlation = tf.cast(
36
+ [[0.56282854, 0.58447580, 0.58447580],
37
+ [0.19482528, 0.00000000,-0.19482528],
38
+ [0.04329450,-0.10823626, 0.06494176]], tf.float32
+ )
images_flat = tf.reshape(images, [-1, 3])
images_flat = tf.matmul(images_flat, imagenet_color_correlation)
return tf.reshape(images_flat, tf.shape(images))
xplique/utils_functions/object_detection.py
@@ -4,9 +4,6 @@
4
from typing import Tuple
5
6
-_EPSILON = tf.constant(1e-4)
def _box_iou(boxes_a: tf.Tensor, boxes_b: tf.Tensor) -> tf.Tensor:
Compute the intersection between two batched bounding boxes.
@@ -40,6 +37,7 @@ def _box_iou(boxes_a: tf.Tensor, boxes_b: tf.Tensor) -> tf.Tensor:
union_area = a_area + b_area - intersection_area
iou_score = intersection_area / (union_area + _EPSILON)
return iou_score
0 commit comments