Buckets:
| # Swin Transformer V2 | |
| [Swin Transformer V2](https://huggingface.co/papers/2111.09883) is a 3B parameter model that focuses on how to scale a vision model to billions of parameters. It introduces techniques like residual-post-norm combined with cosine attention for improved training stability, log-spaced continuous position bias to better handle varying image resolutions between pre-training and fine-tuning, and a new pre-training method (SimMIM) to reduce the need for large amounts of labeled data. These improvements enable efficiently training very large models (up to 3 billion parameters) capable of processing high-resolution images. | |
| You can find official Swin Transformer V2 checkpoints under the [Microsoft](https://huggingface.co/microsoft?search_models=swinv2) organization. | |
| > [!TIP] | |
| > Click on the Swin Transformer V2 models in the right sidebar for more examples of how to apply Swin Transformer V2 to vision tasks. | |
| <hfoptions id="usage"> | |
| <hfoption id="Pipeline"> | |
| ```py | |
| import torch | |
| from transformers import pipeline | |
| pipeline = pipeline( | |
| task="image-classification", | |
| model="microsoft/swinv2-tiny-patch4-window8-256", | |
| dtype=torch.float16, | |
| device=0 | |
| ) | |
| pipeline("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg") | |
| ``` | |
| </hfoption> | |
| <hfoption id="AutoModel"> | |
| ```py | |
| import torch | |
| import requests | |
| from PIL import Image | |
| from transformers import AutoModelForImageClassification, AutoImageProcessor | |
| image_processor = AutoImageProcessor.from_pretrained( | |
| "microsoft/swinv2-tiny-patch4-window8-256", | |
| ) | |
| model = AutoModelForImageClassification.from_pretrained( | |
| "microsoft/swinv2-tiny-patch4-window8-256", | |
| device_map="auto" | |
| ) | |
| url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg" | |
| image = Image.open(requests.get(url, stream=True).raw) | |
| inputs = image_processor(image, return_tensors="pt").to(model.device) | |
| with torch.no_grad(): | |
| logits = model(**inputs).logits | |
| predicted_class_id = logits.argmax(dim=-1).item() | |
| predicted_class_label = model.config.id2label[predicted_class_id] | |
| print(f"The predicted class label is: {predicted_class_label}") | |
| ``` | |
| </hfoption> | |
| </hfoptions> | |
| ## Notes | |
| - Swin Transformer V2 can pad the inputs for any input height and width divisible by `32`. | |
| - Swin Transformer V2 can be used as a [backbone](../backbones). When `output_hidden_states = True`, it outputs both `hidden_states` and `reshaped_hidden_states`. The `reshaped_hidden_states` have a shape of `(batch, num_channels, height, width)` rather than `(batch_size, sequence_length, num_channels)`. | |
| ## Swinv2Config[[transformers.Swinv2Config]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.Swinv2Config</name><anchor>transformers.Swinv2Config</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/swinv2/configuration_swinv2.py#L25</source><parameters>[{"name": "image_size", "val": " = 224"}, {"name": "patch_size", "val": " = 4"}, {"name": "num_channels", "val": " = 3"}, {"name": "embed_dim", "val": " = 96"}, {"name": "depths", "val": " = [2, 2, 6, 2]"}, {"name": "num_heads", "val": " = [3, 6, 12, 24]"}, {"name": "window_size", "val": " = 7"}, {"name": "pretrained_window_sizes", "val": " = [0, 0, 0, 0]"}, {"name": "mlp_ratio", "val": " = 4.0"}, {"name": "qkv_bias", "val": " = True"}, {"name": "hidden_dropout_prob", "val": " = 0.0"}, {"name": "attention_probs_dropout_prob", "val": " = 0.0"}, {"name": "drop_path_rate", "val": " = 0.1"}, {"name": "hidden_act", "val": " = 'gelu'"}, {"name": "use_absolute_embeddings", "val": " = False"}, {"name": "initializer_range", "val": " = 0.02"}, {"name": "layer_norm_eps", "val": " = 1e-05"}, {"name": "encoder_stride", "val": " = 32"}, {"name": "out_features", "val": " = None"}, {"name": "out_indices", "val": " = None"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **image_size** (`int`, *optional*, defaults to 224) -- | |
| The size (resolution) of each image. | |
| - **patch_size** (`int`, *optional*, defaults to 4) -- | |
| The size (resolution) of each patch. | |
| - **num_channels** (`int`, *optional*, defaults to 3) -- | |
| The number of input channels. | |
| - **embed_dim** (`int`, *optional*, defaults to 96) -- | |
| Dimensionality of patch embedding. | |
| - **depths** (`list(int)`, *optional*, defaults to `[2, 2, 6, 2]`) -- | |
| Depth of each layer in the Transformer encoder. | |
| - **num_heads** (`list(int)`, *optional*, defaults to `[3, 6, 12, 24]`) -- | |
| Number of attention heads in each layer of the Transformer encoder. | |
| - **window_size** (`int`, *optional*, defaults to 7) -- | |
| Size of windows. | |
| - **pretrained_window_sizes** (`list(int)`, *optional*, defaults to `[0, 0, 0, 0]`) -- | |
| Size of windows during pretraining. | |
| - **mlp_ratio** (`float`, *optional*, defaults to 4.0) -- | |
| Ratio of MLP hidden dimensionality to embedding dimensionality. | |
| - **qkv_bias** (`bool`, *optional*, defaults to `True`) -- | |
| Whether or not a learnable bias should be added to the queries, keys and values. | |
| - **hidden_dropout_prob** (`float`, *optional*, defaults to 0.0) -- | |
| The dropout probability for all fully connected layers in the embeddings and encoder. | |
| - **attention_probs_dropout_prob** (`float`, *optional*, defaults to 0.0) -- | |
| The dropout ratio for the attention probabilities. | |
| - **drop_path_rate** (`float`, *optional*, defaults to 0.1) -- | |
| Stochastic depth rate. | |
| - **hidden_act** (`str` or `function`, *optional*, defaults to `"gelu"`) -- | |
| The non-linear activation function (function or string) in the encoder. If string, `"gelu"`, `"relu"`, | |
| `"selu"` and `"gelu_new"` are supported. | |
| - **use_absolute_embeddings** (`bool`, *optional*, defaults to `False`) -- | |
| Whether or not to add absolute position embeddings to the patch embeddings. | |
| - **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-05) -- | |
| The epsilon used by the layer normalization layers. | |
| - **encoder_stride** (`int`, *optional*, defaults to 32) -- | |
| Factor to increase the spatial resolution by in the decoder head for masked image modeling. | |
| - **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.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| This is the configuration class to store the configuration of a [Swinv2Model](/docs/transformers/pr_33962/en/model_doc/swinv2#transformers.Swinv2Model). It is used to instantiate a Swin | |
| Transformer v2 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 Swin Transformer v2 | |
| [microsoft/swinv2-tiny-patch4-window8-256](https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256) | |
| architecture. | |
| Configuration objects inherit from [PreTrainedConfig](/docs/transformers/pr_33962/en/main_classes/configuration#transformers.PreTrainedConfig) and can be used to control the model outputs. Read the | |
| documentation from [PreTrainedConfig](/docs/transformers/pr_33962/en/main_classes/configuration#transformers.PreTrainedConfig) for more information. | |
| <ExampleCodeBlock anchor="transformers.Swinv2Config.example"> | |
| Example: | |
| ```python | |
| >>> from transformers import Swinv2Config, Swinv2Model | |
| >>> # Initializing a Swinv2 microsoft/swinv2-tiny-patch4-window8-256 style configuration | |
| >>> configuration = Swinv2Config() | |
| >>> # Initializing a model (with random weights) from the microsoft/swinv2-tiny-patch4-window8-256 style configuration | |
| >>> model = Swinv2Model(configuration) | |
| >>> # Accessing the model configuration | |
| >>> configuration = model.config | |
| ``` | |
| </ExampleCodeBlock> | |
| </div> | |
| ## Swinv2Model[[transformers.Swinv2Model]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.Swinv2Model</name><anchor>transformers.Swinv2Model</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/swinv2/modeling_swinv2.py#L908</source><parameters>[{"name": "config", "val": ""}, {"name": "add_pooling_layer", "val": " = True"}, {"name": "use_mask_token", "val": " = False"}]</parameters><paramsdesc>- **config** ([Swinv2Model](/docs/transformers/pr_33962/en/model_doc/swinv2#transformers.Swinv2Model)) -- | |
| 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 | |
| [from_pretrained()](/docs/transformers/pr_33962/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights. | |
| - **add_pooling_layer** (`bool`, *optional*, defaults to `True`) -- | |
| Whether or not to apply pooling layer. | |
| - **use_mask_token** (`bool`, *optional*, defaults to `False`) -- | |
| Whether or not to create and apply mask tokens in the embedding layer.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| The bare Swinv2 Model outputting raw hidden-states without any specific head on top. | |
| This model inherits from [PreTrainedModel](/docs/transformers/pr_33962/en/main_classes/model#transformers.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](https://pytorch.org/docs/stable/nn.html#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. | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>forward</name><anchor>transformers.Swinv2Model.forward</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/swinv2/modeling_swinv2.py#L933</source><parameters>[{"name": "pixel_values", "val": ": typing.Optional[torch.FloatTensor] = None"}, {"name": "bool_masked_pos", "val": ": typing.Optional[torch.BoolTensor] = None"}, {"name": "output_attentions", "val": ": typing.Optional[bool] = None"}, {"name": "output_hidden_states", "val": ": typing.Optional[bool] = None"}, {"name": "interpolate_pos_encoding", "val": ": bool = False"}, {"name": "return_dict", "val": ": typing.Optional[bool] = None"}]</parameters><paramsdesc>- **pixel_values** (`torch.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](/docs/transformers/pr_33962/en/model_doc/vit#transformers.ViTImageProcessor). See [ViTImageProcessor.__call__()](/docs/transformers/pr_33962/en/model_doc/fuyu#transformers.FuyuImageProcessor.__call__) for details (`processor_class` uses | |
| [ViTImageProcessor](/docs/transformers/pr_33962/en/model_doc/vit#transformers.ViTImageProcessor) for processing images). | |
| - **bool_masked_pos** (`torch.BoolTensor` of shape `(batch_size, num_patches)`, *optional*) -- | |
| Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). | |
| - **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. | |
| - **interpolate_pos_encoding** (`bool`, defaults to `False`) -- | |
| Whether to interpolate the pre-trained position encodings. | |
| - **return_dict** (`bool`, *optional*) -- | |
| Whether or not to return a [ModelOutput](/docs/transformers/pr_33962/en/main_classes/output#transformers.utils.ModelOutput) instead of a plain tuple.</paramsdesc><paramgroups>0</paramgroups><rettype>`transformers.models.swinv2.modeling_swinv2.Swinv2ModelOutput` or `tuple(torch.FloatTensor)`</rettype><retdesc>A `transformers.models.swinv2.modeling_swinv2.Swinv2ModelOutput` 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 ([Swinv2Config](/docs/transformers/pr_33962/en/model_doc/swinv2#transformers.Swinv2Config)) and inputs. | |
| - **last_hidden_state** (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*, defaults to `None`) -- Sequence of hidden-states at the output of the last layer of the model. | |
| - **pooler_output** (`torch.FloatTensor` of shape `(batch_size, hidden_size)`, *optional*, returned when `add_pooling_layer=True` is passed) -- Average pooling of the last layer hidden-state. | |
| - **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. | |
| - **reshaped_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 + one for the output of each stage) of | |
| shape `(batch_size, hidden_size, height, width)`. | |
| Hidden-states of the model at the output of each layer plus the initial embedding outputs reshaped to | |
| include the spatial dimensions.</retdesc></docstring> | |
| The [Swinv2Model](/docs/transformers/pr_33962/en/model_doc/swinv2#transformers.Swinv2Model) forward method, overrides the `__call__` special method. | |
| <Tip> | |
| 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. | |
| </Tip> | |
| <ExampleCodeBlock anchor="transformers.Swinv2Model.forward.example"> | |
| Example: | |
| ```python | |
| ``` | |
| </ExampleCodeBlock> | |
| </div></div> | |
| ## Swinv2ForMaskedImageModeling[[transformers.Swinv2ForMaskedImageModeling]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.Swinv2ForMaskedImageModeling</name><anchor>transformers.Swinv2ForMaskedImageModeling</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/swinv2/modeling_swinv2.py#L1004</source><parameters>[{"name": "config", "val": ""}]</parameters><paramsdesc>- **config** ([Swinv2ForMaskedImageModeling](/docs/transformers/pr_33962/en/model_doc/swinv2#transformers.Swinv2ForMaskedImageModeling)) -- | |
| 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 | |
| [from_pretrained()](/docs/transformers/pr_33962/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| Swinv2 Model with a decoder on top for masked image modeling, as proposed in | |
| [SimMIM](https://huggingface.co/papers/2111.09886). | |
| <Tip> | |
| Note that we provide a script to pre-train this model on custom data in our [examples | |
| directory](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-pretraining). | |
| </Tip> | |
| This model inherits from [PreTrainedModel](/docs/transformers/pr_33962/en/main_classes/model#transformers.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](https://pytorch.org/docs/stable/nn.html#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. | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>forward</name><anchor>transformers.Swinv2ForMaskedImageModeling.forward</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/swinv2/modeling_swinv2.py#L1021</source><parameters>[{"name": "pixel_values", "val": ": typing.Optional[torch.FloatTensor] = None"}, {"name": "bool_masked_pos", "val": ": typing.Optional[torch.BoolTensor] = None"}, {"name": "output_attentions", "val": ": typing.Optional[bool] = None"}, {"name": "output_hidden_states", "val": ": typing.Optional[bool] = None"}, {"name": "interpolate_pos_encoding", "val": ": bool = False"}, {"name": "return_dict", "val": ": typing.Optional[bool] = None"}]</parameters><paramsdesc>- **pixel_values** (`torch.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](/docs/transformers/pr_33962/en/model_doc/vit#transformers.ViTImageProcessor). See [ViTImageProcessor.__call__()](/docs/transformers/pr_33962/en/model_doc/fuyu#transformers.FuyuImageProcessor.__call__) for details (`processor_class` uses | |
| [ViTImageProcessor](/docs/transformers/pr_33962/en/model_doc/vit#transformers.ViTImageProcessor) for processing images). | |
| - **bool_masked_pos** (`torch.BoolTensor` of shape `(batch_size, num_patches)`) -- | |
| Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). | |
| - **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. | |
| - **interpolate_pos_encoding** (`bool`, defaults to `False`) -- | |
| Whether to interpolate the pre-trained position encodings. | |
| - **return_dict** (`bool`, *optional*) -- | |
| Whether or not to return a [ModelOutput](/docs/transformers/pr_33962/en/main_classes/output#transformers.utils.ModelOutput) instead of a plain tuple.</paramsdesc><paramgroups>0</paramgroups><rettype>`transformers.models.swinv2.modeling_swinv2.Swinv2MaskedImageModelingOutput` or `tuple(torch.FloatTensor)`</rettype><retdesc>A `transformers.models.swinv2.modeling_swinv2.Swinv2MaskedImageModelingOutput` 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 ([Swinv2Config](/docs/transformers/pr_33962/en/model_doc/swinv2#transformers.Swinv2Config)) and inputs. | |
| - **loss** (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `bool_masked_pos` is provided) -- Masked image modeling (MLM) loss. | |
| - **reconstruction** (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`) -- Reconstructed pixel values. | |
| - **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. | |
| - **reshaped_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 + one for the output of each stage) of | |
| shape `(batch_size, hidden_size, height, width)`. | |
| Hidden-states of the model at the output of each layer plus the initial embedding outputs reshaped to | |
| include the spatial dimensions.</retdesc></docstring> | |
| The [Swinv2ForMaskedImageModeling](/docs/transformers/pr_33962/en/model_doc/swinv2#transformers.Swinv2ForMaskedImageModeling) forward method, overrides the `__call__` special method. | |
| <Tip> | |
| 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. | |
| </Tip> | |
| <ExampleCodeBlock anchor="transformers.Swinv2ForMaskedImageModeling.forward.example"> | |
| Examples: | |
| ```python | |
| >>> from transformers import AutoImageProcessor, Swinv2ForMaskedImageModeling | |
| >>> import torch | |
| >>> 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("microsoft/swinv2-tiny-patch4-window8-256") | |
| >>> model = Swinv2ForMaskedImageModeling.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256") | |
| >>> num_patches = (model.config.image_size // model.config.patch_size) ** 2 | |
| >>> pixel_values = image_processor(images=image, return_tensors="pt").pixel_values | |
| >>> # create random boolean mask of shape (batch_size, num_patches) | |
| >>> bool_masked_pos = torch.randint(low=0, high=2, size=(1, num_patches)).bool() | |
| >>> outputs = model(pixel_values, bool_masked_pos=bool_masked_pos) | |
| >>> loss, reconstructed_pixel_values = outputs.loss, outputs.reconstruction | |
| >>> list(reconstructed_pixel_values.shape) | |
| [1, 3, 256, 256] | |
| ``` | |
| </ExampleCodeBlock> | |
| </div></div> | |
| ## Swinv2ForImageClassification[[transformers.Swinv2ForImageClassification]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.Swinv2ForImageClassification</name><anchor>transformers.Swinv2ForImageClassification</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/swinv2/modeling_swinv2.py#L1120</source><parameters>[{"name": "config", "val": ""}]</parameters><paramsdesc>- **config** ([Swinv2ForImageClassification](/docs/transformers/pr_33962/en/model_doc/swinv2#transformers.Swinv2ForImageClassification)) -- | |
| 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 | |
| [from_pretrained()](/docs/transformers/pr_33962/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| Swinv2 Model transformer with an image classification head on top (a linear layer on top of the final hidden state | |
| of the [CLS] token) e.g. for ImageNet. | |
| <Tip> | |
| Note that it's possible to fine-tune SwinV2 on higher resolution images than the ones it has been trained on, by | |
| setting `interpolate_pos_encoding` to `True` in the forward of the model. This will interpolate the pre-trained | |
| position embeddings to the higher resolution. | |
| </Tip> | |
| This model inherits from [PreTrainedModel](/docs/transformers/pr_33962/en/main_classes/model#transformers.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](https://pytorch.org/docs/stable/nn.html#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. | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>forward</name><anchor>transformers.Swinv2ForImageClassification.forward</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/swinv2/modeling_swinv2.py#L1135</source><parameters>[{"name": "pixel_values", "val": ": typing.Optional[torch.FloatTensor] = None"}, {"name": "labels", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "output_attentions", "val": ": typing.Optional[bool] = None"}, {"name": "output_hidden_states", "val": ": typing.Optional[bool] = None"}, {"name": "interpolate_pos_encoding", "val": ": bool = False"}, {"name": "return_dict", "val": ": typing.Optional[bool] = None"}]</parameters><paramsdesc>- **pixel_values** (`torch.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](/docs/transformers/pr_33962/en/model_doc/vit#transformers.ViTImageProcessor). See [ViTImageProcessor.__call__()](/docs/transformers/pr_33962/en/model_doc/fuyu#transformers.FuyuImageProcessor.__call__) for details (`processor_class` uses | |
| [ViTImageProcessor](/docs/transformers/pr_33962/en/model_doc/vit#transformers.ViTImageProcessor) for processing images). | |
| - **labels** (`torch.LongTensor` of shape `(batch_size,)`, *optional*) -- | |
| Labels for computing the image classification/regression loss. Indices should be in `[0, ..., | |
| config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If | |
| `config.num_labels > 1` a classification loss is computed (Cross-Entropy). | |
| - **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. | |
| - **interpolate_pos_encoding** (`bool`, defaults to `False`) -- | |
| Whether to interpolate the pre-trained position encodings. | |
| - **return_dict** (`bool`, *optional*) -- | |
| Whether or not to return a [ModelOutput](/docs/transformers/pr_33962/en/main_classes/output#transformers.utils.ModelOutput) instead of a plain tuple.</paramsdesc><paramgroups>0</paramgroups><rettype>`transformers.models.swinv2.modeling_swinv2.Swinv2ImageClassifierOutput` or `tuple(torch.FloatTensor)`</rettype><retdesc>A `transformers.models.swinv2.modeling_swinv2.Swinv2ImageClassifierOutput` 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 ([Swinv2Config](/docs/transformers/pr_33962/en/model_doc/swinv2#transformers.Swinv2Config)) and inputs. | |
| - **loss** (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided) -- Classification (or regression if config.num_labels==1) loss. | |
| - **logits** (`torch.FloatTensor` of shape `(batch_size, config.num_labels)`) -- Classification (or regression if config.num_labels==1) scores (before SoftMax). | |
| - **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. | |
| - **reshaped_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 + one for the output of each stage) of | |
| shape `(batch_size, hidden_size, height, width)`. | |
| Hidden-states of the model at the output of each layer plus the initial embedding outputs reshaped to | |
| include the spatial dimensions.</retdesc></docstring> | |
| The [Swinv2ForImageClassification](/docs/transformers/pr_33962/en/model_doc/swinv2#transformers.Swinv2ForImageClassification) forward method, overrides the `__call__` special method. | |
| <Tip> | |
| 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. | |
| </Tip> | |
| <ExampleCodeBlock anchor="transformers.Swinv2ForImageClassification.forward.example"> | |
| Example: | |
| ```python | |
| >>> from transformers import AutoImageProcessor, Swinv2ForImageClassification | |
| >>> import torch | |
| >>> from datasets import load_dataset | |
| >>> dataset = load_dataset("huggingface/cats-image") | |
| >>> image = dataset["test"]["image"][0] | |
| >>> image_processor = AutoImageProcessor.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256") | |
| >>> model = Swinv2ForImageClassification.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256") | |
| >>> inputs = image_processor(image, return_tensors="pt") | |
| >>> with torch.no_grad(): | |
| ... logits = model(**inputs).logits | |
| >>> # model predicts one of the 1000 ImageNet classes | |
| >>> predicted_label = logits.argmax(-1).item() | |
| >>> print(model.config.id2label[predicted_label]) | |
| ... | |
| ``` | |
| </ExampleCodeBlock> | |
| </div></div> | |
| <EditOnGithub source="https://github.com/huggingface/transformers/blob/main/docs/source/en/model_doc/swinv2.md" /> |
Xet Storage Details
- Size:
- 31.3 kB
- Xet hash:
- f0c138845d1eb08bbe8352a67bd0898a1e02a8b1b60c15c6cbfb18ee5aba78c4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.