-
Notifications
You must be signed in to change notification settings - Fork 0
/
VEEGAN_mix_gaussian.py
396 lines (336 loc) · 14.9 KB
/
VEEGAN_mix_gaussian.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
from __future__ import (division, print_function, )
from collections import OrderedDict
from scipy.stats import multivariate_normal
import numpy.random as npr
import numpy as np
import utils
from itertools import *
from fuel import config
from fuel.datasets import H5PYDataset, IndexableDataset
from fuel.transformers.defaults import uint8_pixels_to_floatX
from fuel.utils import find_in_data_path
from fuel.streams import DataStream
from fuel.schemes import ShuffledScheme
import torch
import torch.nn as nn
import torch.autograd as autograd
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from torch.autograd import Variable
import torch.nn.functional as F
from torch.utils.data import Dataset
import scipy.misc
import imageio
import matplotlib.gridspec as gridspec
import os, time, pickle
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import Gaussian_Sample as GS
"""Generator"""
class Generator(nn.Module):
def __init__(self, G_dim = 400):
super(Generator, self).__init__()
self.input_dim = 2
self.hid_dim = G_dim
self.output_dim = 2
self.fc = nn.Sequential(
nn.Linear(self.input_dim, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.ReLU(),
nn.Linear(self.hid_dim, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.ReLU(),
nn.Linear(self.hid_dim, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.ReLU(),
nn.Linear(self.hid_dim, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.ReLU(),
nn.Linear(self.hid_dim, self.output_dim, bias=True),
)
utils.initialize_weights(self)
def forward(self, z):
x = self.fc(z)
return x
"""Encoder"""
class Encoder(nn.Module):
def __init__(self, E_dim = 400):
super(Encoder, self).__init__()
self.input_dim = 2
self.hid_dim = E_dim
self.output_dim = 2
self.fc = nn.Sequential(
nn.Linear(self.input_dim * 2, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.LeakyReLU(0.2),
nn.Linear(self.hid_dim, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.LeakyReLU(0.2),
nn.Linear(self.hid_dim, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.LeakyReLU(0.2),
nn.Linear(self.hid_dim, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.LeakyReLU(0.2),
nn.Linear(self.hid_dim, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.LeakyReLU(0.2),
nn.Linear(self.hid_dim, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.LeakyReLU(0.2),
nn.Linear(self.hid_dim, self.hid_dim, bias=True),
nn.BatchNorm1d(self.hid_dim),
nn.LeakyReLU(0.2),
nn.Linear(self.hid_dim, self.output_dim, bias=True),
)
utils.initialize_weights(self)
def forward(self, input1, input2):
input = torch.cat((input1, input2), 1)
x= self.fc(input)
return x
"""Discriminator"""
class Discriminator(nn.Module):
def __init__(self, D_dim = 200, maxout_pieces = 5):
super(Discriminator, self).__init__()
self.input_dim = 4
self.hid_dim = D_dim
self.maxout_pieces = maxout_pieces
self.output_dim = 1
self.fc = nn.Sequential(
nn.Linear(self.input_dim, self.hid_dim * self.maxout_pieces),
nn.BatchNorm1d(self.hid_dim * self.maxout_pieces),
)
self.fcmax1 = nn.Sequential(
nn.Linear(self.hid_dim, self.hid_dim * self.maxout_pieces),
nn.BatchNorm1d(self.hid_dim * self.maxout_pieces),
)
self.fcmax2 = nn.Sequential(
nn.Linear(self.hid_dim, self.hid_dim * self.maxout_pieces),
nn.BatchNorm1d(self.hid_dim * self.maxout_pieces),
)
self.fo = nn.Sequential(
nn.Linear(self.hid_dim, self.hid_dim),
nn.BatchNorm1d(self.hid_dim),
nn.LeakyReLU(0.2),
nn.Linear(self.hid_dim, self.output_dim),
nn.Sigmoid(),
)
utils.initialize_weights(self)
def forward(self, x, z):
input = torch.cat((x, z), 1)
x = self.fc(input)
x = x.view(-1, self.maxout_pieces, self.hid_dim)
x = torch.max(x, 1)[0] # maxout layer 1
x = self.fcmax1(x)
x = x.view(-1, self.maxout_pieces, self.hid_dim)
x = torch.max(x, 1)[0] # maxout layer 2
x = self.fcmax2(x)
x = x.view(-1, self.maxout_pieces, self.hid_dim)
x = torch.max(x, 1)[0] # maxout layer 3
x = self.fo(x)
return x
class VEEGAN_mg(object):
def __init__(self, args):
# parameters
self.root = args.root
self.epoch = args.epoch
self.batch_size = args.batch_size
self.save_dir = args.save_dir
self.result_dir = args.result_dir
self.log_dir = args.log_dir
self.model_name = "MixedGaussianExample"
self.z_dim = 2
self.prior = args.prior
"""Generate data"""
train_data, valid_data = GS.main()
train_dataset = GS.Gaussian_Data(train_data)
valid_dataset = GS.Gaussian_Data(valid_data)
self.train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=self.batch_size,
shuffle=True)
self.valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset,
batch_size=self.batch_size,
shuffle=False)
self.G = Generator()
self.E = Encoder()
self.D = Discriminator()
self.G_optimizer = optim.Adam(self.G.parameters(), lr=args.lrG, betas=(args.beta1, args.beta2))
self.D_optimizer = optim.Adam(chain(self.D.parameters()), lr=args.lrD, betas=(args.beta1, args.beta2))
self.E_optimizer = optim.Adam(chain(self.E.parameters()), lr=args.lrE, betas=(args.beta1, args.beta2))
if torch.cuda.is_available():
self.G.cuda()
self.D.cuda()
self.E.cuda()
self.BCE_loss = nn.BCELoss().cuda()
else:
self.BCE_loss = nn.BCELoss()
print('---------- Networks architecture -------------')
utils.print_network(self.G)
utils.print_network(self.D)
utils.print_network(self.E)
print('-----------------------------------------------')
def __reset_grad(self):
self.E_optimizer.zero_grad()
self.G_optimizer.zero_grad()
self.D_optimizer.zero_grad()
def train(self):
self.train_hist = {}
self.train_hist['D_loss'] = []
self.train_hist['G_loss'] = []
self.train_hist['E_loss'] = []
self.train_hist['per_epoch_time'] = []
self.train_hist['total_time'] = []
if torch.cuda.is_available():
self.y_real_, self.y_fake_ = Variable(torch.ones(self.batch_size, 1).cuda()), Variable(torch.zeros(self.batch_size, 1).cuda())
else:
self.y_real_, self.y_fake_ = Variable(torch.ones(self.batch_size, 1)), Variable(torch.zeros(self.batch_size, 1))
self.D.train()
print('training start!!')
start_time = time.time()
for epoch in range(self.epoch):
# reset training mode of G and E
self.G.train()
self.E.train()
epoch_start_time = time.time()
for iter, (X, _) in enumerate(self.train_loader):
X = utils.to_var(X)
"""Discriminator"""
z = utils.generate_z(self.batch_size, self.z_dim, self.prior)
x_noise = utils.generate_z(self.batch_size, self.z_dim, self.prior)
X_hat = self.G(z)
z_hat = self.E(X, x_noise)
D_real = self.D(X, z_hat)
D_fake = self.D(X_hat, z)
D_loss = self.BCE_loss(D_real, self.y_real_) + self.BCE_loss(D_fake, self.y_fake_)
self.train_hist['D_loss'].append(D_loss.data[0])
# Optimize
D_loss.backward()
self.D_optimizer.step() # update D
self.__reset_grad()
"""Generator and Encoder"""
# Use both Discriminator and Encoder to update Generator
z = utils.generate_z(self.batch_size, self.z_dim, self.prior)
x_noise = utils.generate_z(self.batch_size, self.z_dim, self.prior)
X_hat = self.G(z)
D_fake = self.D(X_hat, z)
z_hat = self.E(X_hat, x_noise)
E_loss = torch.mean(torch.sum((z - z_hat) ** 2, 1))
self.train_hist['E_loss'].append(E_loss.data[0])
G_loss = self.BCE_loss(D_fake, self.y_real_)
total_loss = G_loss + E_loss
self.train_hist['G_loss'].append(G_loss.data[0])
# Optimize
total_loss.backward()
self.E_optimizer.step() # update E
self.G_optimizer.step() # update G
self.__reset_grad()
"""Plot"""
if (iter + 1) == self.train_loader.dataset.__len__() // self.batch_size:
print('Epoch-{}; D_loss: {:.4}; G_loss: {:.4}; E_loss: {:.4}\n'
.format(epoch, D_loss.data[0], G_loss.data[0], E_loss.data[0]))
self.visualize_results(epoch+1)
break
self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
# Save model every 5 epochs
if epoch % 5 == 0:
self.save()
self.train_hist['total_time'].append(time.time() - start_time)
print("Avg one epoch time: %.2f, total %d epochs time: %.2f" % (np.mean(self.train_hist['per_epoch_time']),
self.epoch, self.train_hist['total_time'][0]))
print("Training finish!... save training results")
self.save()
def count(self, xx):
import itertools
import collections
#X = [-2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2]
# Y = [-700, 700, 0, -1400, 1400]
# MEANS = []
# for x in X:
# MEANS.append(np.array([x] * 700 + [0]*500))
#VARIANCES = [0.05 ** 2 * np.eye(len(mean)) for mean in MEANS]
#MEANS = [x + [0] * 500 for x in MEANS]
MEANS = [np.array([i, j]) for i, j in itertools.product(range(-4, 5, 2),
range(-4, 5, 2))]
VARIANCES = [0.05 ** 2 * np.eye(len(mean)) for mean in MEANS]
SIGMA = np.log(0.05**2)
l2_store = []
for x_ in xx:
l2_store.append([np.sum((x_ - i) ** 2) for i in MEANS])
mode = np.argmin(l2_store, 1).flatten().tolist()
dis_ = [l2_store[j][i] for j, i in enumerate(mode)]
loglikehood_list = [-1.0*(np.sum(0.5 * (xx[j] - MEANS[i]) ** 2 * np.exp(-SIGMA) + 0.5 * SIGMA + 0.5 * np.log(2*np.pi))) for j, i in enumerate(mode)]
loglikehood = np.mean(loglikehood_list)
print(np.sqrt(dis_[0]))
mode_counter = [mode[i] for i in range(len(mode)) if (np.sqrt(dis_[i])) <= 0.15]
print('Number of Modes Captured: ', len(collections.Counter(mode_counter)))
print('Number of Points Falling Within 3 std. of the Nearest Mode ', np.sum(
collections.Counter(mode_counter).values()))
print('Loglikehood is: ', loglikehood)
def visualize_results(self, epoch):
self.G.eval()
self.E.eval()
save_dir = os.path.join(self.root, self.result_dir, 'mixed_gaussian', self.model_name)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# Store results
Recon = []
Original = []
Z = []
Random = []
color_vec = []
for iter, (X, label) in enumerate(self.valid_loader):
z = utils.to_var(torch.randn(self.batch_size, self.z_dim))
x_noise = utils.to_var(torch.randn(self.batch_size, self.z_dim))
X = utils.to_var(X)
label = utils.to_var(label)
z_hat = self.E(X, x_noise)
X_reconstruc = self.G(z_hat)
X_random = self.G(z)
Original += [x for x in utils.to_np(X)]
Recon += [x for x in utils.to_np(X_reconstruc)]
Z += [x for x in utils.to_np(z_hat)]
Random += [x for x in utils.to_np(X_random)]
color_vec+= [x for x in utils.to_np(label)]
Original = np.array(Original)
Recon = np.array(Recon)
Z = np.array(Z)
Random = np.array(Random)
self.count(Random[:2500])
cmap = plt.get_cmap('gnuplot')
cmap = plt.cm.jet
cmaplist = [cmap(i) for i in range(cmap.N)]
cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
ax.scatter(Original[:,0], Original[:,1], c=color_vec, cmap=cmap)
fig.savefig(os.path.join(save_dir, 'X_original' + '_epoch%03d' % epoch + '.png'))
plt.close()
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
ax.scatter(Recon[:,0], Recon[:,1], c=color_vec, cmap=cmap)
fig.savefig(os.path.join(save_dir, 'X_reconstruc' + '_epoch%03d' % epoch + '.png'))
plt.close()
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
ax.scatter(Random[:,0], Random[:,1])
fig.savefig(os.path.join(save_dir, 'X_random' + '_epoch%03d' % epoch + '.png'))
plt.close()
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
ax.scatter(Z[:,0], Z[:,1], c=color_vec, cmap=cmap)
fig.savefig(os.path.join(save_dir, 'Z_hat' + '_epoch%03d' % epoch + '.png'))
plt.close()
def save(self):
save_dir = os.path.join(self.root, self.save_dir, 'mixed_gaussian', self.model_name)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
torch.save(self.G.state_dict(), os.path.join(save_dir, self.model_name + '_G.pkl'))
torch.save(self.D.state_dict(), os.path.join(save_dir, self.model_name + '_D.pkl'))
torch.save(self.E.state_dict(), os.path.join(save_dir, self.model_name + '_E.pkl'))
with open(os.path.join(save_dir, self.model_name + '_history.pkl'), 'wb') as f:
print("Saving the model...")
pickle.dump(self.train_hist, f)
def load(self):
save_dir = os.path.join(self.root, self.save_dir, self.dataset, self.model_name)
self.G.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_G.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
self.E.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_E.pkl')))