-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_datasets.py
More file actions
executable file
·169 lines (141 loc) · 10.1 KB
/
preprocess_datasets.py
File metadata and controls
executable file
·169 lines (141 loc) · 10.1 KB
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
import pandas as pd
import os
import json
from paths import *
from drivefusion_preprocessing.dataset_preprocessors import (
LingoDatasetPreprocessor,
DriveGPT4BDDXDatasetPreprocessor,
DriveLMDatasetPreprocessor,
)
def preprocess_lingoqa(system_path: str):
# Action Part
print('\n\n--------------------LingoQA Action Part--------------------')
os.makedirs(OUR_LINGO_ACTION_DATASET_PATH, exist_ok=True)
df_action = pd.read_parquet(os.path.join(LINGO_ACTION_PATH, 'train.parquet'))
LINGO_ACTION_IMAGES_PATH = f'{system_path}/datasets/lingoqa_dataset/action' #For datacrunch
# LINGO_ACTION_IMAGES_PATH = '/content/gdrive/MyDrive/drivefusion_project/datasets/lingoqa_dataset/action' # For Colab
action_lingo_preprocessor = LingoDatasetPreprocessor(train_data=df_action, eval_data=None, is_single=True)
action_lingo_preprocessor.llama_train_preprocess(tokens=['<image>'],
images_path=LINGO_ACTION_IMAGES_PATH,
output_path=OUR_LINGO_ACTION_DATASET_PATH,
multi_qa_output_filename='lingoqa_action_llama_format_multi_question.json',
single_qa_output_filename='lingoqa_action_llama_format_single_question.json')
action_lingo_preprocessor.llava_train_preprocess(tokens=['<image>\n'],
images_path=LINGO_ACTION_IMAGES_PATH,
output_path=OUR_LINGO_ACTION_DATASET_PATH,
multi_qa_output_filename='lingoqa_action_llava_format_multi_question.json',
single_qa_output_filename='lingoqa_action_llava_format_single_question.json'
)
# Scenery Part
print('\n\n--------------------LingoQA Scenery Part--------------------')
os.makedirs(OUR_LINGO_SCENERY_DATASET_PATH, exist_ok=True)
df_scenery = pd.read_parquet(os.path.join(LINGO_SCENERY_PATH, 'train.parquet'))
LINGO_SCENERY_IMAGES_PATH = f'{system_path}/datasets/lingoqa_dataset/scenery'
# LINGO_SCENERY_IMAGES_PATH = '/content/gdrive/MyDrive/drivefusion_project/datasets/lingoqa_dataset/scenery'
scenery_lingo_preprocessor = LingoDatasetPreprocessor(train_data=df_scenery, eval_data=None, is_single=True)
scenery_lingo_preprocessor.llama_train_preprocess(tokens=['<image>'],
images_path=LINGO_SCENERY_IMAGES_PATH,
output_path=OUR_LINGO_SCENERY_DATASET_PATH,
multi_qa_output_filename='lingoqa_scenery_llama_format_multi_question.json',
single_qa_output_filename='lingoqa_scenery_llama_format_single_question.json')
action_lingo_preprocessor.llava_train_preprocess(tokens=['<image>\n'],
images_path=LINGO_SCENERY_IMAGES_PATH,
output_path=OUR_LINGO_SCENERY_DATASET_PATH,
multi_qa_output_filename='lingoqa_scenery_llava_format_multi_question.json',
single_qa_output_filename='lingoqa_scenery_llava_format_single_question.json')
# Eval Part
print('\n\n--------------------LingoQA Eval Part--------------------')
os.makedirs(OUR_LINGO_EVAL_DATASET_PATH, exist_ok=True)
df_eval = pd.read_parquet(os.path.join(LINGO_EVAL_PATH, 'val.parquet'))
eval_lingo_preprocessor = LingoDatasetPreprocessor(train_data=None, eval_data=df_eval, is_single=False)
eval_lingo_preprocessor.eval_preprocess(tokens=[''],
output_path=OUR_LINGO_EVAL_DATASET_PATH,
eval_output_filename='lingoqa_eval.json'
)
def preprocess_drivegpt4_bddx(system_path: str):
# Training Part
print('\n\n--------------------DriveGPT4 BDDX Training Part--------------------')
os.makedirs(OUR_DRIVE_BDDX_TRAIN_DATASET_PATH, exist_ok=True)
with open(os.path.join(DRIVE_BDDX_DATASET_PATH, 'BDD_X_training_label.json'), 'r') as f:
drive_bddx_train_data = json.load(f)
with open(os.path.join(DRIVE_BDDX_DATASET_PATH, 'BDD_X_testing_label.json'), 'r') as f:
drive_bddx_test_data = json.load(f)
DRIVE_BDDX_IMAGES_PATH = f'{system_path}/datasets/drivegpt4_dataset/BDD_X_imgs_select'
drivegpt4_preprocessor = DriveGPT4BDDXDatasetPreprocessor(train_data=drive_bddx_train_data, eval_data=drive_bddx_test_data)
drivegpt4_preprocessor.llama_train_preprocess(tokens=['<image>', '\n<video>'],
images_path=DRIVE_BDDX_IMAGES_PATH,
output_path=OUR_DRIVE_BDDX_TRAIN_DATASET_PATH,
multi_qa_output_filename='drivegpt_bddx_llama_format_training_multi_question.json',
single_qa_output_filename='drivegpt_bddx_llama_format_training_single_question.json'
)
drivegpt4_preprocessor.llava_train_preprocess(tokens=['<image>\n', '\n<video>'],
images_path=DRIVE_BDDX_IMAGES_PATH,
output_path=OUR_DRIVE_BDDX_TRAIN_DATASET_PATH,
multi_qa_output_filename='drivegpt_bddx_llava_format_training_multi_question.json',
single_qa_output_filename='drivegpt_bddx_llava_format_training_single_question.json'
)
# Testing Part
print('\n\n--------------------DriveGPT4 BDDX Testing Part--------------------')
os.makedirs(OUR_DRIVE_BDDX_TEST_DATASET_PATH, exist_ok=True)
drivegpt4_preprocessor.eval_preprocess(tokens=[''],
output_path=OUR_DRIVE_BDDX_TEST_DATASET_PATH,
eval_output_filename='drivegpt_bddx_testing.json'
)
def preprocess_drivelm(system_path: str):
print('\n\n--------------------DriveLM Training Part--------------------')
os.makedirs(OUR_DRIVELM_DATASET_PATH, exist_ok=True)
with open(os.path.join(DRIVELM_DATASET_PATH, 'v1_1_train_nus.json'), 'r') as f:
drive_lm_data = json.load(f)
def remove_dots_img_path(json_data: dict) -> dict:
for _, scene_data in json_data.items():
# Iterate through each keyframe
for key_frame_id, key_frame_data in scene_data.get("key_frames", {}).items():
key_frame_data["image_paths"]["CAM_FRONT"] = key_frame_data["image_paths"]["CAM_FRONT"].replace("../", "")
key_frame_data["image_paths"]["CAM_FRONT_LEFT"] = key_frame_data["image_paths"]["CAM_FRONT_LEFT"].replace("../", "")
key_frame_data["image_paths"]["CAM_FRONT_RIGHT"] = key_frame_data["image_paths"]["CAM_FRONT_RIGHT"].replace("../", "")
key_frame_data["image_paths"]["CAM_BACK"] = key_frame_data["image_paths"]["CAM_BACK"].replace("../", "")
key_frame_data["image_paths"]["CAM_BACK_LEFT"] = key_frame_data["image_paths"]["CAM_BACK_LEFT"].replace("../", "")
key_frame_data["image_paths"]["CAM_BACK_RIGHT"] = key_frame_data["image_paths"]["CAM_BACK_RIGHT"].replace("../", "")
return json_data
drive_lm_data = remove_dots_img_path(drive_lm_data)
DRIVELM_IMAGES_PATH = f"{system_path}/datasets/drivelm_dataset/drivelm_nus_imgs_final_train"
drivelm_preprocessor = DriveLMDatasetPreprocessor(train_data=drive_lm_data, eval_data=None)
drivelm_preprocessor.llama_train_preprocess(
tokens=['<image>'],
images_path=DRIVELM_IMAGES_PATH,
output_path=OUR_DRIVELM_DATASET_PATH,
multi_qa_output_filename='drivelm_llama_format_multi_question.json',
single_qa_output_filename='drivelm_llama_format_single_question.json'
)
drivelm_preprocessor.llava_train_preprocess(
tokens=['<image>\n'],
images_path=DRIVELM_IMAGES_PATH,
output_path=OUR_DRIVELM_DATASET_PATH,
multi_qa_output_filename='drivelm_llava_format_multi_question.json',
single_qa_output_filename='drivelm_llava_format_single_question.json'
)
def merge_data():
with open(os.path.join(OUR_LINGO_ACTION_DATASET_PATH, 'lingoqa_action_llama_format_multi_question.json'), 'r') as f:
lingo_action_data = json.load(f)
with open(os.path.join(OUR_LINGO_SCENERY_DATASET_PATH, 'lingoqa_scenery_llama_format_multi_question.json'), 'r') as f:
lingo_scenery_data = json.load(f)
print(f"The number of examples in LingoQA Dataset: {len(lingo_action_data) + len(lingo_scenery_data)}")
with open(os.path.join(OUR_DRIVE_BDDX_TRAIN_DATASET_PATH, 'drivegpt_bddx_llama_format_training_multi_question.json'), 'r') as f:
drive_bddx_data = json.load(f)
print(f"The number of examples in DriveGPT4 Dataset: {len(drive_bddx_data)}")
with open(os.path.join(OUR_DRIVELM_DATASET_PATH, 'drivelm_llama_format_multi_question.json'), 'r') as f:
drivelm_data = json.load(f)
print(f"The number of examples in DriveLM Dataset: {len(drivelm_data)}")
new_data = lingo_action_data + lingo_scenery_data + drive_bddx_data + drivelm_data
print(f'total training examples: {len(new_data)}')
with open(os.path.join(OUR_DATASET_PATH, 'training_multi_qa_all_examples.json'), 'w') as f:
json.dump(new_data, f, indent=4)
def main():
SYSTEM_PATH = DATACRUNCH_LINUX_SYSTEM_PATH
preprocess_lingoqa(system_path=SYSTEM_PATH)
preprocess_drivegpt4_bddx(system_path=SYSTEM_PATH)
preprocess_drivelm(system_path=SYSTEM_PATH)
merge_data()
# check_images_existence(os.path.join(OUR_DATASET_PATH, 'training_multi_qa_all_examples.json'))
if __name__ == '__main__':
main()