Download checkpoints/step_002000/metadata/source/resolution_adapt/data.py from Ratstar216/LIT-ImageWAM-ResolutionAdapt-128to224: direct link, hf CLI and curl.
- Browser
- Download file 7.59 kB
-
https://huggingface.co/Ratstar216/LIT-ImageWAM-ResolutionAdapt-128to224/resolve/main/checkpoints/step_002000/metadata/source/resolution_adapt/data.py
- Command line
-
hf download hf://Ratstar216/LIT-ImageWAM-ResolutionAdapt-128to224/checkpoints/step_002000/metadata/source/resolution_adapt/data.py
-
curl -L -o data.py https://huggingface.co/Ratstar216/LIT-ImageWAM-ResolutionAdapt-128to224/resolve/main/checkpoints/step_002000/metadata/source/resolution_adapt/data.py
7.59 kB
| """Deterministic provenance sampling and shared current/future image transform.""" | |
| from __future__ import annotations | |
| import copy | |
| import hashlib | |
| import json | |
| import math | |
| import random | |
| from collections import Counter, defaultdict | |
| from pathlib import Path | |
| import torch | |
| import torch.nn.functional as F | |
| DATASET = 'Ratstar216/LIBERO_NVIDIA_PLUS_LeRobot_v3' | |
| DATASET_REVISION = 'c289c195d1fcb7eb6406b5c771a11465e81e8aef' | |
| POOLS = ('familiar', 'plus', 'libero90') | |
| WEIGHTS = (0.5, 0.4, 0.1) | |
| def pool_of(ep: dict) -> str: | |
| if ep['source_repo'] == 'lerobot/libero_plus': | |
| return 'plus' | |
| if ep['source_repo'] == 'nvidia/LIBERO_LeRobot_v3': | |
| if ep['source_suite'] == 'libero_90': | |
| return 'libero90' | |
| if ep['source_suite'] in {'libero_spatial', 'libero_object', 'libero_goal', 'libero_10'}: | |
| return 'familiar' | |
| raise ValueError(f"Unknown source provenance: {ep['source_repo']}, {ep['source_suite']}") | |
| def source_identity(ep: dict) -> tuple: | |
| return ep['source_repo'], ep['source_suite'], ep['source_episode_index'] | |
| def digest(value) -> str: | |
| return hashlib.sha256(json.dumps(value, sort_keys=True).encode()).hexdigest() | |
| def split_episodes(episodes: list[dict], fraction=0.05, seed=42) -> dict: | |
| if not 0 < fraction < 1: | |
| raise ValueError('Validation fraction must be in (0,1)') | |
| if len({e['episode_index'] for e in episodes}) != len(episodes): | |
| raise ValueError('Duplicate merged episode ID') | |
| # Group duplicate source identities together; never leak an exact source episode. | |
| groups = defaultdict(dict) | |
| for ep in episodes: | |
| pool = pool_of(ep) | |
| key = source_identity(ep) | |
| groups[pool, ep['task_index']].setdefault(key, []).append(ep) | |
| rows = [] | |
| for (pool, task), identities in sorted(groups.items()): | |
| keys = sorted(identities, key=lambda k: digest([seed, k])) | |
| eligible = [k for k in keys if any(e['length'] > 16 for e in identities[k])] | |
| if len(eligible) < 2: | |
| raise ValueError(f'Cannot stratify eligible episodes for {pool}/{task}') | |
| nval = min(len(eligible)-1, max(1, round(len(eligible)*fraction))) | |
| val = set(eligible[:nval]) | |
| for key in keys: | |
| for ep in identities[key]: | |
| rows.append({**ep, 'pool': pool, 'split': 'validation' if key in val else 'train'}) | |
| rows.sort(key=lambda e: e['episode_index']) | |
| manifest = {'dataset': DATASET, 'dataset_revision': DATASET_REVISION, | |
| 'fraction': fraction, 'seed': seed, 'provenance_sha256': digest(episodes), 'episodes': rows} | |
| verify_split(manifest) | |
| return manifest | |
| def verify_split(manifest: dict) -> None: | |
| sets = {side: {source_identity(e) for e in manifest['episodes'] if e['split'] == side} | |
| for side in ('train', 'validation')} | |
| if sets['train'] & sets['validation']: | |
| raise ValueError('Source episode leakage') | |
| ids = {side: {e['episode_index'] for e in manifest['episodes'] if e['split'] == side} | |
| for side in sets} | |
| if ids['train'] & ids['validation']: | |
| raise ValueError('Merged episode leakage') | |
| class PoolSampler: | |
| """One synchronous draw per consumed sample; no prefetch ambiguity on resume.""" | |
| def __init__(self, manifest: dict, *, split='train', weights=WEIGHTS, seed=42): | |
| verify_split(manifest) | |
| if len(weights) != 3 or any(not math.isfinite(w) or w <= 0 for w in weights): | |
| raise ValueError('Three positive finite pool weights required') | |
| self.weights = tuple(w/sum(weights) for w in weights) | |
| self.manifest_hash = digest(manifest) | |
| self.split = split | |
| self.rng = random.Random(seed) | |
| self.groups = {p: defaultdict(list) for p in POOLS} | |
| self.counts = Counter({p: 0 for p in POOLS}) | |
| self.task_counts = Counter() | |
| self.draws = 0 | |
| for ep in manifest['episodes']: | |
| if ep['split'] == split and ep['length'] > 16: | |
| self.groups[ep['pool']][ep['task_index']].append(ep) | |
| if any(not self.groups[p] for p in POOLS): | |
| raise ValueError(f'Empty eligible pool in {split}') | |
| self.tasks = {p: sorted(self.groups[p]) for p in POOLS} | |
| def draw(self) -> tuple[int, dict]: | |
| pool = self.rng.choices(POOLS, self.weights)[0] | |
| task = self.rng.choice(self.tasks[pool]) | |
| ep = self.rng.choice(self.groups[pool][task]) | |
| t = self.rng.randrange(ep['length'] - 16) | |
| self.counts[pool] += 1 | |
| self.task_counts[pool, task] += 1 | |
| self.draws += 1 | |
| return ep['dataset_from_index'] + t, {**ep, 'anchor': t} | |
| def state_dict(self) -> dict: | |
| return copy.deepcopy({'rng': self.rng.getstate(), 'counts': self.counts, | |
| 'task_counts': self.task_counts, 'draws': self.draws, | |
| 'manifest_hash': self.manifest_hash, 'weights': self.weights, 'split': self.split}) | |
| def load_state_dict(self, state: dict) -> None: | |
| for key in ('manifest_hash', 'weights', 'split'): | |
| if state[key] != getattr(self, key): | |
| raise ValueError(f'Sampler resume mismatch: {key}') | |
| self.rng.setstate(state['rng']) | |
| self.counts, self.task_counts = Counter(state['counts']), Counter(state['task_counts']) | |
| self.draws = state['draws'] | |
| def distribution_report(self, check=False) -> dict: | |
| report = {'draws': self.draws, 'counts': dict(self.counts), | |
| 'percentages': {p: 100*self.counts[p]/max(1,self.draws) for p in POOLS}} | |
| if check and self.draws >= 10000: | |
| for pool, prob in zip(POOLS, self.weights, strict=True): | |
| tolerance = max(0.01, 6*math.sqrt(prob*(1-prob)/self.draws)) | |
| if abs(self.counts[pool]/self.draws-prob) > tolerance: | |
| raise ValueError(f'Unexpected pool distribution: {report}') | |
| n = self.counts[pool] | |
| p = 1/len(self.tasks[pool]) | |
| for task in self.tasks[pool]: | |
| if abs(self.task_counts[pool, task]-n*p) > max(10, 7*math.sqrt(n*p*(1-p))): | |
| raise ValueError(f'Unexpected task distribution: {pool}/{task}') | |
| return report | |
| def resize_rgb(x: torch.Tensor, size: int) -> torch.Tensor: | |
| shape = x.shape | |
| return F.interpolate(x.reshape(-1, 3, *shape[-2:]), size=(size,size), mode='bilinear', | |
| align_corners=False, antialias=True).reshape(*shape[:-2], size,size) | |
| class ResolutionBottleneck(torch.nn.Module): | |
| """After released ToTensor, before photometric augmentation; all temporal frames.""" | |
| def forward(self, x: torch.Tensor) -> torch.Tensor: | |
| if x.shape[-3:] != (3,256,256) or not x.is_floating_point(): | |
| raise ValueError(f'Expected float RGB [...,3,256,256], got {x.shape}/{x.dtype}') | |
| if not bool(torch.isfinite(x).all()) or x.min() < 0 or x.max() > 1: | |
| raise ValueError('Expected finite RGB [0,1]') | |
| return resize_rgb(resize_rgb(x,128),224) | |
| def competition_cameras(front: torch.Tensor, wrist: torch.Tensor, *, rotate_180=True) -> torch.Tensor: | |
| """Simulator uint8 HWC -> normalized CHW. Stored training videos never rotate.""" | |
| cameras = [] | |
| for image in (front,wrist): | |
| if image.shape != (128,128,3) or image.dtype != torch.uint8: | |
| raise ValueError('Competition camera must be uint8 [128,128,3]') | |
| x = image.permute(2,0,1).float()/255 | |
| if rotate_180: | |
| x = x.flip((-2,-1)) | |
| cameras.append(resize_rgb(x,224)) | |
| return torch.cat(cameras,dim=-1)*2-1 | |