Datasets:

ArXiv:
yuxuanw8 commited on
Commit
3532fee
·
verified ·
1 Parent(s): e424a0e

Upload SSL4EO-L-Benchmark.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. SSL4EO-L-Benchmark.py +177 -0
SSL4EO-L-Benchmark.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import shutil
4
+ import datasets
5
+ import tifffile
6
+
7
+ import pandas as pd
8
+ import numpy as np
9
+
10
+ from torchgeo.datasets.cdl import CDL
11
+ from torchgeo.datasets.nlcd import NLCD
12
+
13
+ CMAPS = {
14
+ 'nlcd': NLCD.cmap,
15
+ 'cdl': CDL.cmap,
16
+ }
17
+
18
+ S2_MEAN = [752.40087073, 884.29673756, 1144.16202635, 1297.47289228, 1624.90992062, 2194.6423161, 2422.21248945, 2581.64687018, 2368.51236873, 1805.06846033]
19
+
20
+ S2_STD = [1108.02887453, 1155.15170768, 1183.6292542, 1368.11351514, 1370.265037, 1355.55390699, 1416.51487101, 1439.3086061, 1455.52084939, 1343.48379601]
21
+
22
+ subset_names = ["etm_sr_cdl", "etm_sr_nlcd", "etm_toa_cdl", "etm_toa_nlcd", "oli_sr_cdl", "oli_sr_nlcd", "oli_tirs_toa_cdl", "oli_tirs_toa_nlcd"]
23
+
24
+ num_classes = {
25
+ 'etm_sr_cdl': 134,
26
+ 'etm_sr_nlcd': 21,
27
+ 'etm_toa_cdl': 134,
28
+ 'etm_toa_nlcd': 21,
29
+ 'oli_sr_cdl': 134,
30
+ 'oli_sr_nlcd': 21,
31
+ 'oli_tirs_toa_cdl': 134,
32
+ 'oli_tirs_toa_nlcd': 21,
33
+ }
34
+
35
+ num_channels = {
36
+ 'etm_sr_cdl': 6,
37
+ 'etm_sr_nlcd': 6,
38
+ 'etm_toa_cdl': 9,
39
+ 'etm_toa_nlcd': 9,
40
+ 'oli_sr_cdl': 7,
41
+ 'oli_sr_nlcd': 7,
42
+ 'oli_tirs_toa_cdl': 11,
43
+ 'oli_tirs_toa_nlcd': 11,
44
+ }
45
+
46
+ MEAN = [0]
47
+ STD = [0]
48
+
49
+ metadata = { # TODO: check if info below is correct or not
50
+ 'etm_sr_cdl': {"bands":["B1", "B2", "B3", "B4", "B5", "B7"], "channel_wv": [485.0, 560.0, 660.0, 835.0, 1650.0, 2220.0], "mean": MEAN * 6, 'std': STD * 6}, # B6 (Thermal Band) and B8 (Panchromatic Band) are excluded
51
+ 'etm_sr_nlcd': {"bands":["B1", "B2", "B3", "B4", "B5", "B7"], "channel_wv": [485.0, 560.0, 660.0, 835.0, 1650.0, 2220.0], "mean": MEAN * 6, 'std': STD * 6},
52
+ 'etm_toa_cdl': {"bands":["B1", "B2", "B3", "B4", "B5", "B6L", "B6H", "B7", "B8"], "channel_wv": [485.0, 560.0, 660.0, 835.0, 1650.0, 10900.0, 10900.0, 2220.0, 710.0], "mean": MEAN * 9, 'std': STD * 9},
53
+ 'etm_toa_nlcd': {"bands":["B1", "B2", "B3", "B4", "B5", "B6L", "B6H", "B7", "B8"], "channel_wv": [485.0, 560.0, 660.0, 835.0, 1650.0, 10900.0, 10900.0, 2220.0, 710.0], "mean": MEAN * 9, 'std': STD * 9},
54
+ 'oli_sr_cdl': {"bands":["B1", "B2", "B3", "B4", "B5", "B6", "B7"], "channel_wv": [443.0, 482.0, 562.0, 655.0, 865.0, 1610.0, 2200.0], "mean": MEAN * 7, 'std': STD * 7},
55
+ 'oli_sr_nlcd': {"bands":["B1", "B2", "B3", "B4", "B5", "B6", "B7"], "channel_wv": [443.0, 482.0, 562.0, 655.0, 865.0, 1610.0, 2200.0], "mean": MEAN * 7, 'std': STD * 7},
56
+ 'oli_tirs_toa_cdl': {"bands":["B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10", "B11"], "channel_wv": [443.0, 482.0, 562.0, 655.0, 865.0, 1610.0, 2200.0, 590.0, 1735.0, 10800.0, 12000.0], "mean": MEAN * 11, 'std': STD * 11},
57
+ 'oli_tirs_toa_nlcd': {"bands":["B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10", "B11"], "channel_wv": [443.0, 482.0, 562.0, 655.0, 865.0, 1610.0, 2200.0, 590.0, 1735.0, 10800.0, 12000.0], "mean": MEAN * 11, 'std': STD * 11},
58
+ }
59
+
60
+ class SSL4EOLBenchmarkDataset(datasets.GeneratorBasedBuilder):
61
+ VERSION = datasets.Version("1.0.0")
62
+
63
+ DATA_URL = "https://huggingface.co/datasets/GFM-Bench/SSL4EO-L-Benchmark/resolve/main/SSL4EOLBenchmark.zip"
64
+
65
+ SIZE = HEIGHT = WIDTH = 264
66
+
67
+ spatial_resolution = 30
68
+
69
+ BUILDER_CONFIGS = [datasets.BuilderConfig(name=name) for name in subset_names]
70
+
71
+ DEFAULT_CONFIG_NAME = "etm_sr_cdl"
72
+
73
+ def __init__(self, *args, **kwargs):
74
+ name = kwargs.get('config_name', None)
75
+ print(f"config_name: {name}")
76
+ self.NUM_CLASSES = num_classes[name] if name else num_classes['etm_sr_cdl']
77
+ self.NUM_CHANNELS = num_channels[name] if name else num_channels['etm_sr_cdl']
78
+ self.metadata = metadata[name] if name else metadata['etm_sr_cdl']
79
+
80
+ product = name.split('_')[-1]
81
+ cmap = CMAPS[product]
82
+ classes = list(cmap.keys())
83
+ self.ordinal_map = np.zeros(max(cmap.keys()) + 1, dtype=np.int64)
84
+ for v, k in enumerate(classes):
85
+ self.ordinal_map[k] = v
86
+
87
+ super().__init__(*args, **kwargs)
88
+
89
+ def _info(self):
90
+ metadata = self.metadata
91
+ metadata['size'] = self.SIZE
92
+ metadata['num_classes'] = self.NUM_CLASSES
93
+ metadata['spatial_resolution'] = self.spatial_resolution
94
+ return datasets.DatasetInfo(
95
+ description=json.dumps(metadata),
96
+ features=datasets.Features({
97
+ "optical": datasets.Array3D(shape=(self.NUM_CHANNELS, self.HEIGHT, self.WIDTH), dtype="float32"),
98
+ "label": datasets.Array2D(shape=(self.HEIGHT, self.WIDTH), dtype="int32"),
99
+ "spatial_resolution": datasets.Value("int32"),
100
+ }),
101
+ )
102
+
103
+ def _split_generators(self, dl_manager):
104
+ if isinstance(self.DATA_URL, list):
105
+ downloaded_files = dl_manager.download(self.DATA_URL)
106
+ combined_file = os.path.join(dl_manager.download_config.cache_dir, "combined.tar.gz")
107
+ with open(combined_file, 'wb') as outfile:
108
+ for part_file in downloaded_files:
109
+ with open(part_file, 'rb') as infile:
110
+ shutil.copyfileobj(infile, outfile)
111
+ data_dir = dl_manager.extract(combined_file)
112
+ os.remove(combined_file)
113
+ else:
114
+ data_dir = dl_manager.download_and_extract(self.DATA_URL)
115
+
116
+ return [
117
+ datasets.SplitGenerator(
118
+ name="train",
119
+ gen_kwargs={
120
+ "split": 'train',
121
+ "data_dir": data_dir,
122
+ },
123
+ ),
124
+ datasets.SplitGenerator(
125
+ name="val",
126
+ gen_kwargs={
127
+ "split": 'val',
128
+ "data_dir": data_dir,
129
+ },
130
+ ),
131
+ datasets.SplitGenerator(
132
+ name="test",
133
+ gen_kwargs={
134
+ "split": 'test',
135
+ "data_dir": data_dir,
136
+ },
137
+ )
138
+ ]
139
+
140
+ def _generate_examples(self, split, data_dir):
141
+ spatial_resolution = self.spatial_resolution
142
+
143
+ data_dir = os.path.join(data_dir, "SSL4EOLBenchmark")
144
+ metadata = pd.read_csv(os.path.join(data_dir, f"metadata_{self.config.name}.csv"))
145
+ metadata = metadata[metadata["split"] == split].reset_index(drop=True)
146
+
147
+ for index, row in metadata.iterrows():
148
+ optical_path = os.path.join(data_dir, row.optical_path)
149
+ optical = self._read_image(optical_path).astype(np.float32) # CxHxW
150
+
151
+ label_path = os.path.join(data_dir, row.label_path)
152
+ label = self._read_image(label_path).astype(np.int32)
153
+ label = self.ordinal_map[label]
154
+
155
+ sample = {
156
+ "optical": optical,
157
+ "label": label,
158
+ "spatial_resolution": spatial_resolution,
159
+ }
160
+
161
+ yield f"{index}", sample
162
+
163
+ def _read_image(self, image_path):
164
+ """Read tiff image from image_path
165
+ Args:
166
+ image_path:
167
+ Image path to read from
168
+
169
+ Return:
170
+ image:
171
+ C, H, W numpy array image
172
+ """
173
+ image = tifffile.imread(image_path)
174
+ if len(image.shape) == 3:
175
+ image = np.transpose(image, (2, 0, 1))
176
+
177
+ return image