ViTDet
Last updated
Last updated
The ViTDet model was proposed in by Yanghao Li, Hanzi Mao, Ross Girshick, Kaiming He. VitDet leverages the plain for the task of object detection.
The abstract from the paper is the following:
We explore the plain, non-hierarchical Vision Transformer (ViT) as a backbone network for object detection. This design enables the original ViT architecture to be fine-tuned for object detection without needing to redesign a hierarchical backbone for pre-training. With minimal adaptations for fine-tuning, our plain-backbone detector can achieve competitive results. Surprisingly, we observe: (i) it is sufficient to build a simple feature pyramid from a single-scale feature map (without the common FPN design) and (ii) it is sufficient to use window attention (without shifting) aided with very few cross-window propagation blocks. With plain ViT backbones pre-trained as Masked Autoencoders (MAE), our detector, named ViTDet, can compete with the previous leading methods that were all based on hierarchical backbones, reaching up to 61.3 AP_box on the COCO dataset using only ImageNet-1K pre-training. We hope our study will draw attention to research on plain-backbone detectors.
Tips:
For the moment, only the backbone is available.
This model was contributed by . The original code can be found .
( hidden_size = 768num_hidden_layers = 12num_attention_heads = 12mlp_ratio = 4hidden_act = 'gelu'dropout_prob = 0.0initializer_range = 0.02layer_norm_eps = 1e-06image_size = 224pretrain_image_size = 224patch_size = 16num_channels = 3qkv_bias = Truedrop_path_rate = 0.0window_block_indices = []residual_block_indices = []use_absolute_position_embeddings = Trueuse_relative_position_embeddings = Falsewindow_size = 0out_features = Noneout_indices = None**kwargs )
Parameters
hidden_size (int
, 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.
mlp_ratio (int
, optional, defaults to 4) — Ratio of mlp hidden dim to embedding dim.
hidden_act (str
or function
, 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.
dropout_prob (float
, optional, defaults to 0.0) — The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
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-6) — The epsilon used by the layer normalization layers.
image_size (int
, optional, defaults to 224) — The size (resolution) of each image.
pretrain_image_size (int
, optional, defaults to 224) — The size (resolution) of each image during pretraining.
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 to True
) — Whether to add a bias to the queries, keys and values.
drop_path_rate (float
, optional, defaults to 0.0) — Stochastic depth rate.
window_block_indices (List[int]
, optional) — List of indices of blocks that should have window attention instead of regular global self-attention.
residual_block_indices (List[int]
, optional) — List of indices of blocks that should have an extra residual block after the MLP.
use_absolute_position_embeddings (bool
, optional, defaults to True
) — Whether to add absolute position embeddings to the patch embeddings.
use_relative_position_embeddings (bool
, optional, defaults to False
) — Whether to add relative position embeddings to the attention maps.
window_size (int
, optional, defaults to 0) — The size of the attention window.
out_features (List[str]
, optional) — If used as backbone, list of features to output. Can be any of "stem"
, "stage1"
, "stage2"
, etc. (depending on how many stages the model has). If unset and out_indices
is set, will default to the corresponding stages. If unset and out_indices
is unset, will default to the last stage.
out_indices (List[int]
, optional) — If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and out_features
is set, will default to the corresponding stages. If unset and out_features
is unset, will default to the last stage.
Example:
Copied
( config: VitDetConfig )
Parameters
forward
Parameters
head_mask (torch.FloatTensor
of shape (num_heads,)
or (num_layers, num_heads)
, optional) — Mask to nullify selected heads of the self-attention modules. Mask values selected in [0, 1]
:
1 indicates the head is not masked,
0 indicates the head is masked.
output_attentions (bool
, optional) — Whether or not to return the attentions tensors of all attention layers. See attentions
under returned tensors for more detail.
output_hidden_states (bool
, optional) — Whether or not to return the hidden states of all layers. See hidden_states
under returned tensors for more detail.
Returns
last_hidden_state (torch.FloatTensor
of shape (batch_size, sequence_length, hidden_size)
) — Sequence of hidden-states at the output of the last layer of the model.
hidden_states (tuple(torch.FloatTensor)
, optional, returned when output_hidden_states=True
is passed or when config.output_hidden_states=True
) — Tuple of torch.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 when output_attentions=True
is passed or when config.output_attentions=True
) — Tuple of torch.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.
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:
Copied
This is the configuration class to store the configuration of a . It is used to instantiate an VitDet 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 VitDet architecture.
Configuration objects inherit from and can be used to control the model outputs. Read the documentation from for more information.
config () — Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the method to load the model weights.
The bare VitDet Transformer model outputting raw hidden-states without any specific head on top. This model is a PyTorch subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
( pixel_values: typing.Optional[torch.Tensor] = Nonehead_mask: typing.Optional[torch.Tensor] = Noneoutput_attentions: typing.Optional[bool] = Noneoutput_hidden_states: typing.Optional[bool] = Nonereturn_dict: typing.Optional[bool] = None ) → or tuple(torch.FloatTensor)
pixel_values (torch.FloatTensor
of shape (batch_size, num_channels, height, width)
) — Pixel values. Pixel values can be obtained using . See for details.
return_dict (bool
, optional) — Whether or not to return a instead of a plain tuple.
or tuple(torch.FloatTensor)
A or a tuple of torch.FloatTensor
(if return_dict=False
is passed or when config.return_dict=False
) comprising various elements depending on the configuration () and inputs.
The forward method, overrides the __call__
special method.