File size: 3,840 Bytes
12ecf8e
 
 
 
 
 
 
482faf4
 
 
 
12ecf8e
482faf4
12ecf8e
 
 
34b649d
 
 
482faf4
 
 
34b649d
 
482faf4
34b649d
 
482faf4
34b649d
 
12ecf8e
 
 
 
 
 
 
 
 
482faf4
 
 
 
 
 
 
 
12ecf8e
 
 
 
 
482faf4
12ecf8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
482faf4
 
 
 
 
 
12ecf8e
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
"""Final comparison: chosen checkpoint (steps2000) on the full 160-edit
held-out set at 40 steps, run twice: with the transparency suffix (deployed
usage, matches training captions) and without (apples-to-apples with the
baselines, which ran bare instructions). Reuses eval_edit.py's pipeline,
metrics and seeding so numbers are directly comparable to the baseline run.

Self-contained for job bootstrap: fetches its sibling eval_edit.py from the
dataset repo, resolves the LoRA checkpoint from the model repo via
hf_hub_download, and uploads results to the dataset repo at the end.

Usage:
  python final_eval.py --lora checkpoints/steps2000res768/orbit_alpha_lora/orbit_alpha_lora.safetensors --out /out/final
"""
import argparse, json, os, sys

_here = os.path.dirname(os.path.abspath(__file__))
if _here not in sys.path:
    sys.path.insert(0, _here)

# The job bootstrap only runs this file; pull eval_edit.py (pipeline/metrics/
# seeding shared with the baseline run) from the dataset repo.
if not os.path.exists(os.path.join(_here, 'eval_edit.py')):
    from huggingface_hub import hf_hub_download
    import shutil
    p = hf_hub_download(repo_id='ysharma/gso-orbit-rgba', repo_type='dataset',
                        filename='eval_edit.py')
    shutil.copy(p, os.path.join(_here, 'eval_edit.py'))
    print(f'fetched eval_edit.py -> {os.path.join(_here, "eval_edit.py")}', flush=True)

import eval_edit as E


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('--lora', required=True)
    ap.add_argument('--out', required=True)
    args = ap.parse_args()

    # LoRA: local path if it exists, else a file path inside ysharma/orbit-alpha-lora.
    lora_path = args.lora
    if not os.path.exists(lora_path):
        from huggingface_hub import hf_hub_download
        lora_path = hf_hub_download(repo_id='ysharma/orbit-alpha-lora',
                                    filename=args.lora)
    print(f'using lora: {lora_path}', flush=True)

    pairs = [json.loads(l) for l in open(E.get_file('pairs_eval.jsonl'))]
    assert len(pairs) == 160, f'expected 160 eval pairs, got {len(pairs)}'

    pipe = E.load_pipe()
    m = E.make_metrics()
    pipe.load_lora_weights(lora_path)

    for variant, suffix in (('no_suffix', ''), ('suffix', ' ' + E.ALPHA_SUFFIX)):
        pv = [dict(p, instruction=p['instruction'] + suffix) for p in pairs]
        r = E.run_pairs(pipe, m, pv, 'instruction', os.path.join(args.out, variant))
        E.agg(r, f'FINAL/{variant}')

    # side-by-side contact sheet: pred-with-suffix vs GT for 12 objects
    try:
        from PIL import Image
        sheet_pairs = pairs[::13][:12]
        tiles = []
        for p in sheet_pairs:
            pred = Image.open(os.path.join(args.out, 'suffix', p['pair_id'] + '.png'))
            gt = Image.open(E.get_file(p['target_file'])).convert('RGBA')
            for im in (pred, gt):
                bg = Image.new('RGB', im.size, (128, 128, 128))
                bg.paste(im, mask=im.split()[3])
                tiles.append(bg.resize((256, 256)))
        sheet = Image.new('RGB', (256 * 4, 256 * 6), (30, 30, 30))
        for i, t in enumerate(tiles):
            sheet.paste(t, ((i % 4) * 256, (i // 4) * 256))
        sheet.save(os.path.join(args.out, 'final_sheet.png'))
        print('sheet written', flush=True)
    except Exception as e:
        print(f'sheet failed (non-fatal): {e}', flush=True)

    # results survive the container only if pushed from inside the job
    from huggingface_hub import HfApi
    HfApi().upload_folder(folder_path=args.out, repo_id='ysharma/gso-orbit-rgba',
                          repo_type='dataset', path_in_repo='eval/final')
    print('uploaded results -> ysharma/gso-orbit-rgba/eval/final', flush=True)

    print('DONE', flush=True)


if __name__ == '__main__':
    main()