Download dataset/make_jsonl.py from slz1/wxy-ControlAR: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/datasets/slz1/wxy-ControlAR/resolve/main/dataset/make_jsonl.py
- Command line
-
hf download hf://datasets/slz1/wxy-ControlAR/dataset/make_jsonl.py
-
curl -L -o make_jsonl.py https://huggingface.co/datasets/slz1/wxy-ControlAR/resolve/main/dataset/make_jsonl.py
2.84 kB
| # import os | |
| # import json | |
| # image_dir = "/media2/user/data/wxy/ControlAR_old/data/Captioned_ADE20K/train/image" | |
| # jsonl_path = "/media2/user/data/wxy/ControlAR_old/data/Captioned_ADE20K/train/train.jsonl" | |
| # with open(jsonl_path, "w") as f: | |
| # for filename in sorted(os.listdir(image_dir)): | |
| # if filename.endswith(".png"): | |
| # img_path = os.path.join(image_dir, filename) | |
| # entry = { | |
| # "image_path": img_path | |
| # } | |
| # f.write(json.dumps(entry) + "\n") | |
| 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)) # 通常为 'train' | |
| 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' | |
| ) | |