-
Notifications
You must be signed in to change notification settings - Fork 0
/
KIS.py
128 lines (106 loc) · 4.48 KB
/
KIS.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import random
import csv, os
import numpy as np
from pathlib import Path
from numpy.linalg import norm
from matplotlib import pyplot as plt
class Embedding:
def __init__(self, img, data, text=None, score=None):
self.data = data
self.score = score
self.ID = str(Path(img).parts[-3:])
self.img = img
self.text = text
def Compare(q1, q2):
return np.sum(np.dot(q1.data, q2.data) / (norm(q1.data) * norm(q2.data)))
def KISExperiment(CLIPimg, CLIPtxt, is_random=False):
#set number of queries
if is_random:
numSample = 100
sampleQueries = random.sample(CLIPtxt, numSample)
else:
numSample = len(CLIPtxt)
sampleQueries = CLIPtxt
ranking = []
for i in range(0, numSample):
q = sampleQueries[i]
for img in CLIPimg:
img.score = Compare(img, q)
result = sorted(CLIPimg, key=lambda x: x.score, reverse=True)
for k in range(len(result)):
if result[k].ID == q.ID:
ranking.append(k)
break
return ranking
def Parse_expert_Data(path):
manual_text = []
with open(path, newline='') as f:
reader = csv.reader(f, delimiter=';')
#skip header
next(reader, None)
for row in reader:
img = row[0]
text = np.fromstring(row[1], sep=';')
manual_text.append(Embedding(img, None, text))
return manual_text
def Parse_Manual_Data(path, novice):
img_embeddings = []
manual_text_embeddings = []
ClipCapText_embeddings = []
with open(path, newline='') as f:
reader = csv.reader(f, delimiter=';')
#skip header
next(reader, None)
if novice:
for row in reader:
img = row[0]
CLIP_img = np.fromstring(row[1], sep='|')
ClipCap_text = row[2]
CLIP_ClipCap = np.fromstring(row[3], sep='|')
text = row[4]
CLIP_text = np.fromstring(row[5], sep='|')
img_embeddings.append(Embedding(img, CLIP_img))
manual_text_embeddings.append(Embedding(img, CLIP_text, text))
ClipCapText_embeddings.append(Embedding(img, CLIP_ClipCap, ClipCap_text))
return img_embeddings, ClipCapText_embeddings, manual_text_embeddings
else:
for row in reader:
img = row[0]
text = row[1]
CLIP_text = np.fromstring(row[2], sep='|')
manual_text_embeddings.append(Embedding(img, CLIP_text, text))
return manual_text_embeddings
def Parse_Dataset(path):
img_embeddings = []
text_embeddings = []
folder_dirs = [os.path.join(path, folder) for folder in os.listdir(path)]
for folder_dir in folder_dirs:
dir = Path(folder_dir)
for csv_dir in list(sorted(dir.glob("*.csv"))):
with open(csv_dir, newline='') as f:
reader = csv.reader(f, delimiter=';')
# skip header
next(reader, None)
for row in reader:
img = row[0]
ClipCap_text = row[1]
CLIP_ClipCap = np.fromstring(row[2], sep='|')
CLIP_img = np.fromstring(row[3], sep='|')
img_embeddings.append(Embedding(img, CLIP_img))
text_embeddings.append(Embedding(img, CLIP_ClipCap, ClipCap_text))
return img_embeddings, text_embeddings
if __name__ == '__main__':
# load data
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
expert_descriptions = Parse_expert_Data(ROOT_DIR + "/data/manual_vs_ClipCap/100_expert_descriptions.csv")
novice_img_embeddings, novice_ClipCapText_embeddings, novice_manual_embeddings = Parse_Manual_Data(ROOT_DIR + "/data/manual_vs_ClipCap/100_samples.csv", True)
expert_manual_embeddings = Parse_Manual_Data(ROOT_DIR + "/data/manual_vs_ClipCap/expertQueriesCLIPfeatures.csv", False)
img_dataset_embeddings, text_dataset_embeddings = Parse_Dataset(ROOT_DIR + "/data/extracted_low_res_images_v2")
ClipCap_ranking = KISExperiment(img_dataset_embeddings, novice_ClipCapText_embeddings)
novice_manual_ranking = KISExperiment(img_dataset_embeddings, novice_manual_embeddings)
expert_manual_ranking = KISExperiment(img_dataset_embeddings, expert_manual_embeddings)
#plot 2d
plt.scatter(ClipCap_ranking, expert_manual_ranking)
plt.xlabel("Rank for ClipCap query")
plt.ylabel("Rank for manual novice query")
plt.show()