-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_processor.py
221 lines (181 loc) · 8.38 KB
/
data_processor.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
import json
from transformers import ProcessorMixin, TrainingArguments
from transformers import logging as hf_logging
hf_logging.set_verbosity_info()
logger = hf_logging.get_logger("transformers")
def llava_stage1_preprocessor(example, processor: ProcessorMixin, args: TrainingArguments):
if "caption" in example:
conversation_ls = list()
for caption in example["caption"]:
conversation = [
{"role": "user", "content": json.dumps([{"type": "image"}], ensure_ascii=False)},
{
"role": "assistant",
"content": json.dumps([{"type": "text", "text": caption}], ensure_ascii=False),
},
]
conversation_ls.append(conversation)
example["conversations"] = conversation_ls
preprocess_finish_ls = list()
for idx, conversations in enumerate(example["conversations"]):
for chat in conversations:
content = json.loads(chat["content"])
if isinstance(content, list):
for part in content:
if part["type"] != "text":
continue
part["text"] = str(part["text"]) if isinstance(part["text"], (int, float)) else part["text"]
content = str(content) if isinstance(content, (int, float)) else content
chat["content"] = content
text = processor.apply_chat_template(conversations, tokenize=False)
image = example["image"][idx] if "image" in example else None
image = [i.convert("RGB") for i in image] if isinstance(image, list) else image.convert("RGB")
num_images = len(image) if isinstance(image, list) else 1
if image and text.count(processor.image_token) != num_images:
logger.info(
f"text: {text}\n"
f"image: {image}\n"
f"input_ids: {text}\n"
"image and (config.image_token_id not in input_ids) 필터링 됨.\n"
)
continue
elif (image is None) and (processor.image_token in text):
logger.info(
f"text: {text}\n"
f"image: {image}\n"
f"input_ids: {text}\n"
"(image is None) and (config.image_token_id in input_ids) 필터링 됨."
)
continue
outputs = processor(text=text, images=image, return_tensors="np")
preprocess_finish_ls.append(
{
"pixel_values": outputs["pixel_values"][0] if image else None,
"input_ids": outputs["input_ids"][0],
args.length_column_name: outputs["input_ids"][0].shape[0],
}
)
return_dict = dict()
for res in preprocess_finish_ls:
for key, value in res.items():
return_dict.setdefault(key, []).append(value)
return return_dict
def llava_stage2_preprocessor(example, processor: ProcessorMixin, args: TrainingArguments):
preprocess_finish_ls = list()
for idx, conversations in enumerate(example["conversations"]):
for chat in conversations:
content = json.loads(chat["content"])
content = str(content) if isinstance(content, (int, float)) else content
chat["content"] = content
image = example["image"][idx] if "image" in example else None
image = [i.convert("RGB") for i in image] if isinstance(image, list) else image.convert("RGB")
num_images = len(image) if isinstance(image, list) else 1
text = processor.apply_chat_template(conversations, tokenize=False)
if image and text.count(processor.image_token) != num_images:
logger.info(
f"text: {text}\n"
f"image: {image}\n"
f"input_ids: {text}\n"
"image and (config.image_token_id not in input_ids) 필터링 됨.\n"
)
continue
elif (image is None) and (processor.image_token in text):
logger.info(
f"text: {text}\n"
f"image: {image}\n"
f"input_ids: {text}\n"
"(image is None) and (config.image_token_id in input_ids) 필터링 됨."
)
continue
outputs = processor(text=text, images=image, return_tensors="np")
preprocess_finish_ls.append(
{
"pixel_values": outputs["pixel_values"][0] if image else None,
"input_ids": outputs["input_ids"][0],
args.length_column_name: outputs["input_ids"][0].shape[0],
}
)
return_dict = dict()
for res in preprocess_finish_ls:
for key, value in res.items():
return_dict.setdefault(key, []).append(value)
return return_dict
def llava_next_stage1_5_preprocessor(example, processor: ProcessorMixin, args: TrainingArguments):
preprocess_finish_ls = list()
for image, conversations in zip(example["image"], example["conversations"]):
for chat in conversations:
content = json.loads(chat["content"])
content = str(content) if isinstance(content, (int, float)) else content
chat["content"] = content
text = processor.apply_chat_template(conversations, tokenize=False)
image = [i.convert("RGB") for i in image] if isinstance(image, list) else image.convert("RGB")
num_images = len(image) if isinstance(image, list) else 1
if image and text.count(processor.image_token) != num_images:
logger.info(
f"text: {text}\n"
f"image: {image}\n"
f"input_ids: {text}\n"
"image and (config.image_token_id not in input_ids) 필터링 됨.\n"
)
continue
elif (image is None) and (processor.image_token in text):
logger.info(
f"text: {text}\n"
f"image: {image}\n"
f"input_ids: {text}\n"
"(image is None) and (config.image_token_id in input_ids) 필터링 됨."
)
continue
outputs = processor(text=text, images=image, return_tensors="np")
preprocess_finish_ls.append(
{
"pixel_values": outputs["pixel_values"][0] if image else None,
"input_ids": outputs["input_ids"][0],
"image_sizes": outputs["image_sizes"][0],
args.length_column_name: outputs["input_ids"][0].shape[0],
}
)
return_dict = dict()
for res in preprocess_finish_ls:
for key, value in res.items():
return_dict.setdefault(key, []).append(value)
return return_dict
def llava_next_stage2_preprocessor(example, processor: ProcessorMixin, args: TrainingArguments):
preprocess_finish_ls = list()
for image, conversations in zip(example["image"], example["conversations"]):
for chat in conversations:
content = json.loads(chat["content"])
chat["content"] = content
text = processor.apply_chat_template(conversations, tokenize=False)
image = [i.convert("RGB") for i in image] if isinstance(image, list) else image.convert("RGB")
num_images = len(image) if isinstance(image, list) else 1
if image and text.count(processor.image_token) != num_images:
logger.info(
f"text: {text}\n"
f"image: {image}\n"
f"input_ids: {text}\n"
"image and (config.image_token_id not in input_ids) 필터링 됨.\n"
)
continue
elif (image is None) and (processor.image_token in text):
logger.info(
f"text: {text}\n"
f"image: {image}\n"
f"input_ids: {text}\n"
"(image is None) and (config.image_token_id in input_ids) 필터링 됨."
)
continue
outputs = processor(text=text, images=image, return_tensors="np")
preprocess_finish_ls.append(
{
"pixel_values": outputs["pixel_values"][0] if image else None,
"input_ids": outputs["input_ids"][0],
"image_sizes": outputs["image_sizes"][0],
args.length_column_name: outputs["input_ids"][0].shape[0],
}
)
return_dict = dict()
for res in preprocess_finish_ls:
for key, value in res.items():
return_dict.setdefault(key, []).append(value)
return return_dict