T2I_Data / EvalMuse /read.py
asdadaac's picture
Sync folder via upload_folder API
ba4ece0 verified
Raw
History Blame Contribute Delete
3.96 kB
import pyarrow.parquet as pq
from pathlib import Path
import json
from typing import List, Dict, Any, Optional
def write_json(file_path: str, data: Any):
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
data_dir = Path("/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/EvalMuse/images")
# out_dir = Path("/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/T2I-CoReBench-main/Bench/GEditBench-v2/Images")
# out_dir.mkdir(parents=True, exist_ok=True)
parquet_files = sorted(data_dir.glob("*.parquet"))
total_saved = 0
total_skipped = 0
save_data = []
idx = 1
save_data = []
for parquet_path in parquet_files:
print(f"\nreading {parquet_path}")
pf = pq.ParquetFile(parquet_path)
for rg in range(pf.num_row_groups):
table = pf.read_row_group(rg).combine_chunks()
rows = table.to_pylist()
for i, row in enumerate(rows):
prompt = row['prompt']
image1_score = row['weighted_results_image1_preference']
image2_score = row['weighted_results_image2_preference']
gpt_value = ""
if image1_score > image2_score:
gpt_value = "Image 1 is better than Image 2."
image1_path = f'images/{idx}_chosen.png'
image2_path = f"images/{idx}_rejected.png"
elif image2_score > image1_score:
gpt_value = "Image 2 is better than Image 1."
image2_path = f'images/{idx}_chosen.png'
image1_path = f"images/{idx}_rejected.png"
print(row['image1'].keys())
image1_byte = row['image1']['bytes']
import os
with open(os.path.join("/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/OpenAI-4o_t2i_human_preference",image1_path), "wb") as f:
f.write(image1_byte)
image2_byte = row['image2']['bytes']
with open(os.path.join("/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/OpenAI-4o_t2i_human_preference",image2_path), "wb") as f:
f.write(image2_byte)
template = {
"id": f"{idx}",
"prompt": f"{prompt}",
"conversations": [
{
"from": "human",
"value": "<image>\n <image>\nYou are given a text caption and two generated images based on that caption. Your task is to evaluate and compare these images based on two key criteria:\n1. Alignment with the Caption: Assess how well each image aligns with the provided caption. Consider the accuracy of depicted objects, their relationships, and attributes as described in the caption.\n2. Overall Image Quality: Examine the visual quality of each image, including clarity, detail preservation, color accuracy, and overall aesthetic appeal.\nCompare both images using the above criteria and select the one that better aligns with the caption while exhibiting superior visual quality.\nProvide a clear conclusion such as \"Image 1 is better than Image 2.\", \"Image 2 is better than Image 1.\" and \"Both images are equally good.\".\nYour task is provided as follows:\nText Caption: [A harmoniously crafted glass sculpture, cinematically capturing the interplay of fire and ice, with dramatic lighting and deep, rich colors.]"
},
{
"from": "gpt",
"value": gpt_value
}
],
"images": [
image1_path,
image2_path
]
}
idx += 1
save_data.append(template)
write_json("/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/OpenAI-4o_t2i_human_preference/train_data.json", save_data)