forked from VCEET/ConstAct_HAR_SFU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text_Prompt.py
240 lines (178 loc) · 9.23 KB
/
Text_Prompt.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
import torch
import clip
label_text_map = []
with open('text/ntu120_label_map.txt') as infile:
lines = infile.readlines()
for ind, line in enumerate(lines):
label_text_map.append(line.rstrip().lstrip())
objects_map = []
with open('text/objects.txt') as infile:
lines = infile.readlines()
for ind, line in enumerate(lines):
objects_map.append(line.rstrip().lstrip())
paste_text_map0 = []
with open('text/synonym_openai_t01.txt') as infile:
lines = infile.readlines()
for ind, line in enumerate(lines):
temp_list = line.rstrip().lstrip().split(',')
paste_text_map0.append(temp_list)
paste_text_map1 = []
with open('text/sentence_openai_t01.txt') as infile:
lines = infile.readlines()
for ind, line in enumerate(lines):
temp_list = line.rstrip().lstrip().split('.')
while len(temp_list) < 4:
temp_list.append(" ")
paste_text_map1.append(temp_list)
paste_text_map2 = []
with open('text/pasta_openai_t01.txt') as infile:
lines = infile.readlines()
for ind, line in enumerate(lines):
temp_list = line.rstrip().lstrip().split(';')
paste_text_map2.append(temp_list)
ucla_paste_text_map0 = []
with open('text/ucla_synonym_openai_t01.txt') as infile:
lines = infile.readlines()
for ind, line in enumerate(lines):
temp_list = line.rstrip().lstrip().split(',')
ucla_paste_text_map0.append(temp_list)
ucla_paste_text_map1 = []
with open('text/ucla_pasta_openai_t01.txt') as infile:
lines = infile.readlines()
for ind, line in enumerate(lines):
temp_list = line.rstrip().lstrip().split(';')
ucla_paste_text_map1.append(temp_list)
def text_visual_prompt():
text_aug = [f"a photo of action {{}}", f"a picture of action {{}}", f"Human action of {{}}", f"{{}}, an action",
f"{{}} this is an action", f"{{}}, a video of action", f"Playing action of {{}}", f"{{}}",
f"Playing a kind of action, {{}}", f"Doing a kind of action, {{}}", f"Look, the human is {{}}",
f"Can you recognize the action of {{}}?", f"Video classification of {{}}", f"A video of {{}}",
f"The man is {{}}", f"The woman is {{}}"]
text_dict = {}
num_text_aug = len(text_aug)
for ii, txt in enumerate(text_aug):
text_dict[ii] = torch.cat([clip.tokenize(txt.format(c)) for c in label_text_map])
classes = torch.cat([v for k, v in text_dict.items()])
return classes, num_text_aug, text_dict
def text_visual_prompt_with_object():
text_aug = [f"a photo of action {{}}", f"a picture of action {{}}", f"Human action of {{}}", f"{{}}, an action",
f"{{}} this is an action", f"{{}}, a video of action", f"Playing action of {{}}", f"{{}}",
f"Playing a kind of action, {{}}", f"Doing a kind of action, {{}}", f"Look, the human is {{}}",
f"Can you recognize the action of {{}}?", f"Video classification of {{}}", f"A video of {{}}",
f"The man is {{}}", f"The woman is {{}}"]
text_dict = {}
num_text_aug = len(text_aug)
for ii, txt in enumerate(text_aug):
text_dict[ii] = torch.cat([clip.tokenize(txt.format(c)) for c in objects_map])
classes = torch.cat([v for k, v in text_dict.items()])
return classes, num_text_aug, text_dict
def text_visual_prompt_descriptive():
text_aug = [f"a photo of action {{}}", f"a picture of action {{}}", f"Human action of {{}}", f"{{}}, an action",
f"{{}} this is an action", f"{{}}, a video of action", f"Playing action of {{}}", f"{{}}",
f"Playing a kind of action, {{}}", f"Doing a kind of action, {{}}", f"Look, the human is {{}}",
f"Can you recognize the action of {{}}?", f"Video classification of {{}}", f"A video of {{}}",
f"The man is {{}}", f"The woman is {{}}"]
text_dict = {}
num_text_aug = len(text_aug)
for ii, txt in enumerate(text_aug):
text_dict[ii] = torch.cat([clip.tokenize(txt.format(c[0]+','+','.join(c[1:]))) for c in paste_text_map2])
return text_dict
def text_prompt_openai_random():
print("Use text prompt openai synonym random")
total_list = []
for pasta_list in paste_text_map0:
temp_list = []
for item in pasta_list:
temp_list.append(clip.tokenize(item))
total_list.append(temp_list)
return total_list
def text_prompt_openai_random_bert():
print("Use text prompt openai synonym random bert")
total_list = []
for pasta_list in paste_text_map0:
temp_list = []
for item in pasta_list:
temp_list.append(item)
total_list.append(temp_list)
return total_list
def text_prompt_openai_pasta_pool_4part():
print("Use text prompt openai pasta pool")
text_dict = {}
num_text_aug = 5
for ii in range(num_text_aug):
if ii == 0:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[ii])) for pasta_list in paste_text_map2])
elif ii == 1:
text_dict[ii] = torch.cat([clip.tokenize((','.join(pasta_list[0:2]))) for pasta_list in paste_text_map2])
elif ii == 2:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[0] +','.join(pasta_list[2:4]))) for pasta_list in paste_text_map2])
elif ii == 3:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[0] +','+ pasta_list[4])) for pasta_list in paste_text_map2])
else:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[0]+','+','.join(pasta_list[5:]))) for pasta_list in paste_text_map2])
classes = torch.cat([v for k, v in text_dict.items()])
return classes, num_text_aug, text_dict
def text_prompt_openai_pasta_pool_4part_X(prompt_length):
print("Use text prompt openai pasta pool with X")
text_dict = {}
num_text_aug = 5
for ii in range(num_text_aug):
if ii == 0:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[ii]),length=prompt_length) for pasta_list in paste_text_map2])
elif ii == 1:
text_dict[ii] = torch.cat([clip.tokenize((','.join(pasta_list[0:2])),length=prompt_length) for pasta_list in paste_text_map2])
elif ii == 2:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[0] +','.join(pasta_list[2:4])),length=prompt_length) for pasta_list in paste_text_map2])
elif ii == 3:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[0] +','+ pasta_list[4]),length=prompt_length) for pasta_list in paste_text_map2])
else:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[0]+','+','.join(pasta_list[5:])),length=prompt_length) for pasta_list in paste_text_map2])
classes = torch.cat([v for k, v in text_dict.items()])
return classes, num_text_aug, text_dict
def text_prompt_openai_pasta_pool_4part_bert():
print("Use text prompt openai pasta pool bert")
text_dict = {}
num_text_aug = 5
for ii in range(num_text_aug):
if ii == 0:
input_list = [pasta_list[ii] for pasta_list in paste_text_map2]
text_dict[ii] = input_list
elif ii == 1:
input_list = [','.join(pasta_list[0:2]) for pasta_list in paste_text_map2]
text_dict[ii] = input_list
elif ii == 2:
input_list = [pasta_list[0] +','.join(pasta_list[2:4]) for pasta_list in paste_text_map2]
text_dict[ii] = input_list
elif ii == 3:
input_list = [pasta_list[0] +','+ pasta_list[4] for pasta_list in paste_text_map2]
text_dict[ii] = input_list
else:
input_list = [pasta_list[0]+','+','.join(pasta_list[5:]) for pasta_list in paste_text_map2]
text_dict[ii] = input_list
return num_text_aug, text_dict
def text_prompt_openai_random_ucla():
print("Use text prompt openai synonym random UCLA")
total_list = []
for pasta_list in ucla_paste_text_map0:
temp_list = []
for item in pasta_list:
temp_list.append(clip.tokenize(item))
total_list.append(temp_list)
return total_list
def text_prompt_openai_pasta_pool_4part_ucla():
print("Use text prompt openai pasta pool ucla")
text_dict = {}
num_text_aug = 5
for ii in range(num_text_aug):
if ii == 0:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[ii])) for pasta_list in ucla_paste_text_map1])
elif ii == 1:
text_dict[ii] = torch.cat([clip.tokenize((','.join(pasta_list[0:2]))) for pasta_list in ucla_paste_text_map1])
elif ii == 2:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[0] +','.join(pasta_list[2:4]))) for pasta_list in ucla_paste_text_map1])
elif ii == 3:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[0] +','+ pasta_list[4])) for pasta_list in ucla_paste_text_map1])
else:
text_dict[ii] = torch.cat([clip.tokenize((pasta_list[0]+','+','.join(pasta_list[5:]))) for pasta_list in ucla_paste_text_map1])
classes = torch.cat([v for k, v in text_dict.items()])
return classes, num_text_aug, text_dict