temp / take_image.py
Kaiyue's picture
Upload folder using huggingface_hub
56b30d7 verified
import os
import csv
import shutil
prompt_file = '/group/xihuiliu/sky/reasoning/idiom/benchmark/idiom_200.txt'
rewritten_prompt_file = '/group/xihuiliu/sky/reasoning/idiom/benchmark/idiom_200_rewrite.txt'
output_csv = "/group/xihuiliu/sky/reasoning/human_eval/idiom.csv"
dest_folder = "/group/xihuiliu/sky/reasoning/human_eval/images_eval/idiom"
os.makedirs(dest_folder, exist_ok=True)
folders = [
"/group/xihuiliu/sky/reasoning/images/idiom/bagel",
"/group/xihuiliu/sky/reasoning/images/idiom/GPT",
"/group/xihuiliu/sky/reasoning/images/idiom/hidream",
"/group/xihuiliu/sky/reasoning/images/idiom/janus_pro_7B",
"/group/xihuiliu/sky/reasoning/images/idiom/sd30_medium",
]
# Indices of images to select from each folder
indices = [1,11,21,31,41,51,61,71,81,91,101,111,121,131,141,151,161,171,181,191]
# Read prompts
with open(prompt_file, 'r', encoding='utf-8') as f:
prompts = [line.strip() for line in f.readlines()]
with open(rewritten_prompt_file, 'r', encoding='utf-8') as f:
rewritten_prompts = [line.strip() for line in f.readlines()]
# Gather all image paths and names
image_entries = []
prompt = []
rewritten_prompt = []
for idx in indices:
for folder in folders:
found = False
for ext in ['.png']:
img_name = f"{idx:04d}{ext}"
img_path = os.path.join(folder, img_name)
if os.path.exists(img_path):
model_name = os.path.basename(folder) # Get the folder name as model name
image_entries.append((model_name[:3], img_name))
new_img_name = f"{idx:04d}_{model_name}{ext}" # Ensure the new image name is in the format '0001.png'
dest_path = os.path.join(dest_folder, new_img_name)
shutil.copy2(img_path, dest_path)
prompt.append(prompts[idx - 1]) # idx is 1-based, list is 0-based
rewritten_prompt.append(rewritten_prompts[idx - 1]) # idx is 1-based, list is 0-based
found = True
break
if not found:
print(f"Warning: Image {img_name} not found in {folder}")
# Check that the number of images matches the number of prompts
if not (len(image_entries) == len(prompt) == len(rewritten_prompt)):
print()
raise ValueError("Number of images and lines in prompt files do not match!")
# Prepare CSV
header = [
'model_name', 'image_name', 'prompt', 'rewritten_prompt',
'correctness_1', 'correctness_2', 'correctness_3',
'aesthetic_1', 'aesthetic_2', 'aesthetic_3'
]
with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(header)
for idx, (img_path, img_name) in enumerate(image_entries):
row = [
img_path, # model_name (image path as per your instruction)
img_name, # image_name
prompt[idx], # prompt
rewritten_prompt[idx], # rewritten_prompt
'', '', '', '', '', '' # blank columns
]
writer.writerow(row)
print(f"CSV file created as {output_csv}.")