license: apache-2.0
tags:
- 图像分类
- 视觉
datasets:
- imagenet
蒸馏版数据高效图像Transformer(基础尺寸模型)
蒸馏数据高效图像Transformer(DeiT)模型,先在224x224分辨率下预训练,再于384x384分辨率下在ImageNet-1k(100万张图像,1000个类别)上微调。该模型首次由Touvron等人在论文通过注意力机制训练数据高效的图像Transformer及蒸馏中提出,并发布于此代码库。不过,权重是由Ross Wightman从timm代码库转换而来。
免责声明:发布DeiT的团队未为此模型编写模型卡,因此本模型卡由Hugging Face团队撰写。
模型描述
该模型是一个蒸馏版视觉Transformer(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-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
predicted_class_idx = logits.argmax(-1).item()
print("预测类别:", model.config.id2label[predicted_class_idx])
目前,特征提取器和模型均支持PyTorch。TensorFlow和JAX/FLAX版本即将推出。
训练数据
该模型在ImageNet-1k上通过蒸馏进行了预训练和微调,该数据集包含100万张图像和1000个类别。
训练流程
预处理
训练/验证期间图像预处理的具体细节可在此处找到。
推理时,图像会被调整大小/重新缩放至相同分辨率(438x438),中心裁剪为384x384,并使用ImageNet的均值和标准差对RGB通道进行归一化。
预训练
模型在单个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蒸馏版 |
74.5 |
91.9 |
6M |
https://huggingface.co/facebook/deit-tiny-distilled-patch16-224 |
DeiT-small蒸馏版 |
81.2 |
95.4 |
22M |
https://huggingface.co/facebook/deit-small-distilled-patch16-224 |
DeiT-base蒸馏版 |
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蒸馏版384(1000轮训练) |
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}
}