模型简介
模型特点
模型能力
使用案例
🚀 数据高效图像变换器(小型模型)
数据高效图像变换器(DeiT)模型在 ImageNet - 1k(100 万张图像,1000 个类别)上以 224x224 分辨率进行了预训练和微调。它最初由 Touvron 等人在论文 Training data - efficient image transformers & distillation through attention 中提出,并首次在 此仓库 中发布。不过,这些权重是由 Ross Wightman 从 [timm 仓库](https://github.com/rwightman/pytorch - image - models) 转换而来的。
免责声明:发布 DeiT 的团队并未为此模型编写模型卡片,此模型卡片由 Hugging Face 团队编写。
🚀 快速开始
本模型可用于图像分类任务。你可以在 模型中心 查找针对你感兴趣任务的微调版本。
由于该模型是经过更高效训练的 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-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
# 模型预测 1000 个 ImageNet 类别之一
predicted_class_idx = logits.argmax(-1).item()
print("Predicted class:", model.config.id2label[predicted_class_idx])
目前,特征提取器和模型都支持 PyTorch。Tensorflow 和 JAX/FLAX 支持即将推出。
✨ 主要特性
- 本模型实际上是一个训练更高效的视觉变换器(ViT)。
- 视觉变换器(ViT)是一个类似 BERT 的变换器编码器模型,在大量图像(即 ImageNet - 1k)上以监督方式进行预训练和微调,图像分辨率为 224x224 像素。
- 图像以固定大小的块(分辨率 16x16)序列形式输入模型,这些块经过线性嵌入。同时,在序列开头添加一个 [CLS] 标记用于分类任务。在将序列输入变换器编码器层之前,还会添加绝对位置嵌入。
- 通过预训练,模型学习到图像的内部表示,可用于提取对下游任务有用的特征。例如,如果你有一个带标签的图像数据集,可以在预训练编码器之上放置一个线性层来训练一个标准分类器。通常会在线性层放置在 [CLS] 标记之上,因为该标记的最后隐藏状态可视为整个图像的表示。
📦 安装指南
文档未提及安装步骤,故跳过此章节。
💻 使用示例
基础用法
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
# 模型预测 1000 个 ImageNet 类别之一
predicted_class_idx = logits.argmax(-1).item()
print("Predicted class:", model.config.id2label[predicted_class_idx])
高级用法
文档未提及高级用法代码示例,故跳过此部分。
📚 详细文档
模型描述
本模型实际上是一个训练更高效的视觉变换器(ViT)。
视觉变换器(ViT)是一个类似 BERT 的变换器编码器模型,在大量图像(即 ImageNet - 1k)上以监督方式进行预训练和微调,图像分辨率为 224x224 像素。
图像以固定大小的块(分辨率 16x16)序列形式输入模型,这些块经过线性嵌入。同时,在序列开头添加一个 [CLS] 标记用于分类任务。在将序列输入变换器编码器层之前,还会添加绝对位置嵌入。
通过预训练,模型学习到图像的内部表示,可用于提取对下游任务有用的特征。例如,如果你有一个带标签的图像数据集,可以在预训练编码器之上放置一个线性层来训练一个标准分类器。通常会在线性层放置在 [CLS] 标记之上,因为该标记的最后隐藏状态可视为整个图像的表示。
预期用途和限制
你可以使用原始模型进行图像分类。可在 模型中心 查找针对你感兴趣任务的微调版本。
训练数据
ViT 模型在 ImageNet - 1k 上进行预训练,该数据集包含 100 万张图像和 1000 个类别。
训练过程
预处理
训练/验证期间图像预处理的确切细节可在 此处 找到。
在推理时,图像会被调整大小/缩放至相同分辨率(256x256),中心裁剪为 224x224,并使用 ImageNet 的均值和标准差在 RGB 通道上进行归一化。
预训练
模型在单个 8 - GPU 节点上训练了 3 天。训练分辨率为 224。所有超参数(如批量大小和学习率)可参考原论文的表 9。
评估结果
模型 | ImageNet 前 1 准确率 | ImageNet 前 5 准确率 | 参数数量 | URL |
---|---|---|---|---|
DeiT - tiny | 72.2 | 91.1 | 5M | https://huggingface.co/facebook/deit - tiny - patch16 - 224 |
DeiT - small | 79.9 | 95.0 | 22M | https://huggingface.co/facebook/deit - small - patch16 - 224 |
DeiT - base | 81.8 | 95.6 | 86M | https://huggingface.co/facebook/deit - base - patch16 - 224 |
DeiT - tiny distilled | 74.5 | 91.9 | 6M | https://huggingface.co/facebook/deit - tiny - distilled - patch16 - 224 |
DeiT - small distilled | 81.2 | 95.4 | 22M | https://huggingface.co/facebook/deit - small - distilled - patch16 - 224 |
DeiT - base distilled | 83.4 | 96.5 | 87M | https://huggingface.co/facebook/deit - base - distilled - patch16 - 224 |
DeiT - base 384 | 82.9 | 96.2 | 87M | https://huggingface.co/facebook/deit - base - patch16 - 384 |
DeiT - base distilled 384 (1000 epochs) | 85.2 | 97.2 | 88M | https://huggingface.co/facebook/deit - base - distilled - patch16 - 384 |
注意,对于微调,使用更高分辨率(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}
}
🔧 技术细节
文档未提供足够技术实现细节(少于 50 字),故跳过此章节。
📄 许可证
本模型使用 Apache - 2.0 许可证。









