模型简介
模型特点
模型能力
使用案例
🚀 蒸馏数据高效图像变换器(小型模型)
Distilled data-efficient Image Transformer(DeiT)模型在ImageNet - 1k(100万张图像,1000个类别)上以224x224的分辨率进行了预训练和微调。它最初由Touvron等人在论文Training data-efficient image transformers & distillation through attention中提出,并首次在该仓库发布。不过,权重是由Ross Wightman从timm仓库转换而来。
免责声明:发布DeiT的团队并未为该模型撰写模型卡片,此模型卡片由Hugging Face团队撰写。
✨ 主要特性
- 该模型是一个蒸馏视觉变换器(ViT),除了类别令牌外,还使用蒸馏令牌,在预训练和微调过程中能有效地从教师模型(CNN)中学习。
- 蒸馏令牌通过反向传播学习,通过自注意力层与类别([CLS])和补丁令牌进行交互。
- 图像以固定大小的补丁序列(分辨率16x16)呈现给模型,并进行线性嵌入。
📦 安装指南
文档未提及具体安装步骤,跳过此章节。
💻 使用示例
基础用法
由于该模型是蒸馏ViT模型,你可以将其插入DeiTModel、DeiTForImageClassification或DeiTForImageClassificationWithTeacher。请注意,模型期望使用DeiTFeatureExtractor来准备数据。这里我们使用AutoFeatureExtractor,它会根据模型名称自动使用合适的特征提取器。
以下是如何使用该模型将COCO 2017数据集中的一张图像分类为1000个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-small-distilled-patch16-224')
model = DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-small-distilled-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即将支持。
📚 详细文档
预期用途和限制
你可以使用原始模型进行图像分类。请查看模型中心,以查找针对你感兴趣的任务进行微调的版本。
训练数据
该模型在ImageNet - 1k上进行了预训练和蒸馏微调,这是一个包含100万张图像和1000个类别的数据集。
训练过程
预处理
训练/验证期间图像预处理的确切细节可以在此处找到。
推理时,图像会被调整/重新缩放至相同的分辨率(256x256),中心裁剪为224x224,并在RGB通道上使用ImageNet的均值和标准差进行归一化。
预训练
该模型在单个8 - GPU节点上训练了3天。训练分辨率为224。对于所有超参数(如批量大小和学习率),请参考原论文的表9。
评估结果
模型 | ImageNet top - 1准确率 | ImageNet top - 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}
}
📄 许可证
本项目采用Apache - 2.0许可证。









