🚀 視覺與語言變換器 (ViLT) 預訓練模型
視覺與語言變換器 (ViLT) 模型在 GCC+SBU+COCO+VG 數據集上進行了預訓練(200k 步)。該模型由 Kim 等人在論文 ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision 中提出,並首次在 此倉庫 中發佈。注意:此模型僅包含語言建模頭。
聲明:發佈 ViLT 的團隊未為此模型編寫模型卡片,此模型卡片由 Hugging Face 團隊編寫。
🚀 快速開始
你可以使用該原始模型,結合一張圖像和一段包含 [MASK] 標記的文本進行掩碼語言建模。
💻 使用示例
基礎用法
以下是如何在 PyTorch 中使用此模型的示例:
from transformers import ViltProcessor, ViltForMaskedLM
import requests
from PIL import Image
import re
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
text = "a bunch of [MASK] laying on a [MASK]."
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-mlm")
model = ViltForMaskedLM.from_pretrained("dandelin/vilt-b32-mlm")
encoding = processor(image, text, return_tensors="pt")
outputs = model(**encoding)
tl = len(re.findall("\[MASK\]", text))
inferred_token = [text]
with torch.no_grad():
for i in range(tl):
encoded = processor.tokenizer(inferred_token)
input_ids = torch.tensor(encoded.input_ids).to(device)
encoded = encoded["input_ids"][0][1:-1]
outputs = model(input_ids=input_ids, pixel_values=pixel_values)
mlm_logits = outputs.logits[0]
mlm_logits = mlm_logits[1 : input_ids.shape[1] - 1, :]
mlm_values, mlm_ids = mlm_logits.softmax(dim=-1).max(dim=-1)
mlm_values[torch.tensor(encoded) != 103] = 0
select = mlm_values.argmax().item()
encoded[select] = mlm_ids[select].item()
inferred_token = [processor.decode(encoded)]
selected_token = ""
encoded = processor.tokenizer(inferred_token)
processor.decode(encoded.input_ids[0], skip_special_tokens=True)
📄 許可證
本項目採用 Apache-2.0 許可證。
BibTeX 引用
@misc{kim2021vilt,
title={ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision},
author={Wonjae Kim and Bokyung Son and Ildoo Kim},
year={2021},
eprint={2102.03334},
archivePrefix={arXiv},
primaryClass={stat.ML}
}