New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Notebook for attention map over input image #306
base: main
Are you sure you want to change the base?
Conversation
Hi Iegel, I find an error happens at attention.ipynb: So I checked the attention shape. It turns out to be [1, 4629, 768] instead of [1, 12, 4629, 4629] in your notebook. I know the 768 is the embedding dimension of base model. Why my attention results have a different dimension from yours? |
Hi @LichunZhang my best guess is that one of your core files did not get changed properly, so the model is still only feed-forwarding the 768 dimensional features, instead of the full attention... I would double check that you've cloned the repository directly from https://github.com/3cology/dinov2_with_attention_extraction/tree/main and then run the notebook in that repository. Feel free to share the output here and any further insights. |
Thank you for the quick response. |
I think it happens because you are using the xFormers library, which uses Since It should work if at the beginning of the notebook you set something like |
Hi! Checked that this happens with both xformers and without ( |
Update: confirmed that it happens because of xformers enabled. Before I must haved overlooked it.. |
I solved the issue now. Refer to #90 and find ludles's answer. It turns out that we should modify the code of MemEffAttention . |
Attention heatmap visualization is a common utility that will likely serve several researchers.
In order to implement it, it requires some subtle code changes to fundamental classes that many researchers might wish to have already implemented for convenience.
Inspired by a working implementation from here, I also took further steps of figuring out how to load pre-trained models with registers ("Vision Transformers Need Registers"), which indeed resolves curious artifacts with some background attention tokens.
I've also cleaned up code substantially, provided a simple example on a cool NASA space shuttle launch from Wikimedia Commons, and introduced a nice subtle visualization of the attention mask directly on top of the original image.
I hope this helps several researchers and developers!
This pull request addresses or resolves the following:
P.S. I haven't made many pull requests, and didn't want to mix up with #305, so I forked two different repositories, but in the future will just create branches for pull requests. Thanks!