code-HFD / benchmark /select_cohort.py
Tinghong-Ye22's picture
Add files using upload-large-folder tool
fb0074a verified
Raw History Blame Contribute Delete
5.3 kB
"""GT-only stratification. No generated motion or model score enters selection."""
from collections import Counter, defaultdict
from concurrent.futures import ThreadPoolExecutor
import hashlib
import random
import re
import numpy as np
CATEGORIES = {
'locomotion': r'\b(walk|walking|run|running|jog|jogging|step|stepping|march|marching)\b',
'jump': r'\b(jump\w*|hop\w*|leap\w*|skip\w*)\b',
'dance': r'\b(danc\w*|ballet|disco|salsa|waltz|tango|moonwalk\w*)\b',
'sport_combat': r'\b(kick\w*|punch\w*|box\w*|fight\w*|throw\w*|shoot\w*|swim\w*|golf\w*|tennis|basketball)\b',
'object_interaction': r'\b(grab\w*|grasp\w*|pick\w*|lift\w*|carry\w*|carrying|push\w*|pull\w*|open\w*|clos\w*|reach\w*|catch\w*|put\w*)\b',
'communication': r'\b(wav\w*|greet\w*|point\w*|clap\w*|salut\w*|nod\w*|gestur\w*|beckon\w*|signal\w*)\b',
'posture_balance': r'\b(bend\w*|bow\w*|squat\w*|stretch\w*|balanc\w*|lean\w*|rotat\w*|turn\w*|twist\w*)\b',
'self_contact': r'\b(scratch\w*|rub\w*|touch\w*|itch\w*|wipe\w*)\b',
'expressive': r'\b(angry|happy|sad|afraid|scared|celebrat\w*|cheer\w*|laugh\w*|cry\w*|flex\w*)\b',
'standing': r'\b(stand\w*|idle|listen\w*|wait\w*)\b',
}
def category(text):
labels = [name for name, pattern in CATEGORIES.items() if re.search(pattern, text.lower())]
return labels or ['other']
def motion_features(raw, record):
try:
x = raw._load_motion(record['motion_dir'])
if len(x) < 50 or not np.isfinite(x).all():
return None, 'short_or_nonfinite'
norms = np.linalg.norm(x[:, 32:36], axis=1)
if not np.allclose(norms, 1., atol=.005):
return None, 'invalid_quaternion'
delta = np.diff(x[:, 29:31], axis=0)
travel = float(np.linalg.norm(delta, axis=1).sum())
duration = len(x)/50
# No rejection for large motion, sliding or difficult semantics.
return dict(duration=duration, displacement=float(np.linalg.norm(x[-1,29:31]-x[0,29:31])),
path_length=travel, joint_speed_rms=float(np.sqrt(np.mean(np.diff(x[:,:29],axis=0)**2))*50),
trajectory_sha256=hashlib.sha256(np.ascontiguousarray(x).tobytes()).hexdigest(),
categories=category(record['instruction']),
duration_bin=int(np.searchsorted([5,10,20,40],duration)),
travel_bin=int(np.searchsorted([.3,1.5],travel))), None
except (OSError, ValueError) as e:
return None, type(e).__name__+': '+str(e)
def balanced_select(candidates, count, seed):
"""Round-robin semantic/duration/travel cells; minimize repeated captions."""
if len(candidates)<count: raise ValueError(f'Only {len(candidates)} unique valid motions')
rng=random.Random(seed)
frequency=Counter(label for r in candidates for label in r['features']['categories'])
cells=defaultdict(list)
for record in candidates:
f=record['features']
f['primary_category']=min(f['categories'],key=lambda k:(frequency[k],k))
cells[(f['primary_category'],f['duration_bin'],f['travel_bin'])].append(record)
for group in cells.values(): rng.shuffle(group)
keys=sorted(cells);rng.shuffle(keys)
selected=[];captions=Counter();caption_limit=1
while len(selected)<count:
before=len(selected)
for key in keys:
group=cells[key]
if not group: continue
# Prefer globally unseen text without silently discarding valid variants.
index=min(range(len(group)),key=lambda i:captions[group[i]['caption_key']])
if captions[group[index]['caption_key']]>=caption_limit:continue
row=group.pop(index);selected.append(row);captions[row['caption_key']]+=1
if len(selected)==count: break
if len(selected)==before:caption_limit+=1
rng.shuffle(selected) # Do not let stratum order define retrieval batches.
return selected
def inventory(raw, seed=42, workers=16):
records={r['filename']:r for r in raw.records}
eligible=[];rejected=[]
for name,r in sorted(records.items()):
voices=sorted(v for v in raw._session_voice_names_for_mode(r,'turn-taking') if v.endswith('_en'))
if not voices:
rejected.append(dict(filename=name,reason='no_approved_English_voice'));continue
record=dict(r)
record.update(_session_voice_name=voices[int(record['row_id'])%len(voices)],
_session_voice_variant=0,_session_interaction_mode='turn-taking',
_session_pool_index=int(record['row_id']))
eligible.append(record)
valid=[];hashes=set()
with ThreadPoolExecutor(max_workers=workers) as pool:
for i,(record,(features,error)) in enumerate(zip(eligible,pool.map(lambda r:motion_features(raw,r),eligible))):
if error:
rejected.append(dict(filename=record['filename'],reason=error));continue
if features['trajectory_sha256'] in hashes:
rejected.append(dict(filename=record['filename'],reason='identical_raw_trajectory'));continue
hashes.add(features['trajectory_sha256'])
valid.append(dict(record=record,features=features,caption_key=' '.join(record['instruction'].lower().split())))
if i%500==0:print('INVENTORY',i+1,len(eligible),flush=True)
return valid,rejected