gso-orbit-rgba / materialize_pairs.py
ysharma's picture
ysharma HF Staff
Clean rewrite of materializer
f36575b verified
Raw History Blame
2.04 kB
#!/usr/bin/env python
"""Materialize ai-toolkit edit-training folders from the pair list.
Input: pairs_train.jsonl (from ysharma/gso-orbit-rgba) + renders/ (mounted repo)
Output: <out>/targets/{pair_id}.png + <out>/targets/{pair_id}.txt (caption)
<out>/controls/{pair_id}.png (source view, SAME filename as target)
ai-toolkit edit training: dataset folder_path=targets, control_path=controls;
file names must match. Captions describe the TARGET view.
Usage:
python materialize_pairs.py --pairs pairs_train.jsonl --renders /ds/renders \
--out /train_data [--alpha-suffix] [--limit N]
--alpha-suffix appends the transparency sentence to every caption (the A/B
result decides whether it stays on).
"""
import argparse, json, os, shutil
ALPHA_SUFFIX = "The image has alpha channel and the background is transparent."
def main():
ap = argparse.ArgumentParser()
ap.add_argument('--pairs', required=True)
ap.add_argument('--renders', required=True)
ap.add_argument('--out', required=True)
ap.add_argument('--alpha-suffix', action='store_true')
ap.add_argument('--limit', type=int, default=0)
args = ap.parse_args()
pairs = [json.loads(l) for l in open(args.pairs)]
if args.limit:
pairs = pairs[:args.limit]
suffix = ' ' + ALPHA_SUFFIX if args.alpha_suffix else ''
tgt = os.path.join(args.out, 'targets')
ctl = os.path.join(args.out, 'controls')
os.makedirs(tgt, exist_ok=True)
os.makedirs(ctl, exist_ok=True)
n = 0
for p in pairs:
src_path = os.path.join(args.renders, p['source_file'])
tgt_path = os.path.join(args.renders, p['target_file'])
base = p['pair_id']
shutil.copyfile(tgt_path, os.path.join(tgt, base + '.png'))
shutil.copyfile(src_path, os.path.join(ctl, base + '.png'))
with open(os.path.join(tgt, base + '.txt'), 'w') as f:
f.write(p['instruction'] + suffix + '\n')
n += 1
print(f'materialized {n} pairs into {args.out}')
if __name__ == '__main__':
main()