# NaFlex ViT

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

A Vision Transformer with the patch embedding and position encoding in one module, supporting pre-patchified inputs with
variable aspect ratio and resolution (NaFlex), FlexiViT variable patch size and NaViT factorized position embeddings.

## Papers

* [An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale](https://arxiv.org/abs/2010.11929)
* [FlexiViT: One Model for All Patch Sizes](https://arxiv.org/abs/2212.08013)
* [Patch n' Pack: NaViT, a Vision Transformer for any Aspect Ratio and Resolution](https://arxiv.org/abs/2307.06304)
* [SigLIP 2: Multilingual Vision-Language Encoders with Improved Semantic Understanding, Localization, and Dense
  Features](https://arxiv.org/abs/2502.14786)

## 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('naflexvit_base_patch16_parfac_gap.e300_s576_in1k', pretrained=True)
>>> data_config = timm.data.resolve_model_data_config(model)
```

## Model API support

| Feature | `NaFlexVit` |
|---|---|
| [`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 (`NaFlexVit` with
`naflexvit_base_patch16_parfac_gap` at 384), 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 |
|---|---:|---:|---:|---:|
| `naflexvit_base_patch16_gap` | 86.63 | 55.86 | 384 | 1 |
| `naflexvit_base_patch16_map` | 93.72 | 56.23 | 384 | 0 |
| `naflexvit_base_patch16_par_gap` | 86.63 | 55.86 | 384 | 1 |
| `naflexvit_base_patch16_parfac_gap` | 86.46 | 55.86 | 384 | 1 |
| `naflexvit_base_patch16_siglip` | 92.93 | 56.12 | 384 | 1 |
| `naflexvit_so150m2_patch16_reg1_gap` | 136.06 | 89.53 | 384 | 0 |
| `naflexvit_so150m2_patch16_reg1_map` | 142.46 | 90.33 | 384 | 0 |
| `naflexvit_so400m_patch16_siglip` | 427.89 | 259.65 | 384 | 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 |
|---|---:|---:|---:|---:|---|---|
| `naflexvit_base_patch16_gap.e300_s576_in1k` | 384 | 83.340 | 384 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/naflexvit_base_patch16_gap.e300_s576_in1k) |
| `naflexvit_base_patch16_par_gap.e300_s576_in1k` | 384 | 83.508 | 384 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/naflexvit_base_patch16_par_gap.e300_s576_in1k) |
| `naflexvit_base_patch16_parfac_gap.e300_s576_in1k` | 384 | 83.468 | 384 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/naflexvit_base_patch16_parfac_gap.e300_s576_in1k) |
| `naflexvit_base_patch16_siglip.v2_webli` | 384 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/naflexvit_base_patch16_siglip.v2_webli) |
| `naflexvit_so400m_patch16_siglip.v2_webli` | 384 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/naflexvit_so400m_patch16_siglip.v2_webli) |

