{"id":13679170,"url":"https://github.com/lucidrains/bottleneck-transformer-pytorch","last_synced_at":"2025-05-16T16:09:17.093Z","repository":{"id":41196001,"uuid":"333624050","full_name":"lucidrains/bottleneck-transformer-pytorch","owner":"lucidrains","description":"Implementation of Bottleneck Transformer in Pytorch","archived":false,"fork":false,"pushed_at":"2021-09-20T15:17:29.000Z","size":107,"stargazers_count":678,"open_issues_count":7,"forks_count":83,"subscribers_count":17,"default_branch":"main","last_synced_at":"2025-04-12T15:57:25.362Z","etag":null,"topics":["artificial-intelligence","attention-mechanism","deep-learning","image-classification","transformers","vision"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lucidrains.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-28T02:36:02.000Z","updated_at":"2025-03-30T10:54:38.000Z","dependencies_parsed_at":"2022-08-10T01:43:07.138Z","dependency_job_id":null,"html_url":"https://github.com/lucidrains/bottleneck-transformer-pytorch","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fbottleneck-transformer-pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fbottleneck-transformer-pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fbottleneck-transformer-pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fbottleneck-transformer-pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucidrains","download_url":"https://codeload.github.com/lucidrains/bottleneck-transformer-pytorch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254564127,"owners_count":22092122,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artificial-intelligence","attention-mechanism","deep-learning","image-classification","transformers","vision"],"created_at":"2024-08-02T13:01:02.704Z","updated_at":"2025-05-16T16:09:17.061Z","avatar_url":"https://github.com/lucidrains.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\u003cimg src=\"./bottle-diagram.png\"\u003e\u003c/img\u003e\n\n\u003cimg src=\"./bottle-diagram-2.png\"\u003e\u003c/img\u003e\n\n## Bottleneck Transformer - Pytorch\n\n[![PyPI version](https://badge.fury.io/py/bottleneck-transformer-pytorch.svg)](https://badge.fury.io/py/bottleneck-transformer-pytorch)\n\nImplementation of \u003ca href=\"https://arxiv.org/abs/2101.11605\"\u003eBottleneck Transformer\u003c/a\u003e, SotA visual recognition model with convolution + attention that outperforms EfficientNet and DeiT in terms of performance-computes trade-off, in Pytorch\n\n## Install\n\n```bash\n$ pip install bottleneck-transformer-pytorch\n```\n\n## Usage\n\n```python\nimport torch\nfrom torch import nn\nfrom bottleneck_transformer_pytorch import BottleStack\n\nlayer = BottleStack(\n    dim = 256,              # channels in\n    fmap_size = 64,         # feature map size\n    dim_out = 2048,         # channels out\n    proj_factor = 4,        # projection factor\n    downsample = True,      # downsample on first layer or not\n    heads = 4,              # number of heads\n    dim_head = 128,         # dimension per head, defaults to 128\n    rel_pos_emb = False,    # use relative positional embedding - uses absolute if False\n    activation = nn.ReLU()  # activation throughout the network\n)\n\nfmap = torch.randn(2, 256, 64, 64) # feature map from previous resnet block(s)\n\nlayer(fmap) # (2, 2048, 32, 32)\n```\n\n## BotNet\n\nWith some simple model surgery off a resnet, you can have the 'BotNet' (what a weird name) for training.\n\n```python\nimport torch\nfrom torch import nn\nfrom torchvision.models import resnet50\n\nfrom bottleneck_transformer_pytorch import BottleStack\n\nlayer = BottleStack(\n    dim = 256,\n    fmap_size = 56,        # set specifically for imagenet's 224 x 224\n    dim_out = 2048,\n    proj_factor = 4,\n    downsample = True,\n    heads = 4,\n    dim_head = 128,\n    rel_pos_emb = True,\n    activation = nn.ReLU()\n)\n\nresnet = resnet50()\n\n# model surgery\n\nbackbone = list(resnet.children())\n\nmodel = nn.Sequential(\n    *backbone[:5],\n    layer,\n    nn.AdaptiveAvgPool2d((1, 1)),\n    nn.Flatten(1),\n    nn.Linear(2048, 1000)\n)\n\n# use the 'BotNet'\n\nimg = torch.randn(2, 3, 224, 224)\npreds = model(img) # (2, 1000)\n```\n\n## Citations\n\n```bibtex\n@misc{srinivas2021bottleneck,\n    title   = {Bottleneck Transformers for Visual Recognition}, \n    author  = {Aravind Srinivas and Tsung-Yi Lin and Niki Parmar and Jonathon Shlens and Pieter Abbeel and Ashish Vaswani},\n    year    = {2021},\n    eprint  = {2101.11605},\n    archivePrefix = {arXiv},\n    primaryClass = {cs.CV}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fbottleneck-transformer-pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucidrains%2Fbottleneck-transformer-pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fbottleneck-transformer-pytorch/lists"}