-
Notifications
You must be signed in to change notification settings - Fork 9
/
train.py
282 lines (237 loc) · 11.6 KB
/
train.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
import os
import sys
import time
import random
import logging
import argparse
from datetime import datetime
import cv2
import imageio
import numpy as np
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import itertools
import progressbar
from progressbar import Percentage, Bar, ETA
from tqdm import tqdm
from tensorboardX import SummaryWriter
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
import torchvision.utils as vutils
from data import data_utils
from misc import utils
from misc import metrics
from misc import visualize
from misc import criterion
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', default=0, type=int, help='gpu to use')
parser.add_argument('--seed', default=1, type=int, help='manual seed')
parser.add_argument('--log_dir', default='logs/p2pvg', help='base directory to save logs')
parser.add_argument('--data_root', default='data_root', help='root directory for data')
parser.add_argument('--ckpt', type=str, default='', help='load ckpt for continued training') # load ckpt
parser.add_argument('--dataset', default='mnist', help='dataset to train with (mnist | weizmann | h36m | bair)')
parser.add_argument('--num_digits', type=int, default=1, help='number of digits for moving mnist')
parser.add_argument('--nepochs', type=int, default=200, help='number of epochs to train for')
parser.add_argument('--epoch_size', type=int, default=300, help='how many batches for 1 epoch')
parser.add_argument('--lr', default=0.001, type=float, help='learning rate')
parser.add_argument('--batch_size', default=22, type=int, help='batch size')
parser.add_argument('--beta1', default=0.9, type=float, help='momentum term for adam')
parser.add_argument('--image_width', type=int, default=64, help='the height / width of the input image to network')
parser.add_argument('--channels', default=1, type=int)
parser.add_argument('--n_past', type=int, default=1, help='number of frames to condition on') # NOTE
parser.add_argument('--nsample', type=int, default=20, help='number of samples to generate per test sequence')
parser.add_argument('--rnn_size', type=int, default=256, help='dimensionality of hidden layer')
parser.add_argument('--prior_rnn_layers', type=int, default=1, help='number of layers')
parser.add_argument('--posterior_rnn_layers', type=int, default=1, help='number of layers')
parser.add_argument('--predictor_rnn_layers', type=int, default=2, help='number of layers')
parser.add_argument('--z_dim', type=int, default=10, help='dimensionality of z_t. kth: 32')
parser.add_argument('--g_dim', type=int, default=128, help='dimensionality of encoder output vector and decoder input vector')
parser.add_argument('--beta', type=float, default=0.0001, help='weighting on KL to prior')
parser.add_argument('--backbone', default='dcgan', help='model type (dcgan | vgg | mlp), mlp for h36m')
parser.add_argument('--last_frame_skip', action='store_true',
help='if true, skip connections go between frame t and frame t+t rather than last ground truth frame')
parser.add_argument('--max_seq_len', type=int, default=30, help='number of dynamic length of frames for training.')
parser.add_argument('--delta_len', type=int, default=5, help='train seq: [max_seq_len-delta_len*2, max_seq_len].')
parser.add_argument('--weight_cpc', type=float, default=1000.0, help='weighting for the L2 loss between cp and our generated frame.')
parser.add_argument('--weight_align', type=float, default=0.0, help='weighting for alignment between latent space from encoder and frame predictor.')
parser.add_argument('--skip_prob', type=float, default=0.1, help='probability to skip a frame in training.')
parser.add_argument('--qual_iter', type=int, default=1, help='frequency to eval the quantitative results.')
parser.add_argument('--quan_iter', type=int, default=1, help='frequency to eval the quantitative results.')
parser.add_argument('--test', action='store_true') # test my code
opt = parser.parse_args()
def train(model, x, start_ix, cp_ix, gen=False):
model.zero_grad()
mse_loss, kld_loss, cpc, align_loss = model(x, start_ix, cp_ix)
return mse_loss, kld_loss, cpc, align_loss
# gpu to use
os.environ["CUDA_VISIBLE_DEVICES"] = "{}".format(opt.gpu)
# setup log_dir
if opt.ckpt == '':
log_suffix = {
'dataset': opt.dataset,
'cpc': opt.weight_cpc,
'align': opt.weight_align,
'skip_prob': opt.skip_prob,
'batch_size':opt.batch_size,
'backbone': opt.backbone,
'beta': opt.beta,
'g_dim': opt.g_dim,
'z_dim': opt.z_dim,
'rnn_size': opt.rnn_size,
}
log_name = 'P2PModel'
for key, val in log_suffix.items():
log_name += '-{}_{}'.format(key, val)
opt.log_dir = '%s-%s' % (opt.log_dir, log_name)
if opt.test:
opt.log_dir = 'logs/test-%s-%s' % (os.path.basename(opt.log_dir), datetime.now().strftime('%Y-%m-%d_%H-%M'))
else:
states = torch.load(opt.ckpt)
opt.log_dir = states['opt'].log_dir
os.makedirs('%s/gen_vis/' % opt.log_dir, exist_ok=True)
# tensorboard writer
tboard_dir = os.path.join(opt.log_dir, 'tboard')
try:
writer = SummaryWriter(log_dir=tboard_dir)
except:
writer = SummaryWriter(logdir=tboard_dir)
# setups starts here
# logger
logger = utils.get_logger(logpath=os.path.join(opt.log_dir, 'logs'), filepath=__file__)
logger.info(opt)
# store cmd
cmd = utils.store_cmd(opt=opt)
# set seed
random.seed(opt.seed)
torch.manual_seed(opt.seed)
torch.cuda.manual_seed_all(opt.seed)
logger.info("[*] Random Seed: %d" % opt.seed)
# setups ends here
# setup datasets
train_data, test_data = data_utils.load_dataset(opt)
train_generator = data_utils.get_data_generator(train_data, train=True, opt=opt)
test_dl_generator = data_utils.get_data_generator(test_data, train=False, dynamic_length=True, opt=opt)
if opt.dataset == 'h36m':
from human36m import Skeleton3DVisualizer, STD_SCALE
visualizer = Skeleton3DVisualizer(train_data.skeleton.parents(), plot_3d_limit=[-2*STD_SCALE,2*STD_SCALE],
show_joint=False, show_ticks=False, render=False)
else:
visualizer = None
# set up the models
if opt.dataset != 'h36m':
if opt.backbone == 'dcgan':
if opt.image_width == 64:
import models.dcgan_64 as backbone_net
elif opt.image_width == 128:
import models.dcgan_128 as backbone_net
elif opt.backbone == 'vgg':
if opt.image_width == 64:
import models.vgg_64 as backbone_net
elif opt.image_width == 128:
import models.vgg_128 as backbone_net
elif opt.dataset == 'h36m':
import models.h36m_mlp as backbone_net
else:
raise ValueError('Unknown backbone: %s' % opt.backbone)
opt.backbone_net = backbone_net
from models.p2p_model import P2PModel
# model
model = P2PModel(opt.batch_size, opt.channels, opt.g_dim, opt.z_dim, opt.rnn_size,
opt.prior_rnn_layers, opt.posterior_rnn_layers, opt.predictor_rnn_layers, opt=opt)
# criterions
mse_criterion = nn.MSELoss()
kl_criterion = criterion.KLCriterion()
model.cuda()
mse_criterion.cuda()
kl_criterion.cuda()
if opt.ckpt != '':
states = torch.load(opt.ckpt)
start_epoch = model.load(states=states)
logger.info("[*] Load model from %s. Training continued at: %d" % (opt.ckpt, start_epoch))
else:
start_epoch = 0
# training
# num of lengths to gen for qualitative results
#qual_lengths = [10, opt.max_seq_len]
qual_lengths = [10, 30]
logger.info('[*] Using gpu: %d' % opt.gpu)
logger.info('[*] log dir: %s' % opt.log_dir)
epoch_size = opt.epoch_size
for epoch in range(start_epoch, opt.nepochs):
model.train()
epoch_mse = 0
epoch_kld = 0
epoch_align = 0
epoch_cpc = 0
progress = utils.get_progress_bar('Training epoch: %d' % epoch, epoch_size)
for i in range(epoch_size):
progress.update(i+1)
x = next(train_generator)
# train p2p model
start_ix = 0
cp_ix = -1
cp_ix = len(x)-1
mse, kld, cpc, align = train(model, x, start_ix, cp_ix)
epoch_mse += mse
epoch_kld += kld
epoch_cpc += cpc
epoch_align += align
# log training info
if i % 50 == 0 and i != 0:
step = epoch * epoch_size + i
writer.add_scalar("Train/mse", epoch_mse/i, step)
writer.add_scalar("Train/kld", epoch_kld/i, step)
writer.add_scalar("Train/cpc", epoch_cpc/i, step)
writer.add_scalar("Train/align", epoch_align/i, step)
for name, param in model.named_parameters():
if param.requires_grad:
name = name.replace('.', '/')
writer.add_histogram(name, param.data.cpu().numpy(), step)
try:
writer.add_histogram(name+'/grad', param.grad.data.cpu().numpy(), step)
except:
pass
progress.finish()
utils.clear_progressbar()
logger.info('[%02d] mse loss: %.5f | kld loss: %.5f | align loss: %.5f | cpc loss: %.5f (%d)' %
(epoch, epoch_mse/epoch_size,
epoch_kld/epoch_size,
epoch_align/epoch_size,
epoch_cpc/epoch_size,
epoch*epoch_size*opt.batch_size))
###### qualitative results ######
model.eval()
with torch.no_grad():
if (epoch + 1) % opt.qual_iter == 0: # NOTE for fast training if set opt.quan_iter larger
end = time.time()
x = next(test_dl_generator)
if opt.dataset == 'h36m':
length_to_gen = x[1].shape[0]
else:
length_to_gen = len(x)
visualize.vis_seq(model, x, epoch, length_to_gen, model_mode='full', recon_mode='test', skip_frame=False,
h36m_visualizer=visualizer, writer=writer, opt=opt)
visualize.vis_seq(model, x, epoch, length_to_gen, model_mode='posterior', recon_mode='test', skip_frame=False,
h36m_visualizer=visualizer, writer=writer, opt=opt)
visualize.vis_seq(model, x, epoch, length_to_gen, model_mode='prior', recon_mode='test', skip_frame=False,
h36m_visualizer=visualizer, writer=writer, opt=opt)
for ix, length_to_gen in enumerate(qual_lengths):
# NOTE do not skip frame for qualitative results
visualize.vis_seq(model, x, epoch, length_to_gen, model_mode='full', skip_frame=False,
h36m_visualizer=visualizer, writer=writer, opt=opt)
visualize.vis_seq(model, x, epoch, length_to_gen, model_mode='posterior', skip_frame=False,
h36m_visualizer=visualizer, writer=writer, opt=opt)
visualize.vis_seq(model, x, epoch, length_to_gen, model_mode='prior', skip_frame=False,
h36m_visualizer=visualizer, writer=writer, opt=opt)
print("[*] Time for qualitative results: %.4f" % (time.time() - end))
###### qualitative results ######
# save the model
fname = '%s/model_%d.pth' % (opt.log_dir, epoch)
model.save(fname, epoch)
logger.info("[*] Model saved at: %s" % fname)
os.system("cp %s/model_%d.pth %s/model.pth" % (opt.log_dir, epoch, opt.log_dir)) # latest ckpt
if epoch % 10 == 0:
logger.info('log dir: %s' % opt.log_dir)