-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
357 lines (265 loc) · 11.3 KB
/
main.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
from __future__ import print_function
import argparse
import os
import socket
import sys
import numpy as np
from colordata import colordata
from vae import VAE
from mdn import MDN
from logger import Logger
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
from tqdm import tqdm
parser = argparse.ArgumentParser(description='PyTorch Diverse Colorization')
parser.add_argument('dataset_key', help='Dataset')
parser.add_argument('-g', '--gpu', type=int, default=0,\
help='gpu device id')
parser.add_argument('-e', '--epochs', type=int, default=15,\
help='Number of epochs')
parser.add_argument('-b', '--batchsize', type=int, default=32,\
help='Batch size')
parser.add_argument('-z', '--hiddensize', type=int, default=64,\
help='Latent vector dimension')
parser.add_argument('-n', '--nthreads', type=int, default=4,\
help='Data loader threads')
parser.add_argument('-em', '--epochs_mdn', type=int, default=7,\
help='Number of epochs for MDN')
parser.add_argument('-m', '--nmix', type=int, default=8,\
help='Number of diverse colorization (or output gmm components)')
parser.add_argument('-lg', '--logstep', type=int, default=100,\
help='Interval to log data')
parser.add_argument('-v', '--visdom', action='store_true',\
help='Visdom visualization')
parser.add_argument('-s', '--server', type=str, default='http://vision-gpu-4.cs.illinois.edu',\
help='Visdom server')
parser.add_argument('-p', '--port_num', type=int, default=8097)
args = parser.parse_args()
if(args.visdom):
import visdom
def get_dirpaths(args):
if(args.dataset_key == 'lfw'):
out_dir = 'data/output/lfw/'
listdir = 'data/imglist/lfw/'
featslistdir = 'data/featslist/lfw/'
else:
raise NameError('[ERROR] Incorrect key: %s' % (args.dataset_key))
return out_dir, listdir, featslistdir
def vae_loss(mu, logvar, pred, gt, lossweights, batchsize):
kl_element = torch.add(torch.add(torch.add(mu.pow(2), logvar.exp()), -1), logvar.mul(-1))
kl_loss = torch.sum(kl_element).mul(.5)
gt = gt.view(-1, 64*64*2)
pred = pred.view(-1, 64*64*2)
recon_element = torch.sqrt(torch.sum(torch.mul(torch.add(gt, pred.mul(-1)).pow(2), lossweights), 1))
recon_loss = torch.sum(recon_element).mul(1./(batchsize))
recon_element_l2 = torch.sqrt(torch.sum(torch.add(gt, pred.mul(-1)).pow(2), 1))
recon_loss_l2 = torch.sum(recon_element_l2).mul(1./(batchsize))
return kl_loss, recon_loss, recon_loss_l2
def get_gmm_coeffs(gmm_params):
gmm_mu = gmm_params[..., :args.hiddensize*args.nmix]
gmm_mu.contiguous()
gmm_pi_activ = gmm_params[..., args.hiddensize*args.nmix:]
gmm_pi_activ.contiguous()
gmm_pi = F.softmax(gmm_pi_activ)
return gmm_mu, gmm_pi
def mdn_loss(gmm_params, mu, stddev, batchsize):
gmm_mu, gmm_pi = get_gmm_coeffs(gmm_params)
eps = Variable(torch.randn(stddev.size()).normal_()).cuda()
z = torch.add(mu, torch.mul(eps, stddev))
z_flat = z.repeat(1, args.nmix)
z_flat = z_flat.view(batchsize*args.nmix, args.hiddensize)
gmm_mu_flat = gmm_mu.view(batchsize*args.nmix, args.hiddensize)
dist_all = torch.sqrt(torch.sum(torch.add(z_flat, gmm_mu_flat.mul(-1)).pow(2).mul(50), 1))
dist_all = dist_all.view(batchsize, args.nmix)
dist_min, selectids = torch.min(dist_all, 1)
gmm_pi_min = torch.gather(gmm_pi, 1, selectids.view(-1, 1))
gmm_loss = torch.mean(torch.add(-1*torch.log(gmm_pi_min+1e-30), dist_min))
gmm_loss_l2 = torch.mean(dist_min)
return gmm_loss, gmm_loss_l2
def test_vae(model):
model.train(False)
out_dir, listdir, featslistdir = get_dirpaths(args)
batchsize = args.batchsize
hiddensize = args.hiddensize
nmix = args.nmix
data = colordata(\
os.path.join(out_dir, 'images'), \
listdir=listdir,\
featslistdir=featslistdir,
split='test')
nbatches = np.int_(np.floor(data.img_num/batchsize))
data_loader = DataLoader(dataset=data, num_workers=args.nthreads,\
batch_size=batchsize, shuffle=False, drop_last=True)
test_loss = 0.
for batch_idx, (batch, batch_recon_const, batch_weights, batch_recon_const_outres, _) in \
tqdm(enumerate(data_loader), total=nbatches):
input_color = Variable(batch).cuda()
lossweights = Variable(batch_weights).cuda()
lossweights = lossweights.view(batchsize, -1)
input_greylevel = Variable(batch_recon_const).cuda()
z = Variable(torch.randn(batchsize, hiddensize))
mu, logvar, color_out = model(input_color, input_greylevel, z)
_, _, recon_loss_l2 = \
vae_loss(mu, logvar, color_out, input_color, lossweights, batchsize)
test_loss = test_loss + recon_loss_l2.data[0]
test_loss = (test_loss*1.)/nbatches
model.train(True)
return test_loss
def train_vae(logger=None):
out_dir, listdir, featslistdir = get_dirpaths(args)
batchsize = args.batchsize
hiddensize = args.hiddensize
nmix = args.nmix
nepochs = args.epochs
data = colordata(\
os.path.join(out_dir, 'images'), \
listdir=listdir,\
featslistdir=featslistdir,
split='train')
nbatches = np.int_(np.floor(data.img_num/batchsize))
data_loader = DataLoader(dataset=data, num_workers=args.nthreads,\
batch_size=batchsize, shuffle=True, drop_last=True)
model = VAE()
model.cuda()
model.train(True)
optimizer = optim.Adam(model.parameters(), lr=5e-5)
itr_idx = 0
for epochs in range(nepochs):
train_loss = 0.
for batch_idx, (batch, batch_recon_const, batch_weights, batch_recon_const_outres, _) in \
tqdm(enumerate(data_loader), total=nbatches):
input_color = Variable(batch).cuda()
lossweights = Variable(batch_weights).cuda()
lossweights = lossweights.view(batchsize, -1)
input_greylevel = Variable(batch_recon_const).cuda()
z = Variable(torch.randn(batchsize, hiddensize))
optimizer.zero_grad()
mu, logvar, color_out = model(input_color, input_greylevel, z)
kl_loss, recon_loss, recon_loss_l2 = \
vae_loss(mu, logvar, color_out, input_color, lossweights, batchsize)
loss = kl_loss.mul(1e-2)+recon_loss
recon_loss_l2.detach()
loss.backward()
optimizer.step()
train_loss = train_loss + recon_loss_l2.data[0]
if(logger):
logger.update_plot(itr_idx, \
[kl_loss.data[0], recon_loss.data[0], recon_loss_l2.data[0]], \
plot_type='vae')
itr_idx += 1
if(batch_idx % args.logstep == 0):
data.saveoutput_gt(color_out.cpu().data.numpy(), \
batch.numpy(), \
'train_%05d_%05d' % (epochs, batch_idx), \
batchsize, \
net_recon_const=batch_recon_const_outres.numpy())
train_loss = (train_loss*1.)/(nbatches)
print('[DEBUG] VAE Train Loss, epoch %d has loss %f' % (epochs, train_loss))
test_loss = test_vae(model)
if(logger):
logger.update_test_plot(epochs, test_loss)
print('[DEBUG] VAE Test Loss, epoch %d has loss %f' % (epochs, test_loss))
torch.save(model.state_dict(), '%s/models/model_vae.pth' % (out_dir))
def train_mdn(logger=None):
out_dir, listdir, featslistdir = get_dirpaths(args)
batchsize = args.batchsize
hiddensize = args.hiddensize
nmix = args.nmix
nepochs = args.epochs_mdn
data = colordata(\
os.path.join(out_dir, 'images'), \
listdir=listdir,\
featslistdir=featslistdir,
split='train')
nbatches = np.int_(np.floor(data.img_num/batchsize))
data_loader = DataLoader(dataset=data, num_workers=args.nthreads,\
batch_size=batchsize, shuffle=True, drop_last=True)
model_vae = VAE()
model_vae.cuda()
model_vae.load_state_dict(torch.load('%s/models/model_vae.pth' % (out_dir)))
model_vae.train(False)
model_mdn = MDN()
model_mdn.cuda()
model_mdn.train(True)
optimizer = optim.Adam(model_mdn.parameters(), lr=1e-3)
itr_idx = 0
for epochs_mdn in range(nepochs):
train_loss = 0.
for batch_idx, (batch, batch_recon_const, batch_weights, _, batch_feats) in \
tqdm(enumerate(data_loader), total=nbatches):
input_color = Variable(batch).cuda()
input_greylevel = Variable(batch_recon_const).cuda()
input_feats = Variable(batch_feats).cuda()
z = Variable(torch.randn(batchsize, hiddensize))
optimizer.zero_grad()
mu, logvar, _ = model_vae(input_color, input_greylevel, z)
mdn_gmm_params = model_mdn(input_feats)
loss, loss_l2 = mdn_loss(mdn_gmm_params, mu, torch.sqrt(torch.exp(logvar)), batchsize)
loss.backward()
optimizer.step()
train_loss = train_loss + loss.data[0]
if(logger):
logger.update_plot(itr_idx, [loss.data[0], loss_l2.data[0]], plot_type='mdn')
itr_idx += 1
train_loss = (train_loss*1.)/(nbatches)
print('[DEBUG] Training MDN, epoch %d has loss %f' % (epochs_mdn, train_loss))
torch.save(model_mdn.state_dict(), '%s/models/model_mdn.pth' % (out_dir))
def divcolor():
out_dir, listdir, featslistdir = get_dirpaths(args)
batchsize = args.batchsize
hiddensize = args.hiddensize
nmix = args.nmix
data = colordata(\
os.path.join(out_dir, 'images'), \
listdir=listdir,\
featslistdir=featslistdir,
split='test')
nbatches = np.int_(np.floor(data.img_num/batchsize))
data_loader = DataLoader(dataset=data, num_workers=args.nthreads,\
batch_size=batchsize, shuffle=True, drop_last=True)
model_vae = VAE()
model_vae.cuda()
model_vae.load_state_dict(torch.load('%s/models/model_vae.pth' % (out_dir)))
model_vae.train(False)
model_mdn = MDN()
model_mdn.cuda()
model_mdn.load_state_dict(torch.load('%s/models/model_mdn.pth' % (out_dir)))
model_mdn.train(False)
for batch_idx, (batch, batch_recon_const, batch_weights, \
batch_recon_const_outres, batch_feats) in \
tqdm(enumerate(data_loader), total=nbatches):
input_feats = Variable(batch_feats).cuda()
mdn_gmm_params = model_mdn(input_feats)
gmm_mu, gmm_pi = get_gmm_coeffs(mdn_gmm_params)
gmm_pi = gmm_pi.view(-1, 1)
gmm_mu = gmm_mu.view(-1, hiddensize)
for j in range(batchsize):
batch_j = np.tile(batch[j, ...].numpy(), (batchsize, 1, 1, 1))
batch_recon_const_j = np.tile(batch_recon_const[j, ...].numpy(), (batchsize, 1, 1, 1))
batch_recon_const_outres_j = np.tile(batch_recon_const_outres[j, ...].numpy(), \
(batchsize, 1, 1, 1))
input_color = Variable(torch.from_numpy(batch_j)).cuda()
input_greylevel = Variable(torch.from_numpy(batch_recon_const_j)).cuda()
curr_mu = gmm_mu[j*nmix:(j+1)*nmix, :]
orderid = np.argsort(\
gmm_pi[j*nmix:(j+1)*nmix, 0].cpu().data.numpy().reshape(-1))
z = curr_mu.repeat(np.int((batchsize*1.)/nmix), 1)
_, _, color_out = model_vae(input_color, input_greylevel, z, is_train=False)
data.saveoutput_gt(color_out.cpu().data.numpy()[orderid, ...], \
batch_j[orderid, ...], \
'divcolor_%05d_%05d' % (batch_idx, j), \
nmix, \
net_recon_const=batch_recon_const_outres_j[orderid, ...])
if __name__ == '__main__':
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu)
logger = None
if(args.visdom):
outdir, _, _ = get_dirpaths(args)
logger = Logger(args.server, args.port_num, outdir)
train_vae(logger=logger)
train_mdn(logger=logger)
divcolor()