> This is the documentation for LibreYOLO 1.5.0. Current version: https://www.libreyolo.com/docs/models/vit

# ViT
The classic Vision Transformer: a pure transformer applied to fixed-size image patches, with a learned class token and no convolutions. LibreYOLO ships four AugReg-pretrained sizes for image classification.
Tasks: classify. Install: pip install libreyolo.
Verified against LibreYOLO v1.5.0.

## Install

ViT needs no optional extra. Everything it imports is in the base install.

```bash
pip install libreyolo
```

## Predict

Weights download from Hugging Face on first use and are cached locally.

**Python**

```python
from libreyolo import LibreYOLO, SAMPLE_IMAGE

model = LibreYOLO("LibreViTti-cls.pt")
result = model(SAMPLE_IMAGE, save=True)

probs = result.probs
print(probs.top1, probs.top1conf)
print(probs.top5, probs.top5conf)
```

**CLI**

```bash
libreyolo predict model=LibreViTti-cls.pt source=https://raw.githubusercontent.com/LibreYOLO/libreyolo/release/libreyolo/assets/parkour.jpg save=True
```

A classifier returns `result.probs` instead of `result.boxes`: `top1`
and `top5` give class indices, `top1conf` and `top5conf` give their
confidences. Preprocessing resizes and center-crops to a fixed 224px input,
using timm's AugReg evaluation recipe: bicubic interpolation at a 0.9 crop
fraction. See [prediction](/docs/v1.5.0/predict) for sources, streaming and result
handling.

## Variants

Four sizes, tiny through large, sharing one fixed 224px, patch-16 graph and
differing in embedding width and transformer depth. LibreYOLO ships this
family inference-only: prediction, ImageNet-style top-1/top-5 validation and
export are supported, and the AugReg fine-tuning recipe is not implemented.

## Validate

`val()` runs against an ImageFolder-style split (a directory with `train/` and
`val/` subfolders, one folder per class) and returns top-1 and top-5 accuracy.

**Python**

```python
from libreyolo import LibreYOLO

model = LibreYOLO("LibreViTti-cls.pt")

# data is a directory root with train/ and val/ class-folder splits
# (ImageFolder layout), not a dataset YAML.
metrics = model.val(data="imagenet-1k/")

print(metrics["metrics/accuracy_top1"])
print(metrics["metrics/accuracy_top5"])
```

**CLI**

```bash
libreyolo val model=LibreViTti-cls.pt data=imagenet-1k/
```

## Export

| Task | ONNX | TorchScript | ExecuTorch | TensorRT | OpenVINO | Paddle | MNN | RKNN | ncnn | TFLite | CoreML | Core AI |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| classify | yes | yes | yes | yes | yes |  |  |  | yes |  |  |  |

A "yes" means the export is supported. An empty cell means the exporter refuses that combination.

An exported artifact loads back through `LibreYOLO()` on its file suffix, so a
`.onnx` or `.engine` file behaves like a checkpoint and returns the same
`Results`. [Export](/docs/v1.5.0/export) lists the arguments every format accepts and
the extras a few of them add.

**Python**

```python
from libreyolo import LibreYOLO

model = LibreYOLO("LibreViTti-cls.pt")
model.export(format="onnx")
model.export(format="tensorrt", half=True)
```

**CLI**

```bash
libreyolo export model=LibreViTti-cls.pt format=onnx
libreyolo export model=LibreViTti-cls.pt format=tensorrt half=True
```

**Use the exported file**

```python
from libreyolo import LibreYOLO, SAMPLE_IMAGE

# The factory routes on the file suffix, so an exported artifact loads
# like any checkpoint and returns the same Results object.
model = LibreYOLO("LibreViTti-cls.onnx")
result = model(SAMPLE_IMAGE)

print(result.probs.top1)
```

## Checkpoints

Every published weight file for this family.

| File | Input (px) | Task | Weights license |
| --- | --- | --- | --- |
| `LibreViTti-cls.pt` | 224 | classify | apache-2.0 |
| `LibreViTs-cls.pt` | 224 | classify | apache-2.0 |
| `LibreViTb-cls.pt` | 224 | classify | apache-2.0 |
| `LibreViTl-cls.pt` | 224 | classify | apache-2.0 |

## Licensing

Check the license on the Hugging Face repository of the specific weights you download. That repository is authoritative and licenses are not always uniform across a family. This is a description of the licenses involved, not legal advice.

- Original work: ViT, Google Research
- Upstream license: Apache-2.0
- Upstream source: https://github.com/google-research/vision_transformer
- LibreYOLO code: Apache-2.0
- Weights: Apache-2.0, republished at https://huggingface.co/LibreYOLO
- Interpretation: Apache-2.0 is a permissive license, so these weights can be used in commercial and closed-source products. It asks you to keep its license text and attribution notices with any copy of the weights you redistribute, and it grants a patent license. It places no obligation on your own application code. LibreYOLO's runtime code for this family is a derived port of the Apache-2.0 timm Vision Transformer implementation (Ross Wightman, huggingface/pytorch-image-models), kept checkpoint-compatible with the shipped tensors. The four AugReg checkpoints themselves are timm's Apache-2.0 conversion of Google Research's own AugReg pretraining, so the code and the weights carry the same permissive terms end to end.

## Citation

```bibtex
@article{dosovitskiy2020vit,
  title={An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale},
  author={Dosovitskiy, Alexey and Beyer, Lucas and Kolesnikov, Alexander and Weissenborn, Dirk and Zhai, Xiaohua and Unterthiner, Thomas and  Dehghani, Mostafa and Minderer, Matthias and Heigold, Georg and Gelly, Sylvain and Uszkoreit, Jakob and Houlsby, Neil},
  journal={ICLR},
  year={2021}
}

@article{steiner2021augreg,
  title={How to train your ViT? Data, Augmentation, and Regularization in Vision Transformers},
  author={Steiner, Andreas and Kolesnikov, Alexander and and Zhai, Xiaohua and Wightman, Ross and Uszkoreit, Jakob and Beyer, Lucas},
  journal={arXiv preprint arXiv:2106.10270},
  year={2021}
}
```

Copied from https://github.com/google-research/vision_transformer#bibtex


## ViT architecture

- ViT ti: [SVG](/diagrams/models/vit/ti-classify.svg), [interactive diagram](/diagrams/models/vit/ti-classify.html)
- ViT s: [SVG](/diagrams/models/vit/s-classify.svg), [interactive diagram](/diagrams/models/vit/s-classify.html)
- ViT b: [SVG](/diagrams/models/vit/b-classify.svg), [interactive diagram](/diagrams/models/vit/b-classify.html)
- ViT l: [SVG](/diagrams/models/vit/l-classify.svg), [interactive diagram](/diagrams/models/vit/l-classify.html)
- ViT family: [SVG](/diagrams/models/vit/family-classify.svg), [interactive diagram](/diagrams/models/vit/family-classify.html)
