Update PASTIS.py
Browse files
PASTIS.py
CHANGED
|
@@ -14,49 +14,10 @@ S2_MEAN = [1180.2278549 , 1387.76882557, 1436.67627781, 1773.66437066, 2735.8641
|
|
| 14 |
|
| 15 |
S2_STD = [1976.91493068, 1917.02121286, 1996.45123112, 1903.34296117, 1785.08356262, 1796.4477813 , 1811.90019014, 1793.47036145, 1474.46979658, 1309.88416505]
|
| 16 |
|
| 17 |
-
S1A_MEAN = [-10.91848081, -17.34320436]
|
| 18 |
-
|
| 19 |
-
S1A_STD = [3.26830557, 3.19895575]
|
| 20 |
-
|
| 21 |
-
S1D_MEAN = [-11.07395082, -17.45261358]
|
| 22 |
-
|
| 23 |
-
S1D_STD = [3.33774017, 3.15584225]
|
| 24 |
-
|
| 25 |
-
S1_MEAN = [-10.996215815 -17.39790897]
|
| 26 |
-
|
| 27 |
-
S1_STD = [3.30411987, 3.177943]
|
| 28 |
-
|
| 29 |
-
s1_metadata = {
|
| 30 |
-
'radar': {
|
| 31 |
-
'mean': S1_MEAN,
|
| 32 |
-
'std': S1_STD,
|
| 33 |
-
},
|
| 34 |
-
'radar_a': {
|
| 35 |
-
'mean': S1A_MEAN,
|
| 36 |
-
'std': S1A_STD,
|
| 37 |
-
},
|
| 38 |
-
'radar_d': {
|
| 39 |
-
'mean': S1D_MEAN,
|
| 40 |
-
'std': S1D_STD,
|
| 41 |
-
},
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
s1_num_seq = {
|
| 45 |
-
'radar': 142,
|
| 46 |
-
'radar_a': 71,
|
| 47 |
-
'radar_d': 71,
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
sats = {
|
| 51 |
-
"radar": ["S2", "S1A", "S1D"],
|
| 52 |
-
"radar_a": ["S2", "S1A"],
|
| 53 |
-
"radar_d": ["S2", "S1D"],
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
class PASTISDataset(datasets.GeneratorBasedBuilder):
|
| 57 |
VERSION = datasets.Version("1.0.0")
|
| 58 |
|
| 59 |
-
DATA_URL = "https://huggingface.co/datasets/GFM-Bench/PASTIS/resolve/main/PASTIS.
|
| 60 |
|
| 61 |
metadata = {
|
| 62 |
"s2c": {
|
|
@@ -66,8 +27,10 @@ class PASTISDataset(datasets.GeneratorBasedBuilder):
|
|
| 66 |
"std": S2_STD,
|
| 67 |
},
|
| 68 |
"s1": {
|
| 69 |
-
"bands":
|
| 70 |
-
"channel_wv":
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
|
@@ -77,26 +40,11 @@ class PASTISDataset(datasets.GeneratorBasedBuilder):
|
|
| 77 |
|
| 78 |
NUM_CLASSES = 20 # 0 is background class, and 19 is the void label
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
datasets.BuilderConfig(name="default"),
|
| 83 |
-
*[datasets.BuilderConfig(name=name) for name in ['radar', 'radar_a', 'radar_d']]
|
| 84 |
-
]
|
| 85 |
-
|
| 86 |
-
DEFAULT_CONFIG_NAME = "radar"
|
| 87 |
-
|
| 88 |
-
def __init__(self, reference_date="2018-09-10", config_name="default", **kwargs):
|
| 89 |
-
super().__init__(config_name=config_name, **kwargs)
|
| 90 |
self.reference_date = datetime(*map(int, reference_date.split("-")))
|
| 91 |
print(f"reference_date: {reference_date} -> {self.reference_date}")
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
config_name = "radar" if config_name == "default" else config_name
|
| 95 |
-
self.NUM_RADAR_SEQ = s1_num_seq[config_name]
|
| 96 |
-
self.sats = sats[config_name]
|
| 97 |
-
self.metadata["s1"].update(s1_metadata[config_name])
|
| 98 |
-
self.sats_name = config_name
|
| 99 |
-
|
| 100 |
def _split_generators(self, dl_manager):
|
| 101 |
if isinstance(self.DATA_URL, list):
|
| 102 |
downloaded_files = dl_manager.download(self.DATA_URL)
|
|
@@ -139,26 +87,20 @@ class PASTISDataset(datasets.GeneratorBasedBuilder):
|
|
| 139 |
metadata['size'] = self.SIZE
|
| 140 |
metadata['num_classes'] = self.NUM_CLASSES
|
| 141 |
metadata['spatial_resolution'] = self.spatial_resolution
|
| 142 |
-
|
| 143 |
return datasets.DatasetInfo(
|
| 144 |
description=json.dumps(metadata),
|
| 145 |
features=datasets.Features({
|
| 146 |
"optical": datasets.Array4D(shape=(61, 10, self.HEIGHT, self.WIDTH), dtype="float32"),
|
| 147 |
-
"radar": datasets.Array4D(shape=(self.NUM_RADAR_SEQ, 2, self.HEIGHT, self.WIDTH), dtype="float32"),
|
| 148 |
"label": datasets.Array2D(shape=(self.HEIGHT, self.WIDTH), dtype="int32"),
|
| 149 |
-
"
|
| 150 |
-
"
|
| 151 |
-
"optical_sequence_len": datasets.Value("int32"),
|
| 152 |
-
"radar_sequence_len": datasets.Value("int32"),
|
| 153 |
"optical_channel_wv": datasets.Sequence(datasets.Value("float32")),
|
| 154 |
-
"radar_channel_wv": datasets.Sequence(datasets.Value("float32")),
|
| 155 |
"spatial_resolution": datasets.Value("int32"),
|
| 156 |
}),
|
| 157 |
)
|
| 158 |
|
| 159 |
def _generate_examples(self, split, data_dir):
|
| 160 |
optical_channel_wv = self.metadata["s2c"]["channel_wv"]
|
| 161 |
-
radar_channel_wv = self.metadata["s1"]["channel_wv"]
|
| 162 |
spatial_resolution = self.spatial_resolution
|
| 163 |
|
| 164 |
data_dir = os.path.join(data_dir, "PASTIS")
|
|
@@ -166,53 +108,26 @@ class PASTISDataset(datasets.GeneratorBasedBuilder):
|
|
| 166 |
metadata = metadata[metadata["split"] == split].reset_index(drop=True)
|
| 167 |
|
| 168 |
self._prepare_meta_patch(data_dir)
|
| 169 |
-
self._prepare_date_tables()
|
| 170 |
|
| 171 |
for index, row in metadata.iterrows():
|
| 172 |
id_patch = row.optical_path.replace("DATA_S2/S2_", "").replace(".tif", "")
|
| 173 |
-
|
| 174 |
optical_path = os.path.join(data_dir, row.optical_path)
|
| 175 |
optical = self._read_image(optical_path).astype(np.float32) # TxCxHxW
|
| 176 |
-
|
| 177 |
-
optical = self._pad_sequence(optical
|
| 178 |
-
optical_dates = self._get_dates(id_patch=id_patch, sat="S2")
|
| 179 |
-
|
| 180 |
-
radar_sequence_len = 0
|
| 181 |
-
if self.sats_name in ["radar", "radar_a"]:
|
| 182 |
-
radar_a_path = os.path.join(data_dir, row.radar_a_path)
|
| 183 |
-
radar_a = self._read_image(radar_a_path).astype(np.float32)[:, :2, :, :] # T, 2, 128, 128
|
| 184 |
-
radar_a_dates = self._get_dates(id_patch=id_patch, sat="S1A")
|
| 185 |
-
radar_sequence_len += radar_a.shape[0]
|
| 186 |
-
if self.sats_name == "radar_a":
|
| 187 |
-
radar = self._pad_sequence(radar_a, "S1A") # 71, 2, 128, 128
|
| 188 |
-
radar_dates = radar_a_dates
|
| 189 |
-
|
| 190 |
-
if self.sats_name in ["radar", "radar_d"]:
|
| 191 |
-
radar_d_path = os.path.join(data_dir, row.radar_d_path)
|
| 192 |
-
radar_d = self._read_image(radar_d_path).astype(np.float32)[:, :2, :, :]
|
| 193 |
-
radar_d_dates = self._get_dates(id_patch=id_patch, sat="S1D")
|
| 194 |
-
radar_sequence_len += radar_d.shape[0]
|
| 195 |
-
if self.sats_name == "radar_d":
|
| 196 |
-
radar = self._pad_sequence(radar_d, sat="S1D") # 71, 2, 128, 128
|
| 197 |
-
radar_dates = radar_d_dates
|
| 198 |
-
|
| 199 |
-
if self.sats_name == "radar":
|
| 200 |
-
assert radar_a is not None and radar_d is not None
|
| 201 |
-
radar, radar_dates = self._merge_sort_dates(radar_a_dates, radar_d_dates, radar_a, radar_d)
|
| 202 |
-
radar = self._pad_sequence(radar, sat="S1_both") # 142, 2, 128, 128
|
| 203 |
|
| 204 |
label_path = os.path.join(data_dir, row.label_path) # 3xHxW
|
| 205 |
label = tifffile.imread(label_path)[0] # HxW
|
| 206 |
|
|
|
|
|
|
|
|
|
|
| 207 |
sample = {
|
| 208 |
"optical": optical,
|
| 209 |
"optical_channel_wv": optical_channel_wv,
|
| 210 |
-
"
|
| 211 |
-
"
|
| 212 |
-
"radar": radar,
|
| 213 |
-
"radar_channel_wv": radar_channel_wv,
|
| 214 |
-
"radar_dates": radar_dates,
|
| 215 |
-
"radar_sequence_len": radar_sequence_len,
|
| 216 |
"label": label,
|
| 217 |
"spatial_resolution": spatial_resolution,
|
| 218 |
}
|
|
@@ -220,15 +135,16 @@ class PASTISDataset(datasets.GeneratorBasedBuilder):
|
|
| 220 |
yield f"{index}", sample
|
| 221 |
|
| 222 |
# util functions
|
|
|
|
| 223 |
def _prepare_meta_patch(self, data_dir):
|
| 224 |
self.meta_patch = gpd.read_file(os.path.join(data_dir, "metadata.geojson"))
|
| 225 |
self.meta_patch.index = self.meta_patch["ID_PATCH"].astype(int)
|
| 226 |
self.meta_patch.sort_index(inplace=True)
|
| 227 |
|
| 228 |
-
def _prepare_date_tables(self):
|
| 229 |
-
self.date_tables = {
|
| 230 |
self.date_range = np.array(range(-200, 600))
|
| 231 |
-
for s in
|
| 232 |
dates = self.meta_patch["dates-{}".format(s)]
|
| 233 |
date_table = pd.DataFrame(
|
| 234 |
index=self.meta_patch.index, columns=self.date_range, dtype=int
|
|
@@ -253,33 +169,22 @@ class PASTISDataset(datasets.GeneratorBasedBuilder):
|
|
| 253 |
def _get_dates(self, id_patch, sat="S2"):
|
| 254 |
id_patch = int(id_patch)
|
| 255 |
return self.date_range[np.where(self.date_tables[sat][id_patch] == 1)[0]]
|
| 256 |
-
|
| 257 |
-
def _merge_sort_dates(self, radar_a_dates, radar_d_dates, radar_a, radar_d):
|
| 258 |
-
merged_dates = np.concatenate((radar_a_dates, radar_d_dates))
|
| 259 |
-
sorted_indices = np.argsort(merged_dates)
|
| 260 |
-
|
| 261 |
-
sorted_images = np.concatenate((radar_a, radar_d), axis=0)[sorted_indices]
|
| 262 |
-
sorted_dates = merged_dates[sorted_indices]
|
| 263 |
-
|
| 264 |
-
return sorted_images, sorted_dates
|
| 265 |
|
| 266 |
-
def _pad_sequence(self,
|
| 267 |
-
|
| 268 |
-
sizes = {"S2": 61, "S1A": 71, "S1D": 71, "S1_both": 142}
|
| 269 |
-
assert image.shape[0] <= sizes[sat]
|
| 270 |
-
padding_size = sizes[sat] - image.shape[0]
|
| 271 |
if padding_size == 0:
|
| 272 |
-
return
|
| 273 |
|
| 274 |
-
pad = np.zeros((padding_size, *
|
| 275 |
-
|
| 276 |
-
return
|
| 277 |
|
| 278 |
def _read_image(self, image_path):
|
| 279 |
"""Read tiff image from image_path
|
| 280 |
Args:
|
| 281 |
image_path:
|
| 282 |
Image path to read from
|
|
|
|
| 283 |
Return:
|
| 284 |
image:
|
| 285 |
C, H, W numpy array image
|
|
|
|
| 14 |
|
| 15 |
S2_STD = [1976.91493068, 1917.02121286, 1996.45123112, 1903.34296117, 1785.08356262, 1796.4477813 , 1811.90019014, 1793.47036145, 1474.46979658, 1309.88416505]
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
class PASTISDataset(datasets.GeneratorBasedBuilder):
|
| 18 |
VERSION = datasets.Version("1.0.0")
|
| 19 |
|
| 20 |
+
DATA_URL = "https://huggingface.co/datasets/GFM-Bench/PASTIS/resolve/main/PASTIS.zip" # TODO
|
| 21 |
|
| 22 |
metadata = {
|
| 23 |
"s2c": {
|
|
|
|
| 27 |
"std": S2_STD,
|
| 28 |
},
|
| 29 |
"s1": {
|
| 30 |
+
"bands": None,
|
| 31 |
+
"channel_wv": None,
|
| 32 |
+
"mean": None,
|
| 33 |
+
"std": None
|
| 34 |
}
|
| 35 |
}
|
| 36 |
|
|
|
|
| 40 |
|
| 41 |
NUM_CLASSES = 20 # 0 is background class, and 19 is the void label
|
| 42 |
|
| 43 |
+
def __init__(self, reference_date="2018-09-10", **kwargs):
|
| 44 |
+
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
self.reference_date = datetime(*map(int, reference_date.split("-")))
|
| 46 |
print(f"reference_date: {reference_date} -> {self.reference_date}")
|
| 47 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def _split_generators(self, dl_manager):
|
| 49 |
if isinstance(self.DATA_URL, list):
|
| 50 |
downloaded_files = dl_manager.download(self.DATA_URL)
|
|
|
|
| 87 |
metadata['size'] = self.SIZE
|
| 88 |
metadata['num_classes'] = self.NUM_CLASSES
|
| 89 |
metadata['spatial_resolution'] = self.spatial_resolution
|
|
|
|
| 90 |
return datasets.DatasetInfo(
|
| 91 |
description=json.dumps(metadata),
|
| 92 |
features=datasets.Features({
|
| 93 |
"optical": datasets.Array4D(shape=(61, 10, self.HEIGHT, self.WIDTH), dtype="float32"),
|
|
|
|
| 94 |
"label": datasets.Array2D(shape=(self.HEIGHT, self.WIDTH), dtype="int32"),
|
| 95 |
+
"dates": datasets.Sequence(datasets.Value("int32")),
|
| 96 |
+
"sequence_len": datasets.Value("int32"),
|
|
|
|
|
|
|
| 97 |
"optical_channel_wv": datasets.Sequence(datasets.Value("float32")),
|
|
|
|
| 98 |
"spatial_resolution": datasets.Value("int32"),
|
| 99 |
}),
|
| 100 |
)
|
| 101 |
|
| 102 |
def _generate_examples(self, split, data_dir):
|
| 103 |
optical_channel_wv = self.metadata["s2c"]["channel_wv"]
|
|
|
|
| 104 |
spatial_resolution = self.spatial_resolution
|
| 105 |
|
| 106 |
data_dir = os.path.join(data_dir, "PASTIS")
|
|
|
|
| 108 |
metadata = metadata[metadata["split"] == split].reset_index(drop=True)
|
| 109 |
|
| 110 |
self._prepare_meta_patch(data_dir)
|
| 111 |
+
self._prepare_date_tables(data_dir)
|
| 112 |
|
| 113 |
for index, row in metadata.iterrows():
|
| 114 |
id_patch = row.optical_path.replace("DATA_S2/S2_", "").replace(".tif", "")
|
|
|
|
| 115 |
optical_path = os.path.join(data_dir, row.optical_path)
|
| 116 |
optical = self._read_image(optical_path).astype(np.float32) # TxCxHxW
|
| 117 |
+
sequence_len = optical.shape[0]
|
| 118 |
+
optical = self._pad_sequence(optical) # 61xCxHxW
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
label_path = os.path.join(data_dir, row.label_path) # 3xHxW
|
| 121 |
label = tifffile.imread(label_path)[0] # HxW
|
| 122 |
|
| 123 |
+
# Retrieve date sequences
|
| 124 |
+
dates = self._get_dates(id_patch=id_patch, sat="S2")
|
| 125 |
+
|
| 126 |
sample = {
|
| 127 |
"optical": optical,
|
| 128 |
"optical_channel_wv": optical_channel_wv,
|
| 129 |
+
"dates": dates,
|
| 130 |
+
"sequence_len": sequence_len,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
"label": label,
|
| 132 |
"spatial_resolution": spatial_resolution,
|
| 133 |
}
|
|
|
|
| 135 |
yield f"{index}", sample
|
| 136 |
|
| 137 |
# util functions
|
| 138 |
+
|
| 139 |
def _prepare_meta_patch(self, data_dir):
|
| 140 |
self.meta_patch = gpd.read_file(os.path.join(data_dir, "metadata.geojson"))
|
| 141 |
self.meta_patch.index = self.meta_patch["ID_PATCH"].astype(int)
|
| 142 |
self.meta_patch.sort_index(inplace=True)
|
| 143 |
|
| 144 |
+
def _prepare_date_tables(self, data_dir):
|
| 145 |
+
self.date_tables = {"S2": None}
|
| 146 |
self.date_range = np.array(range(-200, 600))
|
| 147 |
+
for s in ["S2"]:
|
| 148 |
dates = self.meta_patch["dates-{}".format(s)]
|
| 149 |
date_table = pd.DataFrame(
|
| 150 |
index=self.meta_patch.index, columns=self.date_range, dtype=int
|
|
|
|
| 169 |
def _get_dates(self, id_patch, sat="S2"):
|
| 170 |
id_patch = int(id_patch)
|
| 171 |
return self.date_range[np.where(self.date_tables[sat][id_patch] == 1)[0]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
def _pad_sequence(self, optical):
|
| 174 |
+
padding_size = 61 - optical.shape[0]
|
|
|
|
|
|
|
|
|
|
| 175 |
if padding_size == 0:
|
| 176 |
+
return optical
|
| 177 |
|
| 178 |
+
pad = np.zeros((padding_size, *optical.shape[1:]))
|
| 179 |
+
padded_optical = np.concatenate((optical, pad), axis=0)
|
| 180 |
+
return padded_optical
|
| 181 |
|
| 182 |
def _read_image(self, image_path):
|
| 183 |
"""Read tiff image from image_path
|
| 184 |
Args:
|
| 185 |
image_path:
|
| 186 |
Image path to read from
|
| 187 |
+
|
| 188 |
Return:
|
| 189 |
image:
|
| 190 |
C, H, W numpy array image
|