-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdataset.py
375 lines (255 loc) · 10.9 KB
/
dataset.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
import math
import os
import random
import cv2
import lmdb
import numpy as np
import pyarrow as pa
import torch
import torch.utils.data as datautils
from joblib import load
from PIL import Image
from skimage import color
def loads_pyarrow(buff):
return pa.deserialize(buff)
def my_resize_4(img, shape):
out = np.zeros((shape[0], shape[1], 4))
out[:, :, 0:3] = cv2.resize(img[:, :, 0:3], shape)
out[:, :, 3] = cv2.resize(img[:, :, 3], shape)
return out
def my_resize_6(img, shape):
out = np.zeros((shape[0], shape[1], 6))
out[:, :, 0:3] = cv2.resize(img[:, :, 0:3], shape)
out[:, :, 3:6] = cv2.resize(img[:, :, 3:6], shape)
return out
def my_resize_10(img, shape):
out = np.zeros((shape[0], shape[1], 10))
out[:, :, 0:3] = cv2.resize(img[:, :, 0:3], shape)
out[:, :, 3:6] = cv2.resize(img[:, :, 3:6], shape)
out[:, :, 6:9] = cv2.resize(img[:, :, 6:9], shape)
out[:, :, 9] = cv2.resize(img[:, :, 9], shape)
return out
def crop(img, top, left, height, width):
return img[top:top+height, left:left+width, :]
def resized_crop(img, top, left, height, width, size):
img = crop(img, top, left, height, width)
img = my_resize_10(img, size)
return img
class MultispectralResize(object):
def __init__(self, size):
self.size = size
def __call__(self, img):
return my_resize_10(img, self.size)
class MultispectralRandomHorizontalFlip(object):
def __init__(self, p = 0.5):
self. p = p
def __call__(self, img):
if random.random() < self.p:
return img[:, ::-1, :]
return img
class ScalerPCA(object):
def __init__(self, path, use_pca=True):
self.scaler = load(path + '/' + 'scaler.pkl')
self.pca = load(path + '/' + 'pca.pkl')
self.use_pca = use_pca
def __call__(self, img):
img *= 30000
img_shape = img.shape
img = img.reshape((img_shape[0] * img_shape[1], img_shape[2]))
img = self.scaler.transform(img)
if self.use_pca:
img = self.pca.transform(img)
img = img.reshape((img_shape[0], img_shape[1], img_shape[2]))
return img
class MultispectralRandomResizedCrop(object):
def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.)):
if isinstance(size, (tuple, list)):
self.size = size
else:
self.size = (size, size)
self.scale = scale
self.ratio = ratio
@staticmethod
def get_params(img, scale, ratio):
width, height = img.shape[0], img.shape[1]
area = height * width
for _ in range(10):
target_area = random.uniform(*scale) * area
log_ratio = (math.log(ratio[0]), math.log(ratio[1]))
aspect_ratio = math.exp(random.uniform(*log_ratio))
w = int(round(math.sqrt(target_area * aspect_ratio)))
h = int(round(math.sqrt(target_area / aspect_ratio)))
if 0 < w <= width and 0 < h <= height:
i = random.randint(0, height - h)
j = random.randint(0, width - w)
return i, j, h, w
in_ratio = float(width) / float(height)
if (in_ratio < min(ratio)):
w = width
h = int(round(w / min(ratio)))
elif (in_ratio > max(ratio)):
h = height
w = int(round(h * max(ratio)))
else:
w = width
h = height
i = (height - h) // 2
j = (width - w) // 2
return i, j, h, w
def __call__(self, img):
i, j, h, w = self.get_params(img, self.scale, self.ratio)
out = resized_crop(img, i, j, h, w, self.size)
return out
class RGB2Lab(object):
"""Convert RGB PIL image to ndarray Lab."""
def __call__(self, img):
img = np.asarray(img, np.uint8)
img = color.rgb2lab(img)
return img
def pil_loader(path):
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
class ImageDataset(datautils.Dataset):
def __init__(self, root_path, images_to_use, transform=None):
super(ImageDataset, self).__init__()
with open(images_to_use, 'r') as f:
images = f.readlines()
self.samples = [os.path.join(root_path, image_path.strip()) for image_path in images]
self.loader = pil_loader
self.transform = transform
def __len__(self):
return len(self.samples)
def __getitem__(self, index):
path = self.samples[index]
img = self.loader(path)
if self.transform is not None:
img = self.transform(img)
return img, 0, index
class MultispectralImageDataset(datautils.Dataset):
def __init__(self, lmdb_path, images_to_use, transform=None):
super(MultispectralImageDataset, self).__init__()
with open(images_to_use, 'r') as f:
names = f.readlines()
self.names = [name.strip().split('.')[0] for name in names]
self.env = lmdb.open(lmdb_path, readonly = True, lock = False, readahead = False, meminit = False)
self.transform = transform
def __len__(self):
return len(self.names)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
sample = np.zeros((256, 256, 10))
with self.env.begin(write = False) as txn:
byteflow = txn.get(self.names[idx].encode('ascii'))
data_120, data_60 = loads_pyarrow(byteflow)
data_120 = data_120.astype(np.float32).transpose((1, 2, 0))
data_60 = data_60.astype(np.float32).transpose((1, 2, 0))
data_120 = my_resize_4(data_120 / 30000, (256, 256))
data_60 = my_resize_6(data_60 / 30000, (256, 256))
sample[:, :, 0:3] = data_120[:, :, 0:3]
sample[:, :, 3:6] = data_60[:, :, 0:3]
sample[:, :, 6] = data_120[:, :, 3]
sample[:, :, 7:] = data_60[:, :, 3:]
if self.transform:
sample = self.transform(sample)
return sample, 0, idx
class FeatureClassificationDataset(datautils.Dataset):
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return self.features.shape[0]
def __getitem__(self, index):
return self.features[index], self.targets[index]
def loader_big_earth_net(byteflow):
sample = np.zeros((256, 256, 10))
data_120, data_60, label = loads_pyarrow(byteflow)
data_120 = data_120.astype(np.float32).transpose((1, 2, 0))
data_60 = data_60.astype(np.float32).transpose((1, 2, 0))
data_120 = my_resize_4(data_120 / 30000, (256, 256))
data_60 = my_resize_6(data_60 / 30000, (256, 256))
sample[:, :, 0:3] = data_120[:, :, 0:3]
sample[:, :, 3:6] = data_60[:, :, 0:3]
sample[:, :, 6] = data_120[:, :, 3]
sample[:, :, 7:] = data_60[:, :, 3:]
return sample, label
def loader_so2sat(byteflow):
data, label = loads_pyarrow(byteflow)
data = np.array(data)
data /= 2.8
data *= np.array([17561.21923828, 17054, 16508.984375, 16226.00726318,
16108.16802979, 16047.890625, 15946.984375, 15849.51269531,
14715.36694336, 15145])
data = my_resize_10(data / 30000, (256, 256))
return data, label
class MultilabelClassificationImageDataset(datautils.Dataset):
def __init__(self, lmdb_path, images_to_use, transform=None, target_transform=None, dataset='BigEarthNet'):
super(MultilabelClassificationImageDataset, self).__init__()
with open(images_to_use, 'r') as f:
names = f.readlines()
self.names = [os.path.split(name.strip())[1] for name in names]
self.env = lmdb.open(lmdb_path, max_readers = 1, readonly = True, lock = False, readahead = False, meminit = False)
self.transform = transform
self.target_transform = target_transform
if dataset == 'BigEarthNet':
self.num_classes = 19
self.loader = loader_big_earth_net
elif dataset == 'So2Sat':
self.num_classes = 17
self.loader = loader_so2sat
else:
raise ValueError('Not supported dataset!!!')
def __len__(self):
return len(self.names)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
env = self.env
with env.begin(write = False) as txn:
byteflow = txn.get(self.names[idx].encode('ascii'))
sample, target = self.loader(byteflow)
if self.transform:
sample = self.transform(sample)
if self.target_transform:
target = self.target_transform(target)
return sample, target
class ClassificationImageDataset(datautils.Dataset):
def __init__(self, root_path, images_to_use, transform=None, target_transform=None, multilabel_targets=None):
super(ClassificationImageDataset, self).__init__()
with open(images_to_use, 'r') as f:
self.samples = f.readlines()
self.samples.sort()
self.samples = [os.path.join(root_path, image_path.strip()) for image_path in self.samples]
self.loader = pil_loader
self.transform = transform
self.target_transform = target_transform
if multilabel_targets:
self.targets = self._make_targets(multilabel_targets=multilabel_targets)
else:
classes, class_to_idx = self._find_classes(root_path)
self.targets = self._make_targets(class_to_idx=class_to_idx)
def __len__(self):
return len(self.samples)
def __getitem__(self, index):
path, target = self.samples[index], self.targets[index]
img = self.loader(path)
if self.transform:
img = self.transform(img)
if self.target_transform:
target = self.target_transform(target)
return img, target
def _find_classes(self, dir):
classes = [d.name for d in os.scandir(dir) if d.is_dir()]
classes.sort()
class_to_idx = {cls_name: i for i, cls_name in enumerate(classes)}
return classes, class_to_idx
def _make_targets(self, class_to_idx=None, multilabel_targets=None):
if class_to_idx:
self.num_classes = len(class_to_idx)
return np.array([class_to_idx[os.path.split(os.path.split(sample)[0])[1]] for sample in self.samples])
if multilabel_targets:
self.num_classes = len(multilabel_targets[os.path.split(self.samples[0])[1]])
return [multilabel_targets[os.path.split(sample)[1]] for sample in self.samples]
raise ValueError("Either class_to_idx or multilabel_targets must be supplied!!!")