|
|
import datasets |
|
|
import h5py |
|
|
import json |
|
|
import os |
|
|
|
|
|
class MyDatasetConfig(datasets.BuilderConfig): |
|
|
def __init__(self, name, split_file=None, **kwargs): |
|
|
super().__init__(name=name, **kwargs) |
|
|
self.split_file = split_file |
|
|
|
|
|
|
|
|
class MyDataset(datasets.GeneratorBasedBuilder): |
|
|
BUILDER_CONFIGS = [ |
|
|
MyDatasetConfig(name="Arizona", split_file="Train-Test-Split/split_by_patches_Arizona_split_h5.json"), |
|
|
|
|
|
] |
|
|
|
|
|
def _info(self): |
|
|
return datasets.DatasetInfo( |
|
|
description="Patch-level irrigation dataset stored as separate .h5 files", |
|
|
features=datasets.Features({ |
|
|
"id": datasets.Value("string"), |
|
|
"rgb": datasets.Array3D(shape=(None, None, 3), dtype="float32"), |
|
|
"agri_index": datasets.Array2D(shape=(None, None), dtype="float32"), |
|
|
"land_mask": datasets.Array2D(shape=(None, None), dtype="int8"), |
|
|
"crop_mask": datasets.Array2D(shape=(None, None), dtype="int8"), |
|
|
"irr_mask": datasets.Array2D(shape=(None, None), dtype="int8"), |
|
|
"subirr_mask": datasets.Array2D(shape=(None, None), dtype="int8"), |
|
|
"image_path": datasets.Value("string"), |
|
|
"label_type": datasets.Value("string"), |
|
|
"text_prompt": datasets.Value("string"), |
|
|
"polygon": datasets.Value("string"), |
|
|
"crs": datasets.Value("string"), |
|
|
"split": datasets.Value("string"), |
|
|
}) |
|
|
) |
|
|
|
|
|
def _split_generators(self, dl_manager): |
|
|
|
|
|
data_dir = dl_manager.download_and_extract("Data/label") |
|
|
split_path = dl_manager.download(self.config.split_file) |
|
|
|
|
|
with open(split_path, "r") as f: |
|
|
split_dict = json.load(f) |
|
|
|
|
|
def get_file_paths(split_list): |
|
|
return [os.path.join(data_dir, item["h5_path"].replace("Data/label/", "")) for item in split_list] |
|
|
|
|
|
return [ |
|
|
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"files": get_file_paths(split_dict["train"])}), |
|
|
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"files": get_file_paths(split_dict["test"])}), |
|
|
] |
|
|
|
|
|
def _generate_examples(self, files): |
|
|
for h5_path in files: |
|
|
with h5py.File(h5_path, "r") as f: |
|
|
patch_id = os.path.basename(h5_path).replace(".h5", "") |
|
|
yield patch_id, { |
|
|
"id": patch_id, |
|
|
"rgb": f["rgb"][()], |
|
|
"agri_index": f["agri_index"][()], |
|
|
"land_mask": f["land_mask"][()], |
|
|
"crop_mask": f["crop_mask"][()], |
|
|
"irr_mask": f["irr_mask"][()], |
|
|
"subirr_mask": f["subirr_mask"][()], |
|
|
"image_path": f.attrs["image_path"], |
|
|
"label_type": f.attrs["label_type"], |
|
|
"text_prompt": f.attrs["text_prompt"], |
|
|
"polygon": f.attrs["polygon"], |
|
|
"crs": f.attrs["crs"], |
|
|
"split": f.attrs["split"], |
|
|
} |
|
|
|