Download read_wds.py from xxxspatialencoderwds5/data_5: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/datasets/xxxspatialencoderwds5/data_5/resolve/main/read_wds.py
- Command line
-
hf download hf://datasets/xxxspatialencoderwds5/data_5/read_wds.py
-
curl -L -o read_wds.py https://huggingface.co/datasets/xxxspatialencoderwds5/data_5/resolve/main/read_wds.py
1.86 kB
| """Read frame WebDatasets using each dataset's manifest.""" | |
| import json | |
| import io | |
| from pathlib import Path | |
| import numpy as np | |
| from PIL import Image | |
| import webdataset as wds | |
| def decode_sample(sample): | |
| result = dict(sample) | |
| meta = json.loads(sample['json']) | |
| result['json'] = meta | |
| for modality, field in meta['components'].items(): | |
| if field is None: | |
| continue | |
| data = io.BytesIO(sample[field]) | |
| if field.endswith('.npy'): | |
| result[field] = np.load(data, allow_pickle=False) | |
| elif modality == 'depth': | |
| # Generic RGB image decoding would destroy 16-bit depth precision. | |
| with Image.open(data) as image: | |
| result[field] = np.array(image) | |
| else: | |
| with Image.open(data) as image: | |
| result[field] = image.convert('RGB') | |
| return result | |
| def dataset(directory, split='train', decode=True): | |
| directory = Path(directory) | |
| manifest = json.loads((directory / 'manifest.json').read_text()) | |
| if manifest['status'] != 'complete': | |
| raise RuntimeError(f'Dataset is not complete: {manifest["status"]}') | |
| shards = [str(directory / s['path']) for s in manifest['shards'] | |
| if s['path'].split('/')[1] == split] | |
| if not shards: | |
| raise ValueError(f'No shards for split {split!r}') | |
| result = wds.WebDataset(shards, shardshuffle=False) | |
| return result.map(decode_sample) if decode else result | |
| if __name__ == '__main__': | |
| import argparse | |
| p = argparse.ArgumentParser() | |
| p.add_argument('directory') | |
| p.add_argument('--split', default='train') | |
| args = p.parse_args() | |
| sample = next(iter(dataset(args.directory, args.split))) | |
| meta = sample['json'] | |
| print('Fields:', list(sample)) | |
| print('Dataset:', meta['dataset'], 'scene:', meta['scene_id'], 'frame:', meta['frame_id']) | |