-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspatial_gen_api_multilingual.py
342 lines (320 loc) · 12.9 KB
/
spatial_gen_api_multilingual.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
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
import os
from torch.utils.data import DataLoader
import json
from argparse import ArgumentParser
from tqdm import tqdm
from comfort_utils.model_utils.models_api import gpt4o_completion, query_translation
from comfort_utils.data_utils import CLEVR_generic_path
from comfort_utils.helper import get_prompt_template, show_image
num_erroneous_data = 0
PRIORITIZED_LANGUAGES = ["EN-US", "JA", "KO", "ZH", "ha", "ta"]
DEEPL_SUPPORTED_LANGUAGES = ["AR", "BG", "CS", "DA", "DE", "EL", "EN-GB", "EN-US", "ES", "ET", "FI", "FR", "HU", "ID", "IT", "JA", "KO", "LT", "LV", "NB", "NL", "PL", "PT-BR", "PT-PT", "RO", "RU", "SK", "SL", "SV", "TR", "UK", "ZH"]
GOOGLET_SUPPORTED_LANGUAGES = {
"Afrikaans": "af",
"Albanian": "sq",
"Amharic": "am",
"Armenian": "hy",
"Assamese": "as",
"Aymara": "ay",
"Azerbaijani": "az",
"Bambara": "bm",
"Basque": "eu",
"Belarusian": "be",
"Bengali": "bn",
"Bhojpuri": "bho",
"Bosnian": "bs",
"Catalan": "ca",
"Cebuano": "ceb",
"Corsican": "co",
"Croatian": "hr",
"Dhivehi": "dv",
"Dogri": "doi",
"Esperanto": "eo",
"Ewe": "ee",
"Filipino (Tagalog)": "fil",
"Frisian": "fy",
"Galician": "gl",
"Georgian": "ka",
"Guarani": "gn",
"Gujarati": "gu",
"Haitian Creole": "ht",
"Hausa": "ha",
"Hawaiian": "haw",
"Hebrew": "he",
"Hindi": "hi",
"Hmong": "hmn",
"Icelandic": "is",
"Igbo": "ig",
"Ilocano": "ilo",
"Irish": "ga",
"Javanese": "jv",
"Kannada": "kn",
"Kazakh": "kk",
"Khmer": "km",
"Kinyarwanda": "rw",
"Konkani": "gom",
"Krio": "kri",
"Kurdish": "ku",
"Kurdish (Sorani)": "ckb",
"Kyrgyz": "ky",
"Lao": "lo",
"Latin": "la",
"Lingala": "ln",
"Luganda": "lg",
"Luxembourgish": "lb",
"Macedonian": "mk",
"Maithili": "mai",
"Malagasy": "mg",
"Malay": "ms",
"Malayalam": "ml",
"Maltese": "mt",
"Maori": "mi",
"Marathi": "mr",
"Meiteilon (Manipuri)": "mni-Mtei",
"Mizo": "lus",
"Mongolian": "mn",
"Myanmar (Burmese)": "my",
"Nepali": "ne",
"Nyanja (Chichewa)": "ny",
"Odia (Oriya)": "or",
"Oromo": "om",
"Pashto": "ps",
"Persian": "fa",
"Punjabi": "pa",
"Quechua": "qu",
"Samoan": "sm",
"Sanskrit": "sa",
"Scots Gaelic": "gd",
"Sepedi": "nso",
"Serbian": "sr",
"Sesotho": "st",
"Shona": "sn",
"Sindhi": "sd",
"Sinhala (Sinhalese)": "si",
"Somali": "so",
"Sundanese": "su",
"Swahili": "sw",
"Tajik": "tg",
"Tamil": "ta",
"Tatar": "tt",
"Telugu": "te",
"Thai": "th",
"Tigrinya": "ti",
"Tsonga": "ts",
"Turkmen": "tk",
"Twi (Akan)": "ak",
"Urdu": "ur",
"Uyghur": "ug",
"Uzbek": "uz",
"Vietnamese": "vi",
"Welsh": "cy",
"Xhosa": "xh",
"Yiddish": "yi",
"Yoruba": "yo",
"Zulu": "zu"
}
SUPPORTED_LANGUAGES = DEEPL_SUPPORTED_LANGUAGES + list(GOOGLET_SUPPORTED_LANGUAGES.values())
# SUPPORTED_LANGUAGES = PRIORITIZED_LANGUAGES
print("Number of supported languages:", len(SUPPORTED_LANGUAGES))
batch_size = 1
SUPPORTED_MODELS = [
"gpt-4v",
"gpt-4o",
]
parser = ArgumentParser()
# parser.add_argument(
# "--language",
# type=str,
# default="EN-US",
# choices=SUPPORTED_LANGUAGES,
# )
parser.add_argument(
"--model",
type=str,
default="gpt-4o",
choices=SUPPORTED_MODELS,
)
parser.add_argument("--data_path", type=str, default="data/comfort_car_ref_facing_right")
args = parser.parse_args()
print("Using args: ", args)
dataset_path_root = args.data_path
if args.model == "gpt-4o":
model = gpt4o_completion
else:
raise ValueError(f"Model {args.model} not found")
# Use the following dictionaries to convert the dataset configuration to human-readable text
dataset_type = args.data_path.split("_")[1]
dataset_type_full = '_'.join(args.data_path.split('_')[1:])
print("dataset_type full:", dataset_type_full)
for target_lang in SUPPORTED_LANGUAGES:
for perspective_prompt in ["nop", "camera3", "addressee3", "reference3"]:
if dataset_type == "ball":
shape_names = {
"Sphere": "sphere",
"SmoothCube_v2": "cube",
"SmoothCylinder": "cylinder",
"FatCylinder": "cylinder",
"Table": "table",
"Box": "box",
}
color_names = ["red", "blue", "green", "yellow"]
distractor_obj = "green sphere"
results_root = f"results/multilingual/comfort_{dataset_type_full}/{perspective_prompt}"
elif dataset_type == "car":
shape_names = {
# reference objects
"Horse": "horse",
"Bench": "bench",
"Laptop": "laptop",
"Sofa": "sofa",
"Basketball": "basketball",
"Chair": "chair",
"Dog": "dog",
"Bed": "bed",
"Duck": "duck",
"bicycle_mountain": "bicycle",
"car_sedan": "car",
# addressee
"Sophia": "woman",
}
results_root = f"results/multilingual/comfort_{dataset_type_full}/{perspective_prompt}/{target_lang}"
else:
raise ValueError(f"Dataset type not exist: {dataset_type}")
relation_names = {
"above": "above",
"behind": "behind",
"in": "in",
"inbetween": "in between",
"infrontof": "in front of",
"inthemiddleof": "in the middle of",
"totheleft": "to the left of",
"totheright": "to the right of",
"under": "under",
}
x_names = {
"rotate": "angle",
"translate": "translation",
}
# objects_list = []
# for shape in shape_names.values():
# for color in color_names:
# objects_list.append(f"{color} {shape}")
# obj_hallucination_prompt_template = "Is there any {obj}?"
positive_prompt_template = get_prompt_template(perspective_prompt)
model_name = args.model
results_name = f"{model_name}.json"
results_path = os.path.join(results_root, results_name)
if os.path.exists(results_path):
# Load the JSON data into the result variable
with open(results_path, "r") as file:
results = json.load(file)
print(f"{results_path} loaded successfully.")
else:
if not os.path.exists(results_root):
os.makedirs(results_root)
results = {}
print(f"{results_path} does not exist. Setting to empty.")
results["dataset_type"] = dataset_type
results["model"] = model_name
dataset_types = sorted(list(os.listdir(dataset_path_root)))
configurations_pbar_desc = "Total evaluation progress"
configurations = tqdm(dataset_types, desc=configurations_pbar_desc)
for configuration in configurations:
if not os.path.isdir(os.path.join(dataset_path_root, configuration)):
# Skip files in the directory
continue
# if configuration in results.keys():
# continue
if configuration in results:
temp_results_configuration = results[configuration]
else:
temp_results_configuration = False
results[configuration] = {}
relation = relation_names[configuration]
data = {}
variation_types = sorted(
list(os.listdir(os.path.join(dataset_path_root, configuration)))
)
variations_pbar_desc = f'Evaluating "{configuration}"'.ljust(
len(configurations_pbar_desc)
)
with tqdm(
total=len(variation_types) * 1, desc=variations_pbar_desc, leave=False
) as variations_pbar:
for variation_type in variation_types:
if not os.path.isdir(
os.path.join(dataset_path_root, configuration, variation_type)
):
# Skip files in the directory
continue
data[variation_type] = {}
dataset_path = os.path.join(
dataset_path_root, f"{configuration}/{variation_type}"
)
dataset = CLEVR_generic_path(img_dir=dataset_path)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
shape1 = shape_names[dataset.config["var_shape"]]
if dataset_type == "ball":
color1 = dataset.config["var_color"]
obj1 = f"{color1} {shape1}"
elif dataset_type == "car":
obj1 = f"{shape1}"
shape2 = shape_names[dataset.config["ref_shape"]]
if dataset_type == "ball":
color2 = dataset.config["ref_color"]
obj2 = f"{color2} {shape2}"
elif dataset_type == "car":
obj2 = f"{shape2}"
if perspective_prompt[: len("reference")] != "reference":
positive_prompt = positive_prompt_template.format(
obj1=obj1, relation=relation, obj2=obj2
)
else:
positive_prompt = positive_prompt_template.format(
reference=obj2, obj1=obj1, relation=relation, obj2=obj2
)
translated_user_prompt = query_translation(positive_prompt, target_lang)
system_instruction_prompt = f'You will be provided an image and a question, please answer the question only in "{query_translation("Yes", target_lang)}" or "{query_translation("No", target_lang)}"'
translated_system_instruction_prompt = query_translation(system_instruction_prompt, target_lang)
stats_prompt = []
positive_data_idx = 0
for batch_idx, batch in enumerate(dataloader):
if batch_idx in [0, 9, 18, 27]:
image_path = batch[0][0]
if temp_results_configuration == False:
response = model(image_path, translated_user_prompt, translated_system_instruction_prompt)
stats_prompt.append({"response": response, "image_path": image_path})
elif "response.text" in temp_results_configuration["data"][variation_type]["positive"][positive_data_idx]["response"].keys():
# print("batch:", batch)
# show_image(image_path)
num_erroneous_data += 1
print("Found one erroneous data, regenerating... Number of erroneous data:", num_erroneous_data)
response = model(image_path, translated_user_prompt, translated_system_instruction_prompt)
stats_prompt.append({"response": response, "image_path": image_path})
else:
stats_prompt.append({"response": temp_results_configuration["data"][variation_type]["positive"][positive_data_idx]["response"], "image_path": image_path})
positive_data_idx += 1
data[variation_type]["system_prompt_before_translation"] = system_instruction_prompt
data[variation_type]["system_prompt_after_translation"] = translated_system_instruction_prompt
data[variation_type]["positive_prompt_before_translation"] = positive_prompt
data[variation_type]["positive_prompt_after_translation"] = translated_user_prompt
data[variation_type]["positive"] = stats_prompt
variations_pbar.update(1)
data[variation_type]["config"] = dataset.config
results[configuration]["data"] = data
results[configuration]["perspective_prompt"] = perspective_prompt
if perspective_prompt[: len("reference")] != "reference":
results[configuration]["positive_template"] = positive_prompt_template.format(
obj1="[A]", relation=relation, obj2="[B]"
)
else:
results[configuration]["positive_template"] = positive_prompt_template.format(
reference="[B]", obj1="[A]", relation=relation, obj2="[B]"
)
results[configuration]["x_name"] = x_names[dataset.config["path_type"]]
results[configuration]["config"] = results[configuration]["data"]["default"][
"config"
]
with open(results_path, "w") as fp:
json.dump(results, fp, indent=4)
print("num_erroneous_data:", num_erroneous_data)