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-SO400M-14-SigLIP-384"
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-5.0-ViT-SO400M-14-SigLIP-384')
20tokenizer = get_tokenizer('hf-hub:mrzjy/GenshinImpact-5.0-ViT-SO400M-14-SigLIP-384')
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 = F.normalize(image_features, dim=-1)
41 image_features = F.normalize(image_features, dim=-1)
42 text_features = F.normalize(text_features, dim=-1)
43 text_probs = torch.sigmoid(image_features @ text_features.T * model.logit_scale.exp() + model.logit_bias)
44 scores = [f"{s:.3f}" for i, s in enumerate(text_probs.tolist()[0])]
45 print(scores)<img> tag or specified web content.