Download scripts/make_parquet.py from XZhi/ViViD-5k: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/datasets/XZhi/ViViD-5k/resolve/main/scripts/make_parquet.py
- Command line
-
hf download hf://datasets/XZhi/ViViD-5k/scripts/make_parquet.py
-
curl -L -o make_parquet.py https://huggingface.co/datasets/XZhi/ViViD-5k/resolve/main/scripts/make_parquet.py
1.85 kB
| """Build Dataset Viewer parquet shards: 1024px JPEG previews + per-image counts.""" | |
| import io | |
| from pathlib import Path | |
| import pandas as pd | |
| from datasets import Dataset, Features, Image as HFImage, Value | |
| from PIL import Image, ImageFile | |
| from tqdm import tqdm | |
| # 3 source images are truncated (1597915859896.jpg, 1599030751681.jpg, | |
| # 1599042714933.jpg); previews render the decodable portion instead of crashing. | |
| ImageFile.LOAD_TRUNCATED_IMAGES = True | |
| ROOT = Path(__file__).resolve().parents[1] | |
| DATA = ROOT / "data" | |
| OUT = ROOT / "parquet" | |
| SHARDS = {"train": 4, "val": 1, "test": 1} | |
| FEATURES = Features( | |
| {"image": HFImage(), "berry_count": Value("int32"), "cluster_count": Value("int32")} | |
| ) | |
| def preview_bytes(path): | |
| img = Image.open(path).convert("RGB") | |
| img.thumbnail((1024, 1024)) | |
| buf = io.BytesIO() | |
| img.save(buf, format="JPEG", quality=85) | |
| return buf.getvalue() | |
| def main(): | |
| OUT.mkdir(exist_ok=True) | |
| meta = pd.read_csv(DATA / "metadata.csv") | |
| for split, n_shards in SHARDS.items(): | |
| rows = meta[meta.split == split].reset_index(drop=True) | |
| def gen(rows=rows): | |
| for r in tqdm(rows.itertuples(), total=len(rows), desc=split): | |
| yield { | |
| "image": {"bytes": preview_bytes(DATA / "imgs" / r.file_name), | |
| "path": r.file_name}, | |
| "berry_count": int(r.berry_count), | |
| "cluster_count": int(r.cluster_count), | |
| } | |
| ds = Dataset.from_generator(gen, features=FEATURES) | |
| for i in range(n_shards): | |
| shard_path = OUT / f"{split}-{i:05d}-of-{n_shards:05d}.parquet" | |
| ds.shard(num_shards=n_shards, index=i).to_parquet(shard_path) | |
| print("wrote", shard_path, f"{shard_path.stat().st_size/1e6:.0f} MB") | |
| if __name__ == "__main__": | |
| main() | |