Add pair builder: 23-instruction orbit grammar, train + eval pair lists
Browse files- pair_builder.py +146 -0
pair_builder.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""Build edit pairs for the orbit-alpha LoRA from metadata.jsonl + split.json of
|
| 3 |
+
ysharma/gso-orbit-rgba.
|
| 4 |
+
|
| 5 |
+
Grammar (relative azimuth, absolute elevation; 23 instructions):
|
| 6 |
+
azimuth moves x elevations (21): "<orbit> rotate the camera {45|90|135} degrees
|
| 7 |
+
to the {left|right}, {low angle|eye level|elevated}"
|
| 8 |
+
180 (3): "<orbit> rotate the camera 180 degrees, {low angle|eye level|elevated}"
|
| 9 |
+
elevation-only (2): "<orbit> keep the camera angle, {low angle|elevated}"
|
| 10 |
+
|
| 11 |
+
Convention: "to the right" means the camera moves clockwise seen from above,
|
| 12 |
+
i.e. target azimuth = source azimuth - move (mod 360); "left" is +move.
|
| 13 |
+
|
| 14 |
+
Train: 4 pairs per non-rejected train object, instructions drawn round-robin from
|
| 15 |
+
a shuffled 23-list (balanced). Source is always eye level at a random azimuth.
|
| 16 |
+
Eval: 4 pairs per held-out object with fixed buckets {45, 90, 180, elevation-only}
|
| 17 |
+
so results can be broken down by rotation size and elevation change.
|
| 18 |
+
|
| 19 |
+
Outputs: pairs_train.jsonl, pairs_eval.jsonl
|
| 20 |
+
"""
|
| 21 |
+
import json, os, random
|
| 22 |
+
from collections import Counter, defaultdict
|
| 23 |
+
|
| 24 |
+
REPO = 'ysharma/gso-orbit-rgba'
|
| 25 |
+
META = os.environ.get('META', 'metadata.jsonl')
|
| 26 |
+
SPLIT = os.environ.get('SPLIT', 'split.json')
|
| 27 |
+
|
| 28 |
+
ELEV_NAME = {'low': 'low angle', 'eye': 'eye level', 'high': 'elevated'}
|
| 29 |
+
AZS = list(range(0, 360, 45))
|
| 30 |
+
SEED = 20260923
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def instruction(move_deg, side, elev_key):
|
| 34 |
+
if move_deg == 0: # elevation-only
|
| 35 |
+
assert elev_key in ('low', 'high')
|
| 36 |
+
return f"<orbit> keep the camera angle, {ELEV_NAME[elev_key]}"
|
| 37 |
+
if move_deg == 180:
|
| 38 |
+
return f"<orbit> rotate the camera 180 degrees, {ELEV_NAME[elev_key]}"
|
| 39 |
+
return (f"<orbit> rotate the camera {move_deg} degrees to the {side}, "
|
| 40 |
+
f"{ELEV_NAME[elev_key]}")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def plain_instruction(move_deg, side, elev_key):
|
| 44 |
+
e = {'low': 'low-angle', 'eye': 'eye-level', 'high': 'raised'}[elev_key]
|
| 45 |
+
if move_deg == 0:
|
| 46 |
+
return f"Show the same object from a {e} viewpoint."
|
| 47 |
+
if move_deg == 180:
|
| 48 |
+
return f"Show the same object with the camera rotated 180 degrees, from a {e} viewpoint."
|
| 49 |
+
return (f"Show the same object with the camera rotated {move_deg} degrees "
|
| 50 |
+
f"to the {side}, from a {e} viewpoint.")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def target_azimuth(src_az, move_deg, side):
|
| 54 |
+
sign = -1 if side == 'right' else 1 # right = camera clockwise = -az
|
| 55 |
+
if move_deg == 180:
|
| 56 |
+
return (src_az + 180) % 360
|
| 57 |
+
return (src_az + sign * move_deg) % 360
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def make_pair(object_id, move_deg, side, elev_key, src_az, idx):
|
| 61 |
+
tgt_az = src_az if move_deg == 0 else target_azimuth(src_az, move_deg, side)
|
| 62 |
+
src_el = 'eye'
|
| 63 |
+
# elevation-only pairs: target elevation differs from eye; azimuth stays
|
| 64 |
+
assert src_az in AZS
|
| 65 |
+
return {
|
| 66 |
+
'object_id': object_id,
|
| 67 |
+
'instruction': instruction(move_deg, side, elev_key),
|
| 68 |
+
'instruction_plain': plain_instruction(move_deg, side, elev_key),
|
| 69 |
+
'move_deg': move_deg, 'side': side, 'elevation': elev_key,
|
| 70 |
+
'src_azimuth': src_az, 'tgt_azimuth': tgt_az,
|
| 71 |
+
'source_file': f'renders/{object_id}/az{src_az:03d}_el_eye.png',
|
| 72 |
+
'target_file': f'renders/{object_id}/az{tgt_az:03d}_el_{elev_key}.png',
|
| 73 |
+
'pair_id': f'{object_id}_{idx}',
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def main():
|
| 78 |
+
split = json.load(open(SPLIT))
|
| 79 |
+
meta = [json.loads(l) for l in open(META)]
|
| 80 |
+
have = {(r['object_id'], r['azimuth_deg'], r['elevation_name']) for r in meta}
|
| 81 |
+
rng = random.Random(SEED)
|
| 82 |
+
train_objs = sorted(split['train_objects'])
|
| 83 |
+
heldout = sorted(split['heldout_40'])
|
| 84 |
+
|
| 85 |
+
# ---- train pairs: 4/object, balanced over the 23 instructions ----
|
| 86 |
+
instr_cycle = []
|
| 87 |
+
for d in (45, 90, 135):
|
| 88 |
+
for s in ('left', 'right'):
|
| 89 |
+
for e in ('low', 'eye', 'high'):
|
| 90 |
+
instr_cycle.append((d, s, e))
|
| 91 |
+
instr_cycle.append((180, '', 'low'))
|
| 92 |
+
instr_cycle.append((180, '', 'eye'))
|
| 93 |
+
instr_cycle.append((180, '', 'high'))
|
| 94 |
+
instr_cycle.append((0, '', 'low'))
|
| 95 |
+
instr_cycle.append((0, '', 'high'))
|
| 96 |
+
assert len(instr_cycle) == 23
|
| 97 |
+
rng.shuffle(instr_cycle)
|
| 98 |
+
qi = 0
|
| 99 |
+
train_pairs = []
|
| 100 |
+
for obj in train_objs:
|
| 101 |
+
src_az = rng.choice(AZS)
|
| 102 |
+
for k in range(4):
|
| 103 |
+
d, s, e = instr_cycle[qi % 23]; qi += 1
|
| 104 |
+
p = make_pair(obj, d, s, e, src_az, k)
|
| 105 |
+
if (p['object_id'], p['src_azimuth'], 'eye') not in have or \
|
| 106 |
+
(p['object_id'], p['tgt_azimuth'], p['elevation']) not in have:
|
| 107 |
+
continue
|
| 108 |
+
train_pairs.append(p)
|
| 109 |
+
print(f'train pairs: {len(train_pairs)} from {len(train_objs)} objects')
|
| 110 |
+
print(' instruction balance (min/max per instruction):',
|
| 111 |
+
min(Counter(p['instruction'] for p in train_pairs).values()),
|
| 112 |
+
max(Counter(p['instruction'] for p in train_pairs).values()))
|
| 113 |
+
|
| 114 |
+
# ---- eval pairs: 40 x 4, fixed buckets {45, 90, 180, elev-only} ----
|
| 115 |
+
eval_pairs = []
|
| 116 |
+
for obj in heldout:
|
| 117 |
+
src_az = rng.choice(AZS)
|
| 118 |
+
buckets = [(45, None, None), (90, None, None), (180, None, None), (0, None, None)]
|
| 119 |
+
for k, (d, _, _) in enumerate(buckets):
|
| 120 |
+
s = rng.choice(['left', 'right']) if d not in (0, 180) else ''
|
| 121 |
+
e = rng.choice(['low', 'eye', 'high']) if d not in (0,) else \
|
| 122 |
+
rng.choice(['low', 'high'])
|
| 123 |
+
p = make_pair(obj, d, s, e, src_az, k)
|
| 124 |
+
assert (p['object_id'], p['src_azimuth'], 'eye') in have, p
|
| 125 |
+
assert (p['object_id'], p['tgt_azimuth'], p['elevation']) in have, p
|
| 126 |
+
eval_pairs.append(p)
|
| 127 |
+
print(f'eval pairs: {len(eval_pairs)} from {len(heldout)} heldout objects')
|
| 128 |
+
|
| 129 |
+
for name, pairs in (('pairs_train.jsonl', train_pairs), ('pairs_eval.jsonl', eval_pairs)):
|
| 130 |
+
with open(name, 'w') as f:
|
| 131 |
+
for p in pairs:
|
| 132 |
+
f.write(json.dumps(p) + '\n')
|
| 133 |
+
n = len(train_pairs)
|
| 134 |
+
print(f'expected ~3900 train pairs; got {n}')
|
| 135 |
+
|
| 136 |
+
from huggingface_hub import HfApi
|
| 137 |
+
api = HfApi(token=os.environ.get('HF_TOKEN'))
|
| 138 |
+
api.upload_file(repo_id=REPO, repo_type='dataset', path_in_repo='pairs_train.jsonl',
|
| 139 |
+
path_or_fileobj='pairs_train.jsonl')
|
| 140 |
+
api.upload_file(repo_id=REPO, repo_type='dataset', path_in_repo='pairs_eval.jsonl',
|
| 141 |
+
path_or_fileobj='pairs_eval.jsonl')
|
| 142 |
+
print('uploaded pair lists')
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
if __name__ == '__main__':
|
| 146 |
+
main()
|