Views
No views yet

$ pip install git+https://github.com/rinnakk/japanese-clip.git1import io
2import requests
3from PIL import Image
4import torch
5import japanese_clip as ja_clip
6
7device = "cuda" if torch.cuda.is_available() else "cpu"
8
9
10model, preprocess = ja_clip.load("rinna/japanese-clip-vit-b-16", cache_dir="/tmp/japanese_clip", device=device)
11tokenizer = ja_clip.load_tokenizer()
12
13img = Image.open(io.BytesIO(requests.get('https://images.pexels.com/photos/2253275/pexels-photo-2253275.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260').content))
14image = preprocess(img).unsqueeze(0).to(device)
15encodings = ja_clip.tokenize(
16 texts=["犬", "猫", "象"],
17 max_seq_len=77,
18 device=device,
19 tokenizer=tokenizer, # this is optional. if you don't pass, load tokenizer each time
20)
21
22with torch.no_grad():
23 image_features = model.get_image_features(image)
24 text_features = model.get_text_features(**encodings)
25
26 text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
27
28print("Label probs:", text_probs) # prints: [[1.0, 0.0, 0.0]]vit-base-patch16-224 model.1@misc{rinna-japanese-clip-vit-b-16,
2 title = {rinna/japanese-clip-vit-b-16},
3 author = {Shing, Makoto and Zhao, Tianyu and Sawada, Kei},
4 url = {https://huggingface.co/rinna/japanese-clip-vit-b-16}
5}
6
7@inproceedings{sawada2024release,
8 title = {Release of Pre-Trained Models for the {J}apanese Language},
9 author = {Sawada, Kei and Zhao, Tianyu and Shing, Makoto and Mitsui, Kentaro and Kaga, Akio and Hono, Yukiya and Wakatsuki, Toshiaki and Mitsuda, Koh},
10 booktitle = {Proceedings of the 2024 Joint International Conference on Computational Linguistics, Language Resources and Evaluation (LREC-COLING 2024)},
11 month = {5},
12 year = {2024},
13 pages = {13898--13905},
14 url = {https://aclanthology.org/2024.lrec-main.1213},
15 note = {\url{https://arxiv.org/abs/2404.01657}}
16}