-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bjoern
committed
Nov 6, 2023
1 parent
7828983
commit 0555893
Showing
17 changed files
with
147 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
*.pt | ||
*.pth | ||
*pyc | ||
*build* | ||
*.egg* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from PIL import Image | ||
import matplotlib.pyplot as plt | ||
|
||
from atman_magma.captum_helper import ( | ||
CaptumMagma, | ||
) | ||
from multimodal_explain_eval.utils import check_if_a_or_an_and_get_prefix | ||
import numpy as np | ||
from atman_magma.magma import Magma | ||
from magma.image_input import ImageInput | ||
from PIL import Image | ||
|
||
from captum.attr import IntegratedGradients, InputXGradient, GuidedGradCam | ||
from captum.attr import LayerGradCam | ||
|
||
|
||
targets = 'Panda' | ||
final_img = Image.open('openimages-panda.jpg') | ||
|
||
|
||
print('loading model...') | ||
model = Magma.from_checkpoint( | ||
checkpoint_path = "./mp_rank_00_model_states.pt", | ||
device = 'cuda:0' | ||
) | ||
|
||
cmagma = CaptumMagma(magma = model) | ||
# captum_tool = IntegratedGradients(cmagma) | ||
# captum_tool = GuidedGradCam(cmagma, layer = cmagma.magma.lm.transformer.h[0].ln_1) #cmagma.magma.image_prefix.enc.layer4[-1].conv3) #, layer = cmagma.magma.image_prefix.enc.layer4[-1].conv3) | ||
captum_tool = InputXGradient(cmagma) | ||
#captum_tool = IntegratedGradients(cmagma) #! set n_steps below | ||
|
||
|
||
cmagma.mode='text' #hack- leave it as it is - just passes below's image embeddings thru ... | ||
|
||
|
||
label_tokens = model.tokenizer.encode(targets) | ||
|
||
att_combined = np.zeros((12,12)) | ||
for i in range(len(label_tokens)): | ||
|
||
text_prompt = f"This is a picture of {check_if_a_or_an_and_get_prefix(targets.lower())} " | ||
if i >= 1: | ||
text_prompt += model.tokenizer.decode(label_tokens[:i]) | ||
|
||
|
||
prompt = [ | ||
ImageInput(None, pil=final_img), | ||
text_prompt | ||
] | ||
|
||
embeddings = cmagma.magma.preprocess_inputs(prompt) | ||
|
||
attribution = captum_tool.attribute( | ||
embeddings, | ||
target=label_tokens[i], | ||
#n_steps = 1 #integ gradients parameters ! | ||
) | ||
|
||
att = attribution[0].abs().sum(dim = 1).cpu().detach().numpy()[:144].reshape(12,12) | ||
|
||
att_combined += att/att.max() | ||
|
||
|
||
fig = plt.figure() | ||
plt.imshow(att_combined) | ||
fig.savefig('panda-explained-captum.jpg') | ||
print('panda-explained-captum.jpg') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.