Datasets:
Create INRBenchmark.py
Browse files- INRBenchmark.py +88 -0
INRBenchmark.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Custom Hugging Face dataset loader for the INR-benchmark repository.
|
| 3 |
+
|
| 4 |
+
* Place this file in the root of the dataset repo alongside README.md.
|
| 5 |
+
* Use `load_dataset("username/INR-benchmark", "spheres", split="1234", trust_remote_code=True)`.
|
| 6 |
+
* Each example yields only the **file path** to keep memory/lightweight; users can `np.load` or `cv2.imread` themselves.
|
| 7 |
+
|
| 8 |
+
Supported configs
|
| 9 |
+
├── `div2k` – 10 RGB PNG images (HR or ×4 LR)
|
| 10 |
+
├── `ct` – single chest CT slice (PNG)
|
| 11 |
+
├── `spheres` – generated sparse-sphere .npy grids for 5 seeds
|
| 12 |
+
├── `bandlimited` – band-limited white-noise .npy grids for 5 seeds
|
| 13 |
+
├── `sierpinski` – 9 depth levels of Sierpinski triangle .npy
|
| 14 |
+
└── `star_target` – 1 synthetic star-resolution target .npy
|
| 15 |
+
|
| 16 |
+
The loader intentionally returns **file paths** so that 2-D PNGs and 2-/3-D NPYs
|
| 17 |
+
coexist without coercing them into a single Arrow schema.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
from pathlib import Path
|
| 22 |
+
from typing import Dict, List, Tuple
|
| 23 |
+
|
| 24 |
+
import datasets
|
| 25 |
+
|
| 26 |
+
_HOMEPAGE = "https://huggingface.co/datasets/etoilekim/INR-benchmark"
|
| 27 |
+
_LICENSE = "CC-BY-4.0"
|
| 28 |
+
|
| 29 |
+
# Mapping config → {split → glob-pattern list}
|
| 30 |
+
_CONFIG_MAP: Dict[str, List[Tuple[str, str]]] = {
|
| 31 |
+
"div2k": [(str(i), f"DIV2K/{name}.png") for i, name in enumerate(
|
| 32 |
+
["0064", "0007", "0010", "0029", "0063", "0072", "0079", "0088", "0093", "0131"]
|
| 33 |
+
)],
|
| 34 |
+
"ct": [("slice", "chest.png")],
|
| 35 |
+
"spheres": [(seed, f"SparseSphereSignal/{seed}/*.npy") for seed in ["1234", "2024", "5678", "7618", "7890"]],
|
| 36 |
+
"bandlimited": [(seed, f"BandlimitedSignal/{seed}/*.npy") for seed in ["1234", "2024", "5678", "7618", "7890"]],
|
| 37 |
+
"sierpinski": [(f"0.{i+1}", f"sierpinski_triangle/*{i}.npy") for i in range(9)],
|
| 38 |
+
"star_target": [("train", "star_resolution_target.npy")],
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class INRBenchmark(datasets.GeneratorBasedBuilder):
|
| 43 |
+
"""GeneratorBasedBuilder with one config per logical subset."""
|
| 44 |
+
|
| 45 |
+
BUILDER_CONFIGS = [datasets.BuilderConfig(name=cfg, version=datasets.Version("1.0.0"))
|
| 46 |
+
for cfg in _CONFIG_MAP.keys()]
|
| 47 |
+
|
| 48 |
+
DEFAULT_CONFIG_NAME = "div2k"
|
| 49 |
+
|
| 50 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 51 |
+
return datasets.DatasetInfo(
|
| 52 |
+
homepage=_HOMEPAGE,
|
| 53 |
+
license=_LICENSE,
|
| 54 |
+
description="INR-benchmark: collection of synthetic & real signals for implicit neural representation research.",
|
| 55 |
+
features=datasets.Features({
|
| 56 |
+
"file_path": datasets.Value("string"),
|
| 57 |
+
}),
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
def _split_generators(self, dl_manager: datasets.download.DownloadManager):
|
| 61 |
+
cfg_name = self.config.name
|
| 62 |
+
if cfg_name not in _CONFIG_MAP:
|
| 63 |
+
raise ValueError(f"Unknown config: {cfg_name}")
|
| 64 |
+
|
| 65 |
+
# Ensure local path (no remote download; dataset files live in repo)
|
| 66 |
+
base_dir = Path(dl_manager.download_and_extract("."))
|
| 67 |
+
|
| 68 |
+
splits = []
|
| 69 |
+
for split_name, pattern in _CONFIG_MAP[cfg_name]:
|
| 70 |
+
abs_pattern = base_dir / pattern
|
| 71 |
+
# keep as glob pattern; actual resolution happens in _generate_examples
|
| 72 |
+
splits.append(
|
| 73 |
+
datasets.SplitGenerator(name=split_name, gen_kwargs={"glob_pattern": abs_pattern})
|
| 74 |
+
)
|
| 75 |
+
return splits
|
| 76 |
+
|
| 77 |
+
def _generate_examples(self, glob_pattern: Path):
|
| 78 |
+
"""Yields index, {file_path} for each matched file."""
|
| 79 |
+
files = sorted(Path().glob(str(glob_pattern)))
|
| 80 |
+
if not files:
|
| 81 |
+
# allow single file pattern without wildcard
|
| 82 |
+
if glob_pattern.exists():
|
| 83 |
+
files = [glob_pattern]
|
| 84 |
+
else:
|
| 85 |
+
raise FileNotFoundError(f"No files matched pattern: {glob_pattern}")
|
| 86 |
+
|
| 87 |
+
for idx, path in enumerate(files):
|
| 88 |
+
yield idx, {"file_path": str(path.relative_to(Path.cwd()))}
|