Skip to main content

ViX

PWC

Vision Xformers: Efficient Attention for Image Classification

image

We use Linear Attention mechanisms to replace quadratic attention in ViT for image classification. We show that models using linear attention and CNN embedding layers need less parameters and low GPU requirements for achieving good accuracy. These improvements can be used to democratize the use of transformers by practitioners who are limited by data and GPU.

Hybrid ViX uses convolutional layers instead of linear layer for generating embeddings

Rotary Postion Embedding (RoPE) is also used in our models instead of 1D learnable position embeddings

Nomenclature: We replace the X in ViX with the starting alphabet of the attention mechanism used Eg. When we use Performer in ViX, we replace the X with P, calling it ViP (Vision Performer)

'Hybrid' prefix is used in models which uses convolutional layers instead of linear embeddding layer.

We have added RoPE in the title of models which used Rotary Postion Embedding

The code for using all for these models for classification of CIFAR 10/Tiny ImageNet dataset is provided

Models

  • Vision Transformer (ViT)
  • Vision Linformer (ViL)
  • Vision Performer (ViP)
  • Vision Nyströmformer (ViN)
  • FNet
  • Hybrid Vision Transformer (HybridViT)
  • Hybrid Vision Linformer (HybridViL)
  • Hybrid Vision Performer (HybridViP)
  • Hybrid Vision Nyströmformer (HybridViN)
  • Hybrid FNet
  • LeViN (Replacing Transformer in LeViT with Nyströmformer)
  • LeViP (Replacing Transformer in LeViT with Performer)
  • CvN (Replacing Transformer in CvT with Nyströmformer)
  • CvP (Replacing Transformer in CvT with Performer)
  • CCN (Replacing Transformer in CCT with Nyströmformer)
  • CCP(Replacing Transformer in CCT with Performer)

We have adapted the codes for ViT and linear transformers from @lucidrains

Install

$ pip install vision-xformer

Usage

Image Classification

Vision Nyströmformer (ViN)

import torch, vision_xformer
from vision_xformer import ViN

model = ViN(
    image_size = 32,
    patch_size = 1,
    num_classes = 10,             
    dim = 128,  
    depth = 4,             
    heads = 4,      
    mlp_dim = 256,
    num_landmarks = 256,
    pool = 'cls',
    channels = 3,
    dropout = 0.,
    emb_dropout = 0.
    dim_head = 32
)

img = torch.randn(1, 3, 32, 32)

preds = model(img) # (1, 10)

Vision Performer (ViP)

import torch, vision_xformer
from vision_xformer import ViP

model = ViP(
    image_size = 32,
    patch_size = 1,
    num_classes = 10,             
    dim = 128,  
    depth = 4,             
    heads = 4,      
    mlp_dim = 256,
    dropout = 0.25,
    dim_head = 32
)

img = torch.randn(1, 3, 32, 32)

preds = model(img) # (1, 10)

Vision Linformer (ViL)

import torch, vision_xformer
from vision_xformer import ViL

model = ViL(
    image_size = 32,
    patch_size = 1,
    num_classes = 10,             
    dim = 128,  
    depth = 4,             
    heads = 4,      
    mlp_dim = 256,
    dropout = 0.25,
    dim_head = 32
)

img = torch.randn(1, 3, 32, 32)

preds = model(img) # (1, 10)

Parameters

  • image_size: int.
    Size of input image. If you have rectangular images, make sure your image size is the maximum of the width and height
  • patch_size: int.
    Number of patches. image_size must be divisible by patch_size.
  • num_classes: int.
    Number of classes to classify.
  • dim: int.
    Final dimension of token emeddings after linear layer.
  • depth: int.
    Number of layers.
  • heads: int.
    Number of heads in multi-head attention
  • mlp_dim: int.
    Embedding dimension in the MLP (FeedForward) layer.
  • num_landmarks: int. Number of landmark points. Use one-fourth the number of patches.
  • pool: str. Pool type must be either 'cls' (cls token) or 'mean' (mean pooling)
  • dropout: float between [0, 1], default 0..
    Dropout rate.
  • dim_head: int.
    Embedding dimension of token in each head of mulit-head attention.

More information about these models can be obtained from our paper : ArXiv Paper, WACV 2022 Paper

If you wish to cite this, please use:

@misc{jeevan2021vision,
      title={Vision Xformers: Efficient Attention for Image Classification}, 
      author={Pranav Jeevan and Amit Sethi},
      year={2021},
      eprint={2107.02239},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}
@InProceedings{Jeevan_2022_WACV,
    author    = {Jeevan, Pranav and Sethi, Amit},
    title     = {Resource-Efficient Hybrid X-Formers for Vision},
    booktitle = {Proceedings of the IEEE/CVF Winter Conference on Applications of Computer Vision (WACV)},
    month     = {January},
    year      = {2022},
    pages     = {2982-2990}
}

Metadata

Release files for vision-xformer 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vision-xformer 0.2.0
File Size Uploaded Provenance Actions
vision_xformer-0.2.0.tar.gz 13.3 kB Without provenance Details for vision_xformer-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vision-xformer 0.2.0
File Interpreter ABI Platform Actions
vision_xformer-0.2.0-py3-none-any.whl Python 3 none any Details for vision_xformer-0.2.0-py3-none-any.whl

Total release size: 28.3 kB

Release files / vision_xformer-0.2.0.tar.gz

Download URL vision_xformer-0.2.0.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
dd43ee2e6a68d9674a1d360d9677dfa9bf0d510e0b39b6b24d156218a29020ad
BLAKE2b-256 checksum
How to use checksums
c37183055714b73da06dff2f6b82d389407eba72c24cdded7681c0adc529eccf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / vision_xformer-0.2.0-py3-none-any.whl

Download URL vision_xformer-0.2.0-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b4ebfef0a7331ff845a289dbc05556cf88ada592d6b7057031d2eec299bd725
BLAKE2b-256 checksum
How to use checksums
0c429688c73344eb1b0ddafb21410667e2c480c5c48d35860ef209b167c602ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Security for vision-xformer 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page