ViTConfig
ModelConfigViTConfig(image_size: int = 224, patch_size: int = 16, num_classes: int = 1000, in_channels: int = 3, dim: int = 768, depth: int = 12, num_heads: int = 12, mlp_ratio: float = 4.0, dropout: float = 0.0, attention_dropout: float = 0.0, layer_norm_eps: float = 1e-06)Configuration dataclass for every Vision Transformer (ViT) variant.
ViTConfig is an immutable container that fully specifies the
architecture of a Vision Transformer. It is consumed by both
ViT (backbone) and ViTForImageClassification
(classifier head on top of the CLS token). All canonical variants
described in Dosovitskiy et al. (2020) — Base / Large / Huge at patch
sizes 14, 16, and 32 — can be expressed by choosing different values for
dim, depth, num_heads, and patch_size.
The number of input tokens fed into the transformer encoder is
, where
is the number of non-overlapping patches and the extra token is a
learnable [CLS] embedding prepended to the sequence.
Parameters
image_sizeint= 224image_size must be divisible by patch_size. Defaults to
224, matching the ImageNet recipe in the original paper.patch_sizeint= 1616, 32
(Base / Large) or 14 (Huge). Defaults to 16.num_classesint= 1000ViTForImageClassification. Defaults to
1000 (ImageNet-1k).in_channelsint= 33 (RGB).dimint= 768768 (ViT-Base).depthint= 1212 (ViT-Base).num_headsint= 12dim must be divisible by num_heads. Defaults to
12.mlp_ratiofloat= 4.0int(dim * mlp_ratio); the standard recipe is 4.0 (i.e. a
4x expansion). Defaults to 4.0.dropoutfloat= 0.00.0.attention_dropoutfloat= 0.00.0.layer_norm_epsfloat= 1e-06nn.LayerNorm
(the two per-block norms and the final pre-head norm). The
original ViT formulation fixes this at 1e-6. Defaults to
1e-6.Attributes
model_typeClassVar[str]"vit" used by the model registry to associate
config instances with the ViT model class.Notes
The canonical variants registered as factory functions in
lucid.models.vision.vit are (see Table 1 of the paper):
============== ===== ====== ======= =========== Variant dim depth heads patch sizes ============== ===== ====== ======= =========== ViT-Base 768 12 12 16, 32 ViT-Large 1024 24 16 16, 32 ViT-Huge 1280 32 16 14 ============== ===== ====== ======= ===========
Reference: Alexey Dosovitskiy et al., "An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale", ICLR 2021, arXiv:2010.11929.
Examples
Build a custom ViT-Base/16 configuration for CIFAR-10 (10 classes,
32x32 inputs, patch size 4 — yields 64 tokens):
>>> from lucid.models.vision.vit import ViTConfig
>>> cfg = ViTConfig(
... image_size=32,
... patch_size=4,
... num_classes=10,
... dim=768,
... depth=12,
... num_heads=12,
... )
>>> cfg.image_size, cfg.patch_size, cfg.num_classes
(32, 4, 10)
Reuse a canonical variant and override only the number of classes:
>>> from lucid.models.vision.vit._pretrained import _CFG_B16
>>> from dataclasses import replace
>>> cfg = replace(_CFG_B16, num_classes=100)
>>> cfg.dim, cfg.depth, cfg.num_classes
(768, 12, 100)