yuuu94's picture
Publish unpacked matching source code for deployment
90a248e verified
Raw History Blame Contribute Delete
3.93 kB
"""Episode-safe dataset boundary; consumes a separately verified perception cache.
Each cache frame carries four synchronized RGB paths, third-person objects/masks,
and its causal state provenance. Refuse unverified caches for formal training.
"""
import json, hashlib
from pathlib import Path
import numpy as np
from PIL import Image
from .adapter import prepare_example,normalize,action_chunk,CAMERAS
class EpisodeDataset:
def __init__(self, root, split='train', *, allow_provisional=False):
self.root=Path(root)
self.manifest=json.loads((self.root/'manifest.json').read_text())
if self.manifest.get('verified') is not True and not (allow_provisional and self.manifest.get('scope')=='diagnostic_causal_cache'):
raise ValueError('Formal training requires verified causal perception cache')
if self.manifest.get('depth_source')!='da3_estimated':raise ValueError('Wrong depth source')
if self.manifest.get('graph_camera')!='cam_third_person':raise ValueError('Wrong graph camera')
self.mask_repairs={}
repair_dir=Path(__file__).resolve().parent/'visual_mask_repair_20261105'
for row in json.loads((repair_dir/'manifest.json').read_text()):
path=repair_dir/row['file']
if hashlib.sha256(path.read_bytes()).hexdigest()!=row['sha256']:raise ValueError('Original mask repair hash mismatch')
mask=np.load(path)['mask']
if mask.dtype!=np.bool_ or list(mask.shape)!=row['original_shape'] or int(mask.sum())!=row['original_pixels']:raise ValueError('Original mask repair content mismatch')
if row['alignment']!='full_frame_resize_no_crop':raise ValueError('Unverified mask alignment')
self.mask_repairs[(int(row['episode']),int(row['frame']),str(row['id']))]=(row,mask)
self.stats=json.loads((self.root/'normalization.json').read_text())
self.episodes=[e for e in self.manifest['episodes'] if e['split']==split]
def episode(self, index, phase=0):
"""Yield consecutive graph updates. Caller owns memory and episode reset."""
if phase not in self.manifest.get('phases',list(range(8))):raise ValueError('Requested phase not cached')
ep=self.episodes[index];records=ep['frames']
actions=np.load(self.root/ep['actions']);states=np.load(self.root/ep['states'])
assert len(actions)==len(states)==len(records)
for t in range(phase,len(records),8):
row=records[t]
if row['frame_index']!=t or row['phase']!=phase:raise ValueError('Wrong causal phase')
images={c:Image.open(self.root/row['images'][c]).convert('RGB') for c in CAMERAS}
objects=json.loads((self.root/row['objects']).read_text())
masks=np.load(self.root/row['masks'])['masks']
if len(masks)!=len(objects):raise ValueError('Mask-instance mismatch')
for o,m in zip(objects,masks):
o['visual_mask']=m
repair=self.mask_repairs.get((int(ep['id']),t,str(o['id'])))
if repair is not None:
proof,original=repair
if hashlib.sha256((self.root/row['objects']).read_bytes()).hexdigest()!=proof['source_objects_sha256']:raise ValueError('Repair applied to different source objects')
if m.any() or not o['visible_now'] or o['geometry_source']!='fresh' or list(m.shape)!=proof['target_shape']:raise ValueError('Unexpected mask repair state')
o['visual_mask_original']=original
o['visual_mask_original_alignment']=proof['alignment']
yield prepare_example(images,ep['instruction'],objects,
normalize(states[t],self.stats['observation.state']),episode=ep['id'],step=t,
prompt_ids=row['prompt_ids'],K=row['K'],phase=phase,
action=normalize(action_chunk(actions,t),self.stats['action']))