File size: 2,012 Bytes
131c541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import math
from PIL import Image
from sentence_transformers import SentenceTransformer

def resize_image_for_patch(image: Image.Image, patch_size: int = 14, max_patches: int = 400) -> Image.Image:
    orig_width, orig_height = image.size
    aspect_ratio = orig_width / orig_height

    # Max width and height in pixels under the patch constraint
    max_total_pixels = patch_size * math.sqrt(max_patches)

    if aspect_ratio >= 1:
        # Landscape or square orientation
        target_width = patch_size * int(math.floor(math.sqrt(max_patches * aspect_ratio)))
        target_height = int(target_width / aspect_ratio)
    else:
        # Portrait orientation
        target_height = patch_size * int(math.floor(math.sqrt(max_patches / aspect_ratio)))
        target_width = int(target_height * aspect_ratio)

    # Ensure dimensions are multiples of patch_size
    target_width -= target_width % patch_size
    target_height -= target_height % patch_size

    return image.resize((target_width, target_height), Image.BICUBIC)

# Init model
model = SentenceTransformer("./", device="cuda")

images = [
     resize_image_for_patch(Image.open("/home/aki0421/Share/images/00085.png"))
]
image_embeddings = model.encode(images, convert_to_tensor=True)

sentences = [
    "女の子が悲しんでいる。",
    "落ち込んでる人",
    "泣いている",
    "笑っている",
    "ピンクの髪の女の子",
    "赤い髪の女の子",
    "茶色の髪の女の子",
    "赤い目",
    "青い目",
    "曇っている",
    "雨が降っている",
    "晴れている",
    "キッチンにいます。",
    "学校にいる",
    "魔法少女のようだ",
    "戦闘しますか?",
    "男性ですか?",
    "茶色い髪の女の子が悲しんでいるシーン",
    "ピンクの髪の女の子が笑っているシーン"
]
text_embeddings = model.encode(sentences, convert_to_tensor=True)
similarities = model.similarity(text_embeddings, image_embeddings)

print(similarities)