-
Notifications
You must be signed in to change notification settings - Fork 2
/
datasets.py
148 lines (108 loc) · 3.53 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
import os
import torch
def find_files(base_dir, world_size=1, rank=0):
path_list = os.listdir(base_dir)
sort_key = lambda f_name: int(f_name.split('.')[0])
path_list.sort(key=sort_key)
assert len(path_list) % world_size == 0
for i, f in enumerate(path_list):
if i % world_size == rank:
f_path = os.path.join(base_dir, f)
yield f_path
class PairedDataset(torch.utils.data.Dataset):
def __init__(self,
data_dir,
world_size=1,
rank=0,
):
super().__init__()
self.world_size = world_size
self.data_dir = data_dir
latent_dir = os.path.join(data_dir, 'latent')
image_dir = os.path.join(data_dir, 'image')
Z = list()
for f in find_files(latent_dir, world_size, rank):
z = torch.load(f)
Z.append(z)
Z = torch.cat(Z, dim=0)
X = list()
for f in find_files(image_dir, world_size, rank):
x = torch.load(f)
X.append(x)
X = torch.cat(X, dim=0)
assert len(Z) == len(X)
self.Z = Z
self.X = X
def __len__(self):
return len(self.X) * self.world_size
def __getitem__(self, idx):
idx = idx // self.world_size
z, x = self.Z[idx], self.X[idx]
return z, x
class PairedCondDataset(torch.utils.data.Dataset):
def __init__(self,
data_dir,
world_size=1,
rank=0,
):
super().__init__()
self.world_size = world_size
self.data_dir = data_dir
latent_dir = os.path.join(data_dir, 'latent')
image_dir = os.path.join(data_dir, 'image')
Z = list()
C = list()
for f in find_files(latent_dir, world_size, rank):
z, c = torch.load(f)
Z.append(z)
C.append(c)
Z = torch.cat(Z, dim=0)
C = torch.cat(C, dim=0)
X = list()
for f in find_files(image_dir, world_size, rank):
x = torch.load(f)
X.append(x)
X = torch.cat(X, dim=0)
assert len(Z) == len(X)
self.Z = Z
self.C = C
self.X = X
def __len__(self):
return len(self.X) * self.world_size
def __getitem__(self, idx):
idx = idx // self.world_size
z, c, x = self.Z[idx], self.C[idx], self.X[idx]
return z, x, c
class EpochPairedDataset(torch.utils.data.Dataset):
def __init__(self,
data_dir,
total=10,
epoch=0,
world_size=1,
rank=0,
):
super().__init__()
self.world_size = world_size
data_dir = data_dir + f'-{epoch%total}'
self.data_dir = data_dir
latent_dir = os.path.join(data_dir, 'latent')
image_dir = os.path.join(data_dir, 'image')
Z = list()
for f in find_files(latent_dir, world_size, rank):
z = torch.load(f)
Z.append(z)
Z = torch.cat(Z, dim=0)
X = list()
for f in find_files(image_dir, world_size, rank):
x = torch.load(f)
X.append(x)
X = torch.cat(X, dim=0)
assert len(Z) == len(X)
self.Z = Z
self.X = X
def __len__(self):
return len(self.X) * self.world_size
def __getitem__(self, idx):
idx = idx // self.world_size
z, x = self.Z[idx], self.X[idx]
return z, x