Views
No views yet
python3 -m pip install pillow sentencepiece torch torchvision transformersCLIPModel
and VisionTextDualEncoderModel.1import requests
2import torch
3from PIL import Image
4from transformers import AutoModel, AutoProcessor, BatchEncoding
5
6# Download
7model_name = "hakuhodo-tech/japanese-clip-vit-h-14-bert-base"
8device = "cuda" if torch.cuda.is_available() else "cpu"
9model = AutoModel.from_pretrained(model_name, trust_remote_code=True).to(device)
10processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
11
12# Prepare raw inputs
13url = "http://images.cocodataset.org/val2017/000000039769.jpg"
14image = Image.open(requests.get(url, stream=True).raw)
15
16# Process inputs
17inputs = processor(
18 text=["犬", "猫", "象"],
19 images=image,
20 return_tensors="pt",
21 padding=True,
22)
23
24# Infer and output
25outputs = model(**BatchEncoding(inputs).to(device))
26probs = outputs.logits_per_image.softmax(dim=1)
27print([f"{x:.2f}" for x in probs.flatten().tolist()]) # ['0.00', '1.00', '0.00']| Text Retrieval | Image Retrieval | |||||||
| R@1 | R@5 | R@10 | R@1 | R@5 | R@10 | |||
| recruit-jp/japanese-clip-vit-b-32-roberta-base | 23.0 | 46.1 | 57.4 | 16.1 | 35.4 | 46.3 | ||
| rinna/japanese-cloob-vit-b-16 | 37.1 | 63.7 | 74.2 | 25.1 | 48.0 | 58.8 | ||
| rinna/japanese-clip-vit-b-16 | 36.9 | 64.3 | 74.3 | 24.8 | 48.8 | 60.0 | ||
| Japanese CLIP ViT-H/14 (Base) | 39.2 | 66.3 | 76.6 | 28.9 | 53.3 | 63.9 | ||
| Japanese CLIP ViT-H/14 (Deeper) | 48.7 | 74.0 | 82.4 | 36.5 | 61.5 | 71.8 | ||
| Japanese CLIP ViT-H/14 (Wider) | 47.9 | 74.2 | 83.2 | 37.3 | 62.8 | 72.7 |
1@article{japanese-clip-vit-h,
2 author = {王 直 and 細野 健人 and 石塚 湖太 and 奥田 悠太 and 川上 孝介},
3 journal = {言語処理学会年次大会発表論文集},
4 month = {Mar},
5 pages = {1547--1552},
6 title = {日本語特化の視覚と言語を組み合わせた事前学習モデルの開発 Developing Vision-Language Pre-Trained Models for {J}apanese},
7 volume = {30},
8 year = {2024}
9}