-
Notifications
You must be signed in to change notification settings - Fork 2
/
datasets.py
602 lines (501 loc) · 21.2 KB
/
datasets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
from __future__ import print_function, division
import os
import tifffile
import numpy as np
from torch.utils.data import Dataset
from torchvision import transforms
import csv
from PIL import Image
import torch
from utils import get_band
from skimage.transform import resize
"""
EuroSat, AIS ship detection dataset Agenium Space, BigEarthNet (numpy version) and SEN12MS-A/RA dataloaders
"""
class EuroSAT12(Dataset):
"""EuroSAT 12 bands multispectral dataset."""
def __init__(self,
input_dir: str, bands_idx=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12],
transform: transforms.Compose = None):
self.input_dir = input_dir
# create a dictionary to store the path, filenames and associated targets for every samples
keyDict = {"path", "filename", "label"}
self.dataset = dict([(key, []) for key in keyDict])
self.target_list = []
self.transform = transform
dir_list = sorted(os.listdir(self.input_dir))
for e in dir_list:
if e not in self.target_list:
self.target_list.append(e)
sub_path = os.path.join(self.input_dir, e)
subdir_list = sorted(os.listdir(sub_path))
for h in subdir_list:
self.dataset["path"].append(sub_path + '/' + h)
self.dataset["filename"].append(e + '/' + h)
self.dataset["label"].append(self.target_list.index(e))
self.bands_idx = bands_idx
def __len__(self):
"""Returns the length of the dataset.
"""
return len(self.dataset["filename"])
def __getitem__(self, index: int):
"""Returns (sample, target, fname) of item at index.
Args:
index:
Index of the queried item.
Returns:
The image, target, and filename of the item at index.
"""
fname = self.dataset["filename"][index]
img_dir = self.dataset["path"][index]
target = self.dataset["label"][index]
img = tifffile.imread(img_dir)
# get image sample with the given multispectral indices
idx = self.bands_idx
h, w, c = img.shape
sample = np.zeros((h, w, len(idx)), dtype=np.float32)
index = 0
for i in idx:
channel = img[:, :, i]
# clip and normalize the band
mi, ma = np.nanpercentile(channel, (3, 97))
norm_chan = np.clip((channel - mi) / (ma - mi + 0.0001), 0, 1) # +0.0001 to avoid /0 error
sample[:, :, index] = norm_chan
index += 1
if self.transform:
sample = self.transform(sample)
return sample, target, fname
class SEN12MS(Dataset):
"""SEN12MS 12 bands dataset, without cutting smaller patches.
It is adapted for the RA pretext task since it has large region patches
"""
def __init__(self,
input_dir: str, bands_idx=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12],
transform: transforms.Compose = None):
self.input_dir = input_dir
# create a dictionary to store the path, filenames and associated targets for every samples
keyDict = {"path", "filename", "label"}
self.dataset = dict([(key, []) for key in keyDict])
self.target_list = []
self.transform = transform
dir_list = sorted(os.listdir(self.input_dir))
for e in dir_list:
sub_path = os.path.join(self.input_dir, e)
subdir_list = sorted(os.listdir(sub_path))
for h in subdir_list:
subsub_path = os.path.join(sub_path, h)
subsubdir_list = sorted(os.listdir(subsub_path))
for el in subsubdir_list:
self.dataset["path"].append(subsub_path + '/' + el)
self.dataset["filename"].append(e + '/' + h + '/el')
self.dataset["label"].append(0) # we don't care about the labels
self.bands_idx = bands_idx
def __len__(self):
"""Returns the length of the dataset.
"""
return len(self.dataset["filename"])
def __getitem__(self, index: int):
"""Returns (sample, target, fname) of item at index.
Args:
index:
Index of the queried item.
Returns:
The image, target, and filename of the item at index.
"""
fname = self.dataset["filename"][index]
img_dir = self.dataset["path"][index]
target = self.dataset["label"][index]
img = tifffile.imread(img_dir)
idx = self.bands_idx
# idx = list(np.arange(13))
# idx.pop(10)
# image = img[:,:,self.bands_idx]
# sample = image
h, w, c = img.shape
sample = np.zeros((h, w, len(idx)), dtype=np.float32)
index = 0
for i in idx:
channel = img[:, :, i]
mi, ma = np.nanpercentile(channel, (3, 97))
norm_chan = np.clip((channel - mi) / (ma - mi + 0.0001), 0, 1)
# norm_chan = np.clip(channel / np.max(channel), 0, 1)
sample[:, :, index] = norm_chan
index += 1
if self.transform:
sample = self.transform(sample)
return sample, target, fname
class SEN12MS_cut(Dataset):
"""SEN12MS 12 bands dataset, with smaller patches.
It is adapted for the A (simple data augmentations) pretext task
"""
def __init__(self,
input_dir: str, bands_idx=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12],
transform: transforms.Compose = None):
self.input_dir = input_dir
# create a dictionary to store the path, filenames and associated targets for every samples
keyDict = {"path", "filename", "label"}
self.dataset = dict([(key, []) for key in keyDict])
self.target_list = []
self.transform = transform
dir_list = sorted(os.listdir(self.input_dir))
for e in dir_list:
sub_path = os.path.join(self.input_dir, e)
subdir_list = sorted(os.listdir(sub_path))
for h in subdir_list:
subsub_path = os.path.join(sub_path, h)
subsubdir_list = sorted(os.listdir(subsub_path))
for el in subsubdir_list:
self.dataset["path"].append(subsub_path + '/' + el)
self.dataset["filename"].append(e + '/' + h + '/el')
self.dataset["label"].append(0)
self.bands_idx = bands_idx
self.dataset_size = len(self.dataset["filename"])
self.len_data = 16 * self.dataset_size
# within the large original patch of size 256*256, we can get 16 smaller patches of size 64*64.
# The coordinates are the following:
self.patch_coord = [[0, 64, 0, 64], [0, 64, 64, 128], [0, 64, 128, 192], [0, 64, 192, 256],
[64, 128, 0, 64], [64, 128, 64, 128], [64, 128, 128, 192], [64, 128, 192, 256],
[128, 192, 0, 64], [128, 192, 64, 128], [128, 192, 128, 192], [128, 192, 192, 256],
[192, 256, 0, 64], [192, 256, 64, 128], [192, 256, 128, 192], [192, 256, 192, 256]]
def __len__(self):
"""Returns the length of the dataset.
"""
return self.len_data
def __getitem__(self, index: int):
"""Returns (sample, target, fname) of item at index.
Args:
index:
Index of the queried item.
Returns:
The image, target, and filename of the item at index.
"""
img_indx = index
patch_id = -1
while img_indx >= 0:
patch_id += 1
img_indx -= self.dataset_size
img_indx += self.dataset_size
patch_coo = self.patch_coord[patch_id]
fname = self.dataset["filename"][img_indx]
img_dir = self.dataset["path"][img_indx]
target = self.dataset["label"][img_indx]
img = tifffile.imread(img_dir)
idx = self.bands_idx
sample = np.zeros((64, 64, len(idx)), dtype=np.float32)
index = 0
for i in idx:
channel = img[patch_coo[0]:patch_coo[1], patch_coo[2]:patch_coo[3], i]
mi, ma = np.nanpercentile(channel, (3, 97))
norm_chan = np.clip((channel - mi) / (ma - mi + 0.0001), 0, 1)
sample[..., index] = norm_chan
index += 1
if self.transform:
sample = self.transform(sample)
return sample, target, fname
class BigEarthNet(Dataset):
"""BigEarthNet 12 bands dataset."""
def __init__(self,
input_dir: str,
transform: transforms.Compose = None):
self.input_dir = input_dir
keyDict = {"path", "filename", "label"}
self.dataset = dict([(key, []) for key in keyDict])
self.transform = transform
dir_list = sorted(os.listdir(self.input_dir))
for e in dir_list:
sub_path = os.path.join(self.input_dir, e)
self.dataset["path"].append(sub_path)
self.dataset["filename"].append(e)
self.dataset["label"].append(0)
def __len__(self):
"""Returns the length of the dataset.
"""
return len(self.dataset["filename"])
def __getitem__(self, index: int):
"""Returns (sample, target, fname) of item at index.
Args:
index:
Index of the queried item.
Returns:
The image, target, and filename of the item at index.
"""
Bands = ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B09', 'B11', 'B12']
fname = self.dataset["filename"][index]
img_dir = self.dataset["path"][index]
print(img_dir)
target = self.dataset["label"][index]
img_l = get_band(Bands, img_dir)
h, w = img_l[2].shape
sample = np.zeros((h, w, len(Bands)), dtype=np.float32)
for band in range(len(img_l)):
channel = img_l[band]
channel = resize(channel, (h, w))
mi, ma = np.nanpercentile(channel, (3, 97))
norm_chan = np.clip((channel - mi) / (ma - mi + 0.00001), 0, 1)
sample[:, :, band] = norm_chan
if self.transform:
sample = self.transform(sample)
return sample, target, fname
class BigEarthNet_numpy(Dataset):
"""BigEarthNet 12 bands dataset from numpy arrays (in order to speed up the training process."""
def __init__(self,
input_dir: str, idx=[1, 2, 3, 10, 11],
transform: transforms.Compose = None):
self.input_dir = input_dir
self.idx = idx
keyDict = {"path", "filename", "label"}
self.dataset = dict([(key, []) for key in keyDict])
self.transform = transform
dir_list = sorted(os.listdir(self.input_dir))
for e in dir_list:
sub_path = os.path.join(self.input_dir, e)
self.dataset["path"].append(sub_path)
self.dataset["filename"].append(e)
self.dataset["label"].append(0)
self.good_ind = 0
def __len__(self):
"""Returns the length of the dataset.
"""
return len(self.dataset["filename"])
def __getitem__(self, index: int):
"""Returns (sample, target, fname) of item at index.
Args:
index:
Index of the queried item.
Returns:
The image, target, and filename of the item at index.
"""
fname = self.dataset["filename"][index]
img_dir = self.dataset["path"][index]
target = self.dataset["label"][index]
sample = np.load(img_dir)
idx = self.idx
m, n, c = sample.shape
bands5 = np.zeros((m, n, len(idx)))
index = 0
for i in idx:
mi, ma = np.nanpercentile(sample[:, :, i], (3, 97))
norm_chan = np.clip((sample[:, :, i] - mi) / (ma - mi + 0.00001), 0, 1)
bands5[:, :, index] = norm_chan
index += 1
# if only nan values, debug by taking a working sample. Maybe this isn't necessary
bands5 = np.nan_to_num(bands5, nan=0)
if np.sum(bands5) == 0:
fname = self.dataset["filename"][self.good_ind]
img_dir = self.dataset["path"][self.good_ind]
target = self.dataset["label"][self.good_ind]
sample = np.load(img_dir)
idx = self.idx
m, n, c = sample.shape
bands5 = np.zeros((m, n, len(idx)))
index = 0
for i in idx:
mi, ma = np.nanpercentile(sample[:, :, i], (3, 97))
norm_chan = np.clip((sample[:, :, i] - mi) / (ma - mi + 0.00001), 0, 1)
bands5[:, :, index] = norm_chan
index += 1
else:
self.good_ind = index
if self.transform:
bands5 = self.transform(bands5)
return bands5, target, fname
class AIS_sentinel2(Dataset):
"""AIS Sentinel 2 RGB bands dataset.
A csv file is used to indicate which sample to take for pretext or downstream task.
"""
def __init__(self,
data_dir: str, csv_dir: str,
transform: transforms.Compose = None):
self.data_dir = data_dir
self.csv_dir = csv_dir
keyDict = {"path", "filename", "label"}
self.dataset = dict([(key, []) for key in keyDict])
self.transform = transform
with open(self.csv_dir) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['ImageId'] != '':
self.dataset["path"].append(self.data_dir + '/' + row['ImageId'])
self.dataset["filename"].append(row['ImageId'])
self.dataset["label"].append(int(row['labels']))
def __len__(self):
"""Returns the length of the dataset.
"""
return len(self.dataset["filename"])
def __getitem__(self, index: int):
"""Returns (sample, target, fname) of item at index.
Args:
index:
Index of the queried item.
Returns:
The image, target, and filename of the item at index.
"""
fname = self.dataset["filename"][index]
sample = Image.open(self.dataset["path"][index])
target = self.dataset["label"][index]
if self.transform:
sample = self.transform(sample)
return sample, target, fname
class AIS_sentinel2_5(Dataset):
"""AIS Sentinel 2 multispectral 5 bands dataset.
A csv file is used to indicate which sample to take for pretext or downstream task.
"""
def __init__(self,
data_dir: str, csv_dir: str,
transform: transforms.Compose = None):
self.data_dir = data_dir
self.csv_dir = csv_dir
keyDict = {"path", "filename", "label"}
self.dataset = dict([(key, []) for key in keyDict])
self.transform = transform
with open(self.csv_dir) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['ImageId'] != '':
self.dataset["path"].append(self.data_dir + '/' + row['ImageId'])
self.dataset["filename"].append(row['ImageId'])
self.dataset["label"].append(int(row['labels']))
def __len__(self):
"""Returns the length of the dataset.
"""
return len(self.dataset["filename"])
def __getitem__(self, index: int):
"""Returns (sample, target, fname) of item at index.
Args:
index:
Index of the queried item.
Returns:
The image, target, and filename of the item at index.
"""
fname = self.dataset["filename"][index]
sample = tifffile.imread(self.dataset["path"][index]).astype(np.float32)
_, _, c = sample.shape
for i in range(c):
sample[:, :, i] = sample[:, :, i] / 255.
target = self.dataset["label"][index]
if self.transform:
sample = self.transform(sample)
return sample, target, fname
class s2ship_patch(Dataset):
"""s2ship 12 bands training dataset (patches).
some special args here :
- excl_img_id : imgs ids to exclude (like test image, or in case wanting to train on a smaller amount of data)
- min_data : 3rd percentile calculated over S2-SHIPS dataset
- min_data : 97th percentile calculated over S2-SHIPS dataset
"""
def __init__(self,
input_dir: str,
excl_img_id, min_data, max_data,
transform: transforms.Compose = None, indices=[1, 2, 3, 10, 11]):
name_list = ['rome', 'suez1', 'suez2', 'suez3', 'suez4',
'suez5',
'suez6', 'brest1', 'panama', 'toulon', 'marseille',
'portsmouth', 'rotterdam1', 'rotterdam2', 'rotterdam3',
'southampton'] # name of original tiles
self.input_dir = input_dir
self.indices = indices
keyDict = {"filename"}
self.dataset = dict([(key, []) for key in keyDict])
self.transform = transform
dir_list = sorted(os.listdir(self.input_dir))
# exclude images if needed (e.g. test image)
for e in dir_list:
if excl_img_id is not None:
flag = 0
for el in excl_img_id:
if name_list[el - 1] in e:
flag = 1
# if this patch does not require to be excluded, add it to the list
if flag == 0:
self.dataset["filename"].append(e)
self.mi = min_data
self.ma = max_data
def __len__(self):
"""Returns the length of the dataset.
"""
return len(self.dataset["filename"])
def __getitem__(self, index: int):
"""Returns (sample, target, fname) of item at index.
Args:
index:
Index of the queried item.
Returns:
The image, target, and filename of the item at index.
"""
fname = self.dataset["filename"][index]
img_and_label = np.load(self.input_dir + '/' + fname, allow_pickle=True)
sample = img_and_label.item().get("data")
m, n, c = sample.shape
bands5 = np.zeros((m, n, len(self.indices)))
index = 0
for i in self.indices:
# mi, ma = np.nanpercentile(sample[:,:,i], (3, 97)) # if calculated on each patch
# normalisation
norm_chan = np.clip((sample[:, :, i] - self.mi[i]) / (self.ma[i] - self.mi[i] + 0.000001), 0, 1)
bands5[:, :, index] = norm_chan
index += 1
target = img_and_label.item().get("label")
if self.transform:
bands5, target = self.transform(bands5, target)
target = torch.squeeze(target)
return bands5, target
class s2ship_patch_test(Dataset):
"""s2ship 12 bands test dataset (full tiles).
"""
def __init__(self,
input_dir: str,
transform: transforms.Compose = None, indices=[1, 2, 3, 10, 11]):
self.input_dir = input_dir
self.indices = indices
keyDict = {"filename"}
self.dataset = dict([(key, []) for key in keyDict])
self.transform = transform
# calculate the 3rd and 97th percentile for further image processing
min_data = 999999 * np.ones((12, 1))
max_data = -999999 * np.ones((12, 1))
dir_list = sorted(os.listdir(self.input_dir))
for e in dir_list:
self.dataset["filename"].append(e)
img_and_label = np.load(self.input_dir + '/' + e, allow_pickle=True)
sample = img_and_label.item().get("data")
for i in self.indices:
mi, ma = np.nanpercentile(sample[:, :, i], (3, 97))
if mi < min_data[i]:
min_data[i] = mi
if ma > max_data[i]:
max_data[i] = ma
self.mi = min_data
self.ma = max_data
def get_min_max(self):
"""Returns 3rd and 97th percentiles of the dataset for further image processing.
"""
return self.mi, self.ma
def __len__(self):
"""Returns the length of the dataset.
"""
return len(self.dataset["filename"])
def __getitem__(self, index: int):
"""Returns (sample, target, fname) of item at index.
Args:
index:
Index of the queried item.
Returns:
The image, target, and filename of the item at index.
"""
fname = self.dataset["filename"][index]
index_img = fname[:2]
img_and_label = np.load(self.input_dir + '/' + fname, allow_pickle=True)
sample = img_and_label.item().get("data")
m, n, c = sample.shape
bands5 = np.zeros((m, n, len(self.indices)))
index = 0
for i in self.indices:
# mi, ma = np.nanpercentile(sample[:, :, i], (3, 97)) # if patch per patch normalisation
norm_chan = np.clip((sample[:, :, i] - self.mi[i]) / (self.ma[i] - self.mi[i] + 0.000001), 0, 1)
bands5[:, :, index] = norm_chan
index += 1
target = img_and_label.item().get("label")
if self.transform:
bands5, target = self.transform(bands5, target)
target = torch.squeeze(target)
return bands5, target, int(index_img)