Update SSL4EO-L-Benchmark.py
Browse files- SSL4EO-L-Benchmark.py +54 -2
SSL4EO-L-Benchmark.py
CHANGED
@@ -30,6 +30,8 @@ num_classes = {
|
|
30 |
'oli_sr_nlcd': 21,
|
31 |
'oli_tirs_toa_cdl': 134,
|
32 |
'oli_tirs_toa_nlcd': 21,
|
|
|
|
|
33 |
}
|
34 |
|
35 |
num_channels = {
|
@@ -41,6 +43,8 @@ num_channels = {
|
|
41 |
'oli_sr_nlcd': 7,
|
42 |
'oli_tirs_toa_cdl': 11,
|
43 |
'oli_tirs_toa_nlcd': 11,
|
|
|
|
|
44 |
}
|
45 |
|
46 |
MEAN = [0]
|
@@ -55,6 +59,8 @@ metadata = { # TODO: check if info below is correct or not
|
|
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):
|
@@ -146,13 +152,28 @@ class SSL4EOLBenchmarkDataset(datasets.GeneratorBasedBuilder):
|
|
146 |
spatial_resolution = self.spatial_resolution
|
147 |
|
148 |
data_dir = os.path.join(data_dir, "SSL4EOLBenchmark")
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
metadata = metadata[metadata["split"] == split].reset_index(drop=True)
|
151 |
|
152 |
for index, row in metadata.iterrows():
|
153 |
optical_path = os.path.join(data_dir, row.optical_path)
|
154 |
optical = self._read_image(optical_path).astype(np.float32) # CxHxW
|
155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
label_path = os.path.join(data_dir, row.label_path)
|
157 |
label = self._read_image(label_path).astype(np.int32)
|
158 |
label = self.ordinal_map[label]
|
@@ -180,4 +201,35 @@ class SSL4EOLBenchmarkDataset(datasets.GeneratorBasedBuilder):
|
|
180 |
if len(image.shape) == 3:
|
181 |
image = np.transpose(image, (2, 0, 1))
|
182 |
|
183 |
-
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
'oli_sr_nlcd': 21,
|
31 |
'oli_tirs_toa_cdl': 134,
|
32 |
'oli_tirs_toa_nlcd': 21,
|
33 |
+
"etm_oli_toa_nlcd": 21,
|
34 |
+
"etm_oli_toa_cdl": 134,
|
35 |
}
|
36 |
|
37 |
num_channels = {
|
|
|
43 |
'oli_sr_nlcd': 7,
|
44 |
'oli_tirs_toa_cdl': 11,
|
45 |
'oli_tirs_toa_nlcd': 11,
|
46 |
+
"etm_oli_toa_nlcd": 20,
|
47 |
+
"etm_oli_toa_cdl": 20,
|
48 |
}
|
49 |
|
50 |
MEAN = [0]
|
|
|
59 |
'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},
|
60 |
'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},
|
61 |
'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},
|
62 |
+
"etm_oli_toa_nlcd": {"bands":["B1E", "B2E", "B3E", "B4E", "B5E", "B6LE", "B6HE", "B7E", "B8E", "B1O", "B2O", "B3O", "B4O", "B5O", "B6O", "B7O", "B8O", "B9O", "B10O", "B11O"], "channel_wv": [485.0, 560.0, 660.0, 835.0, 1650.0, 10900.0, 10900.0, 2220.0, 710.0, 443.0, 482.0, 562.0, 655.0, 865.0, 1610.0, 2200.0, 590.0, 1735.0, 10800.0, 12000.0], "mean": MEAN * 20, 'std': STD * 20},
|
63 |
+
"etm_oli_toa_cdl": {"bands":["B1E", "B2E", "B3E", "B4E", "B5E", "B6LE", "B6HE", "B7E", "B8E", "B1O", "B2O", "B3O", "B4O", "B5O", "B6O", "B7O", "B8O", "B9O", "B10O", "B11O"], "channel_wv": [485.0, 560.0, 660.0, 835.0, 1650.0, 10900.0, 10900.0, 2220.0, 710.0, 443.0, 482.0, 562.0, 655.0, 865.0, 1610.0, 2200.0, 590.0, 1735.0, 10800.0, 12000.0], "mean": MEAN * 20, 'std': STD * 20},
|
64 |
}
|
65 |
|
66 |
class SSL4EOLBenchmarkDataset(datasets.GeneratorBasedBuilder):
|
|
|
152 |
spatial_resolution = self.spatial_resolution
|
153 |
|
154 |
data_dir = os.path.join(data_dir, "SSL4EOLBenchmark")
|
155 |
+
if self.config.name in ["etm_oli_toa_nlcd", "etm_oli_toa_cdl"]:
|
156 |
+
product = self.config.name.split('_')[-1] # nlcd / cdl
|
157 |
+
metadata_oli = pd.read_csv(os.path.join(data_dir, f"metadata_oli_tirs_toa_{product}.csv"))
|
158 |
+
metadata_etm = pd.read_csv(os.path.join(data_dir, f"metadata_etm_toa_{product}.csv"))
|
159 |
+
|
160 |
+
metadata = self.sort_and_create_new_csv(metadata_oli, metadata_etm)
|
161 |
+
else:
|
162 |
+
metadata = pd.read_csv(os.path.join(data_dir, f"metadata_{self.config.name}.csv"))
|
163 |
+
|
164 |
metadata = metadata[metadata["split"] == split].reset_index(drop=True)
|
165 |
|
166 |
for index, row in metadata.iterrows():
|
167 |
optical_path = os.path.join(data_dir, row.optical_path)
|
168 |
optical = self._read_image(optical_path).astype(np.float32) # CxHxW
|
169 |
|
170 |
+
try:
|
171 |
+
optical_path_etm = os.path.join(data_dir, row.optical_path_etm)
|
172 |
+
optical_etm = self._read_image(optical_path_etm).astype(np.float32)
|
173 |
+
optical = np.concatenate((optical_etm, optical), axis=0)
|
174 |
+
except:
|
175 |
+
pass
|
176 |
+
|
177 |
label_path = os.path.join(data_dir, row.label_path)
|
178 |
label = self._read_image(label_path).astype(np.int32)
|
179 |
label = self.ordinal_map[label]
|
|
|
201 |
if len(image.shape) == 3:
|
202 |
image = np.transpose(image, (2, 0, 1))
|
203 |
|
204 |
+
return image
|
205 |
+
|
206 |
+
def sort_and_create_new_csv(metadata_oli, metadata_etm):
|
207 |
+
def extract_number(optical_path):
|
208 |
+
return optical_path.split('/')[1]
|
209 |
+
|
210 |
+
metadata_oli['number'] = metadata_oli['optical_path'].apply(extract_number) # a number
|
211 |
+
metadata_etm['number'] = metadata_etm['optical_path'].apply(extract_number) # a number
|
212 |
+
|
213 |
+
metadata_oli = metadata_oli.sort_values('number').reset_index(drop=True)
|
214 |
+
metadata_etm = metadata_etm.sort_values('number').reset_index(drop=True)
|
215 |
+
|
216 |
+
new_rows = []
|
217 |
+
|
218 |
+
i, j = 0, 0
|
219 |
+
while i < len(metadata_oli) and j < len(metadata_etm):
|
220 |
+
if metadata_oli.loc[i, 'number'] == metadata_etm.loc[j, 'number']:
|
221 |
+
new_rows.append({
|
222 |
+
'split': metadata_oli.loc[i, 'split'],
|
223 |
+
'optical_path_etm': metadata_etm.loc[j, 'optical_path'],
|
224 |
+
'optical_path': metadata_oli.loc[i, 'optical_path'],
|
225 |
+
'label_path': metadata_oli.loc[i, 'label_path']
|
226 |
+
})
|
227 |
+
i += 1
|
228 |
+
j += 1
|
229 |
+
elif metadata_oli.loc[i, 'number'] < metadata_etm.loc[j, 'number']:
|
230 |
+
i += 1
|
231 |
+
else:
|
232 |
+
j += 1
|
233 |
+
|
234 |
+
new_df = pd.DataFrame(new_rows, columns=['split', 'optical_path_etm', 'optical_path', 'label_path'])
|
235 |
+
return new_df
|