Comments (2)
A simple implementation is like this assuming the model with standard image and patch size:
def patch_cosSim(model, pos_i, pos_j):
"""
Arguments:
model: ViT
pos_i: ith patch (int)
pos_j: jth patch (int)
returns: cos similarity of the pos emb of the (i,j) patch
"""
cos = nn.CosineSimilarity(dim=0)
s = model.embeddings.position_embeddings.shape
pos_patch = model.embeddings.position_embeddings.view(*s[1:])[:-1].view(14,14,-1)
n_rows = pos_patch.shape[0]
n_cols = pos_patch.shape[1]
patches = []
for i in range(n_rows):
for j in range(n_cols):
patches.append(cos(pos_patch[pos_i][pos_j], pos_patch[i][j]).detach().cpu().numpy())
return patches
Plot the cos similarity of the pos emb of the first patch:
i = 0
j = 1
patch = patch_cosSim(i, j)
ax = fig.add_subplot()
ax.set_axis_off()
ax.imshow(np.array(patch).reshape(14,14), cmap='hot', interpolation='nearest')
You can put it in a loop to get the cos similarity of the positional encoding for all the patches.
Hope it helps.
[Edit: added plot]
from vision_transformer.
Hi, will not share code, but it is basically just distance (I don't remember if dot product or euclidean) between all position embeddings to all other position embeddings.
from vision_transformer.
Related Issues (20)
- ModuleNotFoundError: No module named 'aqt' HOT 4
- Problem with kmnist dataset HOT 1
- Fine-Tuning HOT 3
- ERROR: Could not find a version that satisfies the requirement tensorflow_text (from vit-jax) (from versions: none) ERROR: No matching distribution found for tensorflow_text HOT 1
- If the weights of vit-base trained with dropout available? HOT 1
- How do I download the vit_base_patch8_384.pth
- Package versions' confliction [Windows] HOT 2
- Question about commercial usage of LiT model checkpoints
- Shouldn't accumulate_gradient pass rng_key?
- GPU Requirement to use vision transformer HOT 1
- flax.errors.CallCompactUnboundModuleError
- ViT
- can export the pretrained model to onnx or pytorch? HOT 2
- Vision transformer
- Import error in Jax (colab) HOT 3
- fine-tune imagenet21k_ViT-B_16.npz with pre_logits? HOT 2
- KeyError: 'embedding/kernel is not a file in the archive' HOT 1
- Hyperparameter issues HOT 1
- All attempts to get a Google authentication bearer token failed, returning an empty token.
- Where is ViT-22B?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vision_transformer.