Datasets:
Download examples/load_dataset.py from govtech/municipalbench-full: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/datasets/govtech/municipalbench-full/resolve/main/examples/load_dataset.py
- Command line
-
hf download hf://datasets/govtech/municipalbench-full/examples/load_dataset.py
-
curl -L -o load_dataset.py https://huggingface.co/datasets/govtech/municipalbench-full/resolve/main/examples/load_dataset.py
1.31 kB
| """Load a downloaded ImageFolder snapshot and check its full ID population.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| from datasets import Image, load_dataset | |
| def load(root): | |
| dataset = load_dataset("imagefolder", data_dir=str(root / "data"), | |
| split="test", drop_labels=True) | |
| expected = {json.loads(line)["image_id"] for line in | |
| (root / "release/image_manifest.jsonl").read_text().splitlines()} | |
| assert len(dataset) == 11534 | |
| assert set(dataset["image_id"]) == expected | |
| assert "label" not in dataset.column_names, "Shard directory names are not labels" | |
| row = dataset[0] | |
| assert row["image"].size == (row["width"], row["height"]) | |
| streamed = load_dataset("imagefolder", data_dir=str(root / "data"), | |
| split="test", drop_labels=True, streaming=True) | |
| streamed = streamed.cast_column("image", Image(decode=False)) | |
| assert {row["image_id"] for row in streamed} == expected | |
| print(f"Loaded {len(dataset):,} images; full streaming ID population matches.") | |
| return dataset | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[1]) | |
| load(parser.parse_args().root.resolve()) | |