Buckets:
ViTMAE
ViTMAE is a self-supervised vision model that is pretrained by masking large portions of an image (~75%). An encoder processes the visible image patches and a decoder reconstructs the missing pixels from the encoded patches and mask tokens. After pretraining, the encoder can be reused for downstream tasks like image classification or object detection — often outperforming models trained with supervised learning.

You can find all the original ViTMAE checkpoints under the AI at Meta organization.
Click on the ViTMAE models in the right sidebar for more examples of how to apply ViTMAE to vision tasks.
The example below demonstrates how to reconstruct the missing pixels with the ViTMAEForPreTraining class.
import torch
import requests
from PIL import Image
from transformers import ViTImageProcessor, ViTMAEForPreTraining
from accelerate import Accelerator
device = Accelerator().device
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
image = Image.open(requests.get(url, stream=True).raw)
processor = ViTImageProcessor.from_pretrained("facebook/vit-mae-base")
inputs = processor(image, return_tensors="pt")
inputs = {k: v.to(device) for k, v in inputs.items()}
model = ViTMAEForPreTraining.from_pretrained("facebook/vit-mae-base", attn_implementation="sdpa").to(device)
with torch.no_grad():
outputs = model(**inputs)
reconstruction = outputs.logits
Notes
- ViTMAE is typically used in two stages. Self-supervised pretraining with ViTMAEForPreTraining, and then discarding the decoder and fine-tuning the encoder. After fine-tuning, the weights can be plugged into a model like ViTForImageClassification.
- Use ViTImageProcessor for input preparation.
Resources
- Refer to this notebook to learn how to visualize the reconstructed pixels from ViTMAEForPreTraining.
ViTMAEConfig[[transformers.ViTMAEConfig]]
class transformers.ViTMAEConfigtransformers.ViTMAEConfigint, optional, defaults to 768) --
Dimensionality of the encoder layers and the pooler layer.
- num_hidden_layers (
int, optional, defaults to 12) -- Number of hidden layers in the Transformer encoder. - num_attention_heads (
int, optional, defaults to 12) -- Number of attention heads for each attention layer in the Transformer encoder. - intermediate_size (
int, optional, defaults to 3072) -- Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder. - hidden_act (
strorfunction, optional, defaults to"gelu") -- The non-linear activation function (function or string) in the encoder and pooler. If string,"gelu","relu","selu"and"gelu_new"are supported. - hidden_dropout_prob (
float, optional, defaults to 0.0) -- The dropout probability for all fully connected layers in the embeddings, encoder, and pooler. - attention_probs_dropout_prob (
float, optional, defaults to 0.0) -- The dropout ratio for the attention probabilities. - initializer_range (
float, optional, defaults to 0.02) -- The standard deviation of the truncated_normal_initializer for initializing all weight matrices. - layer_norm_eps (
float, optional, defaults to 1e-12) -- The epsilon used by the layer normalization layers. - image_size (
int, optional, defaults to 224) -- The size (resolution) of each image. - patch_size (
int, optional, defaults to 16) -- The size (resolution) of each patch. - num_channels (
int, optional, defaults to 3) -- The number of input channels. - qkv_bias (
bool, optional, defaults toTrue) -- Whether to add a bias to the queries, keys and values. - decoder_num_attention_heads (
int, optional, defaults to 16) -- Number of attention heads for each attention layer in the decoder. - decoder_hidden_size (
int, optional, defaults to 512) -- Dimensionality of the decoder. - decoder_num_hidden_layers (
int, optional, defaults to 8) -- Number of hidden layers in the decoder. - decoder_intermediate_size (
int, optional, defaults to 2048) -- Dimensionality of the "intermediate" (i.e., feed-forward) layer in the decoder. - mask_ratio (
float, optional, defaults to 0.75) -- The ratio of the number of masked tokens in the input sequence. - norm_pix_loss (
bool, optional, defaults toFalse) -- Whether or not to train with normalized pixels (see Table 3 in the paper). Using normalized pixels improved representation quality in the experiments of the authors.0
This is the configuration class to store the configuration of a ViTMAEModel. It is used to instantiate an ViT MAE model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the ViT facebook/vit-mae-base architecture.
Configuration objects inherit from PreTrainedConfig and can be used to control the model outputs. Read the documentation from PreTrainedConfig for more information.
Example:
>>> from transformers import ViTMAEConfig, ViTMAEModel
>>> # Initializing a ViT MAE vit-mae-base style configuration
>>> configuration = ViTMAEConfig()
>>> # Initializing a model (with random weights) from the vit-mae-base style configuration
>>> model = ViTMAEModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
ViTMAEModel[[transformers.ViTMAEModel]]
class transformers.ViTMAEModeltransformers.ViTMAEModel
The bare Vit Mae Model outputting raw hidden-states without any specific head on top.
This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
forwardtransformers.ViTMAEModel.forwardtorch.FloatTensor of shape (batch_size, num_channels, image_size, image_size), optional) --
The tensors corresponding to the input images. Pixel values can be obtained using
ViTImageProcessor. See ViTImageProcessor.call() for details (processor_class uses
ViTImageProcessor for processing images).
noise (
torch.FloatTensorof shape(batch_size, sequence_length), optional) -- Mainly used for testing purposes to control randomness and maintain the reproducibilityinterpolate_pos_encoding (
bool, optional, defaultFalse) -- Whether to interpolate the pre-trained position encodings. This is mainly used to use the model on higher resolution images.0transformers.models.vit_mae.modeling_vit_mae.ViTMAEModelOutputortuple(torch.FloatTensor)Atransformers.models.vit_mae.modeling_vit_mae.ViTMAEModelOutputor a tuple oftorch.FloatTensor(ifreturn_dict=Falseis passed or whenconfig.return_dict=False) comprising various elements depending on the configuration (ViTMAEConfig) and inputs.last_hidden_state (
torch.FloatTensorof shape(batch_size, sequence_length, hidden_size), optional, defaults toNone) -- Sequence of hidden-states at the output of the last layer of the model.mask (
torch.FloatTensorof shape(batch_size, sequence_length)) -- Tensor indicating which patches are masked (1) and which are not (0).ids_restore (
torch.LongTensorof shape(batch_size, sequence_length)) -- Tensor containing the original index of the (shuffled) masked patches.hidden_states (
tuple[torch.FloatTensor], optional, returned whenoutput_hidden_states=Trueis passed or whenconfig.output_hidden_states=True) -- Tuple oftorch.FloatTensor(one for the output of the embeddings, if the model has an embedding layer, + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size).Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
attentions (
tuple[torch.FloatTensor], optional, returned whenoutput_attentions=Trueis passed or whenconfig.output_attentions=True) -- Tuple oftorch.FloatTensor(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length).Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
The ViTMAEModel forward method, overrides the __call__ special method.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while
the latter silently ignores them.
Examples:
>>> from transformers import AutoImageProcessor, ViTMAEModel
>>> from PIL import Image
>>> import requests
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> image_processor = AutoImageProcessor.from_pretrained("facebook/vit-mae-base")
>>> model = ViTMAEModel.from_pretrained("facebook/vit-mae-base")
>>> inputs = image_processor(images=image, return_tensors="pt")
>>> outputs = model(**inputs)
>>> last_hidden_states = outputs.last_hidden_state
ViTMAEForPreTraining[[transformers.ViTMAEForPreTraining]]
class transformers.ViTMAEForPreTrainingtransformers.ViTMAEForPreTraining
The ViTMAE Model transformer with the decoder on top for self-supervised pre-training.
Note that we provide a script to pre-train this model on custom data in our examples directory.
This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
forwardtransformers.ViTMAEForPreTraining.forwardtorch.FloatTensor of shape (batch_size, num_channels, image_size, image_size), optional) --
The tensors corresponding to the input images. Pixel values can be obtained using
ViTImageProcessor. See ViTImageProcessor.call() for details (processor_class uses
ViTImageProcessor for processing images).
noise (
torch.FloatTensorof shape(batch_size, sequence_length), optional) -- Mainly used for testing purposes to control randomness and maintain the reproducibilityinterpolate_pos_encoding (
bool, optional, defaultFalse) -- Whether to interpolate the pre-trained position encodings. This is mainly used to use the model on higher resolution images.0transformers.models.vit_mae.modeling_vit_mae.ViTMAEForPreTrainingOutputortuple(torch.FloatTensor)Atransformers.models.vit_mae.modeling_vit_mae.ViTMAEForPreTrainingOutputor a tuple oftorch.FloatTensor(ifreturn_dict=Falseis passed or whenconfig.return_dict=False) comprising various elements depending on the configuration (ViTMAEConfig) and inputs.loss (
torch.FloatTensorof shape(1,)) -- Pixel reconstruction loss.logits (
torch.FloatTensorof shape(batch_size, sequence_length, patch_size ** 2 * num_channels)) -- Pixel reconstruction logits.mask (
torch.FloatTensorof shape(batch_size, sequence_length)) -- Tensor indicating which patches are masked (1) and which are not (0).ids_restore (
torch.LongTensorof shape(batch_size, sequence_length)) -- Tensor containing the original index of the (shuffled) masked patches.hidden_states (
tuple[torch.FloatTensor], optional, returned whenoutput_hidden_states=Trueis passed or whenconfig.output_hidden_states=True) -- Tuple oftorch.FloatTensor(one for the output of the embeddings, if the model has an embedding layer, + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size).Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
attentions (
tuple[torch.FloatTensor], optional, returned whenoutput_attentions=Trueis passed or whenconfig.output_attentions=True) -- Tuple oftorch.FloatTensor(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length).Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
The ViTMAEForPreTraining forward method, overrides the __call__ special method.
Although the recipe for forward pass needs to be defined within this function, one should call the Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while
the latter silently ignores them.
Examples:
>>> from transformers import AutoImageProcessor, ViTMAEForPreTraining
>>> from PIL import Image
>>> import requests
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> image_processor = AutoImageProcessor.from_pretrained("facebook/vit-mae-base")
>>> model = ViTMAEForPreTraining.from_pretrained("facebook/vit-mae-base")
>>> inputs = image_processor(images=image, return_tensors="pt")
>>> outputs = model(**inputs)
>>> loss = outputs.loss
>>> mask = outputs.mask
>>> ids_restore = outputs.ids_restore
Xet Storage Details
- Size:
- 20.9 kB
- Xet hash:
- a5027b5bfdf1cefb707ce53c3d59c3955ba8f5a20b884a3252b8336de7a05182
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.