# BEiT

**类别：** ViT · **代码：** [`timm/models/beit.py`](https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/beit.py) · **架构：** 7 · **预训练权重：** 13（其中 9 个包含 ImageNet-1k 结果）

BEiT 和 BEiT-v2 视觉 Transformer，通过掩码图像建模进行预训练。BEiT 预测被掩码补丁的离散视觉标记，BEiT-v2 使用向量量化视觉标记器作为目标。此处的权重为微调后的分类权重。

## 论文

* [BEiT：图像 Transformer 的 BERT 预训练](https://arxiv.org/abs/2106.08254)
* [BEiT v2：基于向量量化视觉分词器的掩码图像建模](https://arxiv.org/abs/2208.06366)

## 用法

请参阅[快速入门](../quickstart)了解如何加载预训练权重、进行图像预处理和微调，以及参阅[特征提取](../feature_extraction)了解如何将这些模型用作骨干网络。

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

## 模型 API 支持

| 功能 | `Beit` |
|---|---|
| [`features_only=True` 特征图](../model_api#intermediate-features) | ✓（strides 16, 16, 16） |
| [`forward_intermediates()`](../model_api#intermediate-features) | ✓ |
| [`prune_intermediate_layers()`](../model_api#intermediate-features) | ✓ |
| [任意输入尺寸，无需更改](../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) | ✓ |
| [逐层学习率衰减（`group_matcher()`）](../model_api#training-hooks) | ✓ |
| [`reset_classifier()`](../model_api#classifier) | ✓ |

✓ 支持，✓ (square) 仅支持方形尺寸，✗ 不支持，– 无需支持（模型本身已接受任意输入尺寸）。通过在每个功能上运行随机初始化的模型（`Beit` 使用 `beit_base_patch16_224`，尺寸为 224）进行验证，预期同一类的其他架构表现相同。详情请参阅 [Model API](../model_api) 页面。

## 架构

| 架构 | 参数（M） | GMAC | 输入大小 | 预训练权重 |
|---|---:|---:|---:|---:|
| `beit_base_patch16_224` | 86.53 | 17.58 | 224 | 2 |
| `beit_base_patch16_384` | 86.74 | 55.54 | 384 | 1 |
| `beit_large_patch16_224` | 304.43 | 61.60 | 224 | 2 |
| `beit_large_patch16_384` | 305.00 | 191.21 | 384 | 1 |
| `beit_large_patch16_512` | 305.67 | 362.24 | 512 | 1 |
| `beitv2_base_patch16_224` | 86.53 | 17.58 | 224 | 3 |
| `beitv2_large_patch16_224` | 304.43 | 61.60 | 224 | 3 |

GMACs 和参数量对应于每种架构的默认输入大小和分类器，数据来自[基准测试结果](https://github.com/huggingface/pytorch-image-models/tree/main/results)。

## 预训练权重

Top-1 是在所列评估规模下，来自 [`results-imagenet.csv`](https://github.com/huggingface/pytorch-image-models/blob/main/results/results-imagenet.csv) 的 ImageNet-1k 验证准确率，- 表示该权重没有 ImageNet-1k 结果（例如预训练权重或图文权重）。Classes 是预训练分类器的大小，对于没有分类器的权重则为无。

| 权重 | 输入大小 | Top-1（%） | 评估大小 | 类 | 许可证 | Hub |
|---|---:|---:|---:|---:|---|---|
| `beit_base_patch16_224.in22k_ft_in22k` | 224 | - | - | 21841 | apache-2.0 | [timm](https://huggingface.co/timm/beit_base_patch16_224.in22k_ft_in22k) |
| `beit_base_patch16_224.in22k_ft_in22k_in1k` | 224 | 85.238 | 224 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/beit_base_patch16_224.in22k_ft_in22k_in1k) |
| `beit_base_patch16_384.in22k_ft_in22k_in1k` | 384 | 86.814 | 384 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/beit_base_patch16_384.in22k_ft_in22k_in1k) |
| `beit_large_patch16_224.in22k_ft_in22k` | 224 | - | - | 21841 | apache-2.0 | [timm](https://huggingface.co/timm/beit_large_patch16_224.in22k_ft_in22k) |
| `beit_large_patch16_224.in22k_ft_in22k_in1k` | 224 | 87.486 | 224 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/beit_large_patch16_224.in22k_ft_in22k_in1k) |
| `beit_large_patch16_384.in22k_ft_in22k_in1k` | 384 | 88.380 | 384 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/beit_large_patch16_384.in22k_ft_in22k_in1k) |
| `beit_large_patch16_512.in22k_ft_in22k_in1k` | 512 | 88.576 | 512 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/beit_large_patch16_512.in22k_ft_in22k_in1k) |
| `beitv2_base_patch16_224.in1k_ft_in1k` | 224 | 85.596 | 224 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/beitv2_base_patch16_224.in1k_ft_in1k) |
| `beitv2_base_patch16_224.in1k_ft_in22k` | 224 | - | - | 21841 | apache-2.0 | [timm](https://huggingface.co/timm/beitv2_base_patch16_224.in1k_ft_in22k) |
| `beitv2_base_patch16_224.in1k_ft_in22k_in1k` | 224 | 86.494 | 224 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/beitv2_base_patch16_224.in1k_ft_in22k_in1k) |
| `beitv2_large_patch16_224.in1k_ft_in1k` | 224 | 87.414 | 224 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/beitv2_large_patch16_224.in1k_ft_in1k) |
| `beitv2_large_patch16_224.in1k_ft_in22k` | 224 | - | - | 21841 | apache-2.0 | [timm](https://huggingface.co/timm/beitv2_large_patch16_224.in1k_ft_in22k) |
| `beitv2_large_patch16_224.in1k_ft_in22k_in1k` | 224 | 88.406 | 224 | 1000 | apache-2.0 | [timm](https://huggingface.co/timm/beitv2_large_patch16_224.in1k_ft_in22k_in1k) |

## 引用

```bibtex
@inproceedings{beit,
title={{BEiT}: {BERT} Pre-Training of Image Transformers},
author={Hangbo Bao and Li Dong and Songhao Piao and Furu Wei},
booktitle={International Conference on Learning Representations},
year={2022},
url={https://openreview.net/forum?id=p-BhZSz59o4}
}

@article{beitv2,
title={{BEiT v2}: Masked Image Modeling with Vector-Quantized Visual Tokenizers},
author={Zhiliang Peng and Li Dong and Hangbo Bao and Qixiang Ye and Furu Wei},
year={2022},
eprint={2208.06366},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
```

