-
Notifications
You must be signed in to change notification settings - Fork 8
/
maia_api.py
1137 lines (1011 loc) · 55.1 KB
/
maia_api.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
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
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler, StableDiffusionInstructPix2PixPipeline, AutoPipelineForText2Image
import sys
# import random
import numpy as np
# import cv2
# import PIL
from PIL import Image
import PIL
import torch
import torchvision.models as models
from torchvision import transforms
import baukit #pip install git+https://github.com/davidbau/baukit
from baukit import Trace
import openai
import requests
from io import BytesIO
import sys
sys.path.append('./synthetic-neurons-dataset/')
sys.path.append('./synthetic-neurons-dataset/Grounded-Segment-Anything/')
sys.path.append('./netdissect/')
from netdissect.imgviz import ImageVisualizer
from IPython import embed
import os
import base64
from typing import List, Tuple
from call_agent import ask_agent
import time
import math
import synthetic_neurons
import clip
import torch.nn.functional as F
class System:
"""
A Python class containing the vision model and the specific neuron to interact with.
Attributes
----------
neuron_num : int
The serial number of the neuron.
layer : string
The name of the layer where the neuron is located.
model_name : string
The name of the vision model.
model : nn.Module
The loaded PyTorch model.
neuron : callable
A lambda function to compute neuron activation and activation map per input image.
Use this function to test the neuron activation for a specific image.
device : torch.device
The device (CPU/GPU) used for computations.
Methods
-------
load_model(model_name: str)->nn.Module
Gets the model name and returns the vision model from PyTorch library.
call_neuron(image_list: List[torch.Tensor])->Tuple[List[int], List[str]]
returns the neuron activation for each image in the input image_list as well as the activation map
of the neuron over that image, that highlights the regions of the image where the activations
are higher (encoded into a Base64 string).
"""
def __init__(self, neuron_num: int, layer: str, model_name: str, device: str, thresholds=None):
"""
Initializes a neuron object by specifying its number and layer location and the vision model that the neuron belongs to.
Parameters
-------
neuron_num : int
The serial number of the neuron.
layer : str
The name of the layer that the neuron is located at.
model_name : str
The name of the vision model that the neuron is part of.
device : str
The computational device ('cpu' or 'cuda').
"""
self.neuron_num = neuron_num
self.layer = layer
self.device = torch.device(f"cuda:{device}" if torch.cuda.is_available() else "cpu")
self.model_name = model_name
self.preprocess = None
if 'dino' in model_name or 'resnet' in model_name:
self.preprocess = self.preprocess_imagenet
self.model = self.load_model(model_name) #if clip, the define self.preprocess
if thresholds is not None:
self.threshold = thresholds[self.layer][self.neuron_num]
else:
self.threshold = 0
def load_model(self, model_name: str)->torch.nn.Module:
"""
Gets the model name and returns the vision model from pythorch library.
Parameters
----------
model_name : str
The name of the model to load.
Returns
-------
nn.Module
The loaded PyTorch vision model.
Examples
--------
>>> # load "resnet152"
>>> def execute_command(model_name) -> nn.Module:
>>> model = load_model(model_name: str)
>>> return model
"""
if model_name=='resnet152':
resnet152 = models.resnet152(weights='IMAGENET1K_V1').to(self.device)
model = resnet152.eval()
elif model_name == 'dino_vits8':
model = torch.hub.load('facebookresearch/dino:main', 'dino_vits8').to(self.device).eval()
elif model_name == "clip-RN50":
name = 'RN50'
full_model, preprocess = clip.load(name)
model = full_model.visual.to(self.device).eval()
self.preprocess = preprocess
elif model_name == "clip-ViT-B32":
name = 'ViT-B/32'
full_model, preprocess = clip.load(name)
model = full_model.visual.to(self.device).eval()
self.preprocess = preprocess
return model
@staticmethod
def spatialize_vit_mlp(hiddens: torch.Tensor) -> torch.Tensor:
"""Make ViT MLP activations look like convolutional activations.
Each activation corresponds to an image patch, so we can arrange them
spatially. This allows us to use all the same dissection tools we
used for CNNs.
Args:
hiddens: The hidden activations. Should have shape
(batch_size, n_patches, n_units).
Returns:
Spatially arranged activations, with shape
(batch_size, n_units, sqrt(n_patches - 1), sqrt(n_patches - 1)).
"""
batch_size, n_patches, n_units = hiddens.shape
# Exclude CLS token.
hiddens = hiddens[:, 1:]
n_patches -= 1
# Compute spatial size.
size = math.isqrt(n_patches)
assert size**2 == n_patches
# Finally, reshape.
return hiddens.permute(0, 2, 1).reshape(batch_size, n_units, size, size)
def calc_activations(self, image: torch.Tensor)->Tuple[int, torch.Tensor]:
""""
Returns the neuron activation for the input image, as well as the activation map of the neuron over the image
that highlights the regions of the image where the activations are higher (encoded into a Base64 string).
Parameters
----------
image : torch.Tensor
The input image in PIL format.
Returns
-------
Tuple[int, torch.Tensor]
Returns the maximum activation value of the neuron on the input image and a mask
Examples
--------
>>> # load neuron 62, layer4 of resnet152
>>> def execute_command(model_name) -> callable:
>>> model = load_model(model_name: str)
>>> neuron = load_neuron(neuron_num=62, layer='layer4', model=model)
>>> return neuron
"""
with Trace(self.model, self.layer) as ret:
_ = self.model(image)
hiddens = ret.output
if "dino" in self.model_name:
hiddens = self.spatialize_vit_mlp(hiddens)
batch_size, channels, *_ = hiddens.shape
activations = hiddens.permute(0, 2, 3, 1).reshape(-1, channels)
pooled, _ = hiddens.view(batch_size, channels, -1).max(dim=2)
neuron_activation_map = hiddens[:, self.neuron_num, :, :]
return(pooled[:,self.neuron_num], neuron_activation_map)
def calc_class(self, image: torch.Tensor)->Tuple[int, torch.Tensor]:
""""
Returns the neuron activation for the input image, as well as the activation map of the neuron over the image
that highlights the regions of the image where the activations are higher (encoded into a Base64 string).
Parameters
----------
image : torch.Tensor
The input image in PIL format.
Returns
-------
Tuple[int, torch.Tensor]
Returns the maximum activation value of the neuron on the input image and a mask
Examples
--------
>>> # load neuron 62, layer4 of resnet152
>>> def execute_command(model_name) -> callable:
>>> model = load_model(model_name: str)
>>> neuron = load_neuron(neuron_num=62, layer='layer4', model=model)
>>> return neuron
"""
logits = self.model(image)
prob = F.softmax(logits, dim=1)
image_calss = torch.argmax(logits[0])
activation = prob[0][image_calss]
return activation.item(), image
def call_neuron(self, image_list: List[torch.Tensor])->Tuple[List[int], List[str]]:
"""
The function returns the neuron’s maximum activation value (in int format) over each of the images in the list as well as the activation map of the neuron over each of the images that highlights the regions of the image where the activations are higher (encoded into a Base64 string).
Parameters
----------
image_list : List[torch.Tensor]
The input image
Returns
-------
Tuple[List[int], List[str]]
For each image in image_list returns the maximum activation value of the neuron on that image, and a masked images,
with the region of the image that caused the high activation values highlighted (and the rest of the image is darkened). Each image is encoded into a Base64 string.
Examples
--------
>>> # test the activation value of the neuron for the prompt "a dog standing on the grass"
>>> def execute_command(system, prompt_list) -> Tuple[int, str]:
>>> prompt = ["a dog standing on the grass"]
>>> image = text2image(prompt)
>>> activation_list, activation_map_list = system.call_neuron(image)
>>> return activation_list, activation_map_list
>>> # test the activation value of the neuron for the prompt “a fox and a rabbit watch a movie under a starry night sky” “a fox and a bear watch a movie under a starry night sky” “a fox and a rabbit watch a movie at sunrise”
>>> def execute_command(system.neuron, prompt_list) -> Tuple[int, str]:
>>> prompt_list = [[“a fox and a rabbit watch a movie under a starry night sky”, “a fox and a bear watch a movie under a starry night sky”,“a fox and a rabbit watch a movie at sunrise”]]
>>> images = text2image(prompt_list)
>>> activation_list, activation_map_list = system.call_neuron(images)
>>> return activation_list, activation_map_list
"""
activation_list = []
masked_images_list = []
for image in image_list:
if image==None: #for dalle
activation_list.append(None)
masked_images_list.append(None)
else:
if self.layer == 'last':
tensor = self.preprocess_images(image)
acts, image_class = self.calc_class(tensor)
activation_list.append(torch.round(acts[ind] * 100).item()/100)
masked_images_list.append(image2str(image[0]))
else:
image = self.preprocess_images(image)
acts,masks = self.calc_activations(image)
ind = torch.argmax(acts).item()
masked_image = generate_masked_image(image[ind], masks[ind], "./temp.png", self.threshold)
activation_list.append(torch.round(acts[ind] * 100).item()/100)
masked_images_list.append(masked_image)
return activation_list,masked_images_list
def preprocess_imagenet(self, image, normalize=True, im_size=224):
if normalize:
preprocess = transforms.Compose([
transforms.Resize(im_size),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
else:
preprocess = transforms.Compose([
transforms.Resize(im_size),
transforms.ToTensor(),
])
return preprocess(image)
def preprocess_images(self, images):
image_list = []
if type(images) == list:
for image in images:
image_list.append(self.preprocess(image).to(self.device))
batch_tensor = torch.stack(image_list)
return batch_tensor
else:
return self.preprocess(images).unsqueeze(0).to(self.device)
class Synthetic_System:
"""
A Python class containing the vision model and the specific neuron to interact with.
Attributes
----------
neuron_num : int
The serial number of the neuron.
layer : string
The name of the layer where the neuron is located.
model_name : string
The name of the vision model.
model : nn.Module
The loaded PyTorch model.
neuron : callable
A lambda function to compute neuron activation and activation map per input image.
Use this function to test the neuron activation for a specific image.
device : torch.device
The device (CPU/GPU) used for computations.
Methods
-------
load_model(model_name: str)->nn.Module
Gets the model name and returns the vision model from PyTorch library.
call_neuron(image_list: List[torch.Tensor])->Tuple[List[int], List[str]]
returns the neuron activation for each image in the input image_list as well as the activation map
of the neuron over that image, that highlights the regions of the image where the activations
are higher (encoded into a Base64 string).
"""
def __init__(self, neuron_num: int, neuron_labels: str, neuron_mode: str, device: str):
self.neuron_num = neuron_num
self.neuron_labels = neuron_labels
self.neuron = synthetic_neurons.SAMNeuron(neuron_labels, neuron_mode)
self.device = torch.device(f"cuda:{device}" if torch.cuda.is_available() else "cpu")
self.threshold = 0
self.layer = neuron_mode
def call_neuron(self, image_list: List[torch.Tensor])->Tuple[List[int], List[str]]:
"""
The function returns the neuron’s maximum activation value (in int format) over each of the images in the list as well as the activation map of the neuron over each of the images that highlights the regions of the image where the activations are higher (encoded into a Base64 string).
Parameters
----------
image_list : List[torch.Tensor]
The input image
Returns
-------
Tuple[List[int], List[str]]
For each image in image_list returns the maximum activation value of the neuron on that image, and a masked images,
with the region of the image that caused the high activation values highlighted (and the rest of the image is darkened). Each image is encoded into a Base64 string.
Examples
--------
>>> # test the activation value of the neuron for the prompt "a dog standing on the grass"
>>> def execute_command(system, prompt_list) -> Tuple[int, str]:
>>> prompt = ["a dog standing on the grass"]
>>> image = text2image(prompt)
>>> activation_list, activation_map_list = system.call_neuron(image)
>>> return activation_list, activation_map_list
>>> # test the activation value of the neuron for the prompt “a fox and a rabbit watch a movie under a starry night sky” “a fox and a bear watch a movie under a starry night sky” “a fox and a rabbit watch a movie at sunrise”
>>> def execute_command(system.neuron, prompt_list) -> Tuple[int, str]:
>>> prompt_list = [[“a fox and a rabbit watch a movie under a starry night sky”, “a fox and a bear watch a movie under a starry night sky”,“a fox and a rabbit watch a movie at sunrise”]]
>>> images = text2image(prompt_list)
>>> activation_list, activation_map_list = system.call_neuron(images)
>>> return activation_list, activation_map_list
"""
activation_list = []
masked_images_list = []
for image in image_list:
if image==None: #for dalle
activation_list.append(None)
masked_images_list.append(None)
else:
acts, _, _, masks = self.neuron.calc_activations(image)
ind = np.argmax(acts)
masked_image = image2str(masks[ind], "./temp_synthetic.png")
activation_list.append(acts[ind])
masked_images_list.append(masked_image)
return activation_list,masked_images_list
class Tools:
"""
A Python class containing tools to interact with the neuron implemented in the system class,
in order to run experiments on it.
Attributes
----------
text2image_model_name : str
The name of the text-to-image model.
text2image_model : any
The loaded text-to-image model.
images_per_prompt : int
Number of images to generate per prompt.
path2save : str
Path for saving output images.
threshold : any
Activation threshold for neuron analysis.
device : torch.device
The device (CPU/GPU) used for computations.
experiment_log: str
A log of all the experiments, including the code and the output from the neuron
Methods
-------
text2image(prompt_list: str)->Tuple[torcu.Tensor]
Gets a list of text prompt as an input and generates an image for each prompt in the list using a text to image model.
The function returns a list of images.
load_text2image_model(model_name: str) -> any
Loads a text-to-image model.
text2image(prompt: str) -> List[any]
Generates images based on a text prompt.
sampler(act: any, imgs: List[any], mask: any, prompt: str, method: str = 'max') -> Tuple[List[int], List[str]]
Processes images based on neuron activations.
generate_masked_image(image: any, mask: any, path2save: str) -> str
Generates a masked image highlighting high activation areas.
preprocess_image(self, images: any, normalize: bool = True) -> torch.Tensor
Preprocesses images for the model.
describe_images(image_list: List[str], image_title:List[str], desctiprions = List[str]) -> str
Gets a list of images and generat a textual description of the unmasked regions within each of them.
"""
def __init__(self, path2save, device, DatasetExemplars = None, images_per_prompt=10, text2image_model_name='sd'):
"""
Initializes the Tools object.
Parameters
----------
path2save : str
Path for saving output images.
DatasetExemplars : object
an object from the class DatasetExemplars
device : str
The computational device ('cpu' or 'cuda').
"""
self.device = torch.device(f"cuda:{device}" if torch.cuda.is_available() else "cpu")
self.text2image_model_name = text2image_model_name
self.text2image_model = self.load_text2image_model(model_name=text2image_model_name)
self.images_per_prompt = images_per_prompt
self.p2p_model_name = 'ip2p'
self.p2p_model = self.load_pix2pix_model(model_name=self.p2p_model_name) # consider maybe adding options for other models like pix2pix zero
self.path2save = path2save
self.experiment_log = []
self.im_size = 224
if DatasetExemplars is not None:
self.exemplars = DatasetExemplars.exemplars
self.exemplars_activations = DatasetExemplars.activations
self.exempalrs_thresholds = DatasetExemplars.thresholds
self.activation_threshold = 0
self.results_list = []
def text2image(self, prompt_list: List[str]) -> List[torch.Tensor]:
"""Gets a list of text prompt as an input, generates an image for each prompt in the list using a text to image model.
The function returns a list of images.
Parameters
----------
prompt_list : List[str]
A list of text prompts for image generation.
Returns
-------
List[Image.Image]
A list of images, corresponding to each of the input prompts.
Examples
--------
>>> # test the activation value of the neuron for the prompt "a dog standing on the grass"
>>> def execute_command(system, tools) -> Tuple[int, str]:
>>> prompt = ["a dog standing on the grass"]
>>> image = tools.text2image(prompt)
>>> activation_list, activation_map_list = system.call_neuron(image)
>>> return activation_list, activation_map_list
>>> # test the activation value of the neuron for the prompt “a fox and a rabbit watch a movie under a starry night sky” “a fox and a bear watch a movie under a starry night sky” “a fox and a rabbit watch a movie at sunrise”
>>> def execute_command(system.neuron, tools) -> Tuple[int, str]:
>>> prompt_list = [[“a fox and a rabbit watch a movie under a starry night sky”, “a fox and a bear watch a movie under a starry night sky”,“a fox and a rabbit watch a movie at sunrise”]]
>>> images = tools.text2image(prompt_list)
>>> activation_list, activation_map_list = system.call_neuron(images)
>>> return activation_list, activation_map_list
"""
image_list = []
for prompt in prompt_list:
while True:
try:
images = self.prompt2image(prompt)
break
except Exception as e:
print(e)
image_list.append(images)
return image_list
def edit_images(self, image_prompt_list_org : List[Image.Image], editing_instructions_list : List[str], batch_size=32):
"""Gets a list of prompts to generate images, and list of corresponding editing prompts as an input, edits each image based on the instructions given in the prompt using a text-based image editing model.
Important note: Do not use negative terminology such as "remove ...", try to use terminology like "replace ... with ..." or "change the color of ... to"
The function returns a list of images.
Parameters
----------
image_prompt_list_org : List[Image.Image]
A list of input ptompts to generate images according to, these images are to be edited by the prompts in editing_instructions_list.
editing_instructions_list : List[str]
A list of instructions for how to edit the images in image_list. Should be the same length as image_list.
Returns
-------
List[Image.Image], List[str]
A list of images, corresponding to each of the input images and corresponding editing prompts
and a list of all the prompts that were used in the experiment, in the same order as the images
Examples
--------
>>> # test the activation value of the neuron for the prompt "a dog standing on the grass" and test the effect of changing the dog to a cat
>>> def execute_command(system, prompt_list) -> Tuple[int, str]:
>>> prompt = ["a dog standing on the grass"]
>>> edits = ["replace the dog with a cat"]
>>> images, images_edited = edit_images(prompt, edits)
>>> activation_list, activation_map_list = system.call_neuron(images + images_edited)
>>> return activation_list, activation_map_list
"""
image_list = []
for prompt in image_prompt_list_org:
image_list.append(self.prompt2image(prompt, images_per_prompt=1)[0])
image_list = [item for item in image_list if item is not None]
editing_instructions_list = [item for item, condition in zip(editing_instructions_list, image_list) if condition is not None]
image_prompt_list_org = [item for item, condition in zip(image_prompt_list_org, image_list) if condition is not None]
edited_images = self.p2p_model(editing_instructions_list, image_list).images
all_images= []
all_prompt = []
for i in range(len(image_prompt_list_org)*2):
if i%2 == 0:
all_prompt.append(image_prompt_list_org[i//2])
all_images.append([image_list[i//2]])
else:
all_prompt.append(editing_instructions_list[i//2])
all_images.append([edited_images[i//2]])
return all_images, all_prompt
def save_experiment_log(self, activation_list: List[int], image_list: List[str], image_titles: List[str], image_textual_information: List[str] = None):
"""documents the current experiment results as an entry in the experiment log list. if self.activation_threshold was updated by net_dissect function,
the experiment log will contains instruction to continue with experiments if activations are lower than activation_threshold.
Results that are loged will be available for future experiment (unlogged results will be unavailable).
The function also update the attribure "result_list", such that each element in the result_list is a dictionary of the format: {"<prompt>": {"activation": act, "image": image}}
so the list contains all the resilts that were logged so far.
Parameters
----------
activation_list : List[int]
A list of the activation values that were achived for each of the images in "image_list".
image_list : List[str]
A list of the images that were generated using the text2image model and were tested.
image_titles : List[str]
A list of the text prompts that were tested. according to these prompt the images in "image_list" were generated.
additional_information: (Union[str, List[str]])
A string or a list of additional text to log
Returns
-------
None
Examples
--------
>>> # tests the activation value of the neuron for the prompt "a dog standing on the grass" and logs
>>> def execute_command(System.neuron, prompt_list) -> Tuple[int, str]:
>>> prompt = ["a dog standing on the grass"]
>>> activation_list, activation_map_list = tools.text2activation(System.neuron, prompt)
>>> save_experiment_log(prompt, activation_list, activation_map_list)
>>> # tests the activation value of the neuron for the prompts “a fox and a rabbit watch a movie under a starry night sky” “a fox and a bear watch a movie under a starry night sky” “a fox and a rabbit watch a movie at sunrise” and logs all results
>>> def execute_command(System.neuron, prompt_list) -> Tuple[int, str]:
>>> prompt_list = [“a fox and a rabbit watch a movie under a starry night sky”, “a fox and a bear watch a movie under a starry night sky”,“a fox and a rabbit watch a movie at sunrise”]
>>> activation_list, activation_map_list = text2activation(System.neuron, prompt_list)
>>> save_experiment_log(prompt_list, activation_list, activation_map_list)
>>> # tests the activation value of the neuron for the prompts “a fox and a rabbit watch a movie under a starry night sky” “a fox and a bear watch a movie under a starry night sky” “a fox and a rabbit watch a movie at sunrise” and logs the results and the image descriptions
>>> def execute_command(system, tools):
>>> prompt_list = [“a fox and a rabbit watch a movie under a starry night sky”, “a fox and a bear watch a movie under a starry night sky”,“a fox and a rabbit watch a movie at sunrise”]
>>> images = tools.text2image(prompt_list)
>>> activation_list, activation_map_list = system.call_neuron(images)
>>> descriptions = describe_images(images, prompt_list)
>>> save_experiment_log(prompt_list, activation_list, activation_map_list, descriptions)
>>> return
>>> # tests network dissect exemplars and logs the results and the image descriptions
>>> def execute_command(system, tools):
>>> activation_list, image_list = self.net_dissect(system)
>>> prompt_list = []
>>> for i in range(len(activation_list)):
>>> prompt_list.append(f'network dissection, exemplar {i}') # for the network dissection exemplars e don't have prompts, therefore need to provide text titles
>>> descriptions = describe_images(image_list, prompt_list)
>>> save_experiment_log(prompt_list, activation_list, activation_map_list, descriptions)
>>> return
>>> # tests the activation value of the neuron for the prompt “a fox and a rabbit watch a movie under a starry night sky” “a fox and a bear watch a movie under a starry night sky” “a fox and a rabbit watch a movie at sunrise” but only logs the result with the highest activation
>>> def execute_command(System.neuron, prompt_list) -> Tuple[int, str]:
>>> prompt_list = [“a fox and a rabbit watch a movie under a starry night sky”, “a fox and a bear watch a movie under a starry night sky”,“a fox and a rabbit watch a movie at sunrise”]
>>> activation_list, activation_map_list = text2activation(System.neuron, prompt_list)
>>> max_ind = torch.argmax(act).item()
>>> save_experiment_log(prompt_list[max_ind], activation_list[max_ind], activation_map_list[max_ind])
>>> # tests 10 different prompts and logs 5 result with the highest activation
>>> def execute_command(System.neuron, prompt_list) -> Tuple[int, str]:
>>> prompt_list = [“a fox and a rabbit watch a movie under a starry night sky”, “a fox and a bear watch a movie under a starry night sky”,“a fox and a rabbit watch a movie at sunrise”, ...]
>>> activation_list, activation_map_list = text2activation(System.neuron, prompt_list)
>>> sorted_values, indices = torch.sort(activation_list)
>>> save_experiment_log(prompt_list[indices[-5:]], activation_list[indices[-5:]], activation_map_list[indices[-5:]])
>>> # tests 10 different prompts and logs only results that got activations higher than a defined threshold
>>> def execute_command(System.neuron, prompt_list) -> Tuple[int, str]:
>>> prompt_list = [“a fox and a rabbit watch a movie under a starry night sky”, “a fox and a bear watch a movie under a starry night sky”,“a fox and a rabbit watch a movie at sunrise”, ...]
>>> activation_list, activation_map_list = text2activation(System.neuron, prompt_list)
>>> threshold = THRESHOLD #defined by the user
>>> save_experiment_log(prompt_list[activation_list > THRESHOLD], activation_list[activation_list > THRESHOLD], activation_map_list[activation_list > THRESHOLD])
"""
output = [{"type":"text", "text": 'Neuron activations:\n'}]
for ind,act in enumerate(activation_list):
output.append({"type": "text", "text": f'"{image_titles[ind]}", activation: {act}\nimage: \n'})
output.append({"type": "image_url", "image_url": {"url": "data:image/jpeg;base64," + image_list[ind]}})
self.results_list.append({image_titles[ind]: {"activation": act, "image": image_list[ind]}})
if (self.activation_threshold != 0) and (max(activation_list) < self.activation_threshold):
output.append({"type": "text", "text":f"\nMax activation is smaller than {(self.activation_threshold * 100).round()/100}, please continue with the experiments.\n"})
if image_textual_information != None:
if isinstance(image_textual_information, list):
for text in image_textual_information:
output.append({"type": "text", "text": text})
else:
output.append({"type": "text", "text": image_textual_information})
self.update_experiment_log(role='user', content=output)
def update_experiment_log(self, role, content=None, type=None, type_content=None):
openai_role = {'execution':'user','maia':'assistant','user':'user','system':'system'}
if type == None:
self.experiment_log.append({'role': openai_role[role], 'content': content})
elif content == None:
if type == 'text':
self.experiment_log.append({'role': openai_role[role], 'content': [{"type":type, "text": type_content}]})
# self.experiment_log.append({'role': openai_role[role], 'content': {"type":type, "text": type_content}}) #gemini
if type == 'image_url':
self.experiment_log.append({'role': openai_role[role], 'content': [{"type":type, "image_url": type_content}]}) #gemini
# self.experiment_log.append({'role': role, 'content': {"type":type, "image_url": {'url': type_content}}}) #gemini
def dataset_exemplars(self, system):
"""
Retrieves the activation and exemplar image list for a specific neuron in a given layer.
This method accesses stored data for a specified neuron within a layer of the neural network.
It returns both the activation values and the corresponding exemplar images that were used
to generate these activations. The neuron and layer are specified through a 'system' object.
Parameters
----------
system : System
An object representing the specific neuron and layer within the neural network.
The 'system' object should have 'layer' and 'neuron_num' attributes.
Returns
-------
tuple
A tuple containing two elements:
- The first element is a list of activation values for the specified neuron.
- The second element is a list of exemplar images (as Base64 encoded strings or
in the format they were stored) corresponding to these activations.
Example
-------
>>> def execute_command(system, tools)
>>> activation_list, image_list = tools.net_dissect(system_instance)
>>> prompt_list = []
>>> for i in range(len(activation_list)):
>>> prompt_list.append(f'network dissection, exemplar {i}')
>>> save_experiment_log(prompt_list, activation_list, image_list, self.activation_threshold)
"""
image_list = self.exemplars[system.layer][system.neuron_num]
activation_list = self.exemplars_activations[system.layer][system.neuron_num]
self.activation_threshold = sum(activation_list)/len(activation_list)
activation_list = (activation_list * 100).round()/100
return activation_list, image_list
def load_pix2pix_model(self, model_name):
"""
Loads a pix2pix image editing model.
Parameters
----------
model_name : str
The name of the pix2pix model.
Returns
-------
The loaded pix2pix model.
"""
if model_name == "ip2p": # instruction tuned pix2pix model
device = self.device
model_id = "timbrooks/instruct-pix2pix"
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None)
pipe = pipe.to(device)
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
return pipe
else:
raise("unrecognized pix2pix model name")
def load_text2image_model(self,model_name):
"""
Loads a text-to-image model.
Parameters
----------
model_name : str
The name of the text-to-image model.
Returns
-------
The loaded text-to-image model.
"""
if model_name == "sd":
device = self.device
model_id = "runwayml/stable-diffusion-v1-5"
sdpipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
sdpipe = sdpipe.to(device)
return sdpipe
elif model_name == "sdxl-turbo":
device = self.device
model_id = "stabilityai/sdxl-turbo"
pipe = AutoPipelineForText2Image.from_pretrained(model_id, torch_dtype=torch.float16, variant="fp16")
pipe = pipe.to(device)
return pipe
elif model_name == "dalle":
pipe = None
return pipe
else:
raise("unrecognized text to image model name")
def prompt2image(self, prompt, images_per_prompt=None):
if images_per_prompt == None: images_per_prompt = self.images_per_prompt
if self.text2image_model_name == "sd":
if images_per_prompt > 1:
prompts = [prompt] * images_per_prompt
else: prompts = prompt
images = self.text2image_model(prompts).images
elif self.text2image_model_name == "sdxl-turbo":
if images_per_prompt > 1:
prompts = [prompt] * images_per_prompt
else: prompts = prompt
images = self.text2image_model(prompt=prompts, num_inference_steps=4, guidance_scale=0.0).images
elif self.text2image_model_name == "dalle":
if images_per_prompt > 1:
raise("cannot use DALLE with 'images_per_prompt'>1 due to rate limits")
else:
images = []
try:
response = openai.Image.create(prompt=prompt, n=1, size="256x256")
except Exception as e:
print(e)
return images.append(None)
image_url = response["data"][0]["url"]
response = requests.get(image_url)
# Check if the request was successful (status code 200)
image_data = BytesIO(response.content)
image = Image.open(image_data)
images.append(image)
else:
raise("unrecognized text to image model name")
return images
def sampler(self, act, imgs, mask, prompt, threshold, method = 'max'):
if method=='max':
max_ind = torch.argmax(act).item()
masked_image_max = self.generate_masked_image(image=imgs[max_ind],mask=mask[max_ind],path2save=f"{self.path2save}/{prompt}_masked_max.png", threshold=threshold)
acts= max(act).item()
ims = masked_image_max
elif method=='min_max':
max_ind = torch.argmax(act).item()
min_ind = torch.argmin(act).item()
masked_image_max = self.generate_masked_image(image=imgs[max_ind],mask=mask[max_ind],path2save=f"{self.path2save}/{prompt}_masked_max.png", threshold=threshold)
masked_image_min = self.generate_masked_image(image=imgs[min_ind],mask=mask[min_ind],path2save=f"{self.path2save}/{prompt}_masked_min.png", threshold=threshold)
acts = []
ims = []
acts.append(max(act).item())
acts.append(min(act).item())
ims.append(masked_image_max)
ims.append(masked_image_min)
return acts, ims
def summarize_images(self, image_list: List[str]) -> str:
"""
Gets a list of images and describes what is common to all of them, focusing specifically on unmasked regions.
Parameters
----------
image_list : list
A list of images in Base64 encoded string format.
Returns
-------
str
A string with a descriptions of what is common to all the images.
Example
-------
>>> # tests dataset dissect exemplars and logs the results and the image descriptions
>>> def execute_command(system, tools):
>>> activation_list, image_list = self.dataset_dissect(system)
>>> prompt_list = []
>>> for i in range(len(activation_list)):
>>> prompt_list.append(f'network dissection, exemplar {i}') # for the network dissection exemplars e don't have prompts, therefore need to provide text titles
>>> summarization = tools.summarize_images(image_list)
>>> save_experiment_log(prompt_list, activation_list, activation_map_list, summarization)
>>> return
"""
instructions = "What do all the unmasked regions of these images have in common? There might be more then one common concept, or a few groups of images with different common concept each. In these cases return all of the concepts.. Return your description in the following format: [COMMON]: <your description>."
history = [{'role':'system', 'content':'you are an helpful assistant'}]
user_contet = [{"type":"text", "text": instructions}]
for ind,image in enumerate(image_list):
user_contet.append({"type": "image_url", "image_url": "data:image/jpeg;base64," + image})
history.append({'role': 'user', 'content': user_contet})
description = ask_agent('gpt-4-vision-preview',history)
if isinstance(description, Exception): return description
return description
def describe_images(self, image_list: List[str], image_title:List[str]) -> str:
"""
Generates descriptions for a list of images, focusing specifically on highlighted regions.
This function iterates through a list of images, requesting a description for the
highlighted (unmasked) regions in each image. The final descriptions are concatenated
and returned as a single string, with each description associated with the corresponding
image title.
Parameters
----------
image_list : list
A list of images in Base64 encoded string format.
image_title : callable
A function or lambda that takes an index (integer) and returns a corresponding
title (string) for each image.
Returns
-------
str
A concatenated string of descriptions for each image, where each description
is associated with the image's title and focuses on the highlighted regions
in the image.
Example
-------
>>> def execute_command(system, tools):
>>> prompt_list = [“a fox and a rabbit watch a movie under a starry night sky”, “a fox and a bear watch a movie under a starry night sky”,“a fox and a rabbit watch a movie at sunrise”]
>>> images = tools.text2image(prompt_list)
>>> activation_list, activation_map_list = system.call_neuron(images)
>>> descriptions = describe_images(activation_map_list, prompt_list)
>>> return descriptions
>>> def execute_command(system, tools):
>>> activation_list, image_list = self.net_dissect(system)
>>> prompt_list = []
>>> for i in range(len(activation_list)):
>>> prompt_list.append(f'network dissection, exemplar {i}') # for the network dissection exemplars e don't have prompts, therefore need to provide text titles
>>> descriptions = describe_images(image_list, prompt_list)
>>> return descriptions
"""
description_list = ''
instructions = "Do not describe the full image. Please describe ONLY the unmasked regions in this image (e.g. the regions that are not darkened). Be as concise as possible. Return your description in the following format: [highlighted regions]: <your concise description>"
# time.sleep(60)
for ind,image in enumerate(image_list):
history = [{'role':'system', 'content':'you are an helpful assistant'},{'role': 'user', 'content': [{"type":"text", "text": instructions}, {"type": "image_url", "image_url": "data:image/jpeg;base64," + image}]}]
description = ask_agent('gpt-4-vision-preview',history)
if isinstance(description, Exception): return description_list
description = description.split("[highlighted regions]:")[-1]
description = " ".join([f'"{image_title[ind]}", highlighted regions:',description])
description_list += description + '\n'
return description_list
def generate_html(self,name="experiment.html"):
# Generates an HTML file with the experiment log.
html_string = f'''<html>
<head>
<title>Experiment Log</title>
<!-- Include Prism Core CSS (Choose the theme you prefer) -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/themes/prism.min.css" rel="stylesheet" />
<!-- Include Prism Core JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/prism.min.js"></script>
<!-- Include the Python language component for Prism -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/components/prism-python.min.js"></script>
</head>
<body>
<h1>{self.path2save}</h1>'''
# don't plot system+user prompts (uncomment if you want the html to include the system+user prompts)
'''
html_string += f"<h2>{self.experiment_log[0]['role']}</h2>"
html_string += f"<pre><code>{self.experiment_log[0]['content']}</code></pre><br>"
html_string += f"<h2>{self.experiment_log[1]['role']}</h2>"
html_string += f"<pre>{self.experiment_log[1]['content'][0]}</pre><br>"
initial_images = ''
initial_activations = ''
for cont in self.experiment_log[1]['content'][1:]:
if isinstance(cont, dict):
initial_images += f"<img src="data:image/png;base64,{cont['image']}"/>"
else:
initial_activations += f"{cont} "
html_string += initial_images
html_string += f"<p>Activations:</p>"
html_string += initial_activations
'''
for entry in self.experiment_log[2:]:
if entry['role'] == 'assistant':
html_string += f"<h2>MAIA</h2>"
html_string += f"<pre>{entry['content'][0]['text']}</pre><br>"
else:
html_string += f"<h2>Experiment Execution</h2>"
for content_entry in entry['content']:
if "image_url" in content_entry["type"]:
html_string += f'''<img src="{content_entry['image_url']['url']}"/>'''
elif "text" in content_entry["type"]:
html_string += f"<pre>{content_entry['text']}</pre>"
html_string += '</body></html>'
file_html = open(os.path.join(self.path2save, name), "w")
file_html.write(html_string)
file_html.close()
def generate_masked_image(image,mask,path2save,threshold):
#Generates a masked image highlighting high activation areas.
vis = ImageVisualizer(224, image_size=224, source='imagenet')
masked_tensor = vis.pytorch_masked_image(image, activations=mask, unit=None, level=threshold, outside_bright=0.25) #percent_level=0.95)
masked_image = Image.fromarray(masked_tensor.permute(1, 2, 0).byte().cpu().numpy())
buffer = BytesIO()
masked_image.save(buffer, format="PNG")
buffer.seek(0)
masked_image = base64.b64encode(buffer.read()).decode('ascii')
return(masked_image)
def image2str(image,path2save):
#Converts an image to a Base64 encoded string.
buffer = BytesIO()
image.save(buffer, format="PNG")
buffer.seek(0)
image = base64.b64encode(buffer.read()).decode('ascii')
return(image)
def str2image(image_str):
#Converts a Base64 encoded string to an image.
img_bytes = base64.b64decode(image_str)
img_buffer = BytesIO(img_bytes)
img = Image.open(img_buffer)
return img
class DatasetExemplars():
"""
A class for performing network dissection on a given neural network model.
This class analyzes specific layers and units of a neural network model to
understand what each unit in a layer has learned. It uses a set of exemplar
images to activate the units and stores the resulting activations, along with
visualizations of the activated regions in the images.
Attributes
----------
path2exemplars : str
Path to the directory containing the exemplar images.
n_exemplars : int
Number of exemplar images to use for each unit.
path2save : str
Path to the directory where the results will be saved.
model_name : str
Name of the neural network model being dissected.
layers : list
List of layer names in the model to be dissected.
units : list
List of unit indices to be analyzed. If None, all units are analyzed.
im_size : int, optional
Size to which the images will be resized (default is 224).
Methods
-------
net_dissect(layer: str, im_size: int=224)
Dissects the specified layer of the neural network, analyzing the response
to the exemplar images and saving visualizations of activated regions.
"""
def __init__(self, path2exemplars, path2save, model_name, layers, units, n_exemplars = 15, im_size=224):
"""
Constructs all the necessary attributes for the DatasetExemplars object.
Parameters
----------
path2exemplars : str
Path to the directory containing the exemplar images.
n_exemplars : int
Number of exemplar images to use for each unit.
path2save : str
Path to the directory where the results will be saved.