# Segment Anything ViT

**Category:** ViT · **Code:**
[`timm/models/vision_transformer_sam.py`](https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/vision_transformer_sam.py)
· **Architectures:** 4 · **Pretrained weights:** 3 (0 with ImageNet-1k results)

The ViT image encoder of the Segment Anything Model (SAM), a plain ViT backbone with windowed and global attention
blocks and relative position embeddings as in ViTDet.

## Papers

* [Exploring Plain Vision Transformer Backbones for Object Detection](https://arxiv.org/abs/2203.16527)
* [Segment Anything](https://arxiv.org/abs/2304.02643)
* [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://arxiv.org/abs/2010.11929)

## Usage

See the [Quickstart](../quickstart) for loading pretrained weights, image preprocessing and fine-tuning, and [Feature
Extraction](../feature_extraction) for using the models as backbones.

```py
>>> import timm
>>> model = timm.create_model('samvit_base_patch16.sa1b', pretrained=True)
>>> data_config = timm.data.resolve_model_data_config(model)
```

## Model API support

| Feature | `VisionTransformerSAM` |
|---|---|
| [`features_only=True` feature maps](../model_api#intermediate-features) | ✓ (strides 16, 16, 16) |
| [`forward_intermediates()`](../model_api#intermediate-features) | ✓ |
| [`prune_intermediate_layers()`](../model_api#intermediate-features) | ✓ |
| [Any input size, no changes needed](../model_api#input-size) | ✓ |
| [`set_input_size()`](../model_api#input-size) | – |
| [`dynamic_img_size=True`](../model_api#input-size) | – |
| [`set_grad_checkpointing()`](../model_api#training-hooks) | ✓ |
| [Layer-wise LR decay (`group_matcher()`)](../model_api#training-hooks) | ✓ |
| [`reset_classifier()`](../model_api#classifier) | ✓ |

✓ supported, ✓ (square) works for square sizes only, ✗ not supported, – not needed (the model already accepts any input
size). Checked by running each feature on a randomly initialized model (`VisionTransformerSAM` with
`samvit_base_patch16_224` at 224), other architectures of the same class are expected to behave the same. See the [Model
API](../model_api) page for details.

## Architectures

| Architecture | Params (M) | GMACs | Input size | Pretrained weights |
|---|---:|---:|---:|---:|
| `samvit_base_patch16` | 89.67 | 486.43 | 1024 | 1 |
| `samvit_base_patch16_224` | 86.46 | 17.54 | 224 | 0 |
| `samvit_huge_patch16` | 637.03 | 2982.23 | 1024 | 1 |
| `samvit_large_patch16` | 308.28 | 1493.86 | 1024 | 1 |

GMACs and parameter counts are for the default input size and classifier of each architecture, from the [benchmark
results](https://github.com/huggingface/pytorch-image-models/tree/main/results).

## Pretrained weights

Top-1 is ImageNet-1k validation accuracy from
[`results-imagenet.csv`](https://github.com/huggingface/pytorch-image-models/blob/main/results/results-imagenet.csv) at
the listed eval size, - if the weight has no ImageNet-1k result (e.g. pretraining or image-text weights). Classes is the
size of the pretrained classifier, none for weights without one.

| Weight | Input size | Top-1 (%) | Eval size | Classes | License | Hub |
|---|---:|---:|---:|---:|---|---|
| `samvit_base_patch16.sa1b` | 1024 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/samvit_base_patch16.sa1b) |
| `samvit_huge_patch16.sa1b` | 1024 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/samvit_huge_patch16.sa1b) |
| `samvit_large_patch16.sa1b` | 1024 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/samvit_large_patch16.sa1b) |

