Datasets:

ArXiv:
yuxuanw8 commited on
Commit
e71f865
·
verified ·
1 Parent(s): f4475e0

Upload SSL4EO-L-Benchmark.py with huggingface_hub

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