🚀 數據高效圖像變換器(基礎尺寸模型)
Data-efficient Image Transformer (DeiT) 模型在 ImageNet - 1k(100 萬張圖像,1000 個類別)上以 384x384 的分辨率進行了預訓練和微調。該模型最早由 Touvron 等人在論文 Training data-efficient image transformers & distillation through attention 中提出,並首次在 此倉庫 中發佈。不過,其權重是由 Ross Wightman 從 timm 倉庫 轉換而來。
聲明:發佈 DeiT 的團隊並未為此模型撰寫模型卡片,此模型卡片由 Hugging Face 團隊編寫。
🚀 快速開始
你可以使用原始模型進行圖像分類。前往 模型中心 查找針對你感興趣的任務進行微調的版本。
✨ 主要特性
- 該模型實際上是一個訓練效率更高的視覺變換器(ViT)。
- 能夠學習圖像的內在表示,可用於提取對下游任務有用的特徵。
📦 安裝指南
文檔未提及安裝步驟,故跳過此章節。
💻 使用示例
基礎用法
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-base-patch16-384')
model = ViTForImageClassification.from_pretrained('facebook/deit-base-patch16-384')
inputs = feature_extractor(images=image, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
predicted_class_idx = logits.argmax(-1).item()
print("Predicted class:", model.config.id2label[predicted_class_idx])
高級用法
文檔未提及高級用法相關代碼,故跳過此部分。
📚 詳細文檔
模型描述
視覺變換器(ViT)是一種類似於 BERT 的變換器編碼器模型,在 224 分辨率下進行預訓練,並在 384 分辨率下對大量圖像(即 ImageNet - 1k)進行有監督的微調。
圖像以固定大小的塊(分辨率 16x16)序列的形式呈現給模型,並進行線性嵌入。同時,在序列開頭添加一個 [CLS] 標記,用於分類任務。在將序列輸入到變換器編碼器的各層之前,還會添加絕對位置嵌入。
通過對模型進行預訓練,它可以學習到圖像的內在表示,進而用於提取對下游任務有用的特徵。例如,如果你有一個帶標籤的圖像數據集,可以在預訓練編碼器的基礎上添加一個線性層,訓練一個標準分類器。通常會在 [CLS] 標記上添加一個線性層,因為該標記的最後隱藏狀態可以看作是整個圖像的表示。
預期用途和限制
你可以使用原始模型進行圖像分類。可在 模型中心 查找針對你感興趣的任務進行微調的版本。
訓練數據
ViT 模型在 ImageNet - 1k 數據集上進行預訓練,該數據集包含 100 萬張圖像和 1000 個類別。
訓練過程
預處理
訓練/驗證期間圖像預處理的確切細節可在 此處 找到。
在推理時,圖像會被調整大小/重新縮放至相同的分辨率(438x438),然後在 384x384 處進行中心裁剪,並使用 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}
}
📄 許可證
本項目採用 Apache - 2.0 許可證。