File size: 6,287 Bytes
fb75f2f | 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | #!/usr/bin/env python
"""Build edit pairs for the orbit-alpha LoRA from metadata.jsonl + split.json of
ysharma/gso-orbit-rgba.
Grammar (relative azimuth, absolute elevation; 23 instructions):
azimuth moves x elevations (21): "<orbit> rotate the camera {45|90|135} degrees
to the {left|right}, {low angle|eye level|elevated}"
180 (3): "<orbit> rotate the camera 180 degrees, {low angle|eye level|elevated}"
elevation-only (2): "<orbit> keep the camera angle, {low angle|elevated}"
Convention: "to the right" means the camera moves clockwise seen from above,
i.e. target azimuth = source azimuth - move (mod 360); "left" is +move.
Train: 4 pairs per non-rejected train object, instructions drawn round-robin from
a shuffled 23-list (balanced). Source is always eye level at a random azimuth.
Eval: 4 pairs per held-out object with fixed buckets {45, 90, 180, elevation-only}
so results can be broken down by rotation size and elevation change.
Outputs: pairs_train.jsonl, pairs_eval.jsonl
"""
import json, os, random
from collections import Counter, defaultdict
REPO = 'ysharma/gso-orbit-rgba'
META = os.environ.get('META', 'metadata.jsonl')
SPLIT = os.environ.get('SPLIT', 'split.json')
ELEV_NAME = {'low': 'low angle', 'eye': 'eye level', 'high': 'elevated'}
AZS = list(range(0, 360, 45))
SEED = 20260923
def instruction(move_deg, side, elev_key):
if move_deg == 0: # elevation-only
assert elev_key in ('low', 'high')
return f"<orbit> keep the camera angle, {ELEV_NAME[elev_key]}"
if move_deg == 180:
return f"<orbit> rotate the camera 180 degrees, {ELEV_NAME[elev_key]}"
return (f"<orbit> rotate the camera {move_deg} degrees to the {side}, "
f"{ELEV_NAME[elev_key]}")
def plain_instruction(move_deg, side, elev_key):
e = {'low': 'low-angle', 'eye': 'eye-level', 'high': 'raised'}[elev_key]
if move_deg == 0:
return f"Show the same object from a {e} viewpoint."
if move_deg == 180:
return f"Show the same object with the camera rotated 180 degrees, from a {e} viewpoint."
return (f"Show the same object with the camera rotated {move_deg} degrees "
f"to the {side}, from a {e} viewpoint.")
def target_azimuth(src_az, move_deg, side):
sign = -1 if side == 'right' else 1 # right = camera clockwise = -az
if move_deg == 180:
return (src_az + 180) % 360
return (src_az + sign * move_deg) % 360
def make_pair(object_id, move_deg, side, elev_key, src_az, idx):
tgt_az = src_az if move_deg == 0 else target_azimuth(src_az, move_deg, side)
src_el = 'eye'
# elevation-only pairs: target elevation differs from eye; azimuth stays
assert src_az in AZS
return {
'object_id': object_id,
'instruction': instruction(move_deg, side, elev_key),
'instruction_plain': plain_instruction(move_deg, side, elev_key),
'move_deg': move_deg, 'side': side, 'elevation': elev_key,
'src_azimuth': src_az, 'tgt_azimuth': tgt_az,
'source_file': f'renders/{object_id}/az{src_az:03d}_el_eye.png',
'target_file': f'renders/{object_id}/az{tgt_az:03d}_el_{elev_key}.png',
'pair_id': f'{object_id}_{idx}',
}
def main():
split = json.load(open(SPLIT))
meta = [json.loads(l) for l in open(META)]
have = {(r['object_id'], r['azimuth_deg'], r['elevation_name']) for r in meta}
rng = random.Random(SEED)
train_objs = sorted(split['train_objects'])
heldout = sorted(split['heldout_40'])
# ---- train pairs: 4/object, balanced over the 23 instructions ----
instr_cycle = []
for d in (45, 90, 135):
for s in ('left', 'right'):
for e in ('low', 'eye', 'high'):
instr_cycle.append((d, s, e))
instr_cycle.append((180, '', 'low'))
instr_cycle.append((180, '', 'eye'))
instr_cycle.append((180, '', 'high'))
instr_cycle.append((0, '', 'low'))
instr_cycle.append((0, '', 'high'))
assert len(instr_cycle) == 23
rng.shuffle(instr_cycle)
qi = 0
train_pairs = []
for obj in train_objs:
src_az = rng.choice(AZS)
for k in range(4):
d, s, e = instr_cycle[qi % 23]; qi += 1
p = make_pair(obj, d, s, e, src_az, k)
if (p['object_id'], p['src_azimuth'], 'eye') not in have or \
(p['object_id'], p['tgt_azimuth'], p['elevation']) not in have:
continue
train_pairs.append(p)
print(f'train pairs: {len(train_pairs)} from {len(train_objs)} objects')
print(' instruction balance (min/max per instruction):',
min(Counter(p['instruction'] for p in train_pairs).values()),
max(Counter(p['instruction'] for p in train_pairs).values()))
# ---- eval pairs: 40 x 4, fixed buckets {45, 90, 180, elev-only} ----
eval_pairs = []
for obj in heldout:
src_az = rng.choice(AZS)
buckets = [(45, None, None), (90, None, None), (180, None, None), (0, None, None)]
for k, (d, _, _) in enumerate(buckets):
s = rng.choice(['left', 'right']) if d not in (0, 180) else ''
e = rng.choice(['low', 'eye', 'high']) if d not in (0,) else \
rng.choice(['low', 'high'])
p = make_pair(obj, d, s, e, src_az, k)
assert (p['object_id'], p['src_azimuth'], 'eye') in have, p
assert (p['object_id'], p['tgt_azimuth'], p['elevation']) in have, p
eval_pairs.append(p)
print(f'eval pairs: {len(eval_pairs)} from {len(heldout)} heldout objects')
for name, pairs in (('pairs_train.jsonl', train_pairs), ('pairs_eval.jsonl', eval_pairs)):
with open(name, 'w') as f:
for p in pairs:
f.write(json.dumps(p) + '\n')
n = len(train_pairs)
print(f'expected ~3900 train pairs; got {n}')
from huggingface_hub import HfApi
api = HfApi(token=os.environ.get('HF_TOKEN'))
api.upload_file(repo_id=REPO, repo_type='dataset', path_in_repo='pairs_train.jsonl',
path_or_fileobj='pairs_train.jsonl')
api.upload_file(repo_id=REPO, repo_type='dataset', path_in_repo='pairs_eval.jsonl',
path_or_fileobj='pairs_eval.jsonl')
print('uploaded pair lists')
if __name__ == '__main__':
main() |