license: apache-2.0
tags:
- 图像分类
- 视觉
datasets:
- imagenet
蒸馏版数据高效图像Transformer(微型尺寸模型)
该模型为蒸馏版数据高效图像Transformer(DeiT),在ImageNet-1k(100万张图像,1000个类别)上以224x224分辨率进行预训练和微调。最初由Touvron等人在论文《通过注意力机制训练数据高效的图像Transformer及蒸馏》中提出,并首次发布于此代码库。权重由Ross Wightman从timm库转换而来。
免责声明:DeiT发布团队未为此模型编写说明卡片,本卡片由Hugging Face团队撰写。
模型描述
该模型为蒸馏版视觉Transformer(ViT)。除类别标记外,它使用蒸馏标记在预训练和微调阶段从教师模型(CNN)高效学习。蒸馏标记通过反向传播与类别(CLS)标记及图像块标记在自注意力层中交互学习。
图像被处理为固定尺寸(16x16)的块序列,经线性嵌入后输入模型。
用途与限制
可直接用于图像分类任务。访问模型库寻找针对特定任务的微调版本。
使用方法
作为蒸馏ViT模型,可接入DeiTModel、DeiTForImageClassification或DeiTForImageClassificationWithTeacher。需使用DeiTFeatureExtractor处理数据,此处通过AutoFeatureExtractor自动匹配模型对应的特征提取器。
以下示例展示如何将COCO 2017数据集中的图像分类为ImageNet的1000个类别之一:
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-tiny-distilled-patch16-224')
model = DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-tiny-distilled-patch16-224')
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个类别。
训练流程
数据预处理
训练/验证期间图像预处理细节参见此处。
推理时,图像被缩放至256x256,中心裁剪为224x224,并按ImageNet均值和标准差进行RGB通道归一化。
预训练
模型在8-GPU节点上训练3天,分辨率为224。所有超参数(如批量大小和学习率)请参阅原论文表9。
评估结果
模型 |
ImageNet top-1准确率 |
ImageNet top-5准确率 |
参数量 |
链接 |
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}
}