← 开源
The-AI-Summer

self-attention-cv

Implementation of various self-attention mechanisms focused on computer vision. Ongoing repository.

Model DevelopmentArchitecturePython
在 GitHub 打开
增长势头
+024 小时新增 Star0.0%
1.21k
Star
152
Fork
+-1
本周
2
贡献者
创建于 2021-01-31 · 更新于 2026-10-15 · 今日第 11116 名
主要开发者
black0017SergiosKar

The-AI-Summer/self-attention-cv 排名第 11,116,有 1,214 个星标。

同类别比较

The-AI-Summer/self-attention-cv 排名第 11,116,此处有 8 个同类条目;pdebench/PDEBench 排名第 11,118。

同类别比较
名称排名指标
Shikhargupta/Spiking-Neural-Network11,107
语言:
Python
星标数:
1,217
分叉数:
298
简介:
Pure python implementation of SNN
awslabs/sockeye11,110
语言:
Python
星标数:
1,216
分叉数:
319
简介:
Sequence-to-sequence framework with a focus on Neural Machine Translation based on PyTorch
hasktorch/hasktorch11,111
语言:
Haskell
星标数:
1,216
分叉数:
127
简介:
Tensors and neural networks in Haskell
SJTU-ViSYS/M2DGR11,112
星标数:
1,215
分叉数:
130
简介:
M2DGR: a Multi-modal and Multi-scenario Dataset for Ground Robots(RA-L2021 & ICRA2022)
pdebench/PDEBench11,118
语言:
Python
星标数:
1,214
分叉数:
153
简介:
PDEBench: An Extensive Benchmark for Scientific Machine Learning
Jamie-Stirling/RetNet11,123
语言:
Python
星标数:
1,212
分叉数:
106
简介:
An implementation of "Retentive Network: A Successor to Transformer for Large Language Models"
Profluent-AI/OpenCRISPR11,134
星标数:
1,209
分叉数:
129
简介:
AI-generated gene editing systems
mlmed/torchxrayvision11,137
语言:
Jupyter Notebook
星标数:
1,209
分叉数:
259
简介:
TorchXRayVision: A library of chest X-ray datasets and models. Classifiers, segmentation, and autoencoders.
关键参数与规格
关键参数与规格
参数值
语言Python
类别Model Development
子类别Architecture
关键词Attention visualization, Computer vision
星标数1,214
分叉数152
贡献者2
创建日期2021-01-31
更新日期2026-10-15
1 天星标数0
7 天星标数-1
排名11,116
上次排名19,260
排名变化8,144
7 天星标排名17,243
上次 7 天星标排名17,239
7 天星标排名变化-4
总星标排名9,610
上次总星标排名9,605
总星标排名变化-5
README 摘录

Self-attention building blocks for computer vision applications in PyTorch Implementation of self attention mechanisms for computer vision in PyTorch with einsum and einops. Focused on computer vision self-attention modules. Install it via pip It would be nice to pre-install pytorch in your environment, in case you don't have a GPU. To run the tests from the terminal you may need to run before. Related articles How Attention works in Deep Learning How Transformers work in deep learning

and NLP How the Vision Transformer (ViT) works in 10 minutes: an image is worth 16x16 words Understanding einsum for Deep learning: implement a transformer with multi-head self-attention from scratch How Positional Embeddings work in Self-Attention Why multi-head self attention works: math, intuitions and 10+1 hidden insights Code Examples Multi-head attention Axial attention Vanilla Transformer Encoder Vision Transformer with/without ResNet50 backbone for image classification A re-implementation of Unet with the Vision Transformer

encoder Bottleneck Attention block Position embeddings are also available 1D Positional Embeddings 2D Positional Embeddings Acknowledgments Thanks to Alex Rogozhnikov @arogozhnikov for the awesome einops package. For my re-implementations I have studied and borrowed code from many repositories of Phil Wang @lucidrains. By studying his code I have managed to grasp self-attention, discover nlp stuff that are never referred in the papers, and learn from his clean coding style. Axial-deeplab: Stand-alone axial-attention for panoptic segmentation

