模型:
facebook/deit-tiny-patch16-224
任务:
许可:
数据有效的图像转换器(DeiT)模型在ImageNet-1k(100万张图像,1000个类别)的分辨率为224x224的数据上进行了预训练和微调。它最初在Touvron等人的论文 Training data-efficient image transformers & distillation through attention 中首次提出,并在 this repository 中首次发布。然而,权重是从Ross Wightman的 timm repository 中转换而来的。
声明:发布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数据集中的图像分类为其中一个1000个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-tiny-patch16-224')
model = ViTForImageClassification.from_pretrained('facebook/deit-tiny-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 上进行预训练的,该数据集包含100万张图像和1000个类别。
关于训练/验证过程中图像的预处理的确切细节,可以在 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)可以获得最佳结果。当然,增加模型大小会带来更好的性能。
@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}
}