wangyi111 commited on
Commit
95360c9
·
verified ·
1 Parent(s): a31cd85

upload eurosat-ms and eurosat-sar

Browse files
eurosat_s1sar/dataset_eu.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ from torch.utils.data import Dataset, DataLoader, Subset
4
+ from pathlib import Path
5
+ import os
6
+ import rasterio
7
+ import cv2
8
+ import pdb
9
+ from pyproj import Transformer
10
+
11
+
12
+ EXTENSIONS = (".jpg", ".jpeg", ".png", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", ".webp")
13
+ ALL_BANDS = ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B09', 'B10', 'B11', 'B12', 'B8A']
14
+ S2A_BANDS = ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B09', 'B11', 'B12', 'B8A']
15
+ RGB_BANDS = ['B04', 'B03', 'B02']
16
+ S1_BANDS = ['VV', 'VH']
17
+
18
+ ### SSL4EO stats
19
+ BAND_STATS = {
20
+ 'mean': {
21
+ 'B01': 1353.72696296,
22
+ 'B02': 1117.20222222,
23
+ 'B03': 1041.8842963,
24
+ 'B04': 946.554,
25
+ 'B05': 1199.18896296,
26
+ 'B06': 2003.00696296,
27
+ 'B07': 2374.00874074,
28
+ 'B08': 2301.22014815,
29
+ 'B8A': 2599.78311111,
30
+ 'B09': 732.18207407,
31
+ 'B10': 12.09952894,
32
+ 'B11': 1820.69659259,
33
+ 'B12': 1118.20259259,
34
+ 'VV': -12.54847273,
35
+ 'VH': -20.19237134
36
+ },
37
+ 'std': {
38
+ 'B01': 897.27143653,
39
+ 'B02': 736.01759721,
40
+ 'B03': 684.77615743,
41
+ 'B04': 620.02902871,
42
+ 'B05': 791.86263829,
43
+ 'B06': 1341.28018273,
44
+ 'B07': 1595.39989386,
45
+ 'B08': 1545.52915718,
46
+ 'B8A': 1750.12066835,
47
+ 'B09': 475.11595216,
48
+ 'B10': 98.26600935,
49
+ 'B11': 1216.48651476,
50
+ 'B12': 736.6981037,
51
+ 'VV': 5.25697717,
52
+ 'VH': 5.91150917
53
+ }
54
+ }
55
+
56
+ # BAND_STATS_S1 = {
57
+ # 'mean': {
58
+ # 'VV': -12.54847273,
59
+ # 'VH': -20.19237134
60
+ # },
61
+ # 'std': {
62
+ # 'VV': 5.25697717,
63
+ # 'VH': 5.91150917
64
+ # }
65
+ # }
66
+
67
+
68
+ def is_valid_file(filename):
69
+ return filename.lower().endswith(EXTENSIONS)
70
+
71
+ def normalize(img, mean, std):
72
+ min_value = mean - 2 * std
73
+ max_value = mean + 2 * std
74
+ img = (img - min_value) / (max_value - min_value) * 255.0
75
+ img = np.clip(img, 0, 255).astype(np.uint8)
76
+ #img = (img - min_value) / (max_value - min_value)
77
+ #img = np.clip(img, 0, 1).astype(np.float32)
78
+ return img
79
+
80
+ class EurosatDataset(Dataset):
81
+
82
+ def __init__(self, root, bands='B2', split='train', transform=None, normalize=False, meta=False):
83
+ self.root = Path(root,split)
84
+ self.transform = transform
85
+ if bands=='B13':
86
+ self.bands = ALL_BANDS
87
+ elif bands=='B12':
88
+ self.bands = S2A_BANDS
89
+ elif bands=='RGB':
90
+ self.bands = RGB_BANDS
91
+ elif bands=='B2':
92
+ self.bands = S1_BANDS
93
+
94
+ self.normalize = normalize
95
+
96
+ self.classes = sorted([d.name for d in self.root.iterdir() if d.is_dir()])
97
+ self.class_to_idx = {cls_name: i for i, cls_name in enumerate(self.classes)}
98
+
99
+ self.samples = []
100
+ self.targets = []
101
+
102
+ #pdb.set_trace()
103
+ for froot, _, fnames in sorted(os.walk(self.root, followlinks=True)):
104
+ for fname in sorted(fnames):
105
+ if is_valid_file(fname):
106
+ path = os.path.join(froot, fname)
107
+ self.samples.append(path)
108
+ target = self.class_to_idx[Path(path).parts[-2]]
109
+ self.targets.append(target)
110
+ #print(self.root)
111
+ #print(f"Found {len(self.samples)} images belonging to {len(self.classes)} classes")
112
+ self.meta = meta
113
+
114
+ def __getitem__(self, index):
115
+ path = self.samples[index]
116
+ target = self.targets[index]
117
+
118
+ with rasterio.open(path) as f:
119
+ if self.bands == ALL_BANDS:
120
+ array = f.read().astype(np.int16)
121
+ elif self.bands == S2A_BANDS:
122
+ array = f.read((1,2,3,4,5,6,7,8,9,11,12,13)).astype(np.int16)
123
+ elif self.bands == RGB_BANDS:
124
+ array = f.read((4,3,2)).astype(np.int16)
125
+ elif self.bands == S1_BANDS:
126
+ array = f.read().astype(np.float32)
127
+
128
+ img = array.transpose(1, 2, 0)
129
+
130
+ if self.meta:
131
+ # get lon, lat, time
132
+ cx,cy = f.xy(f.height // 2, f.width // 2)
133
+ # convert to lon, lat
134
+ crs_transformer = Transformer.from_crs(f.crs, 'epsg:4326')
135
+ lon, lat = crs_transformer.transform(cx,cy)
136
+ # no time
137
+ meta_info = np.array([lon, lat, 0, 0]).astype(np.float32)
138
+ #meta_info = np.array([0, 0, 0, 0]).astype(np.float32)
139
+ #meta_info = np.array([np.nan, np.nan, np.nan, np.nan]).astype(np.float32)
140
+
141
+ channels = []
142
+
143
+ for i,b in enumerate(self.bands):
144
+ ch = img[:,:,i]
145
+ if self.normalize:
146
+ ch = normalize(ch, mean=BAND_STATS['mean'][b], std=BAND_STATS['std'][b])
147
+ elif self.bands == S2A_BANDS:
148
+ ch = (ch / 10000.0 * 255.0).astype('uint8')
149
+
150
+ if b=='B8A': # EuSAT band order is different than SSL4EO
151
+ channels.insert(8,ch)
152
+ else:
153
+ channels.append(ch)
154
+ #img = np.dstack(channels)
155
+ img = np.stack(channels, axis=0).astype('float32') / 255.0
156
+
157
+ if self.transform is not None:
158
+ img = self.transform(img)
159
+
160
+ if self.meta:
161
+ return img, target, meta_info
162
+ else:
163
+ return img, target
164
+
165
+ def __len__(self):
166
+ return len(self.samples)
167
+
168
+
169
+ class Subset(Dataset):
170
+ r"""
171
+ Subset of a dataset at specified indices.
172
+
173
+ Arguments:
174
+ dataset (Dataset): The whole Dataset
175
+ indices (sequence): Indices in the whole set selected for subset
176
+ """
177
+ def __init__(self, dataset, indices, transform=None):
178
+ self.dataset = dataset
179
+ self.indices = indices
180
+ self.transform = transform
181
+
182
+ def __getitem__(self, idx):
183
+ im, target = self.dataset[self.indices[idx]]
184
+ if self.transform:
185
+ im = self.transform(im)
186
+ return im, target
187
+
188
+ def __len__(self):
189
+ return len(self.indices)
eurosat_s1sar/eurosat_sar.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3556e8fe70f2043d5a19ebbdc0ec77fadc4e55633307170e0a10b08fb4f47696
3
+ size 922582952
eurosat_s2ms/dataset_eu.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ from torch.utils.data import Dataset, DataLoader, Subset
4
+ from pathlib import Path
5
+ import os
6
+ import rasterio
7
+ import cv2
8
+ import pdb
9
+ from pyproj import Transformer
10
+
11
+
12
+ EXTENSIONS = (".jpg", ".jpeg", ".png", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", ".webp")
13
+ ALL_BANDS = ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B09', 'B10', 'B11', 'B12', 'B8A']
14
+ S2A_BANDS = ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B09', 'B11', 'B12', 'B8A']
15
+ RGB_BANDS = ['B04', 'B03', 'B02']
16
+ S1_BANDS = ['VV', 'VH']
17
+
18
+ ### SSL4EO stats
19
+ BAND_STATS = {
20
+ 'mean': {
21
+ 'B01': 1353.72696296,
22
+ 'B02': 1117.20222222,
23
+ 'B03': 1041.8842963,
24
+ 'B04': 946.554,
25
+ 'B05': 1199.18896296,
26
+ 'B06': 2003.00696296,
27
+ 'B07': 2374.00874074,
28
+ 'B08': 2301.22014815,
29
+ 'B8A': 2599.78311111,
30
+ 'B09': 732.18207407,
31
+ 'B10': 12.09952894,
32
+ 'B11': 1820.69659259,
33
+ 'B12': 1118.20259259,
34
+ 'VV': -12.54847273,
35
+ 'VH': -20.19237134
36
+ },
37
+ 'std': {
38
+ 'B01': 897.27143653,
39
+ 'B02': 736.01759721,
40
+ 'B03': 684.77615743,
41
+ 'B04': 620.02902871,
42
+ 'B05': 791.86263829,
43
+ 'B06': 1341.28018273,
44
+ 'B07': 1595.39989386,
45
+ 'B08': 1545.52915718,
46
+ 'B8A': 1750.12066835,
47
+ 'B09': 475.11595216,
48
+ 'B10': 98.26600935,
49
+ 'B11': 1216.48651476,
50
+ 'B12': 736.6981037,
51
+ 'VV': 5.25697717,
52
+ 'VH': 5.91150917
53
+ }
54
+ }
55
+
56
+ # BAND_STATS_S1 = {
57
+ # 'mean': {
58
+ # 'VV': -12.54847273,
59
+ # 'VH': -20.19237134
60
+ # },
61
+ # 'std': {
62
+ # 'VV': 5.25697717,
63
+ # 'VH': 5.91150917
64
+ # }
65
+ # }
66
+
67
+
68
+ def is_valid_file(filename):
69
+ return filename.lower().endswith(EXTENSIONS)
70
+
71
+ def normalize(img, mean, std):
72
+ min_value = mean - 2 * std
73
+ max_value = mean + 2 * std
74
+ img = (img - min_value) / (max_value - min_value) * 255.0
75
+ img = np.clip(img, 0, 255).astype(np.uint8)
76
+ #img = (img - min_value) / (max_value - min_value)
77
+ #img = np.clip(img, 0, 1).astype(np.float32)
78
+ return img
79
+
80
+ class EurosatDataset(Dataset):
81
+
82
+ def __init__(self, root, bands='B2', split='train', transform=None, normalize=False, meta=False):
83
+ self.root = Path(root,split)
84
+ self.transform = transform
85
+ if bands=='B13':
86
+ self.bands = ALL_BANDS
87
+ elif bands=='B12':
88
+ self.bands = S2A_BANDS
89
+ elif bands=='RGB':
90
+ self.bands = RGB_BANDS
91
+ elif bands=='B2':
92
+ self.bands = S1_BANDS
93
+
94
+ self.normalize = normalize
95
+
96
+ self.classes = sorted([d.name for d in self.root.iterdir() if d.is_dir()])
97
+ self.class_to_idx = {cls_name: i for i, cls_name in enumerate(self.classes)}
98
+
99
+ self.samples = []
100
+ self.targets = []
101
+
102
+ #pdb.set_trace()
103
+ for froot, _, fnames in sorted(os.walk(self.root, followlinks=True)):
104
+ for fname in sorted(fnames):
105
+ if is_valid_file(fname):
106
+ path = os.path.join(froot, fname)
107
+ self.samples.append(path)
108
+ target = self.class_to_idx[Path(path).parts[-2]]
109
+ self.targets.append(target)
110
+ #print(self.root)
111
+ #print(f"Found {len(self.samples)} images belonging to {len(self.classes)} classes")
112
+ self.meta = meta
113
+
114
+ def __getitem__(self, index):
115
+ path = self.samples[index]
116
+ target = self.targets[index]
117
+
118
+ with rasterio.open(path) as f:
119
+ if self.bands == ALL_BANDS:
120
+ array = f.read().astype(np.int16)
121
+ elif self.bands == S2A_BANDS:
122
+ array = f.read((1,2,3,4,5,6,7,8,9,11,12,13)).astype(np.int16)
123
+ elif self.bands == RGB_BANDS:
124
+ array = f.read((4,3,2)).astype(np.int16)
125
+ elif self.bands == S1_BANDS:
126
+ array = f.read().astype(np.float32)
127
+
128
+ img = array.transpose(1, 2, 0)
129
+
130
+ if self.meta:
131
+ # get lon, lat, time
132
+ cx,cy = f.xy(f.height // 2, f.width // 2)
133
+ # convert to lon, lat
134
+ crs_transformer = Transformer.from_crs(f.crs, 'epsg:4326')
135
+ lon, lat = crs_transformer.transform(cx,cy)
136
+ # no time
137
+ meta_info = np.array([lon, lat, 0, 0]).astype(np.float32)
138
+ #meta_info = np.array([0, 0, 0, 0]).astype(np.float32)
139
+ #meta_info = np.array([np.nan, np.nan, np.nan, np.nan]).astype(np.float32)
140
+
141
+ channels = []
142
+
143
+ for i,b in enumerate(self.bands):
144
+ ch = img[:,:,i]
145
+ if self.normalize:
146
+ ch = normalize(ch, mean=BAND_STATS['mean'][b], std=BAND_STATS['std'][b])
147
+ elif self.bands == S2A_BANDS:
148
+ ch = (ch / 10000.0 * 255.0).astype('uint8')
149
+
150
+ if b=='B8A': # EuSAT band order is different than SSL4EO
151
+ channels.insert(8,ch)
152
+ else:
153
+ channels.append(ch)
154
+ #img = np.dstack(channels)
155
+ img = np.stack(channels, axis=0).astype('float32') / 255.0
156
+
157
+ if self.transform is not None:
158
+ img = self.transform(img)
159
+
160
+ if self.meta:
161
+ return img, target, meta_info
162
+ else:
163
+ return img, target
164
+
165
+ def __len__(self):
166
+ return len(self.samples)
167
+
168
+
169
+ class Subset(Dataset):
170
+ r"""
171
+ Subset of a dataset at specified indices.
172
+
173
+ Arguments:
174
+ dataset (Dataset): The whole Dataset
175
+ indices (sequence): Indices in the whole set selected for subset
176
+ """
177
+ def __init__(self, dataset, indices, transform=None):
178
+ self.dataset = dataset
179
+ self.indices = indices
180
+ self.transform = transform
181
+
182
+ def __getitem__(self, idx):
183
+ im, target = self.dataset[self.indices[idx]]
184
+ if self.transform:
185
+ im = self.transform(im)
186
+ return im, target
187
+
188
+ def __len__(self):
189
+ return len(self.indices)
eurosat_s2ms/eurosat_ms.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:201940af1e4e2f40ae2b26491f059a7efd389233ffcfd66a5e27727d2fe92745
3
+ size 2027392300