MMFMChallenge / backup /utils /prepare_sample_answer_dict_files.py
helloai0's picture
Upload 104 files
375239b verified
raw
history blame
4.35 kB
import json
import csv
if __name__ == '__main__':
answer_dict_file = '/system/user/publicdata/LMM_benchmarks/MMFM_challenge_final/doc-vl-eval/answer_dicts/coverted_output_test_all_datasets.json'
answer_dict_w_question_phase1_file = '/system/user/publicdata/LMM_benchmarks/MMFM_challenge_final/doc-vl-eval/answer_dicts/converted_output_test_for_mixtral_eval.json'
answer_dict_w_question_phase2_file = '/system/user/publicdata/LMM_benchmarks/MMFM_challenge_final/doc-vl-eval/answer_dicts/converted_output_test_private_for_mixtral_eval.json'
solution_file = 'MMFM_Challenge_solution.csv'
category_to_sample_ids_dict_file = 'category_to_sample_ids_dict.json'
answer_dict_for_mixtral_eval_file = 'answer_dict_for_mixtral_eval.json'
header = ['id', 'pred', 'split']
# id is in format docvqa_docvqa_0_4
# answer_dict = {}
with open(answer_dict_file, 'r') as f:
answer_dict = json.load(f) # 2000 samples, 200 samples x 10 datasets
answer_dict_w_question_phase1 = json.load(open(answer_dict_w_question_phase1_file)) # 800(=200*4) samples
answer_dict_w_question_phase2 = json.load(open(answer_dict_w_question_phase2_file)) # 200+400+428=1028 samples
answer_dict = {**answer_dict, **answer_dict_w_question_phase2}
answer_dict_w_question = {**answer_dict_w_question_phase1, **answer_dict_w_question_phase2}
category_to_sample_ids_dict = {}
answer_dict_for_mixtral_eval = {}
datasets_w_mixtral_eval_phase1 = ['docvqa', 'infographicvqa', 'websrc', 'wtq']
datasets_w_mixtral_eval_phase2 = ['mydoc', 'mychart', 'myinfographic']
long_key_to_short_key_in_answer_dict = {}
short_key_to_long_key_in_answer_dict = {}
for sample_key in answer_dict.keys():
short_key = '_'.join( sample_key.split('_')[:-1])
long_key_to_short_key_in_answer_dict.update({sample_key: short_key})
short_key_to_long_key_in_answer_dict.update({short_key: sample_key})
long_key_to_short_key_in_answer_dict_w_question = {}
short_key_to_long_key_in_answer_dict_w_question = {}
for sample_key in answer_dict_w_question.keys():
short_key = '_'.join( sample_key.split('_')[:-1])
long_key_to_short_key_in_answer_dict_w_question.update({sample_key: short_key})
short_key_to_long_key_in_answer_dict_w_question.update({short_key: sample_key})
with open(solution_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
dataset_counter_dict = {}
for sample_key, data_dict in answer_dict.items():
items = sample_key.split('_')
id_ = '_'.join(items[:-1])
if items[0] in datasets_w_mixtral_eval_phase2:
category = items[0]
else:
if items[0] == items[1]:
category = items[0]
else:
category = items[0] + '_' + items[1]
if category not in category_to_sample_ids_dict:
category_to_sample_ids_dict[category] = []
if category not in dataset_counter_dict:
dataset_counter_dict[category] = 0
# id_ = f'{category}_{dataset_counter_dict[category]}'
category_to_sample_ids_dict[category].append(id_)
# write to the solution file
writer.writerow([id_, data_dict['ground_truth'], 'public'])
if category in datasets_w_mixtral_eval_phase1 + datasets_w_mixtral_eval_phase2:
short_key = long_key_to_short_key_in_answer_dict[sample_key]
sample_key_w_question = short_key_to_long_key_in_answer_dict_w_question[short_key]
answer_dict_for_mixtral_eval.update({
id_: {
"question_type": "short-answer",
"ground_truth": answer_dict_w_question[sample_key_w_question]['ground_truth'],
'question': answer_dict_w_question[sample_key_w_question]['question'],
"dataset": category,
}
})
dataset_counter_dict[category] += 1
with open(category_to_sample_ids_dict_file, 'w') as f:
json.dump(category_to_sample_ids_dict, f)
with open(answer_dict_for_mixtral_eval_file, 'w') as f:
json.dump(answer_dict_for_mixtral_eval, f)