|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
|
import json |
|
|
from PIL import Image |
|
|
from tqdm import tqdm |
|
|
|
|
|
def generate_jsonl_from_folder( |
|
|
image_dir, |
|
|
output_jsonl_path, |
|
|
code_dir_name='code', |
|
|
caption_emb_dir_name='caption_emb', |
|
|
control_dir_name='control', |
|
|
label_dir_name='label' |
|
|
): |
|
|
""" |
|
|
自动从 image 文件夹构建 .jsonl 文件,记录 image_path 和 code_name |
|
|
适配 Text2ImgDataset 类的数据读取格式 |
|
|
""" |
|
|
image_files = sorted([f for f in os.listdir(image_dir) if f.endswith('.png')]) |
|
|
parent_dir = os.path.basename(os.path.normpath(image_dir)) |
|
|
|
|
|
with open(output_jsonl_path, 'w') as f: |
|
|
for file in tqdm(image_files, desc="Generating .jsonl"): |
|
|
image_path = os.path.join(image_dir, file) |
|
|
code_name = os.path.splitext(file)[0] |
|
|
|
|
|
|
|
|
code_path = os.path.join(os.path.dirname(image_dir), code_dir_name, f"{code_name}.npy") |
|
|
caption_emb_path = os.path.join(os.path.dirname(image_dir), caption_emb_dir_name, f"{code_name}.npz") |
|
|
control_path = os.path.join(os.path.dirname(image_dir), control_dir_name, f"{code_name}.png") |
|
|
label_path = os.path.join(os.path.dirname(image_dir), label_dir_name, f"{code_name}.png") |
|
|
|
|
|
if not (os.path.exists(code_path) and os.path.exists(caption_emb_path) |
|
|
and os.path.exists(control_path) and os.path.exists(label_path)): |
|
|
print(f"⚠️ 缺失对应文件: {code_name}") |
|
|
continue |
|
|
|
|
|
data = { |
|
|
"image_path": image_path, |
|
|
"code_name": int(code_name) |
|
|
} |
|
|
f.write(json.dumps(data) + '\n') |
|
|
|
|
|
print(f"✅ 成功生成: {output_jsonl_path}") |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
root_dir = '/media2/user/data/wxy/ControlAR_old/data/Captioned_ADE20K/train' |
|
|
image_dir = os.path.join(root_dir, 'image') |
|
|
output_jsonl = os.path.join(root_dir, 'train_to_use.jsonl') |
|
|
|
|
|
generate_jsonl_from_folder( |
|
|
image_dir=image_dir, |
|
|
output_jsonl_path=output_jsonl, |
|
|
code_dir_name='code', |
|
|
caption_emb_dir_name='caption_emb', |
|
|
control_dir_name='control', |
|
|
label_dir_name='label' |
|
|
) |
|
|
|