mirror of
https://github.com/openai/CLIP.git
synced 2026-11-05 01:37:26 +00:00
[GH-ISSUE #18] Attention Map Generation #18
Labels
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
openai/CLIP#18
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Hugh0120 on GitHub (Jan 18, 2021).
Original GitHub issue: https://github.com/openai/CLIP/issues/18
Thanks for the release of pretrained model!
I was wondering if it is possible to show attention maps of input images using released ViT-32 model?
@jongwook commented on GitHub (Jan 21, 2021):
It is not very convenient, but it should be possible this way:
nn.MultiheadAttentionin model.py to passneed_weights=True.[0]to the call)jit=Falseapplied, in order to load the modified model implementation.It could have been more elegant with forward hooks, but PyTorch does not currently allow modifying the keyword arguments of forward_pre_hooks.
Another caveat is that the multihead attention implementation only returns the attention weights summed over all heads, so you might want to delete the
.sum(dim=1) / num_headspart in case you want to show per-head visualization.@betterze commented on GitHub (Jan 31, 2021):
@Hugh0120 @jongwook Is there an implementation about this? I am new to transformer, a simple demo will be very helpful. Thx in advance.
@haofanwang commented on GitHub (Feb 21, 2021):
Hi, @betterze and @Hugh0120, I implement the attention map based on suggestions. Here is my implementation which may be helpful to you guys.
@betterze commented on GitHub (Feb 21, 2021):
@haofanwang Thank you for shaing with us this nice implementation. It is very nice.
@g-luo commented on GitHub (Apr 22, 2021):
Does anyone have any insight on which layers are best to use as the saliency layer for Grad CAM (for the visual transformer ViT models)? For the resnet people use the relu from the last visual layer, and I'm wondering what's the best for the visual transformer.
EDIT: realizing that GradCAM is not meant for transformers and mainly CNN / ResNet based models. Feel free to ignore the above question.
@kaizhaol commented on GitHub (Oct 11, 2021):
@g-luo I don't think gradCAM on the activation is a good idea. The patch setting makes it too coarse to be useful. But in general, when dealing with ViT, people usually use the output of the last norm layer of the last transformer block. So far I have mixed results though with CLIP.
@Maddy12 commented on GitHub (Jun 21, 2022):
Does anyone have an implementation on the visual side using CLIP with a ViT?
@ricardodeazambuja commented on GitHub (Jan 11, 2023):
Since this is the top result from google, it may be easier for me to find my fork here than by using the search box on github 😆
I followed the suggestions from the comments above and modified the code so I can extract the weights and visualize the attention for images:
https://github.com/ricardodeazambuja/CLIP/blob/attn_weights/notebooks/Interacting_with_CLIP.ipynb
@zhanjiahui commented on GitHub (Feb 24, 2023):
Hello @ricardodeazambuja , thank you for your recommendation. ,
However, I run this file and this error occurs. Do you know how to solve it?


@ricardodeazambuja commented on GitHub (Feb 24, 2023):
@zhanjiahui, are you using the
attn_weightsbranch from my repository?@zhanjiahui commented on GitHub (Feb 24, 2023):
@ricardodeazambuja commented on GitHub (Feb 24, 2023):
@zhanjiahui, Here everything works just fine (I had a problem related to a change in behaviour on how pillow and numpy exchange stuff). I would bet when you do
import clipyou are importing the normal clip instead of the one from the branchattn_weightsbecause the notebooks are not at the base directory, so they won't see the directoryclipby default.@zhanjiahui commented on GitHub (Feb 24, 2023):
Hi @ricardodeazambuja , I uninstalled normal clip, and moved this ipynb file into the root dir of your project. But this error still happens. I double-checked that the encode_image function has exactly one return value in your repository. This is so confusing to me.
@ricardodeazambuja commented on GitHub (Feb 24, 2023):
@zhanjiahui, keep following the flow of the code and you will find it here:
github.com/ricardodeazambuja/CLIP@9340bb39d0/clip/model.py (L230)Try to pull the latest version of the repo, checkout the
attn_weightsbranch and install usingpip install -e ..