-
Notifications
You must be signed in to change notification settings - Fork 128
/
preprocess_data_unsupervised.py
311 lines (239 loc) · 11.2 KB
/
preprocess_data_unsupervised.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
from tqdm import tqdm
import pickle
import json
import codecs
import requests
import pandas as pd
from transformers import BertTokenizer, AutoTokenizer, LlamaTokenizer
from os import listdir
from os.path import isfile, join
import torch
import numpy as np
import random
import clip
import torch
from transformers import AutoFeatureExtractor, AutoModel, LlamaForCausalLM
json_load = lambda x: json.load(codecs.open(x, 'r', encoding='utf-8'))
json_dump = lambda d, p: json.dump(d, codecs.open(p, 'w', 'utf-8'), indent=2, ensure_ascii=False)
# xxx: 2023-03-21
IGNORE_INDEX = -100
DEFAULT_PAD_TOKEN = "[PAD]"
DEFAULT_EOS_TOKEN = "</s>"
DEFAULT_BOS_TOKEN = "<s>"
DEFAULT_UNK_TOKEN = "<unk>"
PROMPT_DICT = {
"prompt_input": (
"Below is an instruction that describes a task, paired with an input that provides further context. "
"Write a response that appropriately completes the request.\n\n"
"### Instruction:\n{}\n\n### Input:\n{}\n\n### Response:"
),
"prompt_no_input": (
"Below is an instruction that describes a task. "
"Write a response that appropriately completes the request.\n\n"
"### Instruction:\n{}\n\n### Response:"
),
}
def preprocess_coco_to_tensor_dataset(all_visual_names, tokenizer):
all_examples = json_load('data/generated_examples_coco.json')['data']
max_length = 256
all_images, all_null_audios, all_null_videos = [], [], []
all_texts, all_labels = [], []
all_textual_inputs = []
all_native_labels = []
for ind, e in enumerate(tqdm(all_examples)):
if 'caption' in e['instruction'] or 'caption' in e['response'] or ' no ' in e['response'] or 'not' in e['response']:
continue
all_images.append(all_visual_names[e['id']])
e = {
'instruction': e['instruction'],
'input': "",
'output': e['response']
}
texts = PROMPT_DICT['prompt_input'].format(e['instruction'], e['input']) if e['input'] != "" else PROMPT_DICT['prompt_no_input'].format(e['instruction'])
full_texts = texts + '\n {} \n\n'.format(e['output'])
all_textual_inputs.append(full_texts)
t_all = tokenizer.encode(full_texts)
t_texts = tokenizer.encode(texts)
if len(t_texts) >= max_length:
continue
if len(t_all) > max_length:
t_all = t_all[:max_length]
if len(t_all) < max_length:
t_all = t_all + [tokenizer.pad_token_id] * (max_length - len(t_all))
prefix_len = len(t_texts) - 1
labels = [IGNORE_INDEX] * prefix_len + t_all[prefix_len:]
if len(labels) > max_length:
labels = labels[:max_length]
if len(labels) < max_length:
labels = labels + [IGNORE_INDEX] * (max_length - len(labels))
all_texts.append(torch.tensor([t_all], dtype=torch.int))
all_labels.append(torch.tensor([labels], dtype=torch.int))
all_native_labels.append(labels)
all_null_audios = [-1] * len(all_images)
all_null_videos = all_null_audios
tokenized_texts = tokenizer(all_textual_inputs, max_length=max_length, padding='max_length', truncation=True)
tokenized_texts['labels'] = all_native_labels
tokenized_texts['images'] = all_images
tokenized_texts['audios'] = all_null_audios
tokenized_texts['videos'] = all_null_videos
return all_textual_inputs, all_native_labels, all_images, all_null_audios, all_null_videos
def preprocess_alpaca_to_tensor_dataset(tokenizer):
all_examples = json_load('data/alpaca_data/alpaca_data.json')
max_length = 256
all_null_images, all_null_audios, all_null_videos = [], [], []
all_texts, all_labels = [], []
all_textual_inputs = []
all_native_labels = []
for ind, e in enumerate(tqdm(all_examples)):
texts = PROMPT_DICT['prompt_input'].format(e['instruction'], e['input']) if e['input'] != "" else PROMPT_DICT['prompt_no_input'].format(e['instruction'])
full_texts = texts + '\n {} \n\n'.format(e['output'])
t_all = tokenizer.encode(full_texts)
t_texts = tokenizer.encode(texts)
if len(t_texts) >= max_length:
continue
if len(t_all) > max_length:
t_all = t_all[:max_length]
if len(t_all) < max_length:
t_all = t_all + [tokenizer.pad_token_id] * (max_length - len(t_all))
all_textual_inputs.append(full_texts)
prefix_len = len(t_texts) - 1
labels = [IGNORE_INDEX] * prefix_len + t_all[prefix_len:]
if len(labels) > max_length:
labels = labels[:max_length]
if len(labels) < max_length:
labels = labels + [IGNORE_INDEX] * (max_length - len(labels))
all_texts.append(t_all)
all_labels.append(labels)
all_native_labels.append(labels)
all_null_images = [-1] * len(all_texts)
all_null_audios = all_null_images
all_null_videos = all_null_images
tokenized_texts = tokenizer(all_textual_inputs, max_length=max_length, padding='max_length', truncation=True)
tokenized_texts['labels'] = all_native_labels
tokenized_texts['images'] = all_null_images
tokenized_texts['audios'] = all_null_audios
tokenized_texts['videos'] = all_null_videos
return all_textual_inputs, all_native_labels, all_null_images, all_null_audios, all_null_videos
def draw_samples(lis, ratio):
samples = ratio if ratio > 1 else int(ratio * len(lis))
if samples > len(lis):
new_lis = np.random.choice(len(lis), samples, replace=True)
else:
new_lis = np.random.choice(len(lis), samples, replace=False)
n_lis = [lis[i] for i in new_lis]
return n_lis
def preprocess_avsd_to_tensor_dataset(all_visual_names, tokenizer):
train_metadata_dir = 'data/generated_examples_avsd.json'
device = "cuda" if torch.cuda.is_available() else "cpu"
torch.random.manual_seed(0)
max_length = 256
def read_image_and_audio(metadata_dir):
metadata = json_load(metadata_dir)['data']
all_videos, all_audios, all_texts, all_null_images = [], [], [], []
all_labels = []
all_textual_inputs = []
all_native_labels = []
for ind, e in enumerate(tqdm(metadata)):
if 'caption' in e['instruction'] or 'caption' in e['response'] or ' no ' in e['response'] or 'not' in e['response']:
continue
prompt = "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{}\n\n### Response:\n {} \n\n"
q = prompt.format(e['instruction'], e['response'])
t_all = tokenizer.encode(q, max_length=max_length, truncation=True)
q_input = q.split(' Response:')[0] + ' Response:'
if len(t_all) > max_length:
t_all = t_all[:max_length]
if len(t_all) < max_length:
t_all = t_all + [tokenizer.pad_token_id] * (max_length - len(t_all))
len_t_q = len(tokenizer.encode(q_input)) - 1
labels = [IGNORE_INDEX] * len_t_q + t_all[len_t_q:]
if len(labels) > max_length:
labels = labels[:max_length]
if len(labels) < max_length:
labels = labels + [IGNORE_INDEX] * (max_length - len(labels))
all_textual_inputs.append(q)
all_native_labels.append(labels)
all_videos.append(all_visual_names[e['id']])
all_audios.append(all_visual_names[e['id']])
all_null_images.append(-1)
all_texts.append(torch.tensor([t_all], dtype=torch.int))
all_labels.append(torch.tensor([labels], dtype=torch.int))
tokenized_texts = tokenizer(all_textual_inputs, max_length=max_length, padding='max_length', truncation=True)
tokenized_texts['labels'] = all_native_labels
tokenized_texts['images'] = all_null_images
tokenized_texts['audios'] = all_audios
tokenized_texts['videos'] = all_videos
return all_textual_inputs, all_native_labels, all_null_images, all_audios, all_videos
all_textual_inputs, all_native_labels, all_images, all_audios, all_videos = read_image_and_audio(train_metadata_dir)
return all_textual_inputs, all_native_labels, all_images, all_audios, all_videos
def preprocess_all_datasets():
all_visual_names = json_load('data/all_visual_names_instruction.json')['dict']
tokenizer = AutoTokenizer.from_pretrained('trained_models/llama_tokenizer')
# Chenyang: 2023-05-21, add special tokens
special_tokens_dict = {'additional_special_tokens': ['<image>', '</image>', '<audio>', '</audio>', '<video>', '</video>']}
if tokenizer.pad_token is None:
tokenizer.add_special_tokens(dict(pad_token=DEFAULT_PAD_TOKEN))
tokenizer.padding_side = "right"
tokenizer.add_special_tokens(
{
"eos_token": DEFAULT_EOS_TOKEN,
"bos_token": DEFAULT_BOS_TOKEN,
"unk_token": DEFAULT_UNK_TOKEN,
}
)
tokenizer.save_pretrained('trained_models/llama_tokenizer')
all_image_data = preprocess_coco_to_tensor_dataset(all_visual_names, tokenizer)
all_tetx_data = preprocess_alpaca_to_tensor_dataset(tokenizer)
all_video_data = preprocess_avsd_to_tensor_dataset(all_visual_names, tokenizer)
def draw_examples(lis, num):
ri = draw_samples([i for i in range(len(lis))], num)
return ri
ra, rb, rc = None, None, None
all_dataset = []
i = 0
for a,b,c in zip(all_image_data, all_tetx_data, all_video_data):
if ra == None:
print(len(a), len(b), len(c))
ra = draw_examples(a, 50000)
rb = draw_examples(b, 50000)
rc = draw_examples(c, 50000)
a = [a[i] for i in ra]
b = [b[i] for i in rb]
c = [c[i] for i in rc]
new_lis = a + b + c
print(len(new_lis))
all_dataset.append(new_lis)
else:
print(len(a), len(b), len(c))
a = [a[i] for i in ra]
b = [b[i] for i in rb]
c = [c[i] for i in rc]
new_lis = a + b + c
print(len(new_lis))
all_dataset.append(new_lis)
i += 1
max_length = 256
tokenized_texts = tokenizer(all_dataset[0], max_length=max_length, padding='max_length', truncation=True)
tokenized_texts['labels'] = all_dataset[1]
tokenized_texts['images'] = all_dataset[2]
tokenized_texts['audios'] = all_dataset[3]
tokenized_texts['videos'] = all_dataset[4]
for k in tokenized_texts:
print(k)
# import ipdb
# ipdb.set_trace()
pickle.dump(tokenized_texts, open('data/train_total_new_instruction_1.cache', "wb"), protocol=4)
def combine_visual_and_audio_names():
all_names = []
image_examples = json_load('data/generated_examples_coco.json')['data']
video_examples = json_load('data/generated_examples_avsd.json')['data']
for e in image_examples:
all_names.append(e['id'])
for e in video_examples:
all_names.append(e['id'])
all_names_dict = {k:ind for ind, k in enumerate(all_names)}
all_names = {'dict': all_names_dict, 'list': all_names}
json_dump(all_names, 'data/all_visual_names_instruction.json')
if __name__ == '__main__':
combine_visual_and_audio_names()
preprocess_all_datasets()
pass