File size: 2,037 Bytes
d466b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f36575b
 
d466b4a
f36575b
 
d466b4a
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/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()