TODO
Urgent
- Rewrite README
- Implement model
- Convert pretrained weights
- Colab example
-
pip
ViT PyTorch
Quickstart
Install with pip install vit_pytorch and load a pretrained ViT with:
from vit_pytorch import ViT
model = ViT('B_16_imagenet1k', pretrained=True)
Or find a Google Colab example here.
Overview
This repository contains an op-for-op PyTorch reimplementation of the Visual Transformer architecture from Google, along with pre-trained models and examples.
The goal of this implementation is to be simple, highly extensible, and easy to integrate into your own projects.
At the moment, you can easily:
- Load pretrained ViT models
- Evaluate on ImageNet or your own data
(Upcoming features) Coming soon:
- Finetune ViT on your own dataset
- Train ViT from scratch on ImageNet (1K)
- Export to ONNX for efficient inference
Table of contents
About ViT
Visual Transformers (ViT) are a straightforward application of the transformer architecture to image classification. Even in computer vision, it seems, attention is all you need.
The ViT architecture works as follows: (1) it considers an image as a 1-dimensional sequence of patches, (2) it prepends a classification token to the sequence, (3) it passes these patches through a transformer encoder (like BERT), (4) it passes the first token of the output of the transformer through a small MLP to obtain the classification logits. ViT is trained on a large-scale dataset (ImageNet-21k) with a huge amount of compute.
About ViT-PyTorch
ViT-PyTorch is a PyTorch re-implementation of EfficientNet. It is consistent with the original Jax implementation, so that it's easy to load Jax-pretrained weights.
At the same time, we aim to make our PyTorch implementation as simple, flexible, and extensible as possible.
Installation
Install with pip:
pip install vit_pytorch
Or from source:
git clone https://github.com/lukemelas/ViT-PyTorch
cd ViT-Pytorch
pip install -e .
Usage
Loading pretrained models
Loading a pretrained model is easy:
from vit_pytorch import ViT
model = ViT('B_16_imagenet1k', pretrained=True)
Details about the models are below:
| Name | * Pretrained on * | Finetuned on | *Available? * |
|---|---|---|---|
B_16 |
ImageNet-21k | - | ✓ |
B_16_imagenet1k |
ImageNet-21k | ImageNet-1k | ✓ |
B_32_imagenet1k |
ImageNet-21k | ImageNet-1k | ✓ |
L_16_imagenet1k |
ImageNet-21k | ImageNet-1k | ✓ |
L_32_imagenet1k |
ImageNet-21k | ImageNet-1k | ✓ |
Custom ViT
Loading custom configurations is just as easy:
from vit_pytorch import ViT
# The following is equivalent to ViT('B_16')
config = dict(hidden_size=512, num_heads=8, num_layers=6)
model = ViT.from_config(config)
Example: Classification
Below is a simple, complete example. It may also be found as a Jupyter notebook in examples/simple or as a Colab Notebook.
import json
from PIL import Image
import torch
from torchvision import transforms
# Load ViT
from vit_pytorch import ViT
model = ViT('B_16_imagenet1k', pretrained=True)
model.eval()
# Load image
# NOTE: Assumes an image `img.jpg` exists in the current directory
img = transforms.Compose([
transforms.Resize((384, 384)),
transforms.ToTensor(),
transforms.Normalize(0.5, 0.5),
])(Image.open('img.jpg')).unsqueeze(0)
print(img.shape) # torch.Size([1, 3, 384, 384])
# Classify
with torch.no_grad():
outputs = model(img)
print(outputs.shape) # (1, 1000)
ImageNet
See examples/imagenet for details about evaluating on ImageNet.
Credit
Other great repositories with this model include:
Contributing
If you find a bug, create a GitHub issue, or even better, submit a pull request. Similarly, if you have questions, simply post them as GitHub issues.
I look forward to seeing what the community does with these models!
Metadata
Release files for pretrained-vit-pytorch 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pretrained-vit-pytorch-0.0.1.tar.gz | 10.0 kB | Without provenance | Details |
Release files / pretrained-vit-pytorch-0.0.1.tar.gz
| Download URL | pretrained-vit-pytorch-0.0.1.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
46aa24cd6877d1237e388ed9000ea5e882cae820e1243c0981d2c6c2834a9a1f
|
|
BLAKE2b-256 checksum How to use checksums |
f076b7e9c9914ca39ba8fa4e06d813963bbad9e32608404a69d0886d3197adb0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3
|
Security for pretrained-vit-pytorch 0.0.1
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.