|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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", "raw/patched/chabud_test.h5"] |
|
|
|
|
|
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, **kwargs): |
|
super(CaBuArConfig, self).__init__(**kwargs) |
|
self.load_prefire = load_prefire |
|
|
|
|
|
class CaBuAr(datasets.GeneratorBasedBuilder): |
|
"""California Burned Areas dataset.""" |
|
|
|
VERSION = datasets.Version("1.1.0") |
|
|
|
BUILDER_CONFIGS = [ |
|
CaBuArConfig( |
|
name="post-fire", |
|
version=VERSION, |
|
description="Post-fire only version of the dataset", |
|
load_prefire=False, |
|
), |
|
CaBuArConfig( |
|
name="pre-post-fire", |
|
version=VERSION, |
|
description="Pre-fire and post-fire version of the dataset", |
|
load_prefire=True, |
|
), |
|
] |
|
|
|
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, 1), dtype="uint16"), |
|
} |
|
) |
|
|
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=features, |
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
|
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
h5_files = dl_manager.download(_URLS) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=fold, |
|
|
|
gen_kwargs={ |
|
"fold": fold, |
|
"load_prefire": self.config.load_prefire, |
|
"filepath": h5_files[file_index], |
|
}, |
|
) |
|
for fold, file_index in zip(list(range(0, 5)) + ["chabud"], [0] * 5 + [1]) |
|
] |
|
|
|
|
|
def _generate_examples(self, fold: int, load_prefire: bool, filepath): |
|
with h5py.File(filepath, "r") as f: |
|
for uuid, values in f.items(): |
|
if values.attrs["fold"] != fold: |
|
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 |
|
|