File size: 2,455 Bytes
2184c4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
import os
import torch
from PIL import Image
from transformers import ChineseCLIPProcessor, ChineseCLIPModel

def clip_result(image_path):
    # 設置設備
    device = "cuda" if torch.cuda.is_available() else "cpu"

    # Get the directory where this script is located
    script_dir = os.path.dirname(os.path.abspath(__file__))
    
    # Construct the full path to the file in the subfolder
    model_path = os.path.join(script_dir, 'model', 'best_clip_model.pth')

    # 載入訓練好的模型和處理器
    model = ChineseCLIPModel.from_pretrained("OFA-Sys/chinese-clip-vit-base-patch16")
    model.load_state_dict(torch.load(model_path, map_location=device))
    model = model.to(device)
    model.eval()

    processor = ChineseCLIPProcessor.from_pretrained("OFA-Sys/chinese-clip-vit-base-patch16")

    # 1. 加載圖片
    # image_path = '/content/drive/MyDrive/幽靈吉伊卡哇.png'
    image = Image.open(image_path)

    # 2. 加載中文詞彙表
    with open('/content/drive/MyDrive/chiikawa/word_list.txt', 'r', encoding='utf-8') as f:
        vocab = [line.strip() for line in f.readlines()]

    # 3. 圖像和文本處理
    batch_size = 16  # 每次處理16個詞彙
    similarities = []

    # 釋放未使用的顯存
    torch.cuda.empty_cache()

    with torch.no_grad():
        for i in range(0, len(vocab), batch_size):
            batch_vocab = vocab[i:i + batch_size]
            inputs = processor(
                text=batch_vocab,
                images=image,
                return_tensors="pt",
                padding=True
            ).to(device)

            # 推理並進行相似度計算
            outputs = model(**inputs)
            image_embeds = outputs.image_embeds / outputs.image_embeds.norm(dim=-1, keepdim=True)
            text_embeds = outputs.text_embeds / outputs.text_embeds.norm(dim=-1, keepdim=True)
            similarity = torch.matmul(image_embeds, text_embeds.T).squeeze(0)
            similarities.append(similarity)

    # 4. 合併所有相似度
    similarity = torch.cat(similarities, dim=0)

    # 5. 找到相似度最高的詞彙
    top_k = 3
    top_k_indices = torch.topk(similarity, top_k).indices.tolist()
    top_k_words = [vocab[idx] for idx in top_k_indices]

    # 6. 輸出最接近的前3名中文詞彙

    print("最接近的前3名中文詞彙是:")
    for rank, word in enumerate(top_k_words, 1):
        print(f"{rank}. {word}")

    return top_k_words