AustingDong commited on
Commit ·
3912684
1
Parent(s): 9321e89
Update cam.py
Browse files- demo/cam.py +4 -6
demo/cam.py
CHANGED
|
@@ -186,6 +186,7 @@ class AttentionGuidedCAMJanus(AttentionGuidedCAM):
|
|
| 186 |
|
| 187 |
|
| 188 |
# Compute mean of gradients
|
|
|
|
| 189 |
grad_weights = grad.mean(dim=-1, keepdim=True)
|
| 190 |
|
| 191 |
print("act shape", act.shape)
|
|
@@ -242,12 +243,12 @@ class AttentionGuidedCAMJanus(AttentionGuidedCAM):
|
|
| 242 |
print("act_shape:", act.shape)
|
| 243 |
# print("act1_shape:", act[1].shape)
|
| 244 |
|
| 245 |
-
act =
|
| 246 |
|
| 247 |
|
| 248 |
# Compute mean of gradients
|
| 249 |
print("grad_shape:", grad.shape)
|
| 250 |
-
grad_weights = grad.mean(dim=1)
|
| 251 |
|
| 252 |
|
| 253 |
# cam, _ = (act * grad_weights).max(dim=-1)
|
|
@@ -371,7 +372,6 @@ class AttentionGuidedCAMLLaVA(AttentionGuidedCAM):
|
|
| 371 |
print("act shape", act.shape)
|
| 372 |
print("grad shape", grad.shape)
|
| 373 |
|
| 374 |
-
act = F.relu(act)
|
| 375 |
grad = F.relu(grad)
|
| 376 |
|
| 377 |
|
|
@@ -475,8 +475,8 @@ class AttentionGuidedCAMChartGemma(AttentionGuidedCAM):
|
|
| 475 |
|
| 476 |
self.model.zero_grad()
|
| 477 |
# print(outputs_raw)
|
| 478 |
-
# loss = self.target_layers[-1].attention_map.sum()
|
| 479 |
loss = outputs_raw.logits.max(dim=-1).values.sum()
|
|
|
|
| 480 |
loss.backward()
|
| 481 |
|
| 482 |
# get image masks
|
|
@@ -531,10 +531,8 @@ class AttentionGuidedCAMChartGemma(AttentionGuidedCAM):
|
|
| 531 |
print("act shape", act.shape)
|
| 532 |
print("grad shape", grad.shape)
|
| 533 |
|
| 534 |
-
act = F.relu(act)
|
| 535 |
grad = F.relu(grad)
|
| 536 |
|
| 537 |
-
|
| 538 |
cam = act * grad # shape: [1, heads, seq_len, seq_len]
|
| 539 |
cam = cam.sum(dim=1) # shape: [1, seq_len, seq_len]
|
| 540 |
|
|
|
|
| 186 |
|
| 187 |
|
| 188 |
# Compute mean of gradients
|
| 189 |
+
print("grad shape:", grad.shape)
|
| 190 |
grad_weights = grad.mean(dim=-1, keepdim=True)
|
| 191 |
|
| 192 |
print("act shape", act.shape)
|
|
|
|
| 243 |
print("act_shape:", act.shape)
|
| 244 |
# print("act1_shape:", act[1].shape)
|
| 245 |
|
| 246 |
+
act = act.mean(dim=1)
|
| 247 |
|
| 248 |
|
| 249 |
# Compute mean of gradients
|
| 250 |
print("grad_shape:", grad.shape)
|
| 251 |
+
grad_weights = F.relu(grad.mean(dim=1))
|
| 252 |
|
| 253 |
|
| 254 |
# cam, _ = (act * grad_weights).max(dim=-1)
|
|
|
|
| 372 |
print("act shape", act.shape)
|
| 373 |
print("grad shape", grad.shape)
|
| 374 |
|
|
|
|
| 375 |
grad = F.relu(grad)
|
| 376 |
|
| 377 |
|
|
|
|
| 475 |
|
| 476 |
self.model.zero_grad()
|
| 477 |
# print(outputs_raw)
|
|
|
|
| 478 |
loss = outputs_raw.logits.max(dim=-1).values.sum()
|
| 479 |
+
|
| 480 |
loss.backward()
|
| 481 |
|
| 482 |
# get image masks
|
|
|
|
| 531 |
print("act shape", act.shape)
|
| 532 |
print("grad shape", grad.shape)
|
| 533 |
|
|
|
|
| 534 |
grad = F.relu(grad)
|
| 535 |
|
|
|
|
| 536 |
cam = act * grad # shape: [1, heads, seq_len, seq_len]
|
| 537 |
cam = cam.sum(dim=1) # shape: [1, seq_len, seq_len]
|
| 538 |
|