-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVisualTCAV.py
More file actions
1382 lines (1133 loc) · 47.9 KB
/
VisualTCAV.py
File metadata and controls
1382 lines (1133 loc) · 47.9 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#####
# VisualTCAV
#
# All rights reserved.
#
# Main classes
#####
#####
# Imports
#####
# Do not generate "__pycache__" folder
import sys
sys.dont_write_bytecode = True
import os
import numpy as np
from joblib import dump, load
import PIL.Image, PIL.ImageFilter
from tqdm import tqdm
from multiprocessing import dummy as multiprocessing
from prettytable import PrettyTable
#from sklearn.svm import LinearSVC
#from sklearn.linear_model import LogisticRegression, SGDClassifier
#from scipy import stats
#from sklearn.metrics import accuracy_score
from matplotlib import pyplot as plt, cm as cm
from matplotlib.gridspec import GridSpec
# Tensorflow
# 0 = all messages are logged (default behavior)
# 1 = INFO messages are not printed
# 2 = INFO and WARNING messages are not printed
# 3 = INFO, WARNING, and ERROR messages are not printed
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
import tensorflow_probability as tfp
# Keras preprocessing functions
preprocess_resnet_v2 = tf.keras.applications.inception_resnet_v2.preprocess_input
preprocess_v3 = tf.keras.applications.inception_v3.preprocess_input
preprocess_vgg16 = tf.keras.applications.vgg16.preprocess_input
preprocess_convnext = tf.keras.applications.convnext.preprocess_input
# Utils
def cosine_similarity(vec1, vec2):
dot_product = np.dot(vec1, vec2)
norm_vec1 = np.linalg.norm(vec1)
norm_vec2 = np.linalg.norm(vec2)
return dot_product / (norm_vec1 * norm_vec2)
def nth_highest_index(arr, n):
indexed_arr = list(enumerate(arr))
sorted_arr = sorted(indexed_arr, key=lambda x: x[1], reverse=True)
return sorted_arr[n-1][0]
def contraharmonic_mean(arr, axis=(0, 1)):
numerator = tf.reduce_sum(tf.square(arr), axis=axis)
denominator = tf.reduce_sum(arr, axis=axis)
return tf.divide(numerator, (tf.add(denominator, tf.keras.backend.epsilon())))
#####
# VisualTCAV class
#####
class VisualTCAV():
##### Init #####
def __init__(
self,
model,
visual_tcav_dir="VisualTCAV",
clear_cache=False,
batch_size=250,
models_dir=None, cache_dir=None, test_images_dir=None, concept_images_dir=None, random_images_folder=None
):
# Folders and directories
self.models_dir = os.path.join(visual_tcav_dir, "models") if not models_dir else models_dir
self.cache_base_dir = os.path.join(visual_tcav_dir, "cache") if not cache_dir else cache_dir
self.cache_dir = self.cache_base_dir
self.test_images_dir = os.path.join(visual_tcav_dir, "test_images") if not test_images_dir else test_images_dir
self.concept_images_dir = os.path.join(visual_tcav_dir, "concept_images") if not concept_images_dir else concept_images_dir
self.random_images_folder = "random" if not random_images_folder else random_images_folder
os.makedirs(self.models_dir, exist_ok=True)
os.makedirs(self.cache_base_dir, exist_ok=True)
os.makedirs(self.test_images_dir, exist_ok=True)
os.makedirs(self.concept_images_dir, exist_ok=True)
self.batch_size = batch_size
# Model
self.model = None
if model:
self._bindModel(model)
if clear_cache:
for file in os.listdir(self.cache_dir):
os.remove(os.path.join(self.cache_dir, file))
# Concepts/Layers attributes
self.concepts = []
self.layers = []
# Computations
self.computations = {}
self.random_acts = {}
# Set a list of concepts
def setConcepts(self, concept_names):
self.concepts = []
for concept_name in concept_names:
if concept_name not in self.concepts:
self.concepts.append(concept_name)
# Set a list of layers
def setLayers(self, layer_names):
self.layers = []
for layer_name in layer_names:
if layer_name not in self.layers:
self.layers.append(layer_name)
##### Predict #####
def predict(self, no_sort=False):
# Checks
if not isinstance(self, LocalVisualTCAV):
raise Exception("Please use a local explainer")
if not self.model:
raise Exception("Please instantiate a Model first")
# Predict with the provided model wrapper
self.predictions = self.model.model_wrapper.get_predictions(
self.model.preprocessing_function(
self.resized_imgs
)
)
# Sort & add class names
self.predictions = np.array([
self._sortTargetClasses(
prediction,
self.model.model_wrapper.id_to_label,
no_sort
) for prediction in self.predictions
])
# Return the classes
return Predictions(self.predictions, self.test_image_filename, self.model.model_name)
#####
# Private methods
#####
# Bind a model
def _bindModel(self, model):
# Folders and directories
model.graph_path_dir = os.path.join(self.models_dir, model.model_name, model.graph_path_filename)
model.label_path_dir = os.path.join(self.models_dir, model.model_name, model.label_path_filename)
# Wrapper function
model.model_wrapper = model.model_wrapper(model.graph_path_dir, model.label_path_dir, self.batch_size)
# Activate the model
model.activation_generator = model.activation_generator(
model_wrapper=model.model_wrapper,
concept_images_dir=self.concept_images_dir,
cache_dir=self.cache_dir,
preprocessing_function=model.preprocessing_function,
max_examples=model.max_examples,
)
# Model's cache dir
self.cache_dir = os.path.join(self.cache_base_dir, model.model_name)
os.makedirs(self.cache_dir, exist_ok=True)
# Store the model
self.model = model
# Reshape a list of predictions
def _sortTargetClasses(self, predictions, id_to_label, no_sort=False):
# Reshape
indexed_arr = list(enumerate(predictions))
sorted_arr = indexed_arr if no_sort else sorted(indexed_arr, key=lambda x: x[1], reverse=True)
return [
Prediction(
class_index=sorted_element[0],
class_name=id_to_label(sorted_element[0]),
confidence=sorted_element[1],
) for i, sorted_element in enumerate(sorted_arr) if i < 10
]
# Utils to compute the integrated gradients
def _compute_integrated_gradients(self, feature_maps, layer_name, class_index):
# Alphas and baseline image for interpolating images
alphas = tf.linspace(start=0.0, stop=1.0, num=self.m_steps + 1) # Generate m_steps intervals for riemann approximation
baseline = tf.zeros(shape=feature_maps.shape)
# Interpolate images
interpolated_images = self._interpolate_images(feature_maps, baseline, alphas) #VisualTCAV.tf_session.run(
# Generating gradients
#if self.model.model_name == "InceptionV3":
# grads = np.array([])
# for image in interpolated_images:
# grads = np.append(grads,
# # Grad points in the direction which INCREASES probability of class
# self.model.model_wrapper.get_gradient_of_score(np.expand_dims(image, axis=0), layer_name, class_index)[0],
# )
#else:
grads = self.model.model_wrapper.get_gradient_of_score(interpolated_images, layer_name, class_index)
# Compute the gradients
return tf.math.reduce_mean(
(np.array(grads)[:-1] + np.array(grads)[1:]) / tf.constant(2.0),
axis=0,
)
# Utils function to interpolate the fmaps
def _interpolate_images(self, feature_maps, baseline, alphas):
# Interpolating fmaps
image = tf.image.convert_image_dtype(feature_maps, tf.float32)
alphas_x = alphas[:, tf.newaxis, tf.newaxis, tf.newaxis]
baseline_x = tf.expand_dims(baseline, axis=0)
input_x = tf.expand_dims(image, axis=0)
delta = tf.subtract(input_x, baseline_x)
images = tf.add(baseline_x, tf.multiply(alphas_x, delta))
return images
# Function to compute the negative examples activations for a given layer
def _compute_random_activations(self, cache, layer_name):
# Random activations
cache_random_acts_path = os.path.join(self.cache_dir, 'rnd_acts_' + str(self.model.max_examples) + "_" + self.random_images_folder + '_' + layer_name + '.joblib')
if cache and os.path.isfile(cache_random_acts_path):
random_acts = load(cache_random_acts_path)
else:
random_acts = self._compute_random(layer_name)
# If cache is requested
if cache:
dump(random_acts, cache_random_acts_path, compress=3)
# Return
return random_acts
# Compute pooled random
def _compute_random(self, layer_name):
feature_maps_for_concept = self.model.activation_generator.get_feature_maps_for_concept(
self.random_images_folder,
layer_name,
)
return feature_maps_for_concept
# Function to compute the CAV given a concept & a layer
def _compute_cavs(self, cache, concept_name, layer_name, random_acts):
# If cached file exists
cache_path = os.path.join(self.cache_dir, 'cav_' + concept_name + '_' + str(self.model.max_examples) + "_" + self.random_images_folder + '_' + layer_name + '.joblib')
if cache and os.path.isfile(cache_path):
concept_layer = load(cache_path)
else:
# Activations (concept/layer)
concept_acts = self.model.activation_generator.get_feature_maps_for_concept(
concept_name,
layer_name,
)
pooled_concept = tf.reduce_mean(concept_acts, axis=(1,2))
pooled_random = tf.reduce_mean(random_acts, axis=(1,2))
# CAV
concept_layer = ConceptLayer()
concept_layer.cav.centroid0 = tf.reduce_mean(pooled_concept, axis=0)
concept_layer.cav.centroid1 = tf.reduce_mean(pooled_random, axis=0)
concept_layer.cav.direction = tf.subtract(concept_layer.cav.centroid0, concept_layer.cav.centroid1)
emblems = contraharmonic_mean(
tf.nn.relu(
tf.reduce_sum(
tf.multiply(concept_layer.cav.direction[None, None, None, :], concept_acts),
axis=3
)
),
axis=(1, 2)
)
negative_emblems = contraharmonic_mean(
tf.nn.relu(
tf.reduce_sum(
tf.multiply(concept_layer.cav.direction[None, None, None, :], random_acts),
axis=3
)
),
axis=(1, 2)
)
concept_layer.cav.concept_emblem = tf.cast((tfp.stats.percentile(emblems, 50.0),
tfp.stats.percentile(negative_emblems, 50.0)
), tf.float32)
# If cache is requested
if cache:
dump(concept_layer, cache_path, compress=3)
# Return
return concept_layer
#####
# LocalVisualTCAV
#####
class LocalVisualTCAV(VisualTCAV):
##### Init #####
def __init__(
self,
test_image_filename, m_steps=50, n_classes=3, target_class=None,
*args, **kwargs
):
# Super
super().__init__(**kwargs)
# Local attributes
self.test_image_filename = test_image_filename
self.m_steps = m_steps
self.target_class = target_class
if self.target_class is not None:
self.n_classes = 1
self.target_class_index = self.model.model_wrapper.label_to_id(self.target_class)
elif not self.model.binary_classification:
self.n_classes = max(np.min([n_classes, len(self.model.model_wrapper.labels), 3]), 1) # Not implemented more than 3
else:
self.n_classes = 2 # add check that it's actually binary
self.test_images_dir = os.path.join(self.test_images_dir, self.test_image_filename)
self.resized_imgs_size = self.model.model_wrapper.get_image_shape()[:2]
self.predictions = []
self.computations = {}
# Load and resize the image/images
self.imgs = np.array([PIL.Image.open(tf.io.gfile.GFile(self.test_images_dir, 'rb')).convert('RGB')])
self.resized_imgs = np.array([PIL.Image.open(tf.io.gfile.GFile(self.test_images_dir, 'rb')).convert('RGB').resize(self.resized_imgs_size, PIL.Image.BILINEAR)])
##### Explain #####
def explain(self, cache_cav=True, cache_random=True, cav_only=False):
# Checks
if not self.model:
raise Exception("Instantiate a Model first")
if not self.layers or not self.concepts:
raise Exception("Please add at least one concept and one layer first")
if not len(self.predictions):
raise Exception("Please let the model predict the classes first")
# Reset the computation variable
self.computations = {}
# For each layer
for layer_name in tqdm(self.layers, desc="Layers", position=0):
self.computations[layer_name] = {}
# Random activations
random_acts = self._compute_random_activations(cache_random, layer_name)
# Compute the feature maps
feature_maps = self.model.model_wrapper.get_feature_maps(
self.model.preprocessing_function(self.resized_imgs),
layer_name
)[0]
# Compute the CAVs
# Note: computing the direction of the cav using the GAP of the concept's activations and the GAP of the
# random's activations is equivalent to computing the GAP of the direction of the cav, obtained with the
# concept's activations and the random's activations
# cav.direction = GAP(cav.centroid0 - cav.centroid1) = GAP(cav.centroid0) - GAP(cav.centroid1)
for concept_name in self.concepts:
# CAVs
concept_layer = self._compute_cavs(cache_cav, concept_name, layer_name, random_acts)
if not cav_only:
# Concept map
concept_layer.concept_map = tf.nn.relu(tf.math.reduce_sum(tf.multiply(concept_layer.cav.direction[None, None, :], feature_maps), axis=2))
# Normalize Concept Map
if concept_layer.cav.concept_emblem[0] > concept_layer.cav.concept_emblem[1] :
concept_layer.concept_map = tf.where(concept_layer.concept_map > concept_layer.cav.concept_emblem[0], concept_layer.cav.concept_emblem[0], concept_layer.concept_map)
concept_layer.concept_map = tf.where(concept_layer.concept_map < concept_layer.cav.concept_emblem[1], concept_layer.cav.concept_emblem[1], concept_layer.concept_map)
concept_layer.concept_map = (concept_layer.concept_map - concept_layer.cav.concept_emblem[1])/(concept_layer.cav.concept_emblem[0] - concept_layer.cav.concept_emblem[1])
else:
concept_layer.concept_map = tf.multiply(concept_layer.concept_map, 0)
# Save the partial computations
self.computations[layer_name][concept_name] = concept_layer
if not cav_only:
# Compute integrated gradients and attributions
attributions = {}
for n_class in range(self.n_classes):
if not self.model.binary_classification:
logits = self.model.model_wrapper.get_logits(np.expand_dims(feature_maps, axis=0), layer_name)[0]
logits_baseline = self.model.model_wrapper.get_logits(np.expand_dims(tf.zeros(shape=feature_maps.shape), axis=0), layer_name)[0]
ig_expected = tf.nn.relu(tf.subtract(logits, logits_baseline))
ig_expected_max_value = tf.reduce_max(ig_expected)
if(ig_expected_max_value > 0):
ig_expected_norm = tf.divide(ig_expected, ig_expected_max_value)
else:
ig_expected_norm = ig_expected
if self.target_class is not None:
ig_expected_class = ig_expected_norm[self.target_class_index]
#elif self.model.binary_classification:
# ig_expected_class = ig_expected_norm[self.predictions[0][0].class_index]
else:
ig_expected_class = ig_expected_norm[self.predictions[0][n_class].class_index]
# Compute attributions
if self.target_class is not None:
ig = self._compute_integrated_gradients(feature_maps, layer_name, self.target_class_index)
elif self.model.binary_classification:
ig = self._compute_integrated_gradients(feature_maps, layer_name, self.predictions[0][0].class_index)
else:
ig = self._compute_integrated_gradients(feature_maps, layer_name, self.predictions[0][n_class].class_index)
if self.model.binary_classification:
binary_attributions = tf.multiply(ig, feature_maps)
virtual_logit_0 = tf.reduce_sum(tf.nn.relu(binary_attributions))
virtual_logit_1 = tf.reduce_sum(tf.nn.relu(-binary_attributions))
max_virtual_logit = max(virtual_logit_0, virtual_logit_1)
if max_virtual_logit > 0:
virtual_logit_0 /= max_virtual_logit
virtual_logit_1 /= max_virtual_logit
if n_class == 0:
attributions[n_class] = tf.nn.relu(binary_attributions)
attributions[n_class] = tf.multiply(tf.divide(attributions[n_class], tf.add(tf.reduce_sum(attributions[n_class]), tf.keras.backend.epsilon())), virtual_logit_0)
else:
attributions[n_class] = tf.nn.relu(-binary_attributions)
attributions[n_class] = tf.multiply(tf.divide(attributions[n_class], tf.add(tf.reduce_sum(attributions[n_class]), tf.keras.backend.epsilon())), virtual_logit_1)
else:
attributions[n_class] = tf.nn.relu(tf.multiply(ig, feature_maps))
attributions[n_class] = tf.multiply(tf.divide(attributions[n_class], tf.add(tf.reduce_sum(attributions[n_class]), tf.keras.backend.epsilon())), ig_expected_class)
# Iterate again on concepts and n_classes
for concept_name in self.concepts:
for n_class in range(self.n_classes):
# Mask attributions
masked_attributions = tf.multiply(attributions[n_class], self.computations[layer_name][concept_name].concept_map[:, :, None])
pooled_masked_attributions = tf.reduce_sum(masked_attributions, axis=(0, 1))
# Pooled & normalized CAV
if(tf.reduce_min(feature_maps) < 0):
pooled_cav_norm = tf.nn.relu(
tf.multiply(self.computations[layer_name][concept_name].cav.direction,
tf.where(tf.reduce_sum(tf.multiply(
feature_maps, self.computations[layer_name][concept_name].concept_map[:, :, None]), axis=(0, 1)) < 0, -1.0, 1.0)))
else:
pooled_cav_norm = tf.nn.relu(self.computations[layer_name][concept_name].cav.direction)
max_cav = tf.reduce_max(pooled_cav_norm)
if(max_cav > 0):
pooled_cav_norm = tf.divide(pooled_cav_norm, tf.reduce_max(pooled_cav_norm))
# Compute and save concept attributions
self.computations[layer_name][concept_name].attributions[n_class] = tf.tensordot(pooled_cav_norm, pooled_masked_attributions, axes=1)
##### Plot heatmaps and information #####
def plot(self, paper=False):
# Checks
if not self.model:
raise Exception("Instantiate a Model first")
if not self.layers or not self.concepts:
raise Exception("Please add at least one concept and one layer first")
if not len(self.predictions):
raise Exception("Please let the model predict the classes first")
if not self.computations:
raise Exception("Please let the model explain first")
# Escaping
model_name_esc = self.model.model_name.replace("_", "\_")
# Iterate over the concepts
for concept_name in self.concepts:
# Escaping
concept_name_esc = concept_name.replace("_", "\_")
if not paper:
fig = plt.figure(figsize=(4 + 5*(len(self.layers)-1) - 2*(len(self.layers)-1), 7))
gs = GridSpec(3, len(self.layers)*3, height_ratios=[2, 5, 2])
fig.suptitle(f"$\mathbfit{{{model_name_esc}}}$ architecture\n$\mathbfit{{{concept_name_esc}}}$ concept", fontsize=10+1.5*len(self.layers))
else:
fig = plt.figure(figsize=(4 + 5*(len(self.layers)-1) - 2*(len(self.layers)-1), 6))# + (0 if self.imgs[0].shape[0] < self.imgs[0].shape[1] else 1)))
gs = GridSpec(2, len(self.layers)*3, height_ratios=[1, 3.5])
# Examples of concepts
if not paper:
concept_images = self.model.activation_generator.get_images_for_concept(concept_name, False)
for i in range(min(len(concept_images), len(self.layers)*3)):
fig.add_subplot(gs[2,i])
plt.imshow(concept_images[i])
plt.tight_layout()
plt.axis('off')
# Iterate over the layers
for j, layer_name in enumerate(self.layers):
# Escaping
layer_description = "" if len(layer_name) > 11 else "layer"
layer_name_esc = layer_name.replace("_", "\_")
# Obtain concept
concept_layer = self.computations[layer_name][concept_name]
# Obtain heatmap
max_value = np.max(concept_layer.concept_map)
heatmap = tf.image.resize(
np.expand_dims(concept_layer.concept_map, axis=2),
[self.imgs[0].shape[0], self.imgs[0].shape[1]]
)
heatmap = np.reshape(heatmap, (heatmap.shape[0], heatmap.shape[1]))
# Subplot
fig.add_subplot(gs[1,j*3:(j+1)*3])
plt.imshow(self.imgs[0])
# Blurring
heatmap = np.array(PIL.Image.fromarray(np.uint8(heatmap * 255) , 'L')
.filter(PIL.ImageFilter.GaussianBlur(radius = 20))) / 255
if(np.max(heatmap) > 0 and np.max(heatmap) < max_value):
heatmap = (heatmap/np.max(heatmap))*max_value
colormap.imshow(heatmap)
if not paper:
plt.title(f"\n", fontsize=1)
plt.tight_layout()
plt.axis('off')
# Subplot
fig.add_subplot(gs[0,j*3:(j+1)*3])
plt.title(f"$\mathbfit{{{layer_name_esc}}}$ {layer_description}", fontsize=9+1.5*len(self.layers), y=0.95)
plt.tight_layout()
rows = []
for c in range(self.n_classes):
attribution = concept_layer.attributions[c]
if self.target_class is not None:
class_name = self.target_class.replace("-", " ")
elif self.model.binary_classification and c==1:
class_name = "Female"#"Not " + self.predictions[0][0].class_name.replace("-", " ")
else:
class_name = self.predictions[0][c].class_name.replace("-", " ")
if not paper or True: #temp
if len(class_name) > 12: class_name = class_name[:12] + "‥"
class_name = class_name.replace("_", "\_").replace(" ", "\ ")
row = []
row.append(f"$\mathit{{{class_name}}}$")
if paper:
attribution = f"{attribution:.2g}" if (attribution >= 0.001 or attribution == 0.0) else f"{attribution:.1e}"
else:
attribution = f"{attribution:.2g}" if attribution >= 0.001 else f"{attribution:.1e}"
attribution = attribution.replace("e-0", "e-").replace('-', '{-}')
row.append(f"$\mathbf{{{attribution}}}$")
rows.append(row)
cols = [f"$\mathbf{{Class}}$", f"$\mathbf{{Attrib.}}$"]
table = plt.table(
cellText = rows,
rowLabels = [f"" for c in range(self.n_classes)],
colLabels = cols,
rowColours =["silver"] * 10,
colColours =["silver"] * 10,
cellLoc ='center',
rowLoc ='center',
loc = 'center', edges='BRTL'
)
cellDict = table.get_celld()
for i in range(0, len(rows)+1):
cellDict[(i,0)].set_width(.625)
for i in range(0, len(rows)+1):
cellDict[(i,1)].set_width(.375)
for i in range(0,len(cols)):
cellDict[(0,i)].set_height(.2)
for j in range(1, self.n_classes+1):
cellDict[(j,i)].set_height(.2)
# Set font size
if paper:
table.auto_set_font_size(False)
table.set_fontsize(4.5+1.75*len(self.layers))
else:
table.set_fontsize(9+1.75*len(self.layers))
plt.tight_layout()
plt.axis('off')
# Show
fig.tight_layout()
plt.show()
##### Get CAVs #####
def getCAVs(self, layer_name, concept_name):
# Checks
if not self.model:
raise Exception("Instantiate a Model first")
if not self.layers or not self.concepts:
raise Exception("Please add at least one concept and one layer first")
if not len(self.predictions):
raise Exception("Please let the model predict the classes first")
if not self.computations:
raise Exception("Please let the model explain first")
return self.computations[layer_name][concept_name].cav
#####
# GlobalVisualTCAV
#####
class GlobalVisualTCAV(VisualTCAV):
##### Init #####
def __init__(
self,
target_class, test_images_folder, m_steps=50, compute_negative_class = False,
*args, **kwargs
):
# Super
super().__init__(**kwargs)
# Local attributes
self.m_steps = m_steps
self.target_class = target_class
self.compute_negative_class = compute_negative_class
self.test_images_folder = test_images_folder
self.test_image_filename = test_images_folder
self.class_index = self.model.model_wrapper.label_to_id(target_class)
#self.test_images_dir = os.path.join(self.test_images_dir, self.test_images_folder)
self.resized_imgs_size = self.model.model_wrapper.get_image_shape()[:2]
self.predictions = []
self.stats = {}
##### Explain #####
def explain(self, cache_cav=True, cache_random=True):
# Checks
if not self.model:
raise Exception("Instantiate a Model first")
if not self.layers or not self.concepts:
raise Exception("Please add at least one concept and one layer first")
# Reset the computation variable
self.stats = {}
# For each layer
for layer_name in tqdm(self.layers, desc="Layers", position=0):
self.stats[layer_name] = {}
# Random activations
random_acts = self._compute_random_activations(cache_random, layer_name)
# Compute the feature_maps for each class
class_feature_maps = self.computeFeatureMaps(layer_name)
# For each concept
cavs = {}
attribution_list = {}
for concept_name in self.concepts:
# CAVs
concept_layer = self._compute_cavs(cache_cav, concept_name, layer_name, random_acts)
# Save the partial computations
cavs[concept_name] = concept_layer
attribution_list[concept_name] = {}
# For each image
for cl, feature_maps in enumerate(tqdm(class_feature_maps, desc="Attributions", position=1)):
'''
if self.target_class is not None:
ig_expected_class = ig_expected_norm[self.target_class_index]
elif self.model.binary_classification:
ig_expected_class = ig_expected_norm[self.predictions[0][0].class_index]
else:
ig_expected_class = ig_expected_norm[self.predictions[0][n_class].class_index]
# Compute attributions
if self.target_class is not None:
ig = self._compute_integrated_gradients(feature_maps, layer_name, self.target_class_index)
elif self.model.binary_classification:
ig = self._compute_integrated_gradients(feature_maps, layer_name, self.predictions[0][0].class_index)
else:
ig = self._compute_integrated_gradients(feature_maps, layer_name, self.predictions[0][n_class].class_index)
if self.model.binary_classification and n_class == 1:
attributions[n_class] = tf.nn.relu(-tf.multiply(ig, feature_maps))
else:
attributions[n_class] = tf.nn.relu(tf.multiply(ig, feature_maps))
'''
if not self.model.binary_classification:
# Compute logits
logits = self.model.model_wrapper.get_logits(np.expand_dims(feature_maps, axis=0), layer_name)[0]
logits_baseline = self.model.model_wrapper.get_logits(np.expand_dims(tf.zeros(shape=feature_maps.shape), axis=0), layer_name)[0]
ig_expected = tf.nn.relu(tf.subtract(logits, logits_baseline))
ig_expected_max_value = tf.reduce_max(ig_expected)
if(ig_expected_max_value > 0):
ig_expected_norm = tf.divide(ig_expected, ig_expected_max_value)
else:
ig_expected_norm = ig_expected
ig_expected_class = ig_expected_norm[self.class_index]
# Compute attributions
ig = self._compute_integrated_gradients(feature_maps, layer_name, self.class_index)
if self.model.binary_classification:# and self.compute_negative_class == True:
#attributions = tf.nn.relu(-tf.multiply(ig, feature_maps))
binary_attributions = tf.multiply(ig, feature_maps)
virtual_logit_0 = tf.reduce_sum(tf.nn.relu(binary_attributions))
virtual_logit_1 = tf.reduce_sum(tf.nn.relu(-binary_attributions))
max_virtual_logit = max(virtual_logit_0, virtual_logit_1)
if max_virtual_logit > 0:
virtual_logit_0 /= max_virtual_logit
virtual_logit_1 /= max_virtual_logit
if not self.compute_negative_class:
attributions = tf.nn.relu(binary_attributions)
attributions = tf.multiply(tf.divide(attributions, tf.add(tf.reduce_sum(attributions), tf.keras.backend.epsilon())), virtual_logit_0)
else:
attributions = tf.nn.relu(-binary_attributions)
attributions = tf.multiply(tf.divide(attributions, tf.add(tf.reduce_sum(attributions), tf.keras.backend.epsilon())), virtual_logit_1)
else:
attributions = tf.nn.relu(tf.multiply(ig, feature_maps))
attributions = tf.multiply(tf.divide(attributions, tf.add(tf.reduce_sum(attributions), tf.keras.backend.epsilon())), ig_expected_class)
# Again for each concept
for concept_name in self.concepts:
# Concept map
concept_map = tf.nn.relu(tf.math.reduce_sum(tf.multiply(cavs[concept_name].cav.direction[None, None, :], feature_maps), axis=2))
# Normalize Concept Map
if cavs[concept_name].cav.concept_emblem[0] > cavs[concept_name].cav.concept_emblem[1] :
concept_map = tf.where(concept_map > cavs[concept_name].cav.concept_emblem[0], cavs[concept_name].cav.concept_emblem[0], concept_map)
concept_map = tf.where(concept_map < cavs[concept_name].cav.concept_emblem[1], cavs[concept_name].cav.concept_emblem[1], concept_map)
concept_map = (concept_map - cavs[concept_name].cav.concept_emblem[1])/(cavs[concept_name].cav.concept_emblem[0] - cavs[concept_name].cav.concept_emblem[1])
else:
concept_map = tf.multiply(concept_map, 0)
# Mask attributions
pooled_masked_attributions = tf.reduce_sum(tf.multiply(attributions, concept_map[:, :, None]), axis=(0, 1))
# Pooled & normalized CAV
if(tf.reduce_min(feature_maps) < 0):
pooled_cav_norm = tf.nn.relu(
tf.multiply(cavs[concept_name].cav.direction,
tf.where(tf.reduce_sum(tf.multiply(
feature_maps, concept_map[:, :, None]), axis=(0, 1)) < 0, -1.0, 1.0)))
else:
pooled_cav_norm = tf.nn.relu(cavs[concept_name].cav.direction)
max_cav = tf.reduce_max(pooled_cav_norm)
if(max_cav > 0):
pooled_cav_norm = tf.divide(pooled_cav_norm, tf.reduce_max(pooled_cav_norm))
# Compute and save concept attributions
attribution_list[concept_name][cl] = tf.tensordot(pooled_cav_norm, pooled_masked_attributions, axes=1)
# Again for each concept
for concept_name in self.concepts:
# Compute stats
self.stats[layer_name][concept_name] = Stat(list(attribution_list[concept_name].values()))
# Clear memory
del cavs
del attribution_list
##### Plot graphs and information #####
def plot(self, colormap='viridis', paper=False):
# Checks
if not self.model:
raise Exception("Instantiate a Model first")
if not self.layers or not self.concepts:
raise Exception("Please add at least one concept and one layer first")
if not self.stats:
raise Exception("Please let the model explain first")
# Colors
cmap = plt.get_cmap(colormap)
# Escaping
model_name_esc = self.model.model_name.replace("_", "\_").replace("-", "{-}").replace(" ", "\\text{ }")
target_class_esc = self.target_class.replace("_", "\_").replace("-", "{-}").replace(" ", "\\text{ }")
if self.model.binary_classification and self.compute_negative_class==True:
target_class_esc = 'Female'#"Not " + target_class_esc
concept_names = [concept.replace("_", "\_").replace("-", "{-}").replace(" ", "\\text{ }") for concept in self.concepts]
# Figure
fig = plt.figure(figsize=(5 + 1*(len(self.concepts)-1), 4))
gs = GridSpec(1, 1, height_ratios=[1])
fig.suptitle(f"$\mathbfit{{{model_name_esc}}}$ architecture\n$\mathbfit{{{target_class_esc}}}$ target class", fontsize=12)
# Subplot
fig.add_subplot(gs[0])
# Axes
x = np.arange(len(self.concepts))-0.5
# Iterate over the concepts
for i, layer_name in enumerate(self.layers):
# Indexing
#color = i / (len(self.layers)-1) if len(self.layers) > 1 else 0.5
color = cmap(i/(len(self.layers)-1)) if len(self.layers) > 1 else cmap(0.5)
width = 0.1
pos_x = 0.5 + (i-len(self.layers)/2)*width + width/2
# Escaping
layer_name_esc = layer_name.replace("_", "\_").replace("-", "{-}").replace(" ", "\\text{ }")
# Bar
plt.bar(
x+pos_x,
[(self.stats[layer_name][concept_name].begin + self.stats[layer_name][concept_name].end)/2 for concept_name in self.concepts],
yerr=[max(0, (self.stats[layer_name][concept_name].end - self.stats[layer_name][concept_name].begin)/2) for concept_name in self.concepts],
width=width,
label=f'$\mathit{{{layer_name_esc}}}$',
zorder = 2,
capsize = 3.5,
#color=cmap(((color)/8)*6 + 1/8),
color=color
)
# Show
#plt.xlabel('Concept')
plt.ylabel('Attribution (2σ error)')
plt.xticks(np.arange(len(self.concepts)), [f'$\mathit{{{concept}}}$' for concept in concept_names])
plt.grid(linewidth = 0.3, zorder = 1)
if paper:
plt.legend()
else:
plt.legend(bbox_to_anchor=(1.025, 1.0), loc='upper left', borderaxespad=0.0)
#plt.legend()
fig.tight_layout()
plt.ylim(bottom=0, top=max(0.1, plt.ylim()[1]))
plt.xlim(left=-0.5, right=0.5 + len(self.concepts)-1)
plt.show()
##### Print stats and information #####
def statsInfo(self):
# Checks
if not self.model:
raise Exception("Instantiate a Model first")
if not self.layers or not self.concepts:
raise Exception("Please add at least one concept and one layer first")
if not self.stats:
raise Exception("Please let the model explain first")
# Print a table with information
table = PrettyTable(title=f"Model: {self.model.model_name}; Class: {self.target_class}; Examples: {self.test_images_folder}", field_names=["Concept", "Layer", "Attrib. mean", "Attrib. 95.45% CI"], float_format='.2')
for i, concept_name in enumerate(self.concepts):
for j, layer_name in enumerate(self.layers):
table.add_row([
concept_name if j == 0 else "", layer_name,
f"{self.stats[layer_name][concept_name].mean:.3g} +- {self.stats[layer_name][concept_name].std:.3g}",
[f"{self.stats[layer_name][concept_name].begin:.3g}", f"{self.stats[layer_name][concept_name].end:.3g}"],
],
#divider=True if j == len(self.layers)-1 else False,
)
print(table)
##### Function used to compute the FEATURE MAPS #####
def computeFeatureMaps(self, layer_name):
# Checks
if not self.model:
raise Exception("Instantiate a Model first")
if not layer_name:
raise Exception("Please provide the function with one layer")
# Compute the feature maps for each class
self.model.activation_generator.concept_images_dir = self.test_images_dir
class_feature_maps = self.model.activation_generator.get_feature_maps_for_concept(self.test_images_folder, layer_name)
self.model.activation_generator.concept_images_dir = self.concept_images_dir
return class_feature_maps
#####
# Model class
#####
class Model:
##### Init #####
def __init__(self, model_name, graph_path_filename, label_path_filename, preprocessing_function=lambda x: x / 255, binary_classification = False, max_examples=500):
# Attributes
self.model_name = model_name
self.max_examples = max_examples
self.binary_classification = binary_classification
# Folders and directories
self.graph_path_filename = graph_path_filename
self.label_path_filename = label_path_filename
self.graph_path_dir = None
self.label_path_dir = None
# Wrapper & preprocessing functions
self.model_wrapper = KerasModelWrapper
self.activation_generator = ImageActivationGenerator
self.preprocessing_function = preprocessing_function
##### Get layer names #####
def getLayerNames(self):
return [layer_name for layer_name in self.model_wrapper.layer_tensors.keys()]
##### Print model's informations #####
def info(self):
# Print a table with information
table = PrettyTable(title = f"Model: {self.model_name}", field_names=["N. classes", "Layers"], float_format='.2')
for i, layer_name in enumerate(self.getLayerNames()):
table.add_row([len(self.model_wrapper.labels) if i == 0 else "", layer_name])
print(table)
#####
# ConceptLayer class
#####
class ConceptLayer:
##### Init #####
def __init__(self):
# Attributes
self.attributions = {}
self.concept_map = None
# CAV
self.cav = Cav()
#####
# Cav class
#####
class Cav:
##### Init #####
def __init__(self, direction=None, centroid0=None, centroid1=None, concept_emblem=None):
# Attributes
self.direction = direction
self.centroid0 = centroid0
self.centroid1 = centroid1
self.concept_emblem = concept_emblem
#####
# Prediction class
#####
class Prediction:
##### Init #####
def __init__(self, class_name=None, class_index=None, confidence=None):
# Attributes
self.class_name = class_name
self.class_index = class_index
self.confidence = confidence
#####