-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
316 lines (253 loc) · 12.1 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
import numpy as np
import torch
import torch.tensor
import torch.utils
import torch.utils.data
from torchvision.datasets.utils import download_and_extract_archive, download_file_from_google_drive
from torchvision.datasets.mnist import read_image_file
from torch.utils.data import Dataset
from torch.utils.data import random_split
import os
import tarfile
import cv2
from tqdm import tqdm
MNIST_WORKING_DIM=28
VAL_SET_PORTION=0.05
class ImgLoader:
def __init__(self, center_crop_size, resize, normalize, to_torch, dtype):
self.center_crop_size = center_crop_size
self.resize = resize
self.normalize = normalize
self.dtype = dtype
self.to_torch = to_torch
def __call__(self, path):
img = cv2.imread(path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
if self.center_crop_size:
img = center_crop(img, self.center_crop_size)
if self.resize:
img = cv2.resize(img, (self.resize, self.resize))
img = img.transpose(2, 0, 1)
if self.normalize:
img = img / 127.5 - 1
if self.to_torch:
img = torch.tensor(img, dtype=self.dtype)
else:
img = img.astype(self.dtype)
return img
def center_crop(img, size):
y_start = int((img.shape[0] - size)/2)
x_start = int((img.shape[1] - size)/2)
return img[y_start: y_start + size, x_start: x_start + size]
def download_mnist(data_dir):
"""
Taken from torchvision.datasets.mnist
Dwonloads Mnist from the official site
reshapes themas images, normalizes them and saves them as a tensor
"""
raw_folder = os.path.join(data_dir, 'raw')
if not os.path.exists(raw_folder):
os.makedirs(raw_folder, exist_ok=True)
# download files
train_imgs_url = "http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz", "f68b3c2dcbeaaa9fbdd348bbdeb94873"
test_imgs_url = "http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz", "9fb629c4189551a2d022fa330f9573f3"
for url, md5 in [train_imgs_url, test_imgs_url]:
filename = url.rpartition('/')[2]
download_and_extract_archive(url, download_root=raw_folder, filename=filename, md5=md5)
if not os.path.exists(os.path.join(data_dir, 'train_data.pt')):
# process and save as torch files
print('Processing...')
training_set = read_image_file(os.path.join(raw_folder, 'train-images-idx3-ubyte'))
test_set = read_image_file(os.path.join(raw_folder, 't10k-images-idx3-ubyte'))
# preprocess: reshape and normalize from [0,255] to [-1,1]
training_set = training_set.reshape(-1, 1, MNIST_WORKING_DIM, MNIST_WORKING_DIM) / 127.5 - 1
test_set = test_set.reshape(-1, 1, MNIST_WORKING_DIM, MNIST_WORKING_DIM) / 127.5 - 1
with open(os.path.join(data_dir, 'train_data.pt'), 'wb') as f:
torch.save(training_set, f)
with open(os.path.join(data_dir, 'test_data.pt'), 'wb') as f:
torch.save(test_set, f)
print('Done!')
def download_lwf(data_dir):
"""
Dwonloads LFW alligned images (deep funneled version) from the official site
crops and normalizes them and saves them as a tensor
"""
if not os.path.exists(os.path.join(data_dir, 'lfw-deepfunneled.tgz')):
print("Downloadint LFW from official site...")
download_and_extract_archive("http://vis-www.cs.umass.edu/lfw/lfw-deepfunneled.tgz",
md5='68331da3eb755a505a502b5aacb3c201',
download_root=data_dir, filename='lfw-deepfunneled.tgz')
if not os.path.exists(os.path.join(data_dir, 'lfw-deepfunneled')):
f = tarfile.open(os.path.join(data_dir, 'lfw-deepfunneled.tgz'), 'r:gz')
f.extractall(data_dir)
f.close()
def download_celeba(data_dir):
print("Downloading Celeb-a from kaggle...")
os.environ['KAGGLE_USERNAME'] = "ariel415el"
os.environ['KAGGLE_KEY'] = "831db7b1693cd81d31ce16e340ddba03"
import kaggle
kaggle.api.dataset_download_files('jessicali9530/celeba-dataset', path=data_dir, unzip=True, quiet=False)
print("Done!")
def download_ffhq_thumbnails(data_dir):
print("Downloadint FFHQ-thumbnails from kaggle...")
os.environ['KAGGLE_USERNAME'] = "ariel415el"
os.environ['KAGGLE_KEY'] = "831db7b1693cd81d31ce16e340ddba03"
import kaggle
kaggle.api.dataset_download_files('greatgamedota/ffhq-face-data-set', path=data_dir, unzip=True, quiet=False)
print("Done.")
def get_lfw(data_dir, dim):
"""
Returns an LFW train and val datalsets
"""
download_lwf(data_dir)
pt_name = f"LFW-{dim}x{dim}.pt"
if not os.path.exists(os.path.join(data_dir, pt_name)):
print("Preprocessing FFHQ data")
imgs = []
img_loader = ImgLoader(center_crop_size=150, resize=dim, normalize=True, to_torch=False, dtype=np.float32)
for celeb_name in tqdm(os.listdir(os.path.join(data_dir, 'lfw-deepfunneled'))):
for fname in os.listdir(os.path.join(data_dir, 'lfw-deepfunneled', celeb_name)):
img = img_loader(os.path.join(data_dir, 'lfw-deepfunneled', celeb_name, fname))
imgs.append(torch.tensor(img, dtype=torch.float32))
with open(os.path.join(data_dir, pt_name), 'wb') as f:
torch.save(torch.stack(imgs), f)
data = torch.load(os.path.join(data_dir, pt_name))
dataset = MemoryDataset(data)
val_size = int(len(dataset) * VAL_SET_PORTION)
train_dataset, val_dataset = random_split(dataset, [len(dataset) - val_size, val_size])
return train_dataset, val_dataset
def get_mnist(data_dir):
"""
Returns an LFW train and val datalsets
"""
download_mnist(data_dir)
train_data = torch.load(os.path.join(data_dir, "train_data.pt"))
test_data = torch.load(os.path.join(data_dir, "test_data.pt"))
train_dataset, val_dataset = MemoryDataset(train_data), MemoryDataset(test_data)
return train_dataset, val_dataset, MNIST_WORKING_DIM
def get_celeba(data_dir, dim):
imgs_dir = os.path.join(data_dir, 'img_align_celeba', 'img_align_celeba')
if not os.path.exists(imgs_dir):
download_celeba(data_dir)
img_loader = ImgLoader(center_crop_size=170, resize=dim, normalize=True, to_torch=True, dtype=torch.float32)
img_paths = [os.path.join(imgs_dir, fname) for fname in os.listdir(imgs_dir)]
dataset = DiskDataset(img_paths, img_loader)
val_size = int(len(dataset) * VAL_SET_PORTION)
train_dataset, val_dataset = random_split(dataset, [len(dataset) - val_size, val_size])
return train_dataset, val_dataset
def get_ffhq(data_dir, dim):
imgs_dir = os.path.join(data_dir, 'thumbnails128x128')
if not os.path.exists(imgs_dir):
download_ffhq_thumbnails(data_dir)
if dim <= 64:
pt_file = f"FFHQ_Thumbnail-{dim}x{dim}.pt"
if not os.path.exists(os.path.join(data_dir, pt_file)):
print(f"Preprocessing FFHQ: creating a {dim}x{dim} version of all data")
imgs = []
img_loader = ImgLoader(center_crop_size=None, resize=dim, normalize=True, to_torch=True, dtype=torch.float32)
for img_name in tqdm(os.listdir(imgs_dir)):
fname = os.path.join(imgs_dir, img_name)
img = img_loader(fname)
imgs.append(img)
with open(os.path.join(data_dir, pt_file), 'wb') as f:
torch.save(torch.stack(imgs), f)
data = torch.load(os.path.join(data_dir, pt_file))
dataset = MemoryDataset(data)
else:
img_loader = ImgLoader(center_crop_size=None, resize=dim, normalize=True, to_torch=True, dtype=torch.float32)
img_paths = [os.path.join(imgs_dir, img_name) for img_name in os.listdir(imgs_dir)]
dataset = DiskDataset(img_paths, img_loader)
val_size = int(len(dataset) * VAL_SET_PORTION)
train_dataset, val_dataset = random_split(dataset, [len(dataset) - val_size, val_size])
return train_dataset, val_dataset
def get_twdne(data_dir, dim):
imgs_dir = os.path.join(data_dir)
# if not os.path.exists(imgs_dir):
# # download_ffhq_thumbnails(data_dir)
# print("Please Download the dataset in here")
if dim <= 64:
pt_file = f"TWDNE_Thumbnail-{dim}x{dim}.pt"
if not os.path.exists(os.path.join(data_dir, pt_file)):
print(f"Preprocessing TWDNE: creating a {dim}x{dim} version of all data")
imgs = []
img_loader = ImgLoader(center_crop_size=None, resize=dim, normalize=True, to_torch=True, dtype=torch.float32)
for img_name in tqdm(os.listdir(imgs_dir)):
fname = os.path.join(imgs_dir, img_name)
img = img_loader(fname)
imgs.append(img)
with open(os.path.join(data_dir, pt_file), 'wb') as f:
torch.save(torch.stack(imgs), f)
data = torch.load(os.path.join(data_dir, pt_file))
dataset = MemoryDataset(data)
else:
img_loader = ImgLoader(center_crop_size=None, resize=dim, normalize=True, to_torch=True, dtype=torch.float32)
img_paths = [os.path.join(imgs_dir, img_name) for img_name in os.listdir(imgs_dir)]
dataset = DiskDataset(img_paths, img_loader)
val_size = int(len(dataset) * VAL_SET_PORTION)
train_dataset, val_dataset = random_split(dataset, [len(dataset) - val_size, val_size])
return train_dataset, val_dataset
class MemoryDataset(Dataset):
def __init__(self, data_matrix):
self.data_matrix = data_matrix
def __len__(self):
return len(self.data_matrix)
def __getitem__(self, idx):
return self.data_matrix[idx]
def get_data(self):
return self.data_matrix
class DiskDataset(Dataset):
def __init__(self, image_paths, load_image_function):
self.image_paths = image_paths
self.load_image_function = load_image_function
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
return self.load_image_function(self.image_paths[idx])
class EndlessDataloader:
"""
An iterator wrapper for a dataloader that resets when reaches its end
"""
def __init__(self, dataloader):
self.dataloader = dataloader
self.iterator = iter(dataloader)
def next(self):
try:
real_image = next(self.iterator)
except (OSError, StopIteration):
self.iterator = iter(self.dataloader)
real_image = next(self.iterator)
return real_image
def get_dataset(data_root, dataset_name, dim):
if dataset_name.lower() == 'mnist':
assert dim == 28
train_dataset, test_dataset, _ = get_mnist(os.path.join(data_root, 'Mnist'))
elif dataset_name.lower() == 'celeb-a':
train_dataset, test_dataset = get_celeba(os.path.join(data_root, 'Celeb-a'), dim)
elif dataset_name.lower() == 'ffhq':
train_dataset, test_dataset = get_ffhq(os.path.join(data_root, 'FFHQ-thumbnails'), dim)
elif dataset_name.lower() == 'lfw':
train_dataset, test_dataset = get_lfw(os.path.join(data_root, 'LFW'), dim)
elif dataset_name.lower() == 'twdne':
train_dataset, test_dataset = get_twdne(os.path.join(data_root, 'Twdne-128'), dim)
else:
raise ValueError("No such available dataset")
return train_dataset, test_dataset
class RequireGradCollator(object):
def __init__(self, resize, device):
self.device = device
self.resize = resize
def __call__(self, batch):
with torch.no_grad():
# requires_grad=True is necessary for the gradient penalty calculation
# return torch.tensor(batch, requires_grad=True, device=self.device, dtype=torch.float32)
batch_tensor = torch.stack(batch).to(self.device).float()
if self.resize is not None:
batch_tensor = torch.nn.functional.interpolate(batch_tensor, (self.resize, self.resize))
batch_tensor.requires_grad = True
return batch_tensor
def get_dataloader(dataset, batch_size, resize, device):
kwargs = {'batch_size': batch_size, 'shuffle': True, 'collate_fn': RequireGradCollator(resize, device)}
if device == "cuda:0":
kwargs.update({'num_workers': 2,
'pin_memory': True})
return torch.utils.data.DataLoader(dataset, **kwargs)