英文

数据高效图像变换器 (小型模型)

数据高效图像变换器 (DeiT) 模型在 ImageNet-1k (100万张图片,1000个类别) 数据集上进行了预训练和微调,分辨率为224x224。它首次在 Touvron 等人的论文中引入,并首次在 Ross Wightman 进行的转换中发布。

免责声明:发布 DeiT 的团队没有为该模型编写模型卡片,因此这份模型卡片是由 Hugging Face 团队撰写的。

模型描述

该模型实际上是一种训练效率更高的 Vision Transformer (ViT)。

Vision Transformer (ViT) 是一个 transformer 编码器模型(类似于 BERT),在一个大规模图像集合上进行了监督式预训练和微调,即 ImageNet-1k 数据集,分辨率为224x224像素。

图像被表示为固定大小的块序列(分辨率为16x16),然后进行线性嵌入。在序列的开头还添加了一个[CLS]令牌,用于分类任务。在将序列馈送到 Transformer 编码器的层之前,还会添加绝对位置嵌入。

通过预训练模型,它学习了图像的内部表示,然后可以用于提取对下游任务有用的特征:例如,如果您有一个带标签的图像数据集,可以在预训练的编码器之上放置一个线性层,从而训练一个标准分类器。通常在[CLS]令牌之上放置一个线性层,因为该令牌的最后隐藏状态可以看作是整个图像的表示。

预期用途和局限性

您可以使用原始模型进行图像分类。请查看 model hub 以查找您感兴趣的任务上的微调版本。

使用方法

由于该模型是一个训练效率更高的 ViT 模型,您可以将其插入 ViTModel 或 ViTForImageClassification 中。请注意,模型期望使用 DeiTFeatureExtractor 准备数据。在这里,我们使用 AutoFeatureExtractor,它将根据模型名称自动使用适当的特征提取器。

下面是如何使用该模型将 COCO 2017 数据集中的图像分类为1,000个 ImageNet 类别之一的示例:

from transformers import AutoFeatureExtractor, ViTForImageClassification
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-small-patch16-224')
model = ViTForImageClassification.from_pretrained('facebook/deit-small-patch16-224')
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 将很快支持。

训练数据

ViT 模型是在 ImageNet-1k 数据集上进行预训练的,该数据集包含 1 百万张图片和 1 千个类别。

训练过程

预处理

关于训练/验证期间图像的精确预处理细节可以在 here 中找到。

在推理时,图像将被调整大小/缩放到相同的分辨率(256x256),在224x224处进行中心裁剪,并使用 ImageNet 的均值和标准差对 RGB 通道进行归一化。

预训练

该模型在单个8-GPU节点上进行了3天的训练。训练分辨率为224。关于所有超参数(例如批大小和学习率),我们参考了原始论文的表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)获得的。当然,增加模型的大小将带来更好的性能。

BibTeX 条目和引文信息

@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}
}