Nibir commited on
Commit
c26788d
·
verified ·
1 Parent(s): a3e1f28

Update dataset.py

Browse files
Files changed (1) hide show
  1. dataset.py +72 -0
dataset.py CHANGED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import h5py
3
+ import json
4
+ import os
5
+
6
+ class MyDatasetConfig(datasets.BuilderConfig):
7
+ def __init__(self, name, split_file=None, **kwargs):
8
+ super().__init__(name=name, **kwargs)
9
+ self.split_file = split_file
10
+
11
+
12
+ class MyDataset(datasets.GeneratorBasedBuilder):
13
+ BUILDER_CONFIGS = [
14
+ MyDatasetConfig(name="Arizona", split_file="Train-Test-Split/split_by_patches_Arizona_split_h5.json"),
15
+ # Add other state configs similarly
16
+ ]
17
+
18
+ def _info(self):
19
+ return datasets.DatasetInfo(
20
+ description="Patch-level irrigation dataset stored as separate .h5 files",
21
+ features=datasets.Features({
22
+ "id": datasets.Value("string"),
23
+ "rgb": datasets.Array3D(shape=(None, None, 3), dtype="float32"),
24
+ "agri_index": datasets.Array2D(shape=(None, None), dtype="float32"),
25
+ "land_mask": datasets.Array2D(shape=(None, None), dtype="int8"),
26
+ "crop_mask": datasets.Array2D(shape=(None, None), dtype="int8"),
27
+ "irr_mask": datasets.Array2D(shape=(None, None), dtype="int8"),
28
+ "subirr_mask": datasets.Array2D(shape=(None, None), dtype="int8"),
29
+ "image_path": datasets.Value("string"),
30
+ "label_type": datasets.Value("string"),
31
+ "text_prompt": datasets.Value("string"),
32
+ "polygon": datasets.Value("string"),
33
+ "crs": datasets.Value("string"),
34
+ "split": datasets.Value("string"),
35
+ })
36
+ )
37
+
38
+ def _split_generators(self, dl_manager):
39
+ # Download full dataset and split JSON
40
+ data_dir = dl_manager.download_and_extract("Data/label")
41
+ split_path = dl_manager.download(self.config.split_file)
42
+
43
+ with open(split_path, "r") as f:
44
+ split_dict = json.load(f)
45
+
46
+ def get_file_paths(split_list):
47
+ return [os.path.join(data_dir, item["h5_path"].replace("Data/label/", "")) for item in split_list]
48
+
49
+ return [
50
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"files": get_file_paths(split_dict["train"])}),
51
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"files": get_file_paths(split_dict["test"])}),
52
+ ]
53
+
54
+ def _generate_examples(self, files):
55
+ for h5_path in files:
56
+ with h5py.File(h5_path, "r") as f:
57
+ patch_id = os.path.basename(h5_path).replace(".h5", "")
58
+ yield patch_id, {
59
+ "id": patch_id,
60
+ "rgb": f["rgb"][()],
61
+ "agri_index": f["agri_index"][()],
62
+ "land_mask": f["land_mask"][()],
63
+ "crop_mask": f["crop_mask"][()],
64
+ "irr_mask": f["irr_mask"][()],
65
+ "subirr_mask": f["subirr_mask"][()],
66
+ "image_path": f.attrs["image_path"],
67
+ "label_type": f.attrs["label_type"],
68
+ "text_prompt": f.attrs["text_prompt"],
69
+ "polygon": f.attrs["polygon"],
70
+ "crs": f.attrs["crs"],
71
+ "split": f.attrs["split"],
72
+ }