Datasets:

Modalities:
Geospatial
Languages:
English
DOI:
Libraries:
License:
ssl4eo_eu_forest / dataset.py
cmalbrec's picture
Upload 2 files
73c6f98 verified
raw
history blame
3.79 kB
import os
import datasets
import rasterio
import tqdm
class SSL4EOEUForest(datasets.GeneratorBasedBuilder):
def _info(self):
return datasets.DatasetInfo(
description="SSL4EO-EU Forest dataset with four-season of Sentinel-2 timestamps and bounding box metadata extracted from image folders.",
features=datasets.Features({
"image_path": datasets.Value("string"),
"mask_path": datasets.Value("string"),
"start_timestamp": datasets.Value("string"),
"end_timestamp": datasets.Value("string"),
"sentinel_tile_id": datasets.Value("string"),
"bbox": {
"minx": datasets.Value("float32"),
"maxx": datasets.Value("float32"),
"miny": datasets.Value("float32"),
"maxy": datasets.Value("float32"),
}
}),
citation="""@misc{ssl4eo_eu_forest,
author = {Nassim Ait Ali Braham and Conrad M Albrecht},
title = {SSL4EO-EU Forest Dataset},
year = {2025},
howpublished = {https://huggingface.co/datasets/dm4eo/ssl4eo-eu-forest},
note = {This work was carried under the EvoLand project, cf. https://www.evo-land.eu . This project has received funding from the European Union's Horizon Europe research and innovation programme under grant agreement No. 101082130.}
}""",
homepage="https://www.evo-land.eu",
license="CC-BY-4.0",
)
def _split_generators(self, dl_manager):
use_local = os.environ.get("HF_DATASET_LOCAL", "false").lower() == "true"
if use_local:
root = os.path.abspath(".")
else:
root = dl_manager.download_and_extract(".")
images_dir = os.path.join(root, "images")
masks_dir = os.path.join(root, "masks")
return [
datasets.SplitGenerator(name=datasets.Split("all"), gen_kwargs={
"images_dir": images_dir,
"masks_dir": masks_dir
})
]
def _generate_examples(self, images_dir, masks_dir):
sample_id_list = sorted(os.listdir(images_dir))
key = 0
for sample_id in tqdm.tqdm(sample_id_list, desc="Indexing samples", unit="sample"):
sample_path = os.path.join(images_dir, sample_id)
if not os.path.isdir(sample_path):
continue
timestamp_dirs = sorted(os.listdir(sample_path))
mask_path = os.path.join(masks_dir, sample_id, "mask.tif")
if not os.path.exists(mask_path):
continue
for ts in timestamp_dirs:
parts = ts.split("_")
if len(parts) != 3:
continue
start_ts, end_ts, tile_id = parts
image_path = os.path.join(sample_path, ts, "all_bands.tif")
if not os.path.exists(image_path):
continue
try:
with rasterio.open(image_path) as src:
bounds = src.bounds
except Exception:
continue
yield key, {
"image_path": image_path,
"mask_path": mask_path,
"start_timestamp": start_ts,
"end_timestamp": end_ts,
"sentinel_tile_id": tile_id,
"bbox": {
"minx": bounds.left,
"maxx": bounds.right,
"miny": bounds.bottom,
"maxy": bounds.top
}
}
key += 1