-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.py
79 lines (64 loc) · 2.55 KB
/
scripts.py
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
71
72
73
74
75
76
77
78
79
import pandas as pd
import numpy as np
import os
import torch
import clip
from PIL import Image
import sklearn
def get_all_data(dataroot):
data = pd.read_csv(os.path.join(dataroot, 'data_files/train.csv'))
return data
def check_data_exist(data_frame, dataroot):
drop_indices = []
for indx, row in data_frame.iterrows():
image_id = row['image_id']
image_path = os.path.join(dataroot, "sup_images/{}".format(image_id))
if not os.path.isfile(image_path):
drop_indices.append(indx)
return data_frame.drop(drop_indices)
def compute_similarity(data, dataroot):
cos_sim = []
dot_prod = []
model, preprocess = clip.load("ViT-B/32", device='cpu')
for indx, row in data.iterrows():
caption = row['caption']
image_id = row['image_id']
image_path = os.path.join(dataroot, "sup_images/{}".format(image_id))
image = Image.open(image_path).convert("RGB")
# image = preprocess(image)
image = preprocess(image).unsqueeze(0).to('cpu')
text = clip.tokenize(caption).to('cpu')
image_features = model.encode_image(image)
text_features = model.encode_text(text)
# print("shapes")
# print(image_features.shape)
# print(text_features.shape)
dot_product = torch.dot(image_features.reshape(-1), text_features.reshape(-1))
cosine_sim = torch.cosine_similarity(image_features, text_features)
cos_sim.append(cosine_sim)
dot_prod.append(dot_product)
data['cos_sim'] = cos_sim
data['dot_product'] = dot_prod
data.to_csv('~/Desktiop/data_similarity.csv')
def sim(data, dataroot):
cos_sim = []
dot_prod = []
model, preprocess = clip.load("ViT-B/32", device='cpu')
for indx, row in data.iterrows():
caption = row['caption']
image_id = row['image_id']
image_path = os.path.join(dataroot, "sup_images/{}".format(image_id))
image = Image.open(image_path).convert("RGB")
# image = preprocess(image)
image = preprocess(image).unsqueeze(0).to('cpu')
text = clip.tokenize(caption).to('cpu')
image_features = model.encode_image(image)
text_features = model.encode_text(text)
# print("shapes")
# print(image_features.shape)
# print(text_features.shape)
dot_product = torch.dot(image_features.reshape(-1), text_features.reshape(-1))
cosine_sim = torch.cosine_similarity(image_features, text_features)
cos_sim.append(cosine_sim)
dot_prod.append(dot_product)
return cos_sim, dot_prod