Skip to content

Commit 2ce2816

Browse files
Test moving the EPSILON & similar to free some RAM
Signed-off-by: Frederic Boisnard <frederic.boisnard@irt-saintexupery.com>
1 parent 52a6b83 commit 2ce2816

5 files changed

Lines changed: 17 additions & 21 deletions

File tree

xplique/attributions/grad_cam_pp.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ class GradCAMPP(GradCAM):
4040
If a string is provided it will look for the layer name.
4141
"""
4242

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-
4743
@staticmethod
4844
@tf.function
4945
def _compute_weights(feature_maps_gradients: tf.Tensor,
@@ -71,7 +67,11 @@ def _compute_weights(feature_maps_gradients: tf.Tensor,
7167
nominator = feature_maps_gradients_square
7268
denominator = 2.0 * feature_maps_gradients_square + \
7369
feature_maps_gradients_cube * feature_map_avg
74-
denominator += tf.cast(denominator == 0, tf.float32) * GradCAMPP.EPSILON
70+
71+
# Avoid zero division during procedure. (the value is not important, as if the denominator is
72+
# zero, then the nominator will also be zero).
73+
EPSILON = tf.constant(1e-4)
74+
denominator += tf.cast(denominator == 0, tf.float32) * EPSILON
7575

7676
feature_map_alphas = nominator / denominator * tf.nn.relu(feature_maps_gradients)
7777
weights = tf.reduce_mean(feature_map_alphas, axis=(1, 2))

xplique/attributions/rise.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ class Rise(BlackBoxExplainer):
4242
Value used as when applying masks.
4343
"""
4444

45-
# Avoid zero division during procedure. (the value is not important, as if the denominator is
46-
# zero, then the nominator will also be zero).
47-
EPSILON = tf.constant(1e-4)
48-
4945
def __init__(self,
5046
model: Callable,
5147
batch_size: Optional[int] = 32,
@@ -113,7 +109,10 @@ def explain(self,
113109
rise_nominator += tf.reduce_sum(predictions * masks_upsampled, 0)
114110
rise_denominator += tf.reduce_sum(masks_upsampled, 0)
115111

116-
rise_map = rise_nominator / (rise_denominator + Rise.EPSILON)
112+
# Avoid zero division during procedure. (the value is not important, as if the denominator is
113+
# zero, then the nominator will also be zero).
114+
EPSILON = tf.constant(1e-4)
115+
rise_map = rise_nominator / (rise_denominator + EPSILON)
117116
rise_map = rise_map[tf.newaxis]
118117

119118
rise_maps = rise_map if rise_maps is None else tf.concat([rise_maps, rise_map], axis=0)

xplique/commons/operators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import tensorflow as tf
88

99
from ..types import Callable, Optional
10-
from ..utils_functions.object_detection import _box_iou, _format_objects, _EPSILON
10+
from ..utils_functions.object_detection import _box_iou, _format_objects
1111

1212

1313
@tf.function
@@ -197,6 +197,7 @@ def batch_loop(args):
197197
class_refs = tf.repeat(tf.expand_dims(class_refs, axis=1), repeats=size, axis=1)
198198

199199
# (nb_box_ref, nb_box_pred)
200+
_EPSILON = tf.constant(1e-4)
200201
classification_score = tf.reduce_sum(class_refs * classification, axis=-1) \
201202
/ (tf.norm(classification, axis=-1) * tf.norm(class_refs, axis=-1)+ _EPSILON)
202203

xplique/features_visualizations/preconditioning.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616
IMAGENET_SPECTRUM_URL = "https://storage.googleapis.com/serrelab/loupe/"\
1717
"spectrums/imagenet_decorrelated.npy"
1818

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-
2619
def recorrelate_colors(images: tf.Tensor) -> tf.Tensor:
2720
"""
2821
Map uncorrelated colors to 'normal colors' by using empirical color
@@ -39,6 +32,11 @@ def recorrelate_colors(images: tf.Tensor) -> tf.Tensor:
3932
images
4033
Images recorrelated.
4134
"""
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
39+
)
4240
images_flat = tf.reshape(images, [-1, 3])
4341
images_flat = tf.matmul(images_flat, imagenet_color_correlation)
4442
return tf.reshape(images_flat, tf.shape(images))

xplique/utils_functions/object_detection.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
from typing import Tuple
55
import tensorflow as tf
66

7-
_EPSILON = tf.constant(1e-4)
8-
9-
107
def _box_iou(boxes_a: tf.Tensor, boxes_b: tf.Tensor) -> tf.Tensor:
118
"""
129
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:
4037

4138
union_area = a_area + b_area - intersection_area
4239

40+
_EPSILON = tf.constant(1e-4)
4341
iou_score = intersection_area / (union_area + _EPSILON)
4442

4543
return iou_score

0 commit comments

Comments
 (0)