模型:
facebook/deit-base-distilled-patch16-384
任务:
许可:
基于蒸馏的数据高效图像变换器(DeiT)模型是在ImageNet-1k上使用224x224分辨率进行预训练,并在384x384分辨率上进行微调的(100万张图像,1,000个类别)。它最初由Touvron等人在 Training data-efficient image transformers & distillation through attention 论文中提出,并在 this repository 中首次发布。然而,权重是由Ross Wightman从 timm repository 进行转换的。
免责声明:发布DeiT的团队没有为此模型编写模型卡片,因此该模型卡片由Hugging Face团队编写。
此模型是蒸馏的视觉Transformer(ViT)。它在预训练和微调过程中通过与类别([CLS])和补丁令牌通过自注意力层进行交互,使用蒸馏令牌(distillation token)有效地从教师(CNN)中学习。通过反向传播,蒸馏令牌通过自注意力层与类别和补丁令牌进行交互,从而学习得到。
图像以固定大小的补丁序列(16x16分辨率)的形式呈现给模型,然后进行线性嵌入。
您可以使用原始模型进行图像分类。查看 model hub 以找到您感兴趣的任务的微调版本。
由于此模型是蒸馏的ViT模型,您可以将其插入DeiTModel、DeiTForImageClassification或DeiTForImageClassificationWithTeacher模型中。注意,模型期望使用DeiTFeatureExtractor准备数据。在这里,我们使用AutoFeatureExtractor,它将根据模型名称自动使用适当的特征提取器。
以下是如何使用此模型将COCO 2017数据集的图像分类为1,000个ImageNet类别之一的示例:
from transformers import AutoFeatureExtractor, DeiTForImageClassificationWithTeacher
from PIL import Image
import requests
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
feature_extractor = AutoFeatureExtractor.from_pretrained('facebook/deit-base-distilled-patch16-384')
model = DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-384')
inputs = feature_extractor(images=image, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
# model predicts one of the 1000 ImageNet classes
predicted_class_idx = logits.argmax(-1).item()
print("Predicted class:", model.config.id2label[predicted_class_idx])
目前,特征提取器和模型都支持PyTorch。Tensorflow和JAX/FLAX即将推出。
此模型是在包含1百万张图像和1k个类别的 ImageNet-1k 数据集上进行预训练和蒸馏的。
关于训练/验证期间图像的预处理的详细信息可以在 here 中找到。
推理时,图像被调整大小/缩放到相同分辨率(438x438),并在384x384处进行中心裁剪,然后使用ImageNet的均值和标准差对RGB通道进行归一化。
该模型在一台8-GPU节点上进行了3天的训练。预训练分辨率为224x224。有关所有超参数(如批量大小和学习率),请参考原始论文的表格9。
| Model | ImageNet top-1 accuracy | ImageNet top-5 accuracy | # params | URL |
|---|---|---|---|---|
| DeiT-tiny | 72.2 | 91.1 | 5M | 12310321 |
| DeiT-small | 79.9 | 95.0 | 22M | 12311321 |
| DeiT-base | 81.8 | 95.6 | 86M | 12312321 |
| DeiT-tiny distilled | 74.5 | 91.9 | 6M | 12313321 |
| DeiT-small distilled | 81.2 | 95.4 | 22M | 12314321 |
| DeiT-base distilled | 83.4 | 96.5 | 87M | 12315321 |
| DeiT-base 384 | 82.9 | 96.2 | 87M | 12316321 |
| DeiT-base distilled 384 (1000 epochs) | 85.2 | 97.2 | 88M | 12317321 |
请注意,进行微调时,最佳结果是使用更高的分辨率(384x384)。当然,增加模型大小会导致更好的性能。
@misc{touvron2021training,
title={Training data-efficient image transformers & distillation through attention},
author={Hugo Touvron and Matthieu Cord and Matthijs Douze and Francisco Massa and Alexandre Sablayrolles and Hervé Jégou},
year={2021},
eprint={2012.12877},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
@misc{wu2020visual,
title={Visual Transformers: Token-based Image Representation and Processing for Computer Vision},
author={Bichen Wu and Chenfeng Xu and Xiaoliang Dai and Alvin Wan and Peizhao Zhang and Zhicheng Yan and Masayoshi Tomizuka and Joseph Gonzalez and Kurt Keutzer and Peter Vajda},
year={2020},
eprint={2006.03677},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
@inproceedings{deng2009imagenet,
title={Imagenet: A large-scale hierarchical image database},
author={Deng, Jia and Dong, Wei and Socher, Richard and Li, Li-Jia and Li, Kai and Fei-Fei, Li},
booktitle={2009 IEEE conference on computer vision and pattern recognition},
pages={248--255},
year={2009},
organization={Ieee}
}