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//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, }