Download scripts/validate_release.py from KangHui2200/PixelOne-Data: direct link, hf CLI and curl.
- Browser
- Download file 5.42 kB
-
https://huggingface.co/datasets/KangHui2200/PixelOne-Data/resolve/main/scripts/validate_release.py
- Command line
-
hf download hf://datasets/KangHui2200/PixelOne-Data/scripts/validate_release.py
-
curl -L -o validate_release.py https://huggingface.co/datasets/KangHui2200/PixelOne-Data/resolve/main/scripts/validate_release.py
5.42 kB
| import argparse | |
| from collections import Counter | |
| import gzip | |
| import hashlib | |
| import json | |
| import math | |
| from pathlib import Path, PurePosixPath | |
| import re | |
| def digest(path): | |
| with path.open('rb') as stream: | |
| return hashlib.file_digest(stream, 'sha256').hexdigest() | |
| def validate(root): | |
| manifest = json.loads((root / 'manifest.json').read_text()) | |
| assets = {} | |
| image_manifest = root / manifest['images']['file'] | |
| if digest(image_manifest) != manifest['images']['gzip_sha256']: | |
| raise ValueError('Image manifest hash differs') | |
| with gzip.open(image_manifest, 'rt') as stream: | |
| for line in stream: | |
| row = json.loads(line) | |
| if row['image'] in assets: | |
| raise ValueError('Duplicate image manifest key') | |
| assets[row['image']] = row | |
| splits = {} | |
| used_images = set() | |
| for name, specification in manifest['splits'].items(): | |
| path = root / specification['file'] | |
| if digest(path) != specification['gzip_sha256']: | |
| raise ValueError('Compressed annotation hash differs') | |
| payload_hash, semantic_hash = hashlib.sha256(), hashlib.sha256() | |
| ids, pixels, groups, source_counts = set(), set(), set(), Counter() | |
| with gzip.open(path, 'rb') as stream: | |
| for line in stream: | |
| payload_hash.update(line) | |
| row = json.loads(line) | |
| if set(row) != {'id', 'image', 'image_size', 'instruction', 'bbox_xyxy', 'source', 'source_ref'}: | |
| raise ValueError('Unexpected annotation fields') | |
| if row['id'] in ids: | |
| raise ValueError('Duplicate target ID') | |
| ids.add(row['id']) | |
| relative = PurePosixPath(row['image']) | |
| if relative.is_absolute() or '..' in relative.parts or row['image'] not in assets: | |
| raise ValueError('Image reference is unsafe or missing') | |
| if '/mnt/workspace/' in line.decode() or '/root/.ssh' in line.decode(): | |
| raise ValueError('Internal path in annotation') | |
| asset = assets[row['image']] | |
| if row['image_size'] != asset['image_size'] or row['source'] != asset['source']: | |
| raise ValueError('Image metadata differs') | |
| width, height = row['image_size'] | |
| box = row['bbox_xyxy'] | |
| if len(box) != 4 or not all(math.isfinite(value) for value in box): | |
| raise ValueError('Invalid bounding box') | |
| if not (0 <= box[0] < box[2] <= width and 0 <= box[1] < box[3] <= height): | |
| raise ValueError('Bounding box is not in original pixels') | |
| if not isinstance(row['instruction'], str) or not row['instruction'].strip(): | |
| raise ValueError('Empty instruction') | |
| pixel = asset['pixel_sha256'] | |
| pixels.add(pixel) | |
| group = row['source'] + ':' + re.sub(r'_sub\d+(?=\.)', '', asset['source_image']) if row['source'] in ('osatlas_linux', 'osatlas_macos') else 'rgb:' + pixel | |
| groups.add(group) | |
| source_counts[row['source']] += 1 | |
| used_images.add(row['image']) | |
| semantic_hash.update((json.dumps([row['id'], row['instruction'], box, row['image_size'], pixel], | |
| ensure_ascii=False, separators=(',', ':')) + '\n').encode()) | |
| if (len(ids) != specification['targets'] or len(pixels) != specification['unique_rgb_images'] | |
| or dict(source_counts) != specification['source_counts'] | |
| or payload_hash.hexdigest() != specification['jsonl_sha256'] | |
| or semantic_hash.hexdigest() != specification['semantic_sha256']): | |
| raise ValueError('Annotation count or semantic hash differs: ' + name) | |
| splits[name] = {'ids': ids, 'pixels': pixels, 'groups': groups} | |
| print(json.dumps({'split': name, 'targets': len(ids), 'unique_rgb_images': len(pixels), 'verified': True}), flush=True) | |
| if assets.keys() != used_images: | |
| raise ValueError('Image manifest includes unused or missing images') | |
| if not splits['train_200k']['ids'] <= splits['train_500k']['ids']: | |
| raise ValueError('200K is not a subset of 500K') | |
| for name in ('control_21557', 'train_200k', 'train_500k'): | |
| for key in ('ids', 'pixels', 'groups'): | |
| if splits[name][key] & splits['validation_2000'][key]: | |
| raise ValueError('Validation overlap') | |
| return {'status': 'passed', 'image_references': len(assets), | |
| 'splits': {name: {'targets': len(values['ids']), 'unique_rgb_images': len(values['pixels'])} for name, values in splits.items()}, | |
| 'checks': ['compressed and decoded JSONL hashes', 'semantic fingerprints', 'all target counts and source mixtures', | |
| 'image references contain no unused images', 'validation ID/RGB/recorded crop-family separation', | |
| '200K subset of 500K', 'no image bytes or chat training messages in annotations']} | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('root', type=Path) | |
| arguments = parser.parse_args() | |
| report = validate(arguments.root) | |
| (arguments.root / 'validation.json').write_text(json.dumps(report, indent=2) + '\n') | |
| print(json.dumps(report, indent=2)) | |