Datasets:
File size: 5,760 Bytes
d86546a 25e06a1 d86546a 4339992 d86546a 4339992 d86546a | 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 | """Independently audit the completed paired dataset; refresh its file inventory."""
import hashlib
import importlib.util
import io
import json
import os
from pathlib import Path
import shutil
import sys
sys.dont_write_bytecode=True
for variable in ('OMP_NUM_THREADS','OPENBLAS_NUM_THREADS','MKL_NUM_THREADS'):
os.environ[variable]='1'
import numpy as np
from PIL import Image
import pyarrow as pa
import pyarrow.parquet as pq
from datasets import load_dataset
pa.set_cpu_count(2)
pa.set_io_thread_count(2)
ROOT=Path(sys.argv[1])
REPO_ID=sys.argv[2] if len(sys.argv)>2 else 'TipDex-VTLA/TipDex-Data'
CAMERAS=tuple('observation.images.'+x for x in ('top','left_wrist','right_wrist'))
def sha(path):
with path.open('rb') as handle:
return hashlib.file_digest(handle,'sha256').hexdigest()
def jsonl(path):
return [json.loads(x) for x in path.read_text().splitlines()]
def rgb(value):
with Image.open(io.BytesIO(value['bytes'])) as image:
image.load()
assert image.mode=='RGB' and image.size==(224,224)
return np.array(image)
assert json.loads((ROOT/'provenance/validation.json').read_text())['status']=='passed'
old_inventory=json.loads((ROOT/'provenance/sha256_manifest.json').read_text())['files']
assert all((ROOT/f['path']).stat().st_size==f['bytes'] and sha(ROOT/f['path'])==f['sha256'] for f in old_inventory)
spec=importlib.util.spec_from_file_location('paired_lighting',ROOT/'code/lighting.py')
lighting=importlib.util.module_from_spec(spec)
spec.loader.exec_module(lighting)
pairs=jsonl(ROOT/'provenance/pairs.jsonl')
episodes=jsonl(ROOT/'meta/episodes.jsonl')
sources=jsonl(ROOT/'meta/episode_sources.jsonl')
stats=jsonl(ROOT/'meta/episodes_stats.jsonl')
assert len(episodes)==len(sources)==len(stats)==100
assert [r['episode_index'] for r in episodes]==list(range(100))
assert len({r['source_basename'] for r in pairs})==50
assert [r['clean_episode_id'] for r in pairs]==list(range(50))
assert [r['perturbed_episode_id'] for r in pairs]==list(range(50,100))
cursor=0
for episode in episodes:
ep,n=episode['episode_index'],episode['length']
table=pq.read_table(ROOT/f'data/chunk-000/episode_{ep:06d}.parquet',columns=['index','episode_index','frame_index','task_index'])
assert table.num_rows==n
assert table['index'].to_pylist()==list(range(cursor,cursor+n))
assert table['frame_index'].to_pylist()==list(range(n))
assert table['episode_index'].to_pylist()==[ep]*n
assert table['task_index'].to_pylist()==[0]*n
assert sources[ep]['global_start']==cursor and sources[ep]['length']==n
assert all(v['count']==[n] for v in stats[ep]['stats'].values())
cursor+=n
assert cursor==94172
samples=0;zero_ev_samples=0
for pair in pairs:
clean=pq.read_table(ROOT/pair['outputs']['clean']['path'],columns=list(CAMERAS)+['source_timestamp_ns'])
aug=pq.read_table(ROOT/pair['outputs']['perturbed']['path'],columns=list(CAMERAS))
recipe=tuple(pair['recipe'][k] for k in ('category','tier','seed'))
for frame in [0,clean.num_rows//2,clean.num_rows-1]:
original={c:rgb(clean[c][frame].as_py()) for c in CAMERAS}
actual={c:rgb(aug[c][frame].as_py()) for c in CAMERAS}
seconds=(clean['source_timestamp_ns'][frame].as_py()-clean['source_timestamp_ns'][0].as_py())*1e-9
expected=lighting.apply_at_time(original,recipe,seconds)
evs=lighting.ev_maps(*recipe,seconds,(224,224))
for c,ev in zip(CAMERAS,evs):
assert np.array_equal(actual[c],expected[c])
if not ev.any():
assert np.array_equal(actual[c],original[c])
zero_ev_samples+=1
samples+=1
files=[str(ROOT/f'data/chunk-000/episode_{i:06d}.parquet') for i in (0,50)]
ds=load_dataset('parquet',data_files={'train':files},cache_dir=str(ROOT.parent/'.qa_cache'))['train']
loaded=[]
first_n=episodes[0]['length']
for position in [0,first_n-1,first_n,len(ds)-1]:
row=ds[position]
assert all(row[c].mode=='RGB' and row[c].size==(224,224) for c in CAMERAS)
assert len(row['action'])==13 and len(row['observation.state'])==19 and len(row['observation.tactile.right'])==364
loaded.append({'row':position,'episode_index':row['episode_index'],'frame_index':row['frame_index']})
report={'status':'passed','episode_count':100,'pair_count':50,'frames':94172,
'contiguous_global_indexes':[0,94171],'contiguous_episode_indexes':[0,99],
'original_image_decoding_samples':samples,'perturbed_image_transform_samples':samples,
'unchanged_zero_ev_view_samples':zero_ev_samples,'prior_file_sha256_verified':len(old_inventory),
'datasets_load_success':True,'datasets_sample_rows':loaded,
'datasets_image_features':{c:str(ds.features[c]) for c in CAMERAS}}
readme=(ROOT/'README.md').read_text()
readme=readme.replace('repo_id=REPO_ID',f'repo_id="{REPO_ID}"')
readme=readme.replace('# TipDex-Data\n',
'# TipDex-Data\n\n'+
f'Hugging Face repository: [{REPO_ID}](https://huggingface.co/datasets/{REPO_ID}).\n')
(ROOT/'README.md').write_text(readme)
if Path(__file__).resolve() != (ROOT/'code/independent_qa.py').resolve():
shutil.copy2(Path(__file__),ROOT/'code/independent_qa.py')
(ROOT/'provenance/independent_qa.json').write_text(json.dumps(report,indent=2)+'\n')
inventory=ROOT/'provenance/sha256_manifest.json'
files=[dict(path=str(p.relative_to(ROOT)),bytes=p.stat().st_size,sha256=sha(p))
for p in sorted(ROOT.rglob('*')) if p.is_file() and p!=inventory and '__pycache__' not in p.parts]
inventory.write_text(json.dumps({'algorithm':'sha256','excludes':['provenance/sha256_manifest.json'],'files':files},indent=2)+'\n')
print(json.dumps({'status':'READY','bytes':sum(f['bytes'] for f in files),'files':len(files)+1,'audit':report}),flush=True)
|