Views
No views yet
| Model | Checkpoint Size | Val Loss |
|---|---|---|
| GenshinImpact-CLIP-ViT-B-16-laion2B-s34B-b88K | 0.59 GB | 1.152 |
| GenshinImpact-ViT-SO400M-14-SigLIP-384 | 3.51 GB | 0.362 |
1import torch
2import torch.nn.functional as F
3from PIL import Image
4import requests
5from open_clip import create_model_from_pretrained, get_tokenizer
6
7def preprocess_text(string):
8 return "Genshin Impact\n" + string
9
10device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
11
12# load checkpoint from local path
13# model_path = "path/to/open_clip_pytorch_model.bin"
14# model_name = "ViT-B-16"
15# model, preprocess = create_model_from_pretrained(model_name=model_name, pretrained=model_path, device=device)
16# tokenizer = get_tokenizer(model_name)
17
18# or load from hub
19model, preprocess = create_model_from_pretrained('hf-hub:mrzjy/GenshinImpact-CLIP-ViT-B-16-laion2B-s34B-b88K')
20tokenizer = get_tokenizer('hf-hub:mrzjy/GenshinImpact-CLIP-ViT-B-16-laion2B-s34B-b88K')
21
22# image
23image_url = "https://static.wikia.nocookie.net/gensin-impact/images/3/33/Qingce_Village.png"
24image = Image.open(requests.get(image_url, stream=True).raw)
25image = preprocess(image).unsqueeze(0).to(device)
26
27# text choices
28labels = [
29 "This is an area of Liyue",
30 "This is an area of Mondstadt",
31 "This is an area of Sumeru",
32 "This is Qingce Village"
33]
34labels = [preprocess_text(l) for l in labels]
35text = tokenizer(labels, context_length=model.context_length).to(device)
36with torch.autocast(device_type=device.type):
37 with torch.no_grad():
38 image_features = model.encode_image(image)
39 text_features = model.encode_text(text)
40 image_features /= image_features.norm(dim=-1, keepdim=True)
41 text_features /= text_features.norm(dim=-1, keepdim=True)
42 text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
43 print(text_probs) # [0.0319, 0.0062, 0.0012, 0.9608]<img> tag or specified web content.
