-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataloaders.py
382 lines (331 loc) · 14.1 KB
/
dataloaders.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
import os
import random
import numpy as np
import torch.utils.data
import torchvision.transforms as tfs
from PIL import Image
def get_data_loaders(cfgs):
batch_size = cfgs.get("batch_size", 64)
image_size = cfgs.get("image_size", 64)
crop = cfgs.get("crop", None)
run_train = cfgs.get("run_train", False)
run_finetune = cfgs.get("run_finetune", False)
train_val_data_dir = cfgs.get("train_val_data_dir", "./data")
run_test = cfgs.get("run_test", False)
test_data_dir = cfgs.get("test_data_dir", "./data/test")
load_gt_depth = cfgs.get("load_gt_depth", False)
AB_dnames = cfgs.get("paired_data_dir_names", ["A", "B"])
AB_fnames = cfgs.get("paired_data_filename_diff", None)
train_loader = val_loader = test_loader = None
if run_train:
train_data_dir = os.path.join(train_val_data_dir)
val_data_dir = os.path.join(train_val_data_dir)
assert os.path.isdir(train_data_dir), "Training data directory does not exist: %s" % train_data_dir
assert os.path.isdir(val_data_dir), "Validation data directory does not exist: %s" % val_data_dir
print(f"Loading training data from {train_data_dir}")
if load_gt_depth:
train_loader = get_paired_image_loader(
data_dir=train_data_dir,
is_validation=False,
batch_size=batch_size,
image_size=image_size,
crop=crop,
AB_dnames=AB_dnames,
AB_fnames=AB_fnames,
)
else:
train_loader = get_image_loader(
data_dir=train_data_dir,
is_validation=False,
batch_size=batch_size,
image_size=image_size,
crop=crop,
is_finetune=run_finetune,
)
print(f"Loading validation data from {val_data_dir}")
if load_gt_depth:
val_loader = get_paired_image_loader(
data_dir=val_data_dir,
is_validation=True,
batch_size=batch_size,
image_size=image_size,
crop=crop,
AB_dnames=AB_dnames,
AB_fnames=AB_fnames,
)
else:
val_loader = get_image_loader(
data_dir=val_data_dir,
is_validation=True,
batch_size=batch_size,
image_size=image_size,
crop=crop,
is_finetune=run_finetune,
)
if run_test:
assert os.path.isdir(test_data_dir), "Testing data directory does not exist: %s" % test_data_dir
print(f"Loading testing data from {test_data_dir}")
if load_gt_depth:
test_loader = get_paired_image_loader(
data_dir=test_data_dir,
is_validation=True,
batch_size=batch_size,
image_size=image_size,
crop=crop,
AB_dnames=AB_dnames,
AB_fnames=AB_fnames,
)
else:
test_loader = get_image_loader(
data_dir=test_data_dir, is_validation=True, batch_size=batch_size, image_size=image_size, crop=crop
)
return train_loader, val_loader, test_loader
IMG_EXTENSIONS = (".jpg", ".jpeg", ".png", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", "webp")
def is_image_file(filename):
return filename.lower().endswith(IMG_EXTENSIONS)
# simple image dataset ##
def make_dataset(dir):
assert os.path.isdir(dir), "%s is not a valid directory" % dir
if "val" in dir:
rand_num = 10
else:
rand_num = 100
images = []
"""PIE"""
if "pie" in dir: # To be changed
session_list = os.listdir(dir)
for session in session_list:
object_list = os.listdir(os.path.join(dir, session))
for object in object_list:
expression_list = os.listdir(os.path.join(dir, session, object))
for expression in expression_list:
left_list = os.listdir(os.path.join(dir, session, object, expression, "13_0"))
frontal_list = os.listdir(os.path.join(dir, session, object, expression, "05_1"))
right_list = os.listdir(os.path.join(dir, session, object, expression, "04_1"))
for i in range(rand_num):
left = random.choice(left_list)
frontal = random.choice(frontal_list)
right = random.choice(right_list)
left_path = os.path.join(dir, session, object, expression, "13_0", left)
frontal_path = os.path.join(dir, session, object, expression, "05_1", frontal)
right_path = os.path.join(dir, session, object, expression, "04_1", right)
images.append([frontal_path, right_path])
images.append([frontal_path, left_path])
else:
for root, _, fnames in sorted(os.walk(dir)):
for fname in sorted(fnames):
if is_image_file(fname):
fpath = os.path.join(root, fname)
images.append([fpath, fpath])
random.shuffle(images)
return images
class ImageDataset(torch.utils.data.Dataset):
def __init__(self, data_dir, image_size=256, crop=None, is_validation=False):
super(ImageDataset, self).__init__()
if is_validation:
self.root = os.path.join(data_dir, "val")
else:
self.root = os.path.join(data_dir, "train")
self.paths = make_dataset(self.root)
self.size = len(self.paths)
self.image_size = image_size
self.crop = crop
self.is_validation = is_validation
def transform(self, imgs):
img1 = tfs.functional.resize(imgs[0], (self.image_size, self.image_size))
img2 = tfs.functional.resize(imgs[1], (self.image_size, self.image_size))
if "pie" not in self.root:
img2 = tfs.functional.hflip(img2)
img1_tensor = tfs.functional.to_tensor(img1)
img2_tensor = tfs.functional.to_tensor(img2)
return img1_tensor, img2_tensor
def __getitem__(self, index):
fpath = self.paths[index % self.size]
img1 = Image.open(fpath[0]).convert("RGB")
img2 = Image.open(fpath[1]).convert("RGB")
return self.transform([img1, img2])
def __len__(self):
return self.size
def name(self):
return "ImageDataset"
def make_collection_dataset(dir, is_finetune=False):
assert os.path.isdir(dir), "%s is not a valid directory" % dir
videos = sorted(os.listdir(dir))
images = []
for vid in videos:
frames = sorted(os.listdir(dir + "/" + vid))
frames = [x for x in frames if is_image_file(x)]
if is_finetune:
images += [[dir + "/" + vid, x] for x in frames]
else:
if len(frames) < 2:
continue
images.append([dir + "/" + vid, frames])
random.shuffle(images)
return images
class CollectionDataset(torch.utils.data.Dataset):
def __init__(self, data_dir, image_size=256, crop=None, gap=0, is_validation=False, is_finetune=False):
super(CollectionDataset, self).__init__()
if is_validation:
self.root = os.path.join(data_dir, "val")
self.paths = make_collection_dataset(self.root, is_finetune)
else:
self.root = os.path.join(data_dir, "train")
self.paths = make_collection_dataset(self.root, is_finetune)
self.size = len(self.paths)
self.image_size = image_size
self.crop = crop
self.is_validation = is_validation
self.is_finetune = is_finetune
self.gap = gap
def single_transform(self, img):
if self.crop is not None:
if isinstance(self.crop, int):
if not self.is_validation and self.gap > 0:
k1 = random.randint(0, self.gap)
k2 = random.randint(0, k1)
img = tfs.CenterCrop(self.crop + k1)(img)
img = tfs.RandomCrop(self.crop + k2)(img)
else:
img = tfs.CenterCrop(self.crop)(img)
else:
assert (
len(self.crop) == 4
), "Crop size must be an integer for center crop, or a list of 4 integers (y0,x0,h,w)"
img = tfs.functional.crop(img, *self.crop)
img = tfs.functional.resize(img, (self.image_size, self.image_size))
# if random.random() < 0.8:
# img = tfs.RandomRotation(degrees=(-90, 90))(img)
img_tensor = tfs.functional.to_tensor(img)
return img_tensor
def transform(self, imgs):
img1, img2 = imgs[0], imgs[1]
if self.crop is not None:
if isinstance(self.crop, int):
if not self.is_validation and self.gap > 0:
k1 = random.randint(0, self.gap)
k2 = random.randint(0, k1)
img1 = tfs.CenterCrop(self.crop + k1)(img1)
img2 = tfs.CenterCrop(self.crop + k1)(img2)
img1 = tfs.RandomCrop(self.crop + k2)(img1)
img2 = tfs.RandomCrop(self.crop + k2)(img2)
else:
img1 = tfs.CenterCrop(self.crop)(img1)
img2 = tfs.CenterCrop(self.crop)(img2)
else:
assert (
len(self.crop) == 4
), "Crop size must be an integer for center crop, or a list of 4 integers (y0,x0,h,w)"
img1 = tfs.functional.crop(img1, *self.crop)
img2 = tfs.functional.crop(img2, *self.crop)
img1 = tfs.functional.resize(img1, (self.image_size, self.image_size))
img2 = tfs.functional.resize(img2, (self.image_size, self.image_size))
img1_tensor = tfs.functional.to_tensor(img1)
img2_tensor = tfs.functional.to_tensor(img2)
return img1_tensor, img2_tensor
def __getitem__(self, index):
v = self.paths[index % self.size]
if self.is_finetune:
img = Image.open(v[0] + "/" + v[1]).convert("RGB")
return self.single_transform(img)
else:
vid = v[0]
frames = v[1]
idx = random.sample(frames, 2)
# idx = frames
img1 = Image.open(vid + "/" + idx[0]).convert("RGB")
img2 = Image.open(vid + "/" + idx[1]).convert("RGB")
return self.transform([img1, img2])
def __len__(self):
return self.size
def name(self):
return "CollectionDataset"
def get_image_loader(
data_dir, is_validation=False, batch_size=256, num_workers=4, image_size=256, crop=None, is_finetune=False
):
if "casia" in data_dir.lower() or "ytf" in data_dir.lower():
dataset = CollectionDataset(
data_dir, image_size=image_size, crop=crop, is_validation=is_validation, is_finetune=is_finetune
)
else:
dataset = ImageDataset(data_dir, image_size=image_size, crop=crop, is_validation=is_validation)
loader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=not is_validation, num_workers=num_workers, pin_memory=True
)
return loader
def make_paied_dataset(dir, AB_dnames=None, AB_fnames=None):
A_dname, B_dname = AB_dnames or ("A", "B")
dir_A = os.path.join(dir, A_dname)
dir_B = os.path.join(dir, B_dname)
assert os.path.isdir(dir_A), "%s is not a valid directory" % dir_A
assert os.path.isdir(dir_B), "%s is not a valid directory" % dir_B
images = []
for root_A, _, fnames_A in sorted(os.walk(dir_A)):
for fname_A in sorted(fnames_A):
if is_image_file(fname_A):
path_A = os.path.join(root_A, fname_A)
root_B = root_A.replace(dir_A, dir_B, 1)
if AB_fnames is not None:
fname_B = fname_A.replace(*AB_fnames)
else:
fname_B = fname_A
path_B = os.path.join(root_B, fname_B)
if os.path.isfile(path_B):
images.append((path_A, path_B))
return images
class PairedDataset(torch.utils.data.Dataset):
def __init__(self, data_dir, image_size=256, crop=None, is_validation=False, AB_dnames=None, AB_fnames=None):
super(PairedDataset, self).__init__()
self.root = data_dir
self.paths = make_paied_dataset(data_dir, AB_dnames=AB_dnames, AB_fnames=AB_fnames)
self.size = len(self.paths)
self.image_size = image_size
self.crop = crop
self.is_validation = is_validation
def transform(self, img, hflip=False):
if self.crop is not None:
if isinstance(self.crop, int):
img = tfs.CenterCrop(self.crop)(img)
else:
assert (
len(self.crop) == 4
), "Crop size must be an integer for center crop, or a list of 4 integers (y0,x0,h,w)"
img = tfs.functional.crop(img, *self.crop)
img = tfs.functional.resize(img, (self.image_size, self.image_size))
if hflip:
img = tfs.functional.hflip(img)
return tfs.functional.to_tensor(img)
def __getitem__(self, index):
path_A, path_B = self.paths[index % self.size]
img_A = Image.open(path_A).convert("RGB")
img_B = Image.open(path_B).convert("RGB")
hflip = not self.is_validation and np.random.rand() > 0.5
tensor_A = self.transform(img_A, hflip=hflip)
tensor_B = self.transform(img_B, hflip=hflip)
return tensor_A, tensor_A.flip(-1), tensor_B
def __len__(self):
return self.size
def name(self):
return "PairedDataset"
def get_paired_image_loader(
data_dir,
is_validation=False,
batch_size=256,
num_workers=4,
image_size=256,
crop=None,
AB_dnames=None,
AB_fnames=None,
):
dataset = PairedDataset(
data_dir,
image_size=image_size,
crop=crop,
is_validation=is_validation,
AB_dnames=AB_dnames,
AB_fnames=AB_fnames,
)
loader = torch.utils.data.DataLoader(
dataset, batch_size=batch_size, shuffle=not is_validation, num_workers=num_workers, pin_memory=True
)
return loader