[GH-ISSUE #18] Attention Map Generation #18

Closed
opened 2026-10-31 21:09:54 +00:00 by bsakaguchi · 14 comments
Owner

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?

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?
Author
Owner

@jongwook commented on GitHub (Jan 21, 2021):

It is not very convenient, but it should be possible this way:

  • Modify the call to nn.MultiheadAttention in model.py to pass need_weights=True.
  • Use the module's second return value, which will contain the attention weights. (The current code discards the second return value, by appending [0] to the call)
  • Load the CLIP model with jit=False applied, 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_heads part in case you want to show per-head visualization.

<!-- gh-comment-id:764064028 --> @jongwook commented on GitHub (Jan 21, 2021): It is not very convenient, but it should be possible this way: - Modify the call to `nn.MultiheadAttention` in [model.py](https://github.com/openai/CLIP/blob/6bc0bd88737ac3b28a5b3288fdd13b7a6cd94b22/model.py#L182) to pass `need_weights=True`. - Use the module's second return value, which will contain the attention weights. (The current code discards the second return value, by appending `[0]` to the call) - Load the CLIP model with `jit=False` applied, 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](https://github.com/pytorch/pytorch/blob/v1.7.1/torch/nn/functional.py#L4324), so you might want to delete the `.sum(dim=1) / num_heads` part in case you want to show per-head visualization.
Author
Owner

@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.

<!-- gh-comment-id:770464711 --> @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.
Author
Owner

@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.

<!-- gh-comment-id:782891233 --> @haofanwang commented on GitHub (Feb 21, 2021): Hi, @betterze and @Hugh0120, I implement the attention map based on suggestions. Here is my [implementation](https://github.com/haofanwang/natural-language-joint-query-search) which may be helpful to you guys.
Author
Owner

@betterze commented on GitHub (Feb 21, 2021):

@haofanwang Thank you for shaing with us this nice implementation. It is very nice.

<!-- gh-comment-id:782911283 --> @betterze commented on GitHub (Feb 21, 2021): @haofanwang Thank you for shaing with us this nice implementation. It is very nice.
Author
Owner

@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.

<!-- gh-comment-id:825133234 --> @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](https://github.com/HendrikStrobelt/miniClip/blob/19821a0e9a3a1824c0535add2385daaa74f7d29d/app.py#L22), 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.
Author
Owner

@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.

<!-- gh-comment-id:940359694 --> @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.
Author
Owner

@Maddy12 commented on GitHub (Jun 21, 2022):

Does anyone have an implementation on the visual side using CLIP with a ViT?

<!-- gh-comment-id:1162463164 --> @Maddy12 commented on GitHub (Jun 21, 2022): Does anyone have an implementation on the visual side using CLIP with a ViT?
Author
Owner

@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

<!-- gh-comment-id:1379414622 --> @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 :laughing: 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
Author
Owner

@zhanjiahui commented on GitHub (Feb 24, 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

Hello @ricardodeazambuja , thank you for your recommendation. ,

However, I run this file and this error occurs. Do you know how to solve it?
图片
图片

<!-- gh-comment-id:1443657252 --> @zhanjiahui commented on GitHub (Feb 24, 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 Hello @ricardodeazambuja , thank you for your recommendation. , However, I run this file and this error occurs. Do you know how to solve it? ![图片](https://user-images.githubusercontent.com/36075322/221185385-0ddb3715-97e8-4243-88b7-3f98b89fd3db.png) ![图片](https://user-images.githubusercontent.com/36075322/221185721-ada507ef-84dc-4861-b93f-6daafe2213d3.png)
Author
Owner

@ricardodeazambuja commented on GitHub (Feb 24, 2023):

@zhanjiahui, are you using the attn_weights branch from my repository?

<!-- gh-comment-id:1443682379 --> @ricardodeazambuja commented on GitHub (Feb 24, 2023): @zhanjiahui, are you using the `attn_weights` branch from my repository?
Author
Owner

@zhanjiahui commented on GitHub (Feb 24, 2023):

@zhanjiahui, are you using the attn_weights branch from my repository?
@ricardodeazambuja
Thank you for your reply. Yes, I downloaded CLIP-attn-weights from your repository.😭

<!-- gh-comment-id:1443697204 --> @zhanjiahui commented on GitHub (Feb 24, 2023): > @zhanjiahui, are you using the `attn_weights` branch from my repository? @ricardodeazambuja Thank you for your reply. Yes, I downloaded CLIP-attn-weights from your repository.😭
Author
Owner

@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 clip you are importing the normal clip instead of the one from the branch attn_weights because the notebooks are not at the base directory, so they won't see the directory clip by default.

<!-- gh-comment-id:1443776512 --> @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 clip` you are importing the normal clip instead of the one from the branch `attn_weights` because the notebooks are not at the base directory, so they won't see the directory `clip` by default.
Author
Owner

@zhanjiahui 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 clip you are importing the normal clip instead of the one from the branch attn_weights because the notebooks are not at the base directory, so they won't see the directory clip by default.

​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.

图片

<!-- gh-comment-id:1443905499 --> @zhanjiahui 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 clip` you are importing the normal clip instead of the one from the branch `attn_weights` because the notebooks are not at the base directory, so they won't see the directory `clip` by default. ​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. ![图片](https://user-images.githubusercontent.com/36075322/221215669-20174591-98f0-4cee-a5eb-ad08ad90f06d.png)
Author
Owner

@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 clip you are importing the normal clip instead of the one from the branch attn_weights because the notebooks are not at the base directory, so they won't see the directory clip by default.

​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.

图片

@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_weights branch and install using pip install -e ..

<!-- gh-comment-id:1443920269 --> @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 clip` you are importing the normal clip instead of the one from the branch `attn_weights` because the notebooks are not at the base directory, so they won't see the directory `clip` by default. > > ​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. > > ![图片](https://user-images.githubusercontent.com/36075322/221215669-20174591-98f0-4cee-a5eb-ad08ad90f06d.png) @zhanjiahui, keep following the flow of the code and you will find it here: https://github.com/ricardodeazambuja/CLIP/blob/9340bb39d05162605ed38b5999f68e9b7d390e72/clip/model.py#L230 Try to pull the latest version of the repo, checkout the `attn_weights` branch and install using `pip install -e .`.
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openai/CLIP#18
No description provided.