data_checker.py 15.9 KB
Newer Older
mashun1's avatar
mashun1 committed
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
import json
import os
from tqdm import tqdm
from multiprocessing import Pool, cpu_count
import yaml


class DataProcessor:
    def __init__(self, file_path, image_root, video_root):
        self.file_path = file_path
        self.image_root = image_root
        self.data = None
        self.video_root = video_root
        self.load_data()

    def load_data(self):
        if self.file_path.endswith(".json"):
            with open(self.file_path, "r") as f:
                self.data = json.load(f)
        elif self.file_path.endswith(".yaml"):
            with open(self.file_path, "r") as f:
                self.data = yaml.safe_load(f)
        elif self.file_path.endswith(".jsonl"):
            with open(self.file_path, "r") as f:
                self.data = [json.loads(line) for line in f.readlines()]
        else:
            raise ValueError("Unsupported file format")

    def load_json_data(self, json_path):
        if json_path.endswith(".jsonl"):
            cur_data_dict = []
            with open(json_path, "r") as json_file:
                for line in json_file:
                    cur_data_dict.append(json.loads(line.strip()))
            return cur_data_dict
        elif json_path.endswith(".json"):
            with open(json_path, "r") as f:
                return json.load(f)
        else:
            raise ValueError("Unsupported file format")

    def check_image_existence(self, data):
        if "image" in data:
            if type(data["image"]) == list:
                images = data["image"]
            else:
                images = [data["image"]]

            for image in images:
                full_image_path = os.path.join(self.image_root, image)
                if not os.path.exists(full_image_path):
                    print(f"WARNING!!! {full_image_path} not exists !!!")

        if "video" in data:
            full_video_path = os.path.join(self.video_root, data["video"])
            if not os.path.exists(full_video_path):
                print(f"WARNING!!! {full_video_path} not exists !!!")

        # if data["conversations"][0]["value"].count("<image>") > 1:
        #     print(f"WARNING!!! {data['conversations'][0]['value']} has more than one <image> !!!")

    def check_item_structure(self, item):
        if not all(key in item for key in ["conversations"]):
            print(f"WARNING!!! Item {item.get('id', 'unknown')} is missing required fields!")
            return False

        conversations = item["conversations"]
        if not isinstance(conversations, list) or len(conversations) < 2 or len(conversations) % 2 != 0:
            print(f"WARNING!!! Item {item['id']} has invalid conversations structure!")
            return False

        for i, conv in enumerate(conversations):
            if not all(key in conv for key in ["from", "value"]):
                print(f"WARNING!!! Item {item['id']} has invalid conversation format!")
                return False

            expected_from = "human" if i % 2 == 0 else "gpt"
            if conv["from"] != expected_from:
                print(f"WARNING!!! Item {item['id']} has incorrect conversation order!")
                return False

        return True

    def check_image_and_structure(self, item):
        if not self.check_item_structure(item):
            return

        # self.check_image_existence(item)

    def process_images(self):
        if isinstance(self.data, list):
            args = [d for d in self.data]
            with Pool(processes=cpu_count()) as pool:
                list(tqdm(pool.imap(self.check_image_and_structure, args), total=len(self.data)))
        elif isinstance(self.data, dict):
            for d in self.data["datasets"]:
                dd_json_path = d["json_path"]
                data = self.load_json_data(dd_json_path)
                args = [d for d in data]
                with Pool(processes=cpu_count()) as pool:
                    list(tqdm(pool.imap(self.check_image_and_structure, args), total=len(data), desc=f"Processing {dd_json_path}"))

    def count_items(self):
        if isinstance(self.data, list):  # Assuming JSON data loaded directly
            return len(self.data)
        elif isinstance(self.data, dict):  # Assuming YAML data loaded
            total_items_count = 0
            for d in self.data["datasets"]:
                dd_json_path = d["json_path"]
                data = self.load_json_data(dd_json_path)
                current_items_count = len(data)

                sampling_strategy = d["sampling_strategy"]
                try:
                    if sampling_strategy != "all":
                        percentage = float(sampling_strategy.split(":")[-1].replace("%", "")) / 100.0
                    else:
                        percentage = 1.0
                except Exception as e:
                    print(f"Error: {e}")
                    percentage = 1.0

                sampling_count = int(current_items_count * percentage)
                total_items_count += sampling_count
                print(f"{dd_json_path}: {sampling_count}")
            return total_items_count

    def stat_data(self):
        if isinstance(self.data, dict):
            cur_lens_list = []
            single_image_count = 0
            multiple_image_count = 0
            video_count = 0
            total_count = 0
            text_count = 0
            max_tokens_item = None
            max_tokens = 0

            for d in self.data["datasets"]:
                dd_json_path = d["json_path"]
                data = self.load_json_data(dd_json_path)
                sampling_strategy = d["sampling_strategy"]

                try:
                    if sampling_strategy != "all":
                        percentage = float(sampling_strategy.split(":")[-1].replace("%", "")) / 100.0
                    else:
                        percentage = 1.0
                except Exception as e:
                    print(f"Error parsing sampling strategy: {e}")
                    percentage = 1.0

                sampled_count = int(len(data) * percentage)
                print(f"{dd_json_path}: {sampled_count} (sampled from {len(data)})")

                for item in data[:sampled_count]:
                    conversations = item["conversations"]
                    cur_len = sum([len(conv["value"].split()) for conv in conversations])
                    cur_lens_list.append(cur_len)

                    if cur_len > max_tokens:
                        max_tokens = cur_len
                        max_tokens_item = item

                    total_count += 1
                    if "image" in item:
                        if isinstance(item["image"], list):
                            if len(item["image"]) > 1:
                                multiple_image_count += 1
                            else:
                                single_image_count += 1
                        else:
                            single_image_count += 1
                    elif "video" in item:
                        video_count += 1
                    else:
                        text_count += 1

            print(f"Max length: {max(cur_lens_list)}, Min length: {min(cur_lens_list)}, Average length: {sum(cur_lens_list) / len(cur_lens_list)}")
            print(f"Total items: {total_count}")
            print(f"Text items: {text_count} ({text_count/total_count*100:.2f}%)")
            print(f"Single image items: {single_image_count} ({single_image_count/total_count*100:.2f}%)")
            print(f"Multiple image items: {multiple_image_count} ({multiple_image_count/total_count*100:.2f}%)")
            print(f"Video items: {video_count} ({video_count/total_count*100:.2f}%)")

            print("\nItem with the largest number of tokens:")
            print(f"Token count: {max_tokens}")
            print("Item content:")
            print(json.dumps(max_tokens_item, indent=2))

    def filter_data(self):
        if isinstance(self.data, dict):
            for d in self.data["datasets"]:
                dd_json_path = d["json_path"]
                print(f"Processing {dd_json_path}")
                data = self.load_json_data(dd_json_path)

                filtered_data = []
                mismatch_data = []
                mismatch_flag = False
                for item in data:
                    try:
                        if "image" in item:
                            num_image = len(item["image"]) if isinstance(item["image"], list) else 1
                        else:
                            num_image = 0

                        if "video" in item:
                            num_video = len(item["video"]) if isinstance(item["video"], list) else 1
                        else:
                            num_video = 0

                        num_visuals = num_image + num_video
                        conv_text = ""
                        for conv in item["conversations"]:
                            conv_text += conv["value"]

                        num_img_token_appearance = conv_text.count("<image>")
                        if len(conv_text) == 0:
                            print(f"Conversation text is empty for {item}")

                        if num_img_token_appearance == num_visuals or num_img_token_appearance < num_visuals and len(conv_text) > 0:
                            filtered_data.append(item)
                        elif num_img_token_appearance > num_visuals:
                            item["num_img_token_appearance"] = num_img_token_appearance
                            item["num_visuals"] = num_visuals
                            mismatch_data.append(item)

                            if not mismatch_flag:
                                print(f"Data mismatch for {item}")

                            mismatch_flag = True
                    except Exception as e:
                        print(f"Error: {e}")
                        print()

                if mismatch_flag:
                    print(f"Data mismatch for {dd_json_path}")

                if len(filtered_data) < len(data):
                    saving_dd_json_path = dd_json_path.replace(".jsonl", f"fltd_{len(filtered_data)}.json").replace(".json", f"fltd_{len(filtered_data)}.json")
                    with open(saving_dd_json_path, "w") as f:
                        json.dump(filtered_data, f, indent=2)
                    print(f"Filtered data count: {len(filtered_data)}")
                else:
                    pass

    def stat_and_filter_data(self, threshold):
        if isinstance(self.data, dict):
            cur_lens_list = []
            single_image_count = 0
            multiple_image_count = 0
            video_count = 0
            total_count = 0
            text_count = 0

            for d in self.data["datasets"]:
                dd_json_path = d["json_path"]
                data = self.load_json_data(dd_json_path)
                sampling_strategy = d["sampling_strategy"]
                filtered_data = []

                try:
                    if sampling_strategy != "all":
                        percentage = float(sampling_strategy.split(":")[-1].replace("%", "")) / 100.0
                    else:
                        percentage = 1.0
                except Exception as e:
                    print(f"Error parsing sampling strategy: {e}")
                    percentage = 1.0

                sampled_count = int(len(data) * percentage)
                print(f"{dd_json_path}: {sampled_count} (sampled from {len(data)})")

                save_flag = False
                for item in data:
                    total_count += 1
                    conversations = item["conversations"]
                    filtered_conversations = []
                    current_token_count = 0

                    for i in range(0, len(conversations), 2):
                        if i + 1 < len(conversations):
                            human_conv = conversations[i]
                            gpt_conv = conversations[i + 1]
                            pair_tokens = len(human_conv["value"].split()) + len(gpt_conv["value"].split())

                            if current_token_count + pair_tokens <= threshold:
                                filtered_conversations.extend([human_conv, gpt_conv])
                                current_token_count += pair_tokens
                            else:
                                save_flag = True
                                break

                    if filtered_conversations:
                        item["conversations"] = filtered_conversations
                        cur_len = sum([len(conv["value"].split()) for conv in filtered_conversations])
                        cur_lens_list.append(cur_len)
                        filtered_data.append(item)

                        if "image" in item:
                            if isinstance(item["image"], list):
                                if len(item["image"]) > 1:
                                    multiple_image_count += 1
                                else:
                                    single_image_count += 1
                            else:
                                single_image_count += 1
                        elif "video" in item:
                            video_count += 1
                        else:
                            text_count += 1

                # Save filtered data for each dataset
                if filtered_data and save_flag:
                    if dd_json_path.endswith(".jsonl"):
                        output_file = dd_json_path.replace(".jsonl", f"_filtered_{threshold}tokens_{len(filtered_data)}.jsonl")
                        with open(output_file, "w") as f:
                            for item in filtered_data:
                                f.write(json.dumps(item) + "\n")
                    else:
                        output_file = dd_json_path.replace(".json", f"_filtered_{threshold}tokens_{len(filtered_data)}.json")
                        with open(output_file, "w") as f:
                            json.dump(filtered_data, f, indent=2)
                    print(f"Filtered data for {dd_json_path} saved to: {output_file}")

            print(f"Max length: {max(cur_lens_list)}, Min length: {min(cur_lens_list)}, Average length: {sum(cur_lens_list) / len(cur_lens_list)}")
            print(f"Total items: {total_count}")
            print(f"Text items: {text_count} ({text_count/total_count*100:.2f}%)")
            print(f"Single image items: {single_image_count} ({single_image_count/total_count*100:.2f}%)")
            print(f"Multiple image items: {multiple_image_count} ({multiple_image_count/total_count*100:.2f}%)")
            print(f"Video items: {video_count} ({video_count/total_count*100:.2f}%)")


def main(file_path, image_root, operation, video_root, threshold=None):
    processor = DataProcessor(file_path, image_root, video_root)
    if operation == "check":
        processor.process_images()
    elif operation == "count":
        total_items = processor.count_items()
        print(f"Total items: {total_items}")
    elif operation == "filter":
        processor.filter_data()
    elif operation == "stat":
        processor.stat_data()
    elif operation == "stat_and_filter":
        if threshold is None:
            raise ValueError("Threshold must be provided for stat_and_filter operation")
        processor.stat_and_filter_data(threshold)
    else:
        raise ValueError("Unsupported operation")


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--file_path", type=str, default="/mnt/bn/vl-research/workspace/boli01/projects/LLaVA_Next/scripts/i18n/scale_llms/next_continual.yaml")
    parser.add_argument("--image_root", type=str, default="/mnt/bn/vl-research/data/llava_data")
    parser.add_argument("--video_root", type=str, default="/mnt/bn/vl-research/data/llava_video")
    parser.add_argument("--operation", type=str, default="filter")
    parser.add_argument("--threshold", type=int, default=None, help="Threshold for stat_and_filter operation")
    args = parser.parse_args()
    main(args.file_path, args.image_root, args.operation, args.video_root, args.threshold)