|
|
|
|
|
""" |
|
|
修复多图数据集,为第一个用户消息添加 <image> token |
|
|
""" |
|
|
|
|
|
import json |
|
|
import os |
|
|
from pathlib import Path |
|
|
|
|
|
def fix_multi_image_data(input_file, output_file): |
|
|
""" |
|
|
修复多图数据集,在第一个用户消息前添加相应数量的 <image> token |
|
|
|
|
|
Args: |
|
|
input_file: 原始数据文件路径 |
|
|
output_file: 输出文件路径 |
|
|
""" |
|
|
print(f"正在处理文件: {input_file}") |
|
|
|
|
|
|
|
|
with open(input_file, 'r', encoding='utf-8') as f: |
|
|
data = json.load(f) |
|
|
|
|
|
print(f"总样本数: {len(data)}") |
|
|
|
|
|
|
|
|
for i, sample in enumerate(data): |
|
|
|
|
|
num_images = len(sample['images']) |
|
|
|
|
|
|
|
|
image_tokens = '<image>' * num_images |
|
|
|
|
|
|
|
|
conversations = sample['conversations'] |
|
|
for j, conv in enumerate(conversations): |
|
|
if conv['from'] == 'human': |
|
|
|
|
|
original_value = conv['value'] |
|
|
conv['value'] = f"{image_tokens}\n{original_value}" |
|
|
break |
|
|
|
|
|
|
|
|
if (i + 1) % 1000 == 0: |
|
|
print(f"已处理: {i + 1}/{len(data)} 样本") |
|
|
|
|
|
|
|
|
print(f"保存到: {output_file}") |
|
|
with open(output_file, 'w', encoding='utf-8') as f: |
|
|
json.dump(data, f, indent=2, ensure_ascii=False) |
|
|
|
|
|
print("处理完成!") |
|
|
|
|
|
|
|
|
print("\n处理后的示例:") |
|
|
sample = data[0] |
|
|
print(f"图片数量: {len(sample['images'])}") |
|
|
print(f"第一个用户消息: {sample['conversations'][0]['value'][:100]}...") |
|
|
|
|
|
def main(): |
|
|
|
|
|
base_dir = Path(__file__).parent |
|
|
|
|
|
|
|
|
train_input = base_dir / "data_train_multi_llava_vasevl_v6.json" |
|
|
train_output = base_dir / "data_train_multi_llava_vasevl_v6_fixed.json" |
|
|
|
|
|
|
|
|
test_input = base_dir / "data_test_multi_llava_vasevl_v6.json" |
|
|
test_output = base_dir / "data_test_multi_llava_vasevl_v6_fixed.json" |
|
|
|
|
|
print("开始处理多图数据集...") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
if train_input.exists(): |
|
|
fix_multi_image_data(train_input, train_output) |
|
|
print() |
|
|
else: |
|
|
print(f"训练集文件不存在: {train_input}") |
|
|
|
|
|
|
|
|
if test_input.exists(): |
|
|
fix_multi_image_data(test_input, test_output) |
|
|
else: |
|
|
print(f"测试集文件不存在: {test_input}") |
|
|
|
|
|
print("=" * 50) |
|
|
print("所有文件处理完成!") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|