|
import json |
|
import random |
|
import os |
|
import copy |
|
from collections import defaultdict |
|
|
|
json_dir = '/home/data2/hmguo/vlmtest/benchmark' |
|
output_dir = '/home/data2/hmguo/vlmtest/benchmark_shuffle_new' |
|
os.makedirs(output_dir, exist_ok=True) |
|
|
|
global_answer_count = defaultdict(int) |
|
|
|
def get_best_shuffle(item, global_answer_count): |
|
choices = [ |
|
("choice_a", "choice_a_chinese", "A"), |
|
("choice_b", "choice_b_chinese", "B"), |
|
("choice_c", "choice_c_chinese", "C"), |
|
("choice_d", "choice_d_chinese", "D") |
|
] |
|
|
|
|
|
best_shuffle = None |
|
min_variance = float('inf') |
|
for _ in range(10): |
|
temp_choices = copy.deepcopy(choices) |
|
random.shuffle(temp_choices) |
|
for i, (choice_key, _, original_answer) in enumerate(temp_choices): |
|
if item["answer"] == original_answer: |
|
simulated_answer = chr(ord('A') + i) |
|
break |
|
|
|
simulated_count = global_answer_count.copy() |
|
simulated_count[simulated_answer] += 1 |
|
|
|
values = list(simulated_count.values()) |
|
variance = max(values) - min(values) |
|
|
|
if variance < min_variance: |
|
min_variance = variance |
|
best_shuffle = temp_choices |
|
best_answer = simulated_answer |
|
|
|
return best_shuffle, best_answer |
|
|
|
for json_file in os.listdir(json_dir): |
|
if not json_file.endswith('.json'): |
|
continue |
|
|
|
json_path = os.path.join(json_dir, json_file) |
|
with open(json_path, 'r', encoding='utf-8') as file: |
|
data = json.load(file) |
|
|
|
for item in data: |
|
best_choices, new_answer = get_best_shuffle(item, global_answer_count) |
|
|
|
new_data = {} |
|
for i, (choice_key, chinese_key, _) in enumerate(best_choices): |
|
new_choice_key = f"choice_{chr(ord('a') + i)}" |
|
new_chinese_key = f"{new_choice_key}_chinese" |
|
|
|
new_data[new_choice_key] = item[choice_key] |
|
new_data[new_chinese_key] = item[chinese_key] |
|
|
|
new_data["answer"] = new_answer |
|
global_answer_count[new_answer] += 1 |
|
item.update(new_data) |
|
|
|
output_path = os.path.join(output_dir, json_file) |
|
with open(output_path, 'w', encoding='utf-8') as file: |
|
json.dump(data, file, indent=4, ensure_ascii=False) |
|
|
|
print(f"{json_file} 答案分布: {dict(global_answer_count)}") |
|
|