Datasets:
File size: 1,435 Bytes
a248601 73c6f98 7dcdc23 80c0b47 73c6f98 b7d4966 80c0b47 73c6f98 80c0b47 b7d4966 80c0b47 b7d4966 80c0b47 b7d4966 73c6f98 80c0b47 b7d4966 80c0b47 73c6f98 80c0b47 a248601 80c0b47 b7d4966 80c0b47 7dcdc23 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import os
import datasets
class ForestSegmentationDataset(datasets.GeneratorBasedBuilder):
def _info(self):
return datasets.DatasetInfo(
features=datasets.Features({
"sample_id": datasets.Value("string"),
"image": datasets.Image(), # Path to all_bands.tif
"mask": datasets.Image(), # Path to mask.tif
}),
)
def _split_generators(self, dl_manager):
# Streaming mode: use iter_files to lazily iterate over image files
image_paths = dl_manager.iter_files("images")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"image_paths": image_paths}
)
]
def _generate_examples(self, image_paths):
for image_path in image_paths:
if not image_path.endswith("all_bands.tif"):
continue
# Extract sample_id from path: images/0000005/<timestamp>/all_bands.tif
parts = image_path.split("/")
if len(parts) < 3:
continue # Skip malformed paths
sample_id = parts[-3] # "0000005"
mask_path = f"masks/{sample_id}/mask.tif"
yield sample_id, {
"sample_id": sample_id,
"image": image_path,
"mask": mask_path,
}
|