-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
336 lines (282 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
from typing import Callable, List, Tuple
import torch
import torch.utils.data as data
import numpy as np
import pandas as pd
import os
from os.path import join
from PIL import Image, ImageOps
import random
import torchvision.transforms as transforms
from torchvision.io import read_image
from torchvision.transforms.functional import resize
from torch.utils.data import DataLoader
class WrappedDataLoader:
def __init__(self, data_loader: DataLoader, pre_process: Callable):
self.dl = data_loader
self.func = pre_process
def __len__(self):
return len(self.dl)
def __iter__(self):
batches = iter(self.dl)
for b in batches:
yield (self.func(*b))
class DisentanglementDataset(data.Dataset):
@property
def latent_indices(self) -> List[int]:
raise NotImplementedError()
@property
def factor_sizes(self) -> List[int]:
raise NotImplementedError()
# TODO: Test dataset
class MPI3D(DisentanglementDataset):
def __init__(self, arr, resize: int = 64) -> None:
self.imgs = arr['images'] * 255
self.factor_bases = np.divide(
np.prod(self.factor_sizes), np.cumprod(self.factor_sizes)
).astype(int)
self.latents_values = np.stack(list(map(self._index_to_factor, np.arange(self.imgs.shape[0]))))
self.resize = resize
self.input_transform = transforms.Compose([transforms.ToTensor()])
@classmethod
def load_data(cls, resize: int = 64) -> "DisentanglementDataset":
data_dir = os.path.expanduser("~/mpi3d-dataset")
data_path = os.path.join(data_dir, "mpi3d_toy.npz")
arr = np.load(data_path)
return MPI3D(arr, resize=resize)
def _index_to_factor(self, idx: int) -> np.ndarray:
"""Get factor array from index
Parameters
----------
idx : int
Index to convert
Returns
-------
np.ndarray
Factor index array of shape (7,)
"""
bucket_pos = np.floor_divide(idx, self.factor_bases)
return np.mod(bucket_pos, self.factor_sizes)
def __getitem__(self, index: int) -> Tuple[torch.Tensor, np.ndarray]:
img = Image.fromarray(self.imgs[index])
label = self.latents_values[index]
if self.resize != 64:
img = img.resize((self.resize, self.resize), Image.BICUBIC)
img = self.input_transform(img)
return img, label
@property
def latent_indices(self) -> List[int]:
return [0, 1, 2, 3, 4, 5, 6]
@property
def factor_sizes(self) -> List[int]:
return [6,6,2,3,3,40,40]
class MPI3DSmall(MPI3D):
def __init__(self, arr, resize: int = 64) -> None:
self.imgs = arr['images']
self.factor_bases = np.divide(
np.prod(self.orig_factor_sizes), np.cumprod(self.orig_factor_sizes)
).astype(int)
self.latents_values = np.stack(list(map(self._index_to_factor, np.arange(self.imgs.shape[0]))))
horizontal_mask = np.in1d(self.latents_values[:,5], get_spaced_elements(self.latents_values[:,5], 4))
vertical_mask = np.in1d(self.latents_values[:,6], get_spaced_elements(self.latents_values[:,6], 4))
mask = horizontal_mask & vertical_mask
assert mask.sum() == np.prod(self.factor_sizes)
self.latents_values = self.latents_values[mask]
self.imgs = self.imgs[mask] * 255
self.resize = resize
self.input_transform = transforms.Compose([transforms.ToTensor()])
@property
def factor_sizes(self) -> List[int]:
return [6,6,2,3,3,4,4]
@property
def orig_factor_sizes(self) -> List[int]:
return [6,6,2,3,3,40,40]
def _index_to_factor(self, idx: int) -> np.ndarray:
bucket_pos = np.floor_divide(idx, self.factor_bases)
return np.mod(bucket_pos, self.orig_factor_sizes)
@classmethod
def load_data(cls, resize: int = 64) -> "DisentanglementDataset":
data_dir = os.path.expanduser("~/mpi3d-dataset")
data_path = os.path.join(data_dir, "mpi3d_toy.npz")
arr = np.load(data_path)
return MPI3DSmall(arr, resize=resize)
class DSprites(DisentanglementDataset):
def __init__(self, arr, resize: int = 64):
self.imgs = arr['imgs'] * 255
self.latents_values = arr['latents_values']
self.resize = resize
self.input_transform = transforms.Compose([transforms.ToTensor()])
def __len__(self):
return len(self.imgs)
def __getitem__(self, index: int) -> Tuple[torch.Tensor, np.ndarray]:
img = Image.fromarray(self.imgs[index])
label = self.latents_values[index]
if self.resize != 64:
img = img.resize((self.resize, self.resize), Image.BICUBIC)
img = self.input_transform(img)
return img, label
@property
def latent_indices(self) -> List[int]:
return [1, 2, 3, 4, 5]
@property
def factor_sizes(self) -> List[int]:
return [1, 3, 6, 40, 32, 32]
@classmethod
def load_data(cls, resize: int = 64) -> "DisentanglementDataset":
data_dir = os.path.expanduser("~/dsprites-dataset")
data_path = os.path.join(data_dir, "dsprites_ndarray_co1sh3sc6or40x32y32_64x64.npz")
arr = np.load(data_path)
return DSprites(arr, resize=resize)
def get_spaced_elements(arr, n):
"""Returns n evenly spaced values from the unique values of the array.
Parameters
----------
arr : np.array
The array to select the elements from.
n : int
The number of elements to select from the unique values in the array.
"""
unique_values = np.unique(arr)
idx = np.round(np.linspace(0, len(unique_values) - 1, n)).astype(int)
return unique_values[idx]
class DSpritesSmall(DSprites):
def __init__(self, arr, resize: int = 64):
self.latents_values = arr['latents_values']
# reduce number of unique values for orientation, x position and y position
rotation_mask = np.in1d(self.latents_values[:,3], get_spaced_elements(self.latents_values[:,3], 5)[:-1])
x_mask = np.in1d(self.latents_values[:,4], get_spaced_elements(self.latents_values[:,4], 10))
y_mask = np.in1d(self.latents_values[:,5], get_spaced_elements(self.latents_values[:,5], 10))
mask = rotation_mask & x_mask & y_mask
assert mask.sum() == np.prod(self.factor_sizes)
self.latents_values = self.latents_values[mask]
self.imgs = arr['imgs'][mask] * 255
self.resize = resize
self.input_transform = transforms.Compose([transforms.ToTensor()])
@property
def factor_sizes(self) -> List[int]:
return [1, 3, 6, 4, 10, 10]
@classmethod
def load_data(cls, resize: int = 64) -> "DisentanglementDataset":
data_dir = os.path.expanduser("~/dsprites-dataset")
data_path = os.path.join(data_dir, "dsprites_ndarray_co1sh3sc6or40x32y32_64x64.npz")
arr = np.load(data_path)
return DSpritesSmall(arr, resize=resize)
if __name__ == "__main__":
MPI3D.load_data()
class UkiyoE(data.Dataset):
def __init__(self, root, df, category, resize=256):
self.root = root
self.labels = df[category].astype("category")
self.category = category
self.resize = resize
self.entries = [
tuple(r)
for r in zip(df["singleface_filename"], self.labels.cat.codes)
if os.path.exists(os.path.join(self.root, r[0]))
]
self.input_transform = transforms.Compose(
[
transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor(),
]
)
def __len__(self):
return len(self.entries)
def __getitem__(self, index) -> Tuple[torch.Tensor, np.ndarray]:
image_filename, label = self.entries[index]
image_filepath = os.path.join(self.root, image_filename)
image = load_image(
image_filepath,
input_height=256,
output_height=self.resize,
is_mirror=False,
is_random_crop=False,
)
image = self.input_transform(image)
return image, np.array(label)
def get_label(self, index) -> str:
code = self.labels.cat.codes[index]
return self.labels.cat.categories[code]
@classmethod
def load_data(cls, resize: int = 256) -> "DisentanglementDataset":
data_dir = os.path.expanduser("~/arc-ukiyoe-faces/scratch")
image_dir = data_dir + "/arc_extracted_face_images"
return UkiyoE(image_dir, UkiyoE.load_labels(data_dir), "Painter", resize=resize)
@classmethod
def load_labels(cls, data_dir) -> pd.DataFrame:
labels: pd.DataFrame = pd.read_csv(
data_dir + "/arc_extracted_face_metadata.csv"
)
labels.columns = [
"ACNo.",
"Print title",
"Picture name",
"Official title",
"Text",
"Publisher",
"Format",
"Direction",
"Seal",
"Painter",
"revised seals",
"Year in A.D.",
"Year in Japanese Calender",
"Region",
"Theater",
"Title of play",
"Reading of Title of play",
"Performed title",
"Reading of Performed title",
"Main performed title",
"Classification title",
"Library",
"Text",
"homeURL",
"SmallImageURL",
"LargeImageURL",
"filename",
]
labels = labels[["Painter", "Year in A.D.", "Region", "filename"]]
labels["Painter"] = labels["Painter"].astype(str)
return labels
def load_image(
file_path,
input_height=128,
input_width=None,
output_height=128,
output_width=None,
crop_height=None,
crop_width=None,
is_random_crop=True,
is_mirror=True,
is_gray=False,
):
if input_width is None:
input_width = input_height
if output_width is None:
output_width = output_height
if crop_width is None:
crop_width = crop_height
img = Image.open(file_path)
if not is_gray and img.mode != "RGB":
img = img.convert("RGB")
if is_gray and img.mode != "L":
img = img.convert("L")
if is_mirror and random.randint(0, 1) == 0:
img = ImageOps.mirror(img)
if input_height is not None:
img = img.resize((input_width, input_height), Image.BICUBIC)
if crop_height is not None:
[w, h] = img.size
if is_random_crop:
# print([w,cropSize])
cx1 = random.randint(0, w - crop_width)
cx2 = w - crop_width - cx1
cy1 = random.randint(0, h - crop_height)
cy2 = h - crop_height - cy1
else:
cx2 = cx1 = int(round((w - crop_width) / 2.0))
cy2 = cy1 = int(round((h - crop_height) / 2.0))
img = ImageOps.crop(img, (cx1, cy1, cx2, cy2))
img = img.resize((output_width, output_height), Image.BICUBIC)
return img