Spaces:
Sleeping
Sleeping
File size: 28,495 Bytes
08d7644 79059eb 08d7644 d5812e2 08d7644 79059eb 08d7644 |
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 |
import numpy as np
import torch
import copy
def compute_rollout_attention(all_layer_matrices, start_layer=0):
# adding residual consideration
num_tokens = all_layer_matrices[0].shape[1]
eye = torch.eye(num_tokens).to(all_layer_matrices[0].device)
all_layer_matrices = [all_layer_matrices[i] + eye for i in range(len(all_layer_matrices))]
matrices_aug = [all_layer_matrices[i] / all_layer_matrices[i].sum(dim=-1, keepdim=True)
for i in range(len(all_layer_matrices))]
joint_attention = matrices_aug[start_layer]
for i in range(start_layer + 1, len(matrices_aug)):
joint_attention = matrices_aug[i].matmul(joint_attention)
return joint_attention
# rule 5 from paper
def avg_heads(cam, grad):
cam = cam.reshape(-1, cam.shape[-2], cam.shape[-1])
grad = grad.reshape(-1, grad.shape[-2], grad.shape[-1])
cam = grad * cam
cam = cam.clamp(min=0).mean(dim=0)
return cam
# rules 6 + 7 from paper
def apply_self_attention_rules(R_ss, R_sq, cam_ss):
R_sq_addition = torch.matmul(cam_ss, R_sq)
R_ss_addition = torch.matmul(cam_ss, R_ss)
return R_ss_addition, R_sq_addition
# rules 10 + 11 from paper
def apply_mm_attention_rules(R_ss, R_qq, R_qs, cam_sq, apply_normalization=True, apply_self_in_rule_10=True):
R_ss_normalized = R_ss
R_qq_normalized = R_qq
if apply_normalization:
R_ss_normalized = handle_residual(R_ss)
R_qq_normalized = handle_residual(R_qq)
R_sq_addition = torch.matmul(R_ss_normalized.t(), torch.matmul(cam_sq, R_qq_normalized))
if not apply_self_in_rule_10:
R_sq_addition = cam_sq
R_ss_addition = torch.matmul(cam_sq, R_qs)
return R_sq_addition, R_ss_addition
# normalization- eq. 8+9
def handle_residual(orig_self_attention):
self_attention = orig_self_attention.clone()
diag_idx = range(self_attention.shape[-1])
# computing R hat
self_attention -= torch.eye(self_attention.shape[-1]).to(self_attention.device)
assert self_attention[diag_idx, diag_idx].min() >= 0
# normalizing R hat
self_attention = self_attention / self_attention.sum(dim=-1, keepdim=True)
self_attention += torch.eye(self_attention.shape[-1]).to(self_attention.device)
return self_attention
class GeneratorOurs:
def __init__(self, model_usage, save_visualization=False):
self.model_usage = model_usage
self.save_visualization = save_visualization
def handle_self_attention_lang(self, blocks):
for blk in blocks:
grad = blk.attention.self.get_attn_gradients().detach()
if self.use_lrp:
cam = blk.attention.self.get_attn_cam().detach()
else:
cam = blk.attention.self.get_attn().detach()
cam = avg_heads(cam, grad)
R_t_t_add, R_t_i_add = apply_self_attention_rules(self.R_t_t, self.R_t_i, cam)
self.R_t_t += R_t_t_add
self.R_t_i += R_t_i_add
def handle_self_attention_image(self, blocks):
for blk in blocks:
grad = blk.attention.self.get_attn_gradients().detach()
if self.use_lrp:
cam = blk.attention.self.get_attn_cam().detach()
else:
cam = blk.attention.self.get_attn().detach()
cam = avg_heads(cam, grad)
R_i_i_add, R_i_t_add = apply_self_attention_rules(self.R_i_i, self.R_i_t, cam)
self.R_i_i += R_i_i_add
self.R_i_t += R_i_t_add
def handle_co_attn_self_lang(self, block):
grad = block.lang_self_att.self.get_attn_gradients().detach()
if self.use_lrp:
cam = block.lang_self_att.self.get_attn_cam().detach()
else:
cam = block.lang_self_att.self.get_attn().detach()
cam = avg_heads(cam, grad)
R_t_t_add, R_t_i_add = apply_self_attention_rules(self.R_t_t, self.R_t_i, cam)
self.R_t_t += R_t_t_add
self.R_t_i += R_t_i_add
def handle_co_attn_self_image(self, block):
grad = block.visn_self_att.self.get_attn_gradients().detach()
if self.use_lrp:
cam = block.visn_self_att.self.get_attn_cam().detach()
else:
cam = block.visn_self_att.self.get_attn().detach()
cam = avg_heads(cam, grad)
R_i_i_add, R_i_t_add = apply_self_attention_rules(self.R_i_i, self.R_i_t, cam)
self.R_i_i += R_i_i_add
self.R_i_t += R_i_t_add
def handle_co_attn_lang(self, block):
if self.use_lrp:
cam_t_i = block.visual_attention.att.get_attn_cam().detach()
else:
cam_t_i = block.visual_attention.att.get_attn().detach()
grad_t_i = block.visual_attention.att.get_attn_gradients().detach()
cam_t_i = avg_heads(cam_t_i, grad_t_i)
R_t_i_addition, R_t_t_addition = apply_mm_attention_rules(self.R_t_t, self.R_i_i, self.R_i_t, cam_t_i,
apply_normalization=self.normalize_self_attention,
apply_self_in_rule_10=self.apply_self_in_rule_10)
return R_t_i_addition, R_t_t_addition
def handle_co_attn_image(self, block):
if self.use_lrp:
cam_i_t = block.visual_attention_copy.att.get_attn_cam().detach()
else:
cam_i_t = block.visual_attention_copy.att.get_attn().detach()
grad_i_t = block.visual_attention_copy.att.get_attn_gradients().detach()
cam_i_t = avg_heads(cam_i_t, grad_i_t)
R_i_t_addition, R_i_i_addition = apply_mm_attention_rules(self.R_i_i, self.R_t_t, self.R_t_i, cam_i_t,
apply_normalization=self.normalize_self_attention,
apply_self_in_rule_10=self.apply_self_in_rule_10)
return R_i_t_addition, R_i_i_addition
def generate_ours(self, input, index=None, use_lrp=True, normalize_self_attention=True, apply_self_in_rule_10=True,
method_name="ours"):
self.use_lrp = use_lrp
self.normalize_self_attention = normalize_self_attention
self.apply_self_in_rule_10 = apply_self_in_rule_10
kwargs = {"alpha": 1}
output = self.model_usage.forward(input).question_answering_score
model = self.model_usage.model
# initialize relevancy matrices
text_tokens = self.model_usage.text_len
image_bboxes = self.model_usage.image_boxes_len
# text self attention matrix
self.R_t_t = torch.eye(text_tokens, text_tokens).to(model.device)
# image self attention matrix
self.R_i_i = torch.eye(image_bboxes, image_bboxes).to(model.device)
# impact of images on text
self.R_t_i = torch.zeros(text_tokens, image_bboxes).to(model.device)
# impact of text on images
self.R_i_t = torch.zeros(image_bboxes, text_tokens).to(model.device)
if index is None:
index = np.argmax(output.cpu().data.numpy(), axis=-1)
one_hot = np.zeros((1, output.size()[-1]), dtype=np.float32)
one_hot[0, index] = 1
one_hot_vector = one_hot
one_hot = torch.from_numpy(one_hot).requires_grad_(True)
one_hot = torch.sum(one_hot * output)
model.zero_grad()
one_hot.backward(retain_graph=True)
if self.use_lrp:
model.relprop(torch.tensor(one_hot_vector).to(output.device), **kwargs)
# language self attention
blocks = model.lxmert.encoder.layer
self.handle_self_attention_lang(blocks)
# image self attention
blocks = model.lxmert.encoder.r_layers
self.handle_self_attention_image(blocks)
# cross attn layers
blocks = model.lxmert.encoder.x_layers
for i, blk in enumerate(blocks):
# in the last cross attention module, only the text cross modal
# attention has an impact on the CLS token, since it's the first
# token in the language tokens
if i == len(blocks) - 1:
break
# cross attn- first for language then for image
R_t_i_addition, R_t_t_addition = self.handle_co_attn_lang(blk)
R_i_t_addition, R_i_i_addition = self.handle_co_attn_image(blk)
self.R_t_i += R_t_i_addition
self.R_t_t += R_t_t_addition
self.R_i_t += R_i_t_addition
self.R_i_i += R_i_i_addition
# language self attention
self.handle_co_attn_self_lang(blk)
# image self attention
self.handle_co_attn_self_image(blk)
# take care of last cross attention layer- only text
blk = model.lxmert.encoder.x_layers[-1]
# cross attn- first for language then for image
R_t_i_addition, R_t_t_addition = self.handle_co_attn_lang(blk)
self.R_t_i += R_t_i_addition
self.R_t_t += R_t_t_addition
# language self attention
self.handle_co_attn_self_lang(blk)
# disregard the [CLS] token itself
self.R_t_t[0, 0] = 0
return self.R_t_t, self.R_t_i
class GeneratorOursAblationNoAggregation:
def __init__(self, model_usage, save_visualization=False):
self.model_usage = model_usage
self.save_visualization = save_visualization
def handle_self_attention_lang(self, blocks):
for blk in blocks:
grad = blk.attention.self.get_attn_gradients().detach()
if self.use_lrp:
cam = blk.attention.self.get_attn_cam().detach()
else:
cam = blk.attention.self.get_attn().detach()
cam = avg_heads(cam, grad)
R_t_t_add, R_t_i_add = apply_self_attention_rules(self.R_t_t, self.R_t_i, cam)
self.R_t_t = R_t_t_add
self.R_t_i = R_t_i_add
def handle_self_attention_image(self, blocks):
for blk in blocks:
grad = blk.attention.self.get_attn_gradients().detach()
if self.use_lrp:
cam = blk.attention.self.get_attn_cam().detach()
else:
cam = blk.attention.self.get_attn().detach()
cam = avg_heads(cam, grad)
R_i_i_add, R_i_t_add = apply_self_attention_rules(self.R_i_i, self.R_i_t, cam)
self.R_i_i = R_i_i_add
self.R_i_t = R_i_t_add
def handle_co_attn_self_lang(self, block):
grad = block.lang_self_att.self.get_attn_gradients().detach()
if self.use_lrp:
cam = block.lang_self_att.self.get_attn_cam().detach()
else:
cam = block.lang_self_att.self.get_attn().detach()
cam = avg_heads(cam, grad)
R_t_t_add, R_t_i_add = apply_self_attention_rules(self.R_t_t, self.R_t_i, cam)
self.R_t_t = R_t_t_add
self.R_t_i = R_t_i_add
def handle_co_attn_self_image(self, block):
grad = block.visn_self_att.self.get_attn_gradients().detach()
if self.use_lrp:
cam = block.visn_self_att.self.get_attn_cam().detach()
else:
cam = block.visn_self_att.self.get_attn().detach()
cam = avg_heads(cam, grad)
R_i_i_add, R_i_t_add = apply_self_attention_rules(self.R_i_i, self.R_i_t, cam)
self.R_i_i = R_i_i_add
self.R_i_t = R_i_t_add
def handle_co_attn_lang(self, block):
if self.use_lrp:
cam_t_i = block.visual_attention.att.get_attn_cam().detach()
else:
cam_t_i = block.visual_attention.att.get_attn().detach()
grad_t_i = block.visual_attention.att.get_attn_gradients().detach()
cam_t_i = avg_heads(cam_t_i, grad_t_i)
R_t_i_addition, R_t_t_addition = apply_mm_attention_rules(self.R_t_t, self.R_i_i, self.R_i_t, cam_t_i,
apply_normalization=self.normalize_self_attention)
return R_t_i_addition, R_t_t_addition
def handle_co_attn_image(self, block):
if self.use_lrp:
cam_i_t = block.visual_attention_copy.att.get_attn_cam().detach()
else:
cam_i_t = block.visual_attention_copy.att.get_attn().detach()
grad_i_t = block.visual_attention_copy.att.get_attn_gradients().detach()
cam_i_t = avg_heads(cam_i_t, grad_i_t)
R_i_t_addition, R_i_i_addition = apply_mm_attention_rules(self.R_i_i, self.R_t_t, self.R_t_i, cam_i_t,
apply_normalization=self.normalize_self_attention)
return R_i_t_addition, R_i_i_addition
def generate_ours_no_agg(self, input, index=None, use_lrp=False, normalize_self_attention=True,
method_name="ours_no_agg"):
self.use_lrp = use_lrp
self.normalize_self_attention = normalize_self_attention
kwargs = {"alpha": 1}
output = self.model_usage.forward(input).question_answering_score
model = self.model_usage.model
# initialize relevancy matrices
text_tokens = self.model_usage.text_len
image_bboxes = self.model_usage.image_boxes_len
# text self attention matrix
self.R_t_t = torch.eye(text_tokens, text_tokens).to(model.device)
# image self attention matrix
self.R_i_i = torch.eye(image_bboxes, image_bboxes).to(model.device)
# impact of images on text
self.R_t_i = torch.zeros(text_tokens, image_bboxes).to(model.device)
# impact of text on images
self.R_i_t = torch.zeros(image_bboxes, text_tokens).to(model.device)
if index is None:
index = np.argmax(output.cpu().data.numpy(), axis=-1)
one_hot = np.zeros((1, output.size()[-1]), dtype=np.float32)
one_hot[0, index] = 1
one_hot_vector = one_hot
one_hot = torch.from_numpy(one_hot).requires_grad_(True)
one_hot = torch.sum(one_hot * output)
model.zero_grad()
one_hot.backward(retain_graph=True)
if self.use_lrp:
model.relprop(torch.tensor(one_hot_vector).to(output.device), **kwargs)
# language self attention
blocks = model.lxmert.encoder.layer
self.handle_self_attention_lang(blocks)
# image self attention
blocks = model.lxmert.encoder.r_layers
self.handle_self_attention_image(blocks)
# cross attn layers
blocks = model.lxmert.encoder.x_layers
for i, blk in enumerate(blocks):
# in the last cross attention module, only the text cross modal
# attention has an impact on the CLS token, since it's the first
# token in the language tokens
if i == len(blocks) - 1:
break
# cross attn- first for language then for image
R_t_i_addition, R_t_t_addition = self.handle_co_attn_lang(blk)
R_i_t_addition, R_i_i_addition = self.handle_co_attn_image(blk)
self.R_t_i = R_t_i_addition
self.R_t_t = R_t_t_addition
self.R_i_t = R_i_t_addition
self.R_i_i = R_i_i_addition
# language self attention
self.handle_co_attn_self_lang(blk)
# image self attention
self.handle_co_attn_self_image(blk)
# take care of last cross attention layer- only text
blk = model.lxmert.encoder.x_layers[-1]
# cross attn- first for language then for image
R_t_i_addition, R_t_t_addition = self.handle_co_attn_lang(blk)
self.R_t_i = R_t_i_addition
self.R_t_t = R_t_t_addition
# language self attention
self.handle_co_attn_self_lang(blk)
# disregard the [CLS] token itself
self.R_t_t[0, 0] = 0
return self.R_t_t, self.R_t_i
class GeneratorBaselines:
def __init__(self, model_usage, save_visualization=False):
self.model_usage = model_usage
self.save_visualization = save_visualization
def generate_transformer_attr(self, input, index=None, method_name="transformer_attr"):
kwargs = {"alpha": 1}
output = self.model_usage.forward(input).question_answering_score
model = self.model_usage.model
# initialize relevancy matrices
text_tokens = self.model_usage.text_len
image_bboxes = self.model_usage.image_boxes_len
# text self attention matrix
self.R_t_t = torch.eye(text_tokens, text_tokens).to(model.device)
# image self attention matrix
self.R_i_i = torch.eye(image_bboxes, image_bboxes).to(model.device)
# impact of images on text
self.R_t_i = torch.zeros(text_tokens, image_bboxes).to(model.device)
# impact of text on images
self.R_i_t = torch.zeros(image_bboxes, text_tokens).to(model.device)
if index == None:
index = np.argmax(output.cpu().data.numpy(), axis=-1)
one_hot = np.zeros((1, output.size()[-1]), dtype=np.float32)
one_hot[0, index] = 1
one_hot_vector = one_hot
one_hot = torch.from_numpy(one_hot).requires_grad_(True)
one_hot = torch.sum(one_hot * output)
model.zero_grad()
one_hot.backward(retain_graph=True)
model.relprop(torch.tensor(one_hot_vector).to(output.device), **kwargs)
# language self attention
blocks = model.lxmert.encoder.layer
for blk in blocks:
grad = blk.attention.self.get_attn_gradients().detach()
cam = blk.attention.self.get_attn_cam().detach()
cam = avg_heads(cam, grad)
self.R_t_t += torch.matmul(cam, self.R_t_t)
# image self attention
blocks = model.lxmert.encoder.r_layers
for blk in blocks:
grad = blk.attention.self.get_attn_gradients().detach()
cam = blk.attention.self.get_attn_cam().detach()
cam = avg_heads(cam, grad)
self.R_i_i += torch.matmul(cam, self.R_i_i)
# cross attn layers
blocks = model.lxmert.encoder.x_layers
for i, blk in enumerate(blocks):
# in the last cross attention module, only the text cross modal
# attention has an impact on the CLS token, since it's the first
# token in the language tokens
if i == len(blocks) - 1:
break
# language self attention
grad = blk.lang_self_att.self.get_attn_gradients().detach()
cam = blk.lang_self_att.self.get_attn_cam().detach()
cam = avg_heads(cam, grad)
self.R_t_t += torch.matmul(cam, self.R_t_t)
# image self attention
grad = blk.visn_self_att.self.get_attn_gradients().detach()
cam = blk.visn_self_att.self.get_attn_cam().detach()
cam = avg_heads(cam, grad)
self.R_i_i += torch.matmul(cam, self.R_i_i)
# take care of last cross attention layer- only text
blk = model.lxmert.encoder.x_layers[-1]
# cross attn cam will be the one used for the R_t_i matrix
cam_t_i = blk.visual_attention.att.get_attn_cam().detach()
grad_t_i = blk.visual_attention.att.get_attn_gradients().detach()
cam_t_i = avg_heads(cam_t_i, grad_t_i)
# self.R_t_i = torch.matmul(self.R_t_t.t(), torch.matmul(cam_t_i, self.R_i_i))
self.R_t_i = cam_t_i
# language self attention
grad = blk.lang_self_att.self.get_attn_gradients().detach()
cam = blk.lang_self_att.self.get_attn_cam().detach()
cam = avg_heads(cam, grad)
self.R_t_t += torch.matmul(cam, self.R_t_t)
self.R_t_t[0, 0] = 0
return self.R_t_t, self.R_t_i
def generate_partial_lrp(self, input, index=None, method_name="partial_lrp"):
kwargs = {"alpha": 1}
output = self.model_usage.forward(input).question_answering_score
model = self.model_usage.model
# initialize relevancy matrices
text_tokens = self.model_usage.text_len
image_bboxes = self.model_usage.image_boxes_len
# text self attention matrix
self.R_t_t = torch.zeros(text_tokens, text_tokens).to(model.device)
# image self attention matrix
self.R_i_i = torch.zeros(image_bboxes, image_bboxes).to(model.device)
# impact of images on text
self.R_t_i = torch.zeros(text_tokens, image_bboxes).to(model.device)
# impact of text on images
self.R_i_t = torch.zeros(image_bboxes, text_tokens).to(model.device)
if index == None:
index = np.argmax(output.cpu().data.numpy(), axis=-1)
one_hot = np.zeros((1, output.size()[-1]), dtype=np.float32)
one_hot[0, index] = 1
one_hot_vector = one_hot
model.relprop(torch.tensor(one_hot_vector).to(output.device), **kwargs)
# last cross attention + self- attention layer
blk = model.lxmert.encoder.x_layers[-1]
# cross attn cam will be the one used for the R_t_i matrix
cam_t_i = blk.visual_attention.att.get_attn_cam().detach()
cam_t_i = cam_t_i.reshape(-1, cam_t_i.shape[-2], cam_t_i.shape[-1]).mean(dim=0)
self.R_t_i = cam_t_i
# language self attention
cam = blk.lang_self_att.self.get_attn_cam().detach()
cam = cam.reshape(-1, cam.shape[-2], cam.shape[-1]).mean(dim=0)
self.R_t_t = cam
# normalize to get non-negative cams
self.R_t_t = (self.R_t_t - self.R_t_t.min()) / (self.R_t_t.max() - self.R_t_t.min())
self.R_t_i = (self.R_t_i - self.R_t_i.min()) / (self.R_t_i.max() - self.R_t_i.min())
# disregard the [CLS] token itself
self.R_t_t[0, 0] = 0
return self.R_t_t, self.R_t_i
def generate_raw_attn(self, input, method_name="raw_attention"):
output = self.model_usage.forward(input).question_answering_score
model = self.model_usage.model
# initialize relevancy matrices
text_tokens = self.model_usage.text_len
image_bboxes = self.model_usage.image_boxes_len
# text self attention matrix
self.R_t_t = torch.zeros(text_tokens, text_tokens).to(model.device)
# image self attention matrix
self.R_i_i = torch.zeros(image_bboxes, image_bboxes).to(model.device)
# impact of images on text
self.R_t_i = torch.zeros(text_tokens, image_bboxes).to(model.device)
# impact of text on images
self.R_i_t = torch.zeros(image_bboxes, text_tokens).to(model.device)
# last cross attention + self- attention layer
blk = model.lxmert.encoder.x_layers[-1]
# cross attn cam will be the one used for the R_t_i matrix
cam_t_i = blk.visual_attention.att.get_attn().detach()
cam_t_i = cam_t_i.reshape(-1, cam_t_i.shape[-2], cam_t_i.shape[-1]).mean(dim=0)
# self.R_t_i = torch.matmul(self.R_t_t.t(), torch.matmul(cam_t_i, self.R_i_i))
self.R_t_i = cam_t_i
# language self attention
cam = blk.lang_self_att.self.get_attn().detach()
cam = cam.reshape(-1, cam.shape[-2], cam.shape[-1]).mean(dim=0)
self.R_t_t = cam
# disregard the [CLS] token itself
self.R_t_t[0, 0] = 0
return self.R_t_t, self.R_t_i
def gradcam(self, cam, grad):
cam = cam.reshape(-1, cam.shape[-2], cam.shape[-1])
grad = grad.reshape(-1, grad.shape[-2], grad.shape[-1])
grad = grad.mean(dim=[1, 2], keepdim=True)
cam = (cam * grad).mean(0).clamp(min=0)
return cam
def generate_attn_gradcam(self, input, index=None, method_name="gradcam"):
output = self.model_usage.forward(input).question_answering_score
model = self.model_usage.model
# initialize relevancy matrices
text_tokens = self.model_usage.text_len
image_bboxes = self.model_usage.image_boxes_len
# text self attention matrix
self.R_t_t = torch.eye(text_tokens, text_tokens).to(model.device)
# image self attention matrix
self.R_i_i = torch.eye(image_bboxes, image_bboxes).to(model.device)
# impact of images on text
self.R_t_i = torch.zeros(text_tokens, image_bboxes).to(model.device)
# impact of text on images
self.R_i_t = torch.zeros(image_bboxes, text_tokens).to(model.device)
if index == None:
index = np.argmax(output.cpu().data.numpy(), axis=-1)
one_hot = np.zeros((1, output.size()[-1]), dtype=np.float32)
one_hot[0, index] = 1
one_hot = torch.from_numpy(one_hot).requires_grad_(True)
one_hot = torch.sum(one_hot.cuda() * output)
model.zero_grad()
one_hot.backward(retain_graph=True)
# last cross attention + self- attention layer
blk = model.lxmert.encoder.x_layers[-1]
# cross attn cam will be the one used for the R_t_i matrix
grad_t_i = blk.visual_attention.att.get_attn_gradients().detach()
cam_t_i = blk.visual_attention.att.get_attn().detach()
cam_t_i = self.gradcam(cam_t_i, grad_t_i)
# self.R_t_i = torch.matmul(self.R_t_t.t(), torch.matmul(cam_t_i, self.R_i_i))
self.R_t_i = cam_t_i
# language self attention
grad = blk.lang_self_att.self.get_attn_gradients().detach()
cam = blk.lang_self_att.self.get_attn().detach()
self.R_t_t = self.gradcam(cam, grad)
# disregard the [CLS] token itself
self.R_t_t[0, 0] = 0
return self.R_t_t, self.R_t_i
def generate_rollout(self, input, method_name="rollout"):
output = self.model_usage.forward(input).question_answering_score
model = self.model_usage.model
# initialize relevancy matrices
text_tokens = self.model_usage.text_len
image_bboxes = self.model_usage.image_boxes_len
# text self attention matrix
self.R_t_t = torch.eye(text_tokens, text_tokens).to(model.device)
# image self attention matrix
self.R_i_i = torch.eye(image_bboxes, image_bboxes).to(model.device)
# impact of images on text
self.R_t_i = torch.zeros(text_tokens, image_bboxes).to(model.device)
# impact of text on images
self.R_i_t = torch.zeros(image_bboxes, text_tokens).to(model.device)
cams_text = []
cams_image = []
# language self attention
blocks = model.lxmert.encoder.layer
for blk in blocks:
cam = blk.attention.self.get_attn().detach()
cam = cam.reshape(-1, cam.shape[-2], cam.shape[-1]).mean(dim=0)
cams_text.append(cam)
# image self attention
blocks = model.lxmert.encoder.r_layers
for blk in blocks:
cam = blk.attention.self.get_attn().detach()
cam = cam.reshape(-1, cam.shape[-2], cam.shape[-1]).mean(dim=0)
cams_image.append(cam)
# cross attn layers
blocks = model.lxmert.encoder.x_layers
for i, blk in enumerate(blocks):
# in the last cross attention module, only the text cross modal
# attention has an impact on the CLS token, since it's the first
# token in the language tokens
if i == len(blocks) - 1:
break
# language self attention
cam = blk.lang_self_att.self.get_attn().detach()
cam = cam.reshape(-1, cam.shape[-2], cam.shape[-1]).mean(dim=0)
cams_text.append(cam)
# image self attention
cam = blk.visn_self_att.self.get_attn().detach()
cam = cam.reshape(-1, cam.shape[-2], cam.shape[-1]).mean(dim=0)
cams_image.append(cam)
# take care of last cross attention layer- only text
blk = model.lxmert.encoder.x_layers[-1]
# cross attn cam will be the one used for the R_t_i matrix
cam_t_i = blk.visual_attention.att.get_attn().detach()
cam_t_i = cam_t_i.reshape(-1, cam_t_i.shape[-2], cam_t_i.shape[-1]).mean(dim=0)
self.R_t_t = compute_rollout_attention(copy.deepcopy(cams_text))
self.R_i_i = compute_rollout_attention(cams_image)
self.R_t_i = torch.matmul(self.R_t_t.t(), torch.matmul(cam_t_i, self.R_i_i))
# language self attention
cam = blk.lang_self_att.self.get_attn().detach()
cam = cam.reshape(-1, cam.shape[-2], cam.shape[-1]).mean(dim=0)
cams_text.append(cam)
self.R_t_t = compute_rollout_attention(cams_text)
# disregard the [CLS] token itself
self.R_t_t[0, 0] = 0
return self.R_t_t, self.R_t_i
|