|
|
| 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(),
|
| "mask": datasets.Image(),
|
| }),
|
| )
|
|
|
| def _split_generators(self, dl_manager):
|
|
|
| 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):
|
|
|
| mask_lookup = {}
|
| for mask_path in mask_paths:
|
| sample_id = os.path.basename(os.path.dirname(mask_path))
|
| mask_lookup[sample_id] = mask_path
|
|
|
|
|
| for image_path in image_paths:
|
| if not image_path.endswith("all_bands.tif"):
|
| continue
|
|
|
| parts = image_path.split("/")
|
| sample_id = parts[-3]
|
|
|
| if sample_id not in mask_lookup:
|
| continue
|
|
|
| yield self.generate_index(), {
|
| "sample_id": sample_id,
|
| "image": image_path,
|
| "mask": mask_lookup[sample_id],
|
| }
|
|
|