-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatasets.py
202 lines (169 loc) · 7.74 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
import os
import urllib.request
import numpy as np
import torch
import torch.utils.data
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.utils.data import Dataset, DataLoader, TensorDataset
from scipy.io import loadmat
num_workers = 4
lamb = 0.05
class MNIST():
def __init__(self, batch_size, binarize=False, logit_transform=False):
""" [-1, 1, 28, 28]
"""
self.binarize = binarize
self.logit_transform = logit_transform
directory='./datasets/MNIST'
if not os.path.exists(directory):
os.makedirs(directory)
kwargs = {'num_workers': num_workers, 'pin_memory': True} if torch.cuda.is_available() else {}
self.train_loader = DataLoader(
datasets.MNIST('./datasets/MNIST', train=True, download=True,
transform=transforms.ToTensor()),
batch_size=batch_size, shuffle=True, **kwargs)
self.test_loader = DataLoader(
datasets.MNIST('./datasets/MNIST', train=False, transform=transforms.ToTensor()),
batch_size=batch_size, shuffle=False, **kwargs)
self.dim = [1,28,28]
if self.binarize:
pass
else:
train = torch.stack([data for data, _ in
list(self.train_loader.dataset)], 0).cuda()
train = train.view(train.shape[0], -1)
if self.logit_transform:
train = train * 255.0
train = (train + torch.rand_like(train)) / 256.0
train = lamb + (1 - 2.0 * lamb) * train
train = torch.log(train) - torch.log(1.0 - train)
self.mean = train.mean(0)
self.logvar = torch.log(torch.mean((train - self.mean)**2)).unsqueeze(0)
def preprocess(self, x):
if self.binarize:
x = x.view([-1, np.prod(self.dim)])
return (torch.rand_like(x).cuda() < x).to(torch.float)
elif self.logit_transform:
# apply uniform noise and renormalize
x = x.view([-1, np.prod(self.dim)]) * 255.0
x = (x + torch.rand_like(x)) / 256.0
x = lamb + (1 - 2.0 * lamb) * x
x = torch.log(x) - torch.log(1.0 - x)
return x - self.mean
else:
return x.view([-1, np.prod(self.dim)]) - self.mean
def unpreprocess(self, x):
if self.binarize:
return x.view([-1] + self.dim)
elif self.logit_transform:
x = x + self.mean
x = torch.sigmoid(x)
x = (x - lamb) / (1.0 - 2.0 * lamb)
return x.view([-1] + self.dim)
else:
return (x + self.mean).view([-1] + self.dim)
class FashionMNIST():
def __init__(self, batch_size, binarize=False, logit_transform=False):
""" [-1, 1, 28, 28]
"""
if binarize:
raise NotImplementedError
self.logit_transform = logit_transform
directory='./datasets/FashionMNIST'
if not os.path.exists(directory):
os.makedirs(directory)
kwargs = {'num_workers': num_workers, 'pin_memory': True} if torch.cuda.is_available() else {}
self.train_loader = DataLoader(
datasets.FashionMNIST(directory, train=True, download=True,
transform=transforms.ToTensor()),
batch_size=batch_size, shuffle=True, **kwargs)
self.test_loader = DataLoader(
datasets.FashionMNIST(directory, train=False, download=True, transform=transforms.ToTensor()),
batch_size=batch_size, shuffle=False, **kwargs)
self.dim = [1,28,28]
train = torch.stack([data for data, _ in
list(self.train_loader.dataset)], 0).cuda()
train = train.view(train.shape[0], -1)
if self.logit_transform:
train = train * 255.0
train = (train + torch.rand_like(train)) / 256.0
train = lamb + (1 - 2.0 * lamb) * train
train = torch.log(train) - torch.log(1.0 - train)
self.mean = train.mean(0)
self.logvar = torch.log(torch.mean((train - self.mean)**2)).unsqueeze(0)
def preprocess(self, x):
if self.logit_transform:
# apply uniform noise and renormalize
x = x.view([-1, np.prod(self.dim)]) * 255.0
x = (x + torch.rand_like(x)) / 256.0
x = lamb + (1 - 2.0 * lamb) * x
x = torch.log(x) - torch.log(1.0 - x)
return x - self.mean
else:
return x.view([-1, np.prod(self.dim)]) - self.mean
def unpreprocess(self, x):
if self.logit_transform:
x = x + self.mean
x = torch.sigmoid(x)
x = (x - lamb) / (1.0 - 2.0 * lamb)
return x.view([-1] + self.dim)
else:
return (x + self.mean).view([-1] + self.dim)
class OMNIGLOT(Dataset):
def __init__(self, batch_size, binarize=False, logit_transform=False):
""" [ -1, 1, 28, 28]
"""
if binarize:
raise NotImplementedError
self.logit_transform = logit_transform
directory='./datasets/OMNIGLOT'
if not os.path.exists(directory):
os.makedirs(directory)
if not os.path.exists(os.path.join(directory, 'chardata.mat')):
print ('Downloading Omniglot images_background.zip...')
urllib.request.urlretrieve('/~https://github.com/yburda/iwae/raw/master/datasets/OMNIGLOT/chardata.mat',
os.path.join(directory, 'chardata.mat'))
data = loadmat(os.path.join(directory, 'chardata.mat'))
# between 0~1.
train = data['data'].swapaxes(0,1).reshape((-1, 1, 28, 28)).astype('float32')
test = data['testdata'].swapaxes(0,1).reshape((-1, 1, 28, 28)).astype('float32')
train_labels = np.zeros(train.shape[0])
test_labels = np.zeros(test.shape[0])
train_dataset = TensorDataset(torch.from_numpy(train), torch.from_numpy(train_labels))
test_dataset = TensorDataset(torch.from_numpy(test), torch.from_numpy(test_labels))
kwargs = {'num_workers': num_workers, 'pin_memory': True} if torch.cuda.is_available() else {}
self.train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, **kwargs)
self.test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, **kwargs)
self.dim = [1, 28, 28]
train = torch.stack([data for data, _ in
list(self.train_loader.dataset)], 0).cuda()
train = train.view(train.shape[0], -1)
if self.logit_transform:
train = train * 255.0
train = (train + torch.rand_like(train)) / 256.0
train = lamb + (1 - 2.0 * lamb) * train
train = torch.log(train) - torch.log(1.0 - train)
self.mean = train.mean(0)
self.logvar = torch.log(torch.mean((train - self.mean)**2)).unsqueeze(0)
def preprocess(self, x):
if self.logit_transform:
# apply uniform noise and renormalize
x = x.view([-1, np.prod(self.dim)]) * 255.0
x = (x + torch.rand_like(x)) / 256.0
x = lamb + (1 - 2.0 * lamb) * x
x = torch.log(x) - torch.log(1.0 - x)
return x - self.mean
else:
return x.view([-1, np.prod(self.dim)]) - self.mean
def unpreprocess(self, x):
if self.logit_transform:
x = x + self.mean
x = torch.sigmoid(x)
x = (x - lamb) / (1.0 - 2.0 * lamb)
return x.view([-1] + self.dim)
else:
return (x + self.mean).view([-1] + self.dim)
#dataset = MNIST(batch_size=32, logit_transform=True)
#dataset = FashionMNIST(batch_size=32, logit_transform=True)
#dataset = OMNIGLOT(batch_size=32, logit_transform=True)