. In European Conference on Computer Vision (pp. Bottleneck Transformers for Visual Recognition. An image is worth 16x16 words: Transformers for image recognition at scale. Stand-alone self-attention in vision models. Transunet: Transformers make strong encoders for medical image segmentation. Linformer: Self-attention with linear complexity. Is Space-Time Attention All You Need for Video Understanding? Self-attention with relative position representations. Support If you really like this repository and find it useful, please consider (★) starring it, so that it

can reach a broader audience of like-minded people.

常见问题

记录了多少星标和分叉?

The-AI-Summer/self-attention-cv 有 1,214 个星标和 152 个分叉。

仓库排名是多少?

The-AI-Summer/self-attention-cv 排名第 11,116。

记录的语言和类别是什么?

The-AI-Summer/self-attention-cv 有 1,214 个星标,语言为 Python,类别为 Model Development。

哪个仓库排名最接近?

The-AI-Summer/self-attention-cv 与 pdebench/PDEBench 比较,后者排名第 11,118,有 1,214 个星标。

README

Self-attention building blocks for computer vision applications in PyTorch

Implementation of self attention mechanisms for computer vision in PyTorch with einsum and einops. Focused on computer vision self-attention modules.

Install it via pip

$ pip install self-attention-cv

It would be nice to pre-install pytorch in your environment, in case you don't have a GPU. To run the tests from the terminal $ pytest you may need to run export PYTHONPATH=$PATHONPATH:`pwd` before.

Related articles

Code Examples

Multi-head attention

import torch
from self_attention_cv import MultiHeadSelfAttention

model = MultiHeadSelfAttention(dim=64)
x = torch.rand(16, 10, 64)  # [batch, tokens, dim]
mask = torch.zeros(10, 10)  # tokens X tokens
mask[5:8, 5:8] = 1
y = model(x, mask)

Axial attention

import torch
from self_attention_cv import AxialAttentionBlock
model = AxialAttentionBlock(in_channels=256, dim=64, heads=8)
x = torch.rand(1, 256, 64, 64)  # [batch, tokens, dim, dim]
y = model(x)

Vanilla Transformer Encoder

import torch
from self_attention_cv import TransformerEncoder
model = TransformerEncoder(dim=64,blocks=6,heads=8)
x = torch.rand(16, 10, 64)  # [batch, tokens, dim]
mask = torch.zeros(10, 10)  # tokens X tokens
mask[5:8, 5:8] = 1
y = model(x,mask)

Vision Transformer with/without ResNet50 backbone for image classification

import torch
from self_attention_cv import ViT, ResNet50ViT

model1 = ResNet50ViT(img_dim=128, pretrained_resnet=False, 
                        blocks=6, num_classes=10, 
                        dim_linear_block=256, dim=256)
# or
model2 = ViT(img_dim=256, in_channels=3, patch_dim=16, num_classes=10,dim=512)
x = torch.rand(2, 3, 256, 256)
y = model2(x) # [2,10]

A re-implementation of Unet with the Vision Transformer encoder

import torch
from self_attention_cv.transunet import TransUnet
a = torch.rand(2, 3, 128, 128)
model = TransUnet(in_channels=3, img_dim=128, vit_blocks=8,
vit_dim_linear_mhsa_block=512, classes=5)
y = model(a) # [2, 5, 128, 128]

Bottleneck Attention block

import torch
from self_attention_cv.bottleneck_transformer import BottleneckBlock
inp = torch.rand(1, 512, 32, 32)
bottleneck_block = BottleneckBlock(in_channels=512, fmap_size=(32, 32), heads=4, out_channels=1024, pooling=True)
y = bottleneck_block(inp)

Position embeddings are also available

1D Positional Embeddings

import torch
from self_attention_cv.pos_embeddings import AbsPosEmb1D,RelPosEmb1D

model = AbsPosEmb1D(tokens=20, dim_head=64)
# batch heads tokens dim_head
q = torch.rand(2, 3, 20, 64)
y1 = model(q)

model = RelPosEmb1D(tokens=20, dim_head=64, heads=3)
q = torch.rand(2, 3, 20, 64)
y2 = model(q)

2D Positional Embeddings

import torch
from self_attention_cv.pos_embeddings import RelPosEmb2D
dim = 32  # spatial dim of the feat map
model = RelPosEmb2D(
    feat_map_size=(dim, dim),
    dim_head=128)

q = torch.rand(2, 4, dim*dim, 128)
y = model(q)

Acknowledgments

Thanks to Alex Rogozhnikov @arogozhnikov for the awesome einops package. For my re-implementations I have studied and borrowed code from many repositories of Phil Wang @lucidrains. By studying his code I have managed to grasp self-attention, discover nlp stuff that are never referred in the papers, and learn from his clean coding style.

Cited as

@article{adaloglou2021transformer,
    title   = "Transformers in Computer Vision",
    author  = "Adaloglou, Nikolas",
    journal = "https://theaisummer.com/",
    year    = "2021",
    howpublished = {https://github.com/The-AI-Summer/self-attention-cv},
  }

References

  1. Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., ... & Polosukhin, I. (2017). Attention is all you need. arXiv preprint arXiv:1706.03762.
  2. Wang, H., Zhu, Y., Green, B., Adam, H., Yuille, A., & Chen, L. C. (2020, August). Axial-deeplab: Stand-alone axial-attention for panoptic segmentation. In European Conference on Computer Vision (pp. 108-126). Springer, Cham.
  3. Srinivas, A., Lin, T. Y., Parmar, N., Shlens, J., Abbeel, P., & Vaswani, A. (2021). Bottleneck Transformers for Visual Recognition. arXiv preprint arXiv:2101.11605.
  4. Dosovitskiy, A., Beyer, L., Kolesnikov, A., Weissenborn, D., Zhai, X., Unterthiner, T., ... & Houlsby, N. (2020). An image is worth 16x16 words: Transformers for image recognition at scale. arXiv preprint arXiv:2010.11929.
  5. Ramachandran, P., Parmar, N., Vaswani, A., Bello, I., Levskaya, A., & Shlens, J. (2019). Stand-alone self-attention in vision models. arXiv preprint arXiv:1906.05909.
  6. Chen, J., Lu, Y., Yu, Q., Luo, X., Adeli, E., Wang, Y., ... & Zhou, Y. (2021). Transunet: Transformers make strong encoders for medical image segmentation. arXiv preprint arXiv:2102.04306.
  7. Wang, S., Li, B., Khabsa, M., Fang, H., & Ma, H. (2020). Linformer: Self-attention with linear complexity. arXiv preprint arXiv:2006.04768.
  8. Bertasius, G., Wang, H., & Torresani, L. (2021). Is Space-Time Attention All You Need for Video Understanding?. arXiv preprint arXiv:2102.05095.
  9. Shaw, P., Uszkoreit, J., & Vaswani, A. (2018). Self-attention with relative position representations. arXiv preprint arXiv:1803.02155.

Support

If you really like this repository and find it useful, please consider (★) starring it, so that it can reach a broader audience of like-minded people. It would be highly appreciated :) !

排名相邻
同类项目