File size: 1,499 Bytes
5ef5618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Unrehearsed observation-conditioned nonparametric action retrieval."""
from pathlib import Path
import numpy as np
import cv2
_mem=None
_phase=0.
def reset_episode():
 global _phase
 _phase=0.
def act(obs):
 global _mem,_phase
 if int(obs.get('step_id',1))>=500:return None
 if _mem is None:
  with np.load(Path(__file__).with_name('memory.npz')) as z:_mem={k:z[k] for k in z.files}
 try:
  q=np.asarray(obs['proprioception']['agent/qpos']).reshape(-1)
  v=np.asarray(obs['proprioception']['agent/qvel']).reshape(-1)
  fs=[]
  for cam in ['front_camera','left_camera','top_camera']:
   im=np.asarray(obs['rgb'][cam]);im=im[0] if im.ndim==4 else im
   fs.append(cv2.resize(im,(16,12),interpolation=cv2.INTER_AREA).reshape(-1)/255.)
  f=np.concatenate(fs)
  if q.shape!=(23,) or v.shape!=(23,) or not np.isfinite(np.r_[q,v,f]).all():return np.zeros(22,dtype=np.float32)
 except (KeyError,ValueError,cv2.error):return np.zeros(22,dtype=np.float32)
 # Local phase continuity limits ambiguous revisits; current RGB and joints choose every action.
 ix=np.flatnonzero((_mem['phase']>=max(0,_phase-.035))&(_mem['phase']<=_phase+.09))
 dist=np.mean(((_mem['q'][ix]-q)/.35)**2,axis=1)+.04*np.mean(((_mem['v'][ix]-v)/2)**2,axis=1)+12*np.mean((_mem['image'][ix]-f)**2,axis=1)
 j=ix[int(np.argmin(dist))]
 # No timed advancement and no demo/seed commitment. Stall remains possible.
 _phase=min(.998,max(_phase+1/440.,float(_mem['phase'][j])))
 a=_mem['a'][j].copy()
 return np.clip(a,-1,1).astype(np.float32)