File size: 10,698 Bytes
55866f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
    This is just a wrapper around the various baselines implemented in the 
    Chefer et. al. Transformer Explainability repository. 

    Implements
    - CheferLRPSegmentationModel
    - CheferRolloutSegmentationModel
    - CheferLastLayerAttentionSegmentationModel
    - CheferAttentionGradCAMSegmentationModel
    - CheferTransformerAttributionSegmentationModel
    - CheferFullLRPSegmentationModel
    - CheferLastLayerLRPSegmentationModel
"""

#  # segmentation test for the rollout baseline
#     if args.method == 'rollout':
#         Res = baselines.generate_rollout(image.cuda(), start_layer=1).reshape(batch_size, 1, 14, 14)
    
#     # segmentation test for the LRP baseline (this is full LRP, not partial)
#     elif args.method == 'full_lrp':
#         Res = orig_lrp.generate_LRP(image.cuda(), method="full").reshape(batch_size, 1, 224, 224)
    
#     # segmentation test for our method
#     elif args.method == 'transformer_attribution':
#         Res = lrp.generate_LRP(image.cuda(), start_layer=1, method="transformer_attribution").reshape(batch_size, 1, 14, 14)
    
#     # segmentation test for the partial LRP baseline (last attn layer)
#     elif args.method == 'lrp_last_layer':
#         Res = orig_lrp.generate_LRP(image.cuda(), method="last_layer", is_ablation=args.is_ablation)\
#             .reshape(batch_size, 1, 14, 14)
    
#     # segmentation test for the raw attention baseline (last attn layer)
#     elif args.method == 'attn_last_layer':
#         Res = orig_lrp.generate_LRP(image.cuda(), method="last_layer_attn", is_ablation=args.is_ablation)\
#             .reshape(batch_size, 1, 14, 14)
    
#     # segmentation test for the GradCam baseline (last attn layer)
#     elif args.method == 'attn_gradcam':
#         Res = baselines.generate_cam_attn(image.cuda()).reshape(batch_size, 1, 14, 14)

#     if args.method != 'full_lrp':
#         # interpolate to full image size (224,224)
#         Res = torch.nn.functional.interpolate(Res, scale_factor=16, mode='bilinear').cuda()

import torch
import PIL

from concept_attention.binary_segmentation_baselines.chefer_vit_explainability.ViT_explanation_generator import LRP
from concept_attention.segmentation import SegmentationAbstractClass
from concept_attention.binary_segmentation_baselines.chefer_vit_explainability.ViT_explanation_generator import Baselines, LRP
from concept_attention.binary_segmentation_baselines.chefer_vit_explainability.ViT_new import vit_base_patch16_224
from concept_attention.binary_segmentation_baselines.chefer_vit_explainability.ViT_LRP import vit_base_patch16_224 as vit_LRP
from concept_attention.binary_segmentation_baselines.chefer_vit_explainability.ViT_orig_LRP import vit_base_patch16_224 as vit_orig_LRP


# # Model
# model = vit_base_patch16_224(pretrained=True).cuda()
# baselines = Baselines(model)

# # LRP
# model_LRP = vit_LRP(pretrained=True).cuda()
# model_LRP.eval()
# lrp = LRP(model_LRP)

# # orig LRP
# model_orig_LRP = vit_orig_LRP(pretrained=True).cuda()
# model_orig_LRP.eval()
# orig_lrp = LRP(model_orig_LRP)

# model.eval()

class CheferLRPSegmentationModel(SegmentationAbstractClass):

    def __init__(
        self,
        device: str = "cuda",
        width: int = 224,
        height: int = 224,
    ):
        """
            Initialize the segmentation model.
        """
        super(CheferLRPSegmentationModel, self).__init__()
        self.width = width
        self.height = height
        self.device = device
        # Load the LRP model
        model_orig_LRP = vit_orig_LRP(pretrained=True).to(self.device)
        model_orig_LRP.eval()
        self.orig_lrp = LRP(model_orig_LRP)

    def segment_individual_image(self, image: torch.Tensor, concepts: list[str], caption: str, **kwargs):
        """
            Takes a real image and generates a concept segmentation map
            it by adding noise and running the DiT on it. 
        """
        if len(image.shape) == 3:
            image = image.unsqueeze(0)
            
        prediction_map = self.orig_lrp.generate_LRP(
            image.to(self.device), 
            method="full"
        )
        prediction_map = prediction_map.unsqueeze(0)
        # Rescale the prediction map to 64x64
        prediction_map = torch.nn.functional.interpolate(
            prediction_map, 
            size=(self.width, self.height), 
            mode="nearest"
        ).reshape(1, self.width, self.height)

        return prediction_map, None

class CheferRolloutSegmentationModel(SegmentationAbstractClass):

    def __init__(self, device: str = "cuda", width: int = 224, height: int = 224):
        super(CheferRolloutSegmentationModel, self).__init__()
        self.width = width
        self.height = height
        self.device = device
        model = vit_base_patch16_224(pretrained=True).to(device)
        self.baselines = Baselines(model)

    def segment_individual_image(self, image: torch.Tensor, concepts: list[str], caption: str, **kwargs):
        if len(image.shape) == 3:
            image = image.unsqueeze(0)
        prediction_map = self.baselines.generate_rollout(
            image.to(self.device), start_layer=1
        ).reshape(1, 1, 14, 14)
        # Rescale the prediction map to 64x64
        prediction_map = torch.nn.functional.interpolate(
            prediction_map, 
            size=(self.width, self.height), 
            mode="nearest"
        ).reshape(1, self.width, self.height)

        return prediction_map, None


class CheferLastLayerAttentionSegmentationModel(SegmentationAbstractClass):

    def __init__(self, device: str = "cuda", width: int = 224, height: int = 224):
        super(CheferLastLayerAttentionSegmentationModel, self).__init__()
        self.width = width
        self.height = height
        self.device = device
        model_orig_LRP = vit_orig_LRP(pretrained=True).to(device)
        model_orig_LRP.eval()
        self.orig_lrp = LRP(model_orig_LRP)

    def segment_individual_image(self, image: torch.Tensor, concepts: list[str], caption: str, **kwargs):
        if len(image.shape) == 3:
            image = image.unsqueeze(0)

        prediction_map = self.orig_lrp.generate_LRP(
            image.to(self.device), method="last_layer_attn"
        ).reshape(1, 1, 14, 14)
        # Rescale the prediction map to 64x64
        prediction_map = torch.nn.functional.interpolate(
            prediction_map, 
            size=(self.width, self.height), 
            mode="nearest"
        ).reshape(1, self.width, self.height)
        
        return prediction_map, None


class CheferAttentionGradCAMSegmentationModel(SegmentationAbstractClass):

    def __init__(self, device: str = "cuda", width: int = 224, height: int = 224):
        super(CheferAttentionGradCAMSegmentationModel, self).__init__()
        self.width = width
        self.height = height
        self.device = device
        model = vit_base_patch16_224(pretrained=True).to(device)
        self.baselines = Baselines(model)

    def segment_individual_image(self, image: torch.Tensor, concepts: list[str], caption: str, **kwargs):
        if len(image.shape) == 3:
            image = image.unsqueeze(0)
        prediction_map = self.baselines.generate_cam_attn(
            image.to(self.device)
        ).reshape(1, 1, 14, 14)
        # Rescale the prediction map to 64x64
        prediction_map = torch.nn.functional.interpolate(
            prediction_map, 
            size=(self.width, self.height), 
            mode="nearest"
        ).reshape(1, self.width, self.height)

        return prediction_map, None


class CheferTransformerAttributionSegmentationModel(SegmentationAbstractClass):

    def __init__(self, device: str = "cuda", width: int = 224, height: int = 224):
        super(CheferTransformerAttributionSegmentationModel, self).__init__()
        self.width = width
        self.height = height
        self.device = device
        model_LRP = vit_LRP(pretrained=True).to(device)
        model_LRP.eval()
        self.lrp = LRP(model_LRP)

    def segment_individual_image(self, image: torch.Tensor, concepts: list[str], caption: str, **kwargs):
        if len(image.shape) == 3:
            image = image.unsqueeze(0)
        prediction_map = self.lrp.generate_LRP(
            image.to(self.device), start_layer=1, method="transformer_attribution"
        ).reshape(1, 1, 14, 14)
        # Rescale the prediction map to 64x64
        prediction_map = torch.nn.functional.interpolate(
            prediction_map, 
            size=(self.width, self.height), 
            mode="nearest"
        ).reshape(1, self.width, self.height)

        return prediction_map, None


class CheferFullLRPSegmentationModel(SegmentationAbstractClass):

    def __init__(self, device: str = "cuda", width: int = 224, height: int = 224):
        super(CheferFullLRPSegmentationModel, self).__init__()
        self.width = width
        self.height = height
        self.device = device
        model_LRP = vit_LRP(pretrained=True).to(device)
        model_LRP.eval()
        self.lrp = LRP(model_LRP)

    def segment_individual_image(self, image: torch.Tensor, concepts: list[str], caption: str, **kwargs):
        if len(image.shape) == 3:
            image = image.unsqueeze(0)
        prediction_map = self.lrp.generate_LRP(
            image.to(self.device), method="full"
        ).reshape(1, 1, 224, 224)
        # Rescale the prediction map to 64x64
        prediction_map = torch.nn.functional.interpolate(
            prediction_map, 
            size=(self.width, self.height), 
            mode="nearest"
        ).reshape(1, self.width, self.height)

        return prediction_map, None


class CheferLastLayerLRPSegmentationModel(SegmentationAbstractClass):

    def __init__(self, device: str = "cuda", width: int = 224, height: int = 224):
        super(CheferLastLayerLRPSegmentationModel, self).__init__()
        self.width = width
        self.height = height
        self.device = device
        model_LRP = vit_LRP(pretrained=True).to(device)
        model_LRP.eval()
        self.lrp = LRP(model_LRP)

    def segment_individual_image(self, image: torch.Tensor, concepts: list[str], caption: str, **kwargs):
        if len(image.shape) == 3:
            image = image.unsqueeze(0)
        prediction_map = self.lrp.generate_LRP(
            image.to(self.device), method="last_layer"
        ).reshape(1, 1, 14, 14)
        # Rescale the prediction map to 64x64
        prediction_map = torch.nn.functional.interpolate(
            prediction_map, 
            size=(self.width, self.height), 
            mode="nearest"
        ).reshape(1, self.width, self.height)

        return prediction_map, None