ManiLadder_Models / astra /ClutterGrasp-v1 /build_reference.py
Linslab's picture
Move frozen Astra policies to astra/<task>/ (BuildTent-v1 .. DualArmFoldHandKerchief-v1)
592ea41 verified
Raw History Blame Contribute Delete
1.83 kB
"""Offline reference compilation from the 20 authorized demos only. No fitting."""
import importlib.util,json,sys
from pathlib import Path
import numpy as np
sys.path.insert(0,str(Path(__file__).resolve().parent))
from controller import image_features,CAMERAS
s=importlib.util.spec_from_file_location('worker_api','/worker_api.py');m=importlib.util.module_from_spec(s);s.loader.exec_module(m);api=m.API()
manifest=json.load(open('/demo_manifest.json'))
assert len(manifest['episodes'])==20
rows={k:[] for k in ['features','qpos','qvel','actions','demo','frame']};audit=[]
for di,e in enumerate(manifest['episodes']):
n=e['length'];fields=['actions','obs/agent/qpos','obs/agent/qvel'];d=api.read_demo(e['demo_id'],fields=fields)
indices=list(range(21,n,2));audit.append({'demo_id':e['demo_id'],'computational_rgb_frames':indices,'label_intervals':[[t,min(t+2,n)] for t in indices]})
for start in range(21,n,64):
stop=min(n,start+64)
imgs=api.read_demo(e['demo_id'],start,stop,fields=['obs/sensor_data/'+c+'/rgb' for c in CAMERAS])
for t in range(start,stop,2):
rows['features'].append(image_features({c:imgs['obs/sensor_data/'+c+'/rgb'][t-start] for c in CAMERAS}))
rows['qpos'].append(d['obs/agent/qpos'][t]);rows['qvel'].append(d['obs/agent/qvel'][t])
rows['actions'].append(d['actions'][t:min(t+2,n)].mean(0));rows['demo'].append(di);rows['frame'].append(t)
print(e['demo_id'],len(indices),flush=True)
b={k:np.asarray(v,dtype=np.int32 if k in ['demo','frame'] else np.float32) for k,v in rows.items()}
b['qscale']=np.maximum(np.std(b['qpos'],axis=0),.20);b['vscale']=np.maximum(np.std(b['qvel'],axis=0),.10)
np.savez_compressed(Path(__file__).with_name('reference_bank.npz'),**b)
json.dump(audit,open(Path(__file__).with_name('reference_index.json'),'w'),indent=2)
print('bank',len(b['actions']),flush=True)