Datasets:

License:
PeterTor commited on
Commit
10d5469
·
verified ·
1 Parent(s): 5b30348

Create AGBD

Browse files
Files changed (1) hide show
  1. AGBD +146 -0
AGBD ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # TODO: Address all TODOs and remove all explanatory comments
15
+ """TODO: Add a description here."""
16
+
17
+ import csv
18
+ import json
19
+ import os
20
+ import numpy as np
21
+ import datasets
22
+ from datasets import Value
23
+ # TODO: Add BibTeX citation
24
+ # Find for instance the citation on arxiv or on the dataset repo/website
25
+ _CITATION = """\
26
+ @InProceedings{huggingface:dataset,
27
+ title = {A great new dataset},
28
+ author={huggingface, Inc.
29
+ },
30
+ year={2020}
31
+ }
32
+ """
33
+
34
+ # TODO: Add description of the dataset here
35
+ # You can copy an official description
36
+ _DESCRIPTION = """\
37
+ This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
38
+ """
39
+
40
+ # TODO: Add a link to an official homepage for the dataset here
41
+ _HOMEPAGE = ""
42
+
43
+ # TODO: Add the licence for the dataset here if you can find it
44
+ _LICENSE = ""
45
+
46
+ norm_values = {
47
+ 'B01': {'mean': 0.12478869, 'std': 0.024433358, 'min': 1e-04, 'max': 1.8808, 'p1': 0.0787, 'p99': 0.1946},
48
+ 'B02': {'mean': 0.13480005, 'std': 0.02822557, 'min': 1e-04, 'max': 2.1776, 'p1': 0.0925, 'p99': 0.2216},
49
+ 'B03': {'mean': 0.16031432, 'std': 0.032037303, 'min': 1e-04, 'max': 2.12, 'p1': 0.1035, 'p99': 0.2556},
50
+ 'B04': {'mean': 0.1532097, 'std': 0.038628064, 'min': 1e-04, 'max': 2.0032, 'p1': 0.1023, 'p99': 0.2816},
51
+ 'B05': {'mean': 0.20312776, 'std': 0.04205057, 'min': 0.0422, 'max': 1.7502, 'p1': 0.1178, 'p99': 0.319},
52
+ 'B06': {'mean': 0.32636437, 'std': 0.07139242, 'min': 0.0502, 'max': 1.7245, 'p1': 0.1633, 'p99': 0.519},
53
+ 'B07': {'mean': 0.36605212, 'std': 0.08555025, 'min': 0.0616, 'max': 1.7149, 'p1': 0.1776, 'p99': 0.6076},
54
+ 'B08': {'mean': 0.3811653, 'std': 0.092815965, 'min': 1e-04, 'max': 1.7488, 'p1': 0.1691, 'p99': 0.646},
55
+ 'B8A': {'mean': 0.3910436, 'std': 0.0896364, 'min': 0.055, 'max': 1.688, 'p1': 0.1871, 'p99': 0.6386},
56
+ 'B09': {'mean': 0.3910644, 'std': 0.0836445, 'min': 0.0012, 'max': 1.7915, 'p1': 0.2124, 'p99': 0.6241},
57
+ 'B11': {'mean': 0.2917373, 'std': 0.07472579, 'min': 0.0953, 'max': 1.648, 'p1': 0.1334, 'p99': 0.4827},
58
+ 'B12': {'mean': 0.21169408, 'std': 0.05880649, 'min': 0.0975, 'max': 1.6775, 'p1': 0.115, 'p99': 0.3872}}
59
+
60
+ feature_dtype = {'s2_num_days': Value('int16'),
61
+ 'gedi_num_days': Value('uint16'),
62
+ 'lat': Value('float32'),
63
+ 'lon': Value('float32'),
64
+ "agbd_se": Value('float32'),
65
+ "elev_lowes": Value('float32'),
66
+ "leaf_off_f": Value('uint8'),
67
+ "pft_class": Value('uint8'),
68
+ "region_cla": Value('uint8'),
69
+ "rh98": Value('float32'),
70
+ "sensitivity": Value('float32'),
71
+ "solar_elev": Value('float32'),
72
+ "urban_prop":Value('uint8')}
73
+
74
+ class NewDataset(datasets.GeneratorBasedBuilder):
75
+ def __init__(self, *args, additional_features=[], normalize_data=True, patch_size=15, **kwargs):
76
+ self.inner_dataset_kwargs = kwargs
77
+ self._is_streaming = False
78
+ self.patch_size = patch_size
79
+ self.normalize_data = normalize_data
80
+ self.additional_features = additional_features
81
+ super().__init__(*args, **kwargs)
82
+
83
+ VERSION = datasets.Version("1.1.0")
84
+
85
+ BUILDER_CONFIGS = [
86
+ datasets.BuilderConfig(name="default", version=VERSION, description="Normalized data"),
87
+ datasets.BuilderConfig(name="unnormalized", version=VERSION, description="Unnormalized data"),
88
+ ]
89
+
90
+ DEFAULT_CONFIG_NAME = "default"
91
+
92
+ def as_streaming_dataset(self, split=None, base_path=None):
93
+ self._is_streaming = True
94
+ return super().as_streaming_dataset(split=split, base_path=base_path)
95
+
96
+ def _info(self):
97
+ all_features = {
98
+ 'input': datasets.Sequence(datasets.Sequence(datasets.Sequence(datasets.Value('float32')))),
99
+ 'label': Value('float32')
100
+ }
101
+ for feat in self.additional_features:
102
+ all_features[feat] = feature_dtype[feat]
103
+ features = datasets.Features(all_features)
104
+
105
+ return datasets.DatasetInfo(
106
+ description=_DESCRIPTION,
107
+ features=features,
108
+ homepage=_HOMEPAGE,
109
+ license=_LICENSE,
110
+ citation=_CITATION,
111
+ )
112
+
113
+ def denormalize_s2(self, patch):
114
+ res = []
115
+ for band, band_value in zip(['B04', 'B03', 'B02'], [patch[3], patch[2], patch[1]]):
116
+ p1, p99 = norm_values[band]['p1'], norm_values[band]['p99']
117
+ band_value = (p99 - p1) * band_value + p1
118
+ res.append(band_value)
119
+ patch[3], patch[2], patch[1] = res
120
+ return patch
121
+
122
+ def _split_generators(self, dl_manager):
123
+ self.original_dataset = datasets.load_dataset("prs-eth/AGBD_raw", streaming=self._is_streaming)
124
+ return [
125
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"split": "train"}),
126
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"split": "val"}),
127
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"split": "test"}),
128
+ ]
129
+
130
+ def _generate_examples(self, split):
131
+ for i, d in enumerate(self.original_dataset[split]):
132
+ if self.config.name == "default":
133
+ data = {'input': np.asarray(d["input"]), 'label': d["label"]}
134
+ elif self.config.name == "unnormalized":
135
+ data = {'input': np.asarray(self.denormalize_s2(np.array(d["input"]))), 'label': d["label"]}
136
+
137
+ start_x = (data["input"].shape[1] - self.patch_size) // 2
138
+ start_y = (data["input"].shape[2] - self.patch_size) // 2
139
+ data["input"] = data["input"][:, start_x:start_x + self.patch_size, start_y:start_y + self.patch_size]
140
+
141
+ for feat in self.additional_features:
142
+ data[feat] = d["metadata"][feat]
143
+
144
+ yield i, data
145
+
146
+