TrueMICL / clevr /dataprocess1.py
ShuoChen99's picture
Upload folder using huggingface_hub
1e9a83f verified
raw
history blame
1.56 kB
import json
import os
# 文件路径
json_dir = "/nfs/data3/zhen/Flamingo_jz/jz-icl/VL-ICL/clevr"
query_file = os.path.join(json_dir, "query.json")
support_file = os.path.join(json_dir, "support.json")
# 加载 JSON 数据
with open(query_file, 'r') as f:
query_data = json.load(f)
with open(support_file, 'r') as f:
support_data = json.load(f)
# 定义一个函数,将数据根据属性种类进行分类并保存为独立的 JSON 文件
def split_by_attribute(data, base_folder, data_type):
# 创建一个字典来存储不同属性的样本
attribute_dict = {}
for sample in data:
# 从 question 提取属性类型
attribute_type, _ = sample["question"].split(": ")
# 将样本按属性类型添加到相应的列表中
if attribute_type not in attribute_dict:
attribute_dict[attribute_type] = []
attribute_dict[attribute_type].append(sample)
# 将不同属性的样本分别保存为 JSON 文件
for attribute_type, samples in attribute_dict.items():
output_file = os.path.join(base_folder, f"{data_type}_{attribute_type}.json")
with open(output_file, 'w') as f:
json.dump(samples, f, indent=4)
print(f"{data_type} samples for attribute '{attribute_type}' saved to {output_file}")
# 对 query 和 support 数据分别进行分类并保存
split_by_attribute(query_data, json_dir, "query")
split_by_attribute(support_data, json_dir, "support")
print("文件分类并保存完成。")