-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_lang_prototypes.py
387 lines (292 loc) · 11.3 KB
/
generate_lang_prototypes.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import os
import numpy as np
import torch
import pickle
from sentence_transformers import SentenceTransformer
import plotly.express as px
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from typing import Tuple, List, Dict
from transformers import AutoTokenizer, BertModel
from kmeans_pytorch import kmeans
import clip
import argparse
__DATA_DIR = os.getcwd()
__ANNOTATION_DIR = None
def getUniqueActions50Salads(root_dir:str) -> str:
"""
Get unique action lists from annotation files on 50Salads dataset
Args:
root_dir:str -> path to annotations directory
Return:
actions:set(str) -> Unique actions found in the dataset
"""
annotation_files = os.listdir(root_dir)
actions = set()
for file_ in annotation_files:
f = open(os.path.join(root_dir, file_), "r")
content = f.readlines()
for line in content:
action = line.split()[-1]
for ending in ["prep", "core", "post"]:
if ending in action.split("_"):
action = action.replace(ending, "")
action = action.replace("_", " ")
actions.add(action)
return actions
def ActionLanguageEncoding(actions:str, save_path:str):
"""
Encodes action using a LLM
Args:
actions:list[str] -> List of actions
save_path:str -> path to save encodings
Returns:
ActionEncodingDict:dict[str, float] -> Dictionary containing action name and its absolute representation.
"""
#build encoder
encoder = SentenceTransformer("stsb-mpnet-base-v2")
#encode actions
ActionEncodingDict = {}
for action in actions:
encoding = encoder.encode(action)
ActionEncodingDict[action] = encoding
return ActionEncodingDict
def ActionLanguageEncodingCLIP(actions:str, save_path:str):
"""
Encodes action using a LLM
Args:
actions:list[str] -> List of actions
save_path:str -> path to save encodings
Returns:
ActionEncodingDict:dict[str, float] -> Dictionary containing action name and its absolute representation.
"""
#build encoder
# encoder = SentenceTransformer("stsb-mpnet-base-v2")
encoder, preprocess = clip.load("ViT-B/16", device='cpu')
#encpde actions
ActionEncodingDict = {}
for action in actions:
action_tokens = clip.tokenize(action).to('cpu')
encoding = encoder.encode_text(action_tokens)
ActionEncodingDict[action] = encoding.squeeze()
return ActionEncodingDict
def GetSimilarities(encodings:torch.Tensor)->torch.Tensor:
"""
Calculate similarities between encodings
Args:
actions:str -> list of actions
encodings:list[torch.Tensor] -> list of encodings
Return:
similarities:torch.Tensor -> similarity scores
"""
# similarities = []
cosine = torch.nn.CosineSimilarity(dim=2, eps=1e-08)
similarities = cosine(encodings.unsqueeze(dim=1), encodings.unsqueeze(dim=0))
return similarities
def getClassActionEgtea(file_path):
"""
Args:
file_path (str): Path to action.csv file
Return:
out_action (dict): Dictionaries of action class to action name mapping
"""
file_ = open(file=file_path, mode="r")
lines = file_.readlines()
out_actions = {}
out_verbs = {}
out_nouns = {}
for line in lines:
line = line.split(",")
action = line[0]
verb_noun = line[1].split("_")
verb, noun = verb_noun[0], verb_noun[1]
text_action = line[-1].strip("\n").split("_")
text_verb = text_action[0].split("/")[0][1:] #Removes the blank space before the verb
text_noun = text_action[1].replace(":", " ")
text_action = " ".join([text_verb, text_noun])
out_actions[action] = text_action
out_verbs[action] = text_verb
out_nouns[action] = text_noun
return out_actions, out_nouns, out_verbs
def ActionLanguageEncodingEgtea(actions, save_path="./encodings_egtea.pth"):
"""
Encodes action using a LLM
Args:
actions:list[str] -> List of actions
save_path:str -> path to save encodings
Returns:
ActionEncodingDict:dict[str, float] -> Dictionary containing action name and its absolute representation.
"""
#build encoder
# encoder = SentenceTransformer("stsb-mpnet-base-v2")
encoder = SentenceTransformer("all-mpnet-base-v2")
#encpde actions
EncodedDict = {}
for action in actions:
encoded_action = encoder.encode(actions[action], convert_to_tensor=True).to(torch.float32).cpu()
EncodedDict[int(action)] = encoded_action
torch.save(EncodedDict, save_path)
return EncodedDict
def ActionLanguageEncodingEgteaCLIP(actions, save_path="./encodings_egtea.pth"):
"""
Encodes action using a LLM
Args:
actions:list[str] -> List of actions
save_path:str -> path to save encodings
Returns:
ActionEncodingDict:dict[str, float] -> Dictionary containing action name and its absolute representation.
"""
#build encoder
# encoder = SentenceTransformer("stsb-mpnet-base-v2")
encoder, preprocess = clip.load("ViT-B/16", device='cpu')
#encpde actions
EncodedDict = {}
for action in actions:
action_tokens = clip.tokenize(actions[action]).cpu()
with torch.no_grad():
encoded_action = encoder.encode_text(action_tokens).cpu()
EncodedDict[int(action)] = encoded_action.squeeze()
torch.save(EncodedDict, save_path)
return EncodedDict
def GetSimilaritiesEgtea(encodings_dict, save_path="./similarities_egtea.pth"):
"""
Calculate similarities between encodings
Args:
encodings_dict:{action:encoding} -> dict of encodings
Return:
similarities:torch.Tensor -> similarity scores
"""
# similarities = []
cosine = torch.nn.CosineSimilarity(dim=2, eps=1e-08)
encodings = torch.stack(list(encodings_dict.values()))
encodings = encodings.unsqueeze(dim=0)
similarities = {}
print(encodings.shape)
for action in encodings_dict:
action_encoding = torch.tensor(encodings_dict[action].reshape(1, 1, -1))
similarities_ = cosine(action_encoding, encodings)
similarities[int(action)] = similarities_.cpu().to(torch.float32)
torch.save(similarities, save_path)
return similarities
def GetSimilaritiesEgteaCLIP(encodings_dict, save_path="./similarities_egtea.pth"):
"""
Calculate similarities between encodings
Args:
encodings_dict:{action:encoding} -> dict of encodings
Return:
similarities:torch.Tensor -> similarity scores
"""
cosine = torch.nn.CosineSimilarity(dim=2, eps=1e-08)
encodings = torch.stack(list(encodings_dict.values()))
encodings = encodings.unsqueeze(dim=0)
similarities = {}
for action in encodings_dict:
action_encoding = torch.tensor(encodings_dict[action].reshape(1, 1, -1))
similarities_ = cosine(action_encoding, encodings)
torch.save(similarities, save_path)
return similarities
def KMeans(path_to_encodings, num_clusters):
"""
k-means clustering on a set of language embeddigs
Args:
encodings_dict (dict): dict containing encodings
num_clusters (int): number of actual clusters to learn
max_ters (int): number of iterations
Returns:
cluster_ids_x (torch.tensor): cluster idx for every point
cluster_centers (torch.tensor): cluster centers
"""
encodings_dict = torch.load(path_to_encodings)
points = torch.tensor(list(encodings_dict.values()))
print("points:", points)
cluster_ids_x, cluster_centers = kmeans(X=points, num_clusters=num_clusters, distance='cosine', device=torch.device('cuda:0'))
return cluster_ids_x, cluster_centers
def getClassActionEpic(file_path):
"""
Args:
file_path (str): Path to action.csv file
Return:
out_action (dict): Dictionaries of action class to action name mapping
"""
file_ = open(file=file_path, mode="r")
lines = file_.readlines()
out_actions = {}
out_verbs = {}
out_nouns = {}
for line in lines:
line = line.split(",")
action = line[0]
verb_noun = line[1].split("_")
verb, noun = verb_noun[0], verb_noun[1]
noun_splitted = noun.split(":")
if len(noun_splitted) > 1:
noun = " ".join([noun_splitted[-1], noun_splitted[0]])
print(f"{action} {verb} {noun}")
text_action = " ".join([verb, noun])
out_actions[action] = text_action
out_verbs[action] = verb
out_nouns[action] = noun
return out_actions, out_nouns, out_verbs
def getClassActionEpic100(file_path):
"""
Args:
file_path (str): Path to action.csv file
Return:
out_action (dict): Dictionaries of action class to action name mapping
"""
file_ = open(file=file_path, mode="r")
lines = file_.readlines()
out_actions = {}
out_verbs = {}
out_nouns = {}
for line in lines:
line = line.split(",")
action = line[0]
verb_noun = line[-1].strip("\n").split(" ")
verb, noun = verb_noun[0], verb_noun[1]
noun_splitted = noun.split(":")
if len(noun_splitted) > 1:
noun = " ".join([noun_splitted[-1], noun_splitted[0]])
# print(f"{action} {verb} {noun}")
text_action = " ".join([verb, noun])
# print(text_action)
out_actions[action] = text_action
out_verbs[action] = verb
out_nouns[action] = noun
return out_actions, out_nouns, out_verbs
def getClassAction50Salads(file_path):
"""
Args:
file_path (str): Path to action.csv file
Return:
out_action (dict): Dictionaries of action class to action name mapping
"""
file_ = open(file=file_path, mode="r")
lines = file_.readlines()
out_actions = {}
for line in lines:
line = line.split(" ")
action = line[0]
action_text = line[1].replace("\n", "")
action_text = action_text.replace("_", " ")
# print(f"{action} : {action_text}")
out_actions[action] = action_text
return out_actions
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create bank of actions")
parser.add_argument("--dataset", type=str, default="egtea", help="Dataset to use")
parser.add_argument("--label_file", type=str, default="./actions.csv", help="Path to action file in path relative to cwd")
args = parser.parse_args()
if args.dataset != "epic100" and args.dataset != "epic55":
__ANNOTATION_DIR = os.path.join(__DATA_DIR, args.label_file)
actions, nouns, verbs = getClassActionEgtea(__ANNOTATION_DIR)
elif args.dataset == "epic100":
__ANNOTATION_DIR = os.path.join(__DATA_DIR, args.label_file)
actions, nouns, verbs = getClassActionEpic100(__ANNOTATION_DIR)
else:
__ANNOTATION_DIR = os.path.join(__DATA_DIR, args.label_file)
actions, nouns, verbs = getClassActionEpic(__ANNOTATION_DIR)
encodings_path = f"./encodings_{args.dataset}.pth"
similarities_path = f"./similarities_{args.dataset}.pth"
Encoded_dict = ActionLanguageEncodingEgtea(actions, save_path=encodings_path)
similarities = GetSimilaritiesEgtea(Encoded_dict, save_path=similarities_path)