Datasets:

Modalities:
Geospatial
Languages:
English
DOI:
Libraries:
License:
ssl4eo_eu_forest / dataset.py
cmalbrec's picture
test new custom dataset loader for streaming
b7d4966 verified
raw
history blame
1.71 kB
import os
import datasets
class MyDataset(datasets.GeneratorBasedBuilder):
def _info(self):
return datasets.DatasetInfo(
features=datasets.Features({
"sample_id": datasets.Value("string"),
"image": datasets.Image(), # or datasets.Value("string") if you want paths
"mask": datasets.Image(),
}),
)
def _split_generators(self, dl_manager):
# Streaming mode: use iter_files to avoid downloading everything
image_paths = dl_manager.iter_files("images")
mask_paths = dl_manager.iter_files("masks")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"image_paths": image_paths,
"mask_paths": mask_paths,
}
)
]
def _generate_examples(self, image_paths, mask_paths):
# Build a lookup for masks
mask_lookup = {}
for mask_path in mask_paths:
sample_id = os.path.basename(os.path.dirname(mask_path))
mask_lookup[sample_id] = mask_path
# Iterate over image folders
for image_path in image_paths:
if not image_path.endswith("all_bands.tif"):
continue
parts = image_path.split("/")
sample_id = parts[-3] # e.g., "0000005"
if sample_id not in mask_lookup:
continue
yield self.generate_index(), {
"sample_id": sample_id,
"image": image_path,
"mask": mask_lookup[sample_id],
}