crisislandmark / crisislandmark.py
DarthReca's picture
Create crisislandmark.py
7454832 verified
import os
import datasets
import h5py
import hdf5plugin
import pandas as pd
import pyarrow
pyarrow.PyExtensionType.set_auto_load(True)
_CITATION = """\
@misc{cambrin2025texttoremotesensingimageretrievalrgbsources,
title={Text-to-Remote-Sensing-Image Retrieval beyond RGB Sources},
author={Daniele Rege Cambrin and Lorenzo Vaiani and Giuseppe Gallipoli and Luca Cagliero and Paolo Garza},
year={2025},
eprint={2507.10403},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2507.10403},
}
"""
_DESCRIPTION = """\
CrisisLandMark is a large-scale, multimodal corpus for Text-to-Remote-Sensing-Image Retrieval (T2RSIR).
It contains over 647,000 Sentinel-1 (SAR) and Sentinel-2 (multispectral optical) images enriched with
structured textual and geospatial annotations. The dataset is designed to move beyond standard RGB imagery,
enabling the development of retrieval systems that can leverage the rich physical information from different
satellite sensors for applications in Land Use/Land Cover (LULC) mapping and crisis management.
"""
_HOMEPAGE = "https://github.com/DarthReca/closp" # Replace with your actual project page if different
_LICENSE = "Creative Commons Attribution Non Commercial 4.0"
# Define the satellite data sources as in the original script
_SATELLITE_DATASETS = {
"s2": ["benv2s2", "cabuar", "sen2flood"],
"s1": ["benv2s1", "mmflood", "sen1flood", "quakeset"],
}
_URLS = {"main": "crisislandmark.h5", "metadata": "metadata.parquet"}
class CrisisLandMarkConfig(datasets.BuilderConfig):
"""BuilderConfig for CrisisLandMark."""
def __init__(self, satellite_type, **kwargs):
"""
Args:
satellite_type (str): Type of satellite data to load ('s1', 's2', or 'all').
**kwargs: keyword arguments forwarded to super.
"""
super(CrisisLandMarkConfig, self).__init__(**kwargs)
self.satellite_type = satellite_type
class CrisisLandMark(datasets.GeneratorBasedBuilder):
"""CrisisLandMark Dataset."""
VERSION = datasets.Version("1.0.0")
# Configure for different satellite types
BUILDER_CONFIGS = [
CrisisLandMarkConfig(
name="s1",
version=VERSION,
description="Load only Sentinel-1 (SAR) images.",
satellite_type="s1",
),
CrisisLandMarkConfig(
name="s2",
version=VERSION,
description="Load only Sentinel-2 (Optical) images.",
satellite_type="s2",
),
CrisisLandMarkConfig(
name="all",
version=VERSION,
description="Load all images (Sentinel-1 and Sentinel-2).",
satellite_type="all",
),
]
DEFAULT_CONFIG_NAME = "s2"
def _info(self):
# Define the features of the dataset
features = datasets.Features(
{
"key": datasets.Value("string"),
"image": datasets.Array3D(shape=(None, 120, 120), dtype="float32"),
"coords": datasets.Array3D(shape=(2, 120, 120), dtype="float32"),
"labels": datasets.Sequence(datasets.Value("string")),
"crs": datasets.Value("int64"),
"timestamp": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
files = dl_manager.download(_URLS)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"split": "train"} | files,
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"split": "corpus"} | files,
),
]
def _generate_examples(self, split, metadata, main, **kwargs):
"""Yields examples."""
# --- Load and filter metadata ---
metadata_df = pd.read_parquet(metadata)
metadata_df = metadata_df[metadata_df["split"] == split]
# Filter by satellite type based on the config
satellite_type = self.config.satellite_type
if satellite_type != "all":
satellite_filter = "|".join(_SATELLITE_DATASETS[satellite_type])
metadata_df = metadata_df[
metadata_df["key"].str.contains(satellite_filter, regex=True)
]
sample_keys = metadata_df[["key", "labels"]].to_records(index=False)
# Open the HDF5 file once and yield examples
with h5py.File(main, "r") as f:
for key, labels in sample_keys:
sample_group = f[key]
# Read data
image_np = sample_group["image"][:].astype("float32")
coords_np = sample_group["coords"][:].astype("float32")
# Read attributes
crs = sample_group.attrs["crs"]
timestamp = sample_group.attrs["timestamp"]
sample = {
"key": key,
"image": image_np,
"coords": coords_np,
"labels": list(labels), # Ensure it's a list
"crs": crs,
"timestamp": timestamp,
}
yield (key, sample)