|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from typing import List |
|
|
|
import datasets |
|
import h5py |
|
|
|
|
|
_CITATION = """\ |
|
@article{cabuar, |
|
title={Ca{B}u{A}r: California {B}urned {A}reas dataset for delineation}, |
|
author={Rege Cambrin, Daniele and Colomba, Luca and Garza, Paolo}, |
|
journal={IEEE Geoscience and Remote Sensing Magazine}, |
|
doi={10.1109/MGRS.2023.3292467}, |
|
year={2023} |
|
} |
|
""" |
|
|
|
|
|
_DESCRIPTION = """\ |
|
CaBuAr dataset contains images from Sentinel-2 satellites taken before and after a wildfire. |
|
The ground truth masks are provided by the California Department of Forestry and Fire Protection and they are mapped on the images. |
|
""" |
|
|
|
_HOMEPAGE = "https://huggingface.co/datasets/DarthReca/california_burned_areas" |
|
|
|
_LICENSE = "OPENRAIL" |
|
|
|
_URLS = "raw/patched/512x512.hdf5" |
|
|
|
|
|
class CaBuArConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for CaBuAr. |
|
|
|
Parameters |
|
---------- |
|
|
|
load_prefire: bool |
|
whether to load prefire data |
|
train_folds: List[int] |
|
list of folds to use for training |
|
validation_folds: List[int] |
|
list of folds to use for validation |
|
test_folds: List[int] |
|
list of folds to use for testing |
|
**kwargs |
|
keyword arguments forwarded to super. |
|
""" |
|
|
|
def __init__( |
|
self, |
|
load_prefire: bool, |
|
train_folds: List[int], |
|
validation_folds: List[int], |
|
test_folds: List[int], |
|
**kwargs |
|
): |
|
super(CaBuArConfig, self).__init__(**kwargs) |
|
self.load_prefire = load_prefire |
|
self.train_folds = train_folds |
|
self.validation_folds = validation_folds |
|
self.test_folds = test_folds |
|
|
|
|
|
class CaBuAr(datasets.GeneratorBasedBuilder): |
|
"""California Burned Areas dataset.""" |
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
BUILDER_CONFIGS = [ |
|
CaBuArConfig( |
|
name="post-fire", |
|
version=VERSION, |
|
description="Post-fire only version of the dataset", |
|
load_prefire=False, |
|
train_folds=None, |
|
validation_folds=None, |
|
test_folds=None, |
|
), |
|
CaBuArConfig( |
|
name="pre-post-fire", |
|
version=VERSION, |
|
description="Pre-fire and post-fire version of the dataset", |
|
load_prefire=True, |
|
train_folds=None, |
|
validation_folds=None, |
|
test_folds=None, |
|
), |
|
] |
|
|
|
DEFAULT_CONFIG_NAME = "post-fire" |
|
BUILDER_CONFIG_CLASS = CaBuArConfig |
|
|
|
def _info(self): |
|
if self.config.name == "pre-post-fire": |
|
features = datasets.Features( |
|
{ |
|
"post_fire": datasets.Array3D((512, 512, 12), dtype="uint16"), |
|
"pre_fire": datasets.Array3D((512, 512, 12), dtype="uint16"), |
|
"mask": datasets.Array3D((512, 512, 1), dtype="uint16"), |
|
} |
|
) |
|
else: |
|
features = datasets.Features( |
|
{ |
|
"post_fire": datasets.Array3D((512, 512, 12), dtype="uint16"), |
|
"mask": datasets.Array3D((512, 512, 12), dtype="uint16"), |
|
} |
|
) |
|
|
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=features, |
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
|
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
h5_file = dl_manager.download(_URLS) |
|
|
|
if ( |
|
self.config.train_folds is None |
|
or self.config.validation_folds is None |
|
or self.config.test_folds is None |
|
): |
|
raise ValueError("train_folds, validation_folds and test_folds must be set") |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"folds": self.config.train_folds, |
|
"load_prefire": self.config.load_prefire, |
|
"filepath": h5_file, |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
|
|
gen_kwargs={ |
|
"folds": self.config.validation_folds, |
|
"load_prefire": self.config.load_prefire, |
|
"filepath": h5_file, |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
|
|
gen_kwargs={ |
|
"folds": self.config.test_folds, |
|
"load_prefire": self.config.load_prefire, |
|
"filepath": h5_file, |
|
}, |
|
), |
|
] |
|
|
|
|
|
def _generate_examples(self, folds: List[int], load_prefire: bool, filepath): |
|
with h5py.File(filepath, "r") as f: |
|
for uuid, values in f.items(): |
|
if values.attrs["fold"] not in folds: |
|
continue |
|
if load_prefire and "pre_fire" not in values: |
|
continue |
|
sample = { |
|
"post_fire": values["post_fire"][...], |
|
"mask": values["mask"][...], |
|
} |
|
if load_prefire: |
|
sample["pre_fire"] = values["pre_fire"][...] |
|
yield uuid, sample |
|
|