Datasets:
bug fix dataset.py
Browse files- dataset.py +13 -23
dataset.py
CHANGED
|
@@ -2,51 +2,41 @@
|
|
| 2 |
import os
|
| 3 |
import datasets
|
| 4 |
|
| 5 |
-
class
|
| 6 |
def _info(self):
|
| 7 |
return datasets.DatasetInfo(
|
| 8 |
features=datasets.Features({
|
| 9 |
"sample_id": datasets.Value("string"),
|
| 10 |
-
"image": datasets.Image(), #
|
| 11 |
-
"mask": datasets.Image(),
|
| 12 |
}),
|
| 13 |
)
|
| 14 |
|
| 15 |
def _split_generators(self, dl_manager):
|
| 16 |
-
# Streaming mode: use iter_files to
|
| 17 |
image_paths = dl_manager.iter_files("images")
|
| 18 |
-
mask_paths = dl_manager.iter_files("masks")
|
| 19 |
-
|
| 20 |
return [
|
| 21 |
datasets.SplitGenerator(
|
| 22 |
name=datasets.Split.TRAIN,
|
| 23 |
-
gen_kwargs={
|
| 24 |
-
"image_paths": image_paths,
|
| 25 |
-
"mask_paths": mask_paths,
|
| 26 |
-
}
|
| 27 |
)
|
| 28 |
]
|
| 29 |
|
| 30 |
-
def _generate_examples(self, image_paths
|
| 31 |
-
# Build a lookup for masks
|
| 32 |
-
mask_lookup = {}
|
| 33 |
-
for mask_path in mask_paths:
|
| 34 |
-
sample_id = os.path.basename(os.path.dirname(mask_path))
|
| 35 |
-
mask_lookup[sample_id] = mask_path
|
| 36 |
-
|
| 37 |
-
# Iterate over image folders
|
| 38 |
for image_path in image_paths:
|
| 39 |
if not image_path.endswith("all_bands.tif"):
|
| 40 |
continue
|
| 41 |
|
|
|
|
| 42 |
parts = image_path.split("/")
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
yield
|
| 49 |
"sample_id": sample_id,
|
| 50 |
"image": image_path,
|
| 51 |
-
"mask":
|
| 52 |
}
|
|
|
|
| 2 |
import os
|
| 3 |
import datasets
|
| 4 |
|
| 5 |
+
class ForestSegmentationDataset(datasets.GeneratorBasedBuilder):
|
| 6 |
def _info(self):
|
| 7 |
return datasets.DatasetInfo(
|
| 8 |
features=datasets.Features({
|
| 9 |
"sample_id": datasets.Value("string"),
|
| 10 |
+
"image": datasets.Image(), # Path to all_bands.tif
|
| 11 |
+
"mask": datasets.Image(), # Path to mask.tif
|
| 12 |
}),
|
| 13 |
)
|
| 14 |
|
| 15 |
def _split_generators(self, dl_manager):
|
| 16 |
+
# Streaming mode: use iter_files to lazily iterate over image files
|
| 17 |
image_paths = dl_manager.iter_files("images")
|
|
|
|
|
|
|
| 18 |
return [
|
| 19 |
datasets.SplitGenerator(
|
| 20 |
name=datasets.Split.TRAIN,
|
| 21 |
+
gen_kwargs={"image_paths": image_paths}
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
]
|
| 24 |
|
| 25 |
+
def _generate_examples(self, image_paths):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
for image_path in image_paths:
|
| 27 |
if not image_path.endswith("all_bands.tif"):
|
| 28 |
continue
|
| 29 |
|
| 30 |
+
# Extract sample_id from path: images/0000005/<timestamp>/all_bands.tif
|
| 31 |
parts = image_path.split("/")
|
| 32 |
+
if len(parts) < 3:
|
| 33 |
+
continue # Skip malformed paths
|
| 34 |
|
| 35 |
+
sample_id = parts[-3] # "0000005"
|
| 36 |
+
mask_path = f"masks/{sample_id}/mask.tif"
|
| 37 |
|
| 38 |
+
yield sample_id, {
|
| 39 |
"sample_id": sample_id,
|
| 40 |
"image": image_path,
|
| 41 |
+
"mask": mask_path,
|
| 42 |
}
|