-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathinbetween.py
481 lines (384 loc) · 18.7 KB
/
inbetween.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
""" This script handling the training process. """
import os
import time
import random
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data
from datasets import fetch_dataloader
from datasets import fetch_videoloader
import random
from utils.log import Logger
from torch.optim import *
import warnings
from tqdm import tqdm
import itertools
import pdb
import numpy as np
import models
import datetime
import sys
import json
import cv2
from utils.visualize_inbetween3 import visualize
# from utils.visualize_inbetween import visualize
from utils.visualize_video import visvid as visgen
import matplotlib.cm as cm
# from models.utils import make_matching_seg_plot
warnings.filterwarnings('ignore')
# a, b, c, d = check_data_distribution('/mnt/lustre/lisiyao1/dance/dance2/DanceRevolution/data/aistpp_train')
import matplotlib.pyplot as plt
import pdb
class DraftRefine():
def __init__(self, args):
self.config = args
torch.backends.cudnn.benchmark = True
torch.multiprocessing.set_sharing_strategy('file_system')
self._build()
def train(self):
opt = self.config
print(opt)
# store viz results
# eval_output_dir = Path(self.expdir)
# eval_output_dir.mkdir(exist_ok=True, parents=True)
# print('Will write visualization images to',
# 'directory \"{}\"'.format(eval_output_dir))
# load training data
model = self.model
checkpoint = torch.load(self.config.corr_weights)
dict = {k.replace('module.', ''): checkpoint['model'][k] for k in checkpoint['model']}
model.module.corr.load_state_dict(dict)
if hasattr(self.config, 'init_weight'):
checkpoint = torch.load(self.config.init_weight)
model.load_state_dict(checkpoint['model'])
# if torch.cuda.is_available():
# model.cuda() # make sure it trains on GPU
# else:
# print("### CUDA not available ###")
# return
optimizer = self.optimizer
schedular = self.schedular
mean_loss = []
log = Logger(self.config, self.expdir)
updates = 0
# set seed
random.seed(opt.seed)
torch.manual_seed(opt.seed)
torch.cuda.manual_seed(opt.seed)
np.random.seed(opt.seed)
# print(opt.seed)
# start training
for epoch in range(1, opt.epoch+1):
np.random.seed(opt.seed + epoch)
train_loader = self.train_loader
log.set_progress(epoch, len(train_loader))
batch_loss = 0
batch_epe = 0
batch_acc = 0
batch_iter = 0
model.train()
avg_time = 0
avg_num = 0
# torch.cuda.synchronize()
for i, data in enumerate(train_loader):
pred = model(data)
if True:
loss = pred['loss'].mean()
# print(loss.item(), opt.batch_size)
batch_loss += loss.item() / opt.batch_size
batch_acc += pred['Visibility Acc'].mean().item() / opt.batch_size
batch_epe += pred['EPE'].mean().item() / opt.batch_size
loss.backward()
batch_iter += 1
else:
print('Skip!')
if ((i + 1) % opt.batch_size == 0) or (i + 1 == len(train_loader)):
optimizer.step()
optimizer.zero_grad()
batch_iter = 1 if batch_iter == 0 else batch_iter
stats = {
'updates': updates,
'loss': batch_loss,
'accuracy': batch_acc,
'EPE': batch_epe
}
log.update(stats)
updates += 1
batch_loss = 0
batch_acc = 0
batch_epe = 0
batch_iter = 0
# tend = time.time()
# avg_time = (tend - tstart)
# print('Time is ', avg_time)
# torch.cuda.synchronize()
# avg_num += 1
# for name, params in model.named_parameters():
# print('-->name:, ', name, '-->grad mean', params.grad.mean())
# print("All time is ", avg_time, "AVG time is ", avg_time * 1.0 /avg_num, "number is ", avg_num, flush=True)
# save checkpoint
if epoch % opt.save_per_epochs == 0 or epoch == 1:
checkpoint = {
'model': model.state_dict(),
'config': opt,
'epoch': epoch
}
filename = os.path.join(self.ckptdir, f'epoch_{epoch}.pt')
torch.save(checkpoint, filename)
# validate
if epoch % opt.test_freq == 0:
if not os.path.exists(os.path.join(self.visdir, 'epoch' + str(epoch))):
os.mkdir(os.path.join(self.visdir, 'epoch' + str(epoch)))
eval_output_dir = os.path.join(self.visdir, 'epoch' + str(epoch))
test_loader = self.test_loader
with torch.no_grad():
# Visualize the matches.
mean_acc = []
mean_epe = []
model.eval()
for i_eval, data in enumerate(tqdm(test_loader, desc='Refining motion and visibility...')):
pred = model(data)
# for k, v in data.items():
# pred[k] = v[0]
# pred = {**pred, **data}
mean_acc.append(pred['Visibility Acc'].mean().item())
mean_epe.append(pred['EPE'].mean().item())
log.log_eval({
'updates': opt.epoch,
'Visibility Accuracy': np.mean(mean_acc),
'EPE': np.mean(mean_epe),
})
print('Epoch [{}/{}]], Vis Acc.: {:.4f}, EPE: {:.4f}'
.format(epoch, opt.epoch, np.mean(mean_acc), np.mean(mean_epe)) )
sys.stdout.flush()
# make_matching_plot(
# image0, image1, kpts0, kpts1, mkpts0, mkpts1, color,
# text, viz_path, stem, stem, True,
# True, False, 'Matches')
self.schedular.step()
def eval(self):
train_action = ['breakdance_1990', 'capoeira', 'chapa-giratoria', 'fist_fight', 'flying_knee', 'freehang_climb', 'running', 'shove', 'magic', 'tripping']
test_action = ['great_sword_slash', 'hip_hop_dancing']
train_model = ['ganfaul', 'girlscout', 'jolleen', 'kachujin', 'knight', 'maria_w_jj', 'michelle', 'peasant_girl', 'timmy', 'uriel_a_plotexia']
test_model = ['police', 'warrok']
config = self.config
if not os.path.exists(config.imwrite_dir):
os.mkdir(config.imwrite_dir)
log = Logger(self.config, self.expdir)
with torch.no_grad():
model = self.model.eval()
config = self.config
epoch_tested = self.config.testing.ckpt_epoch
if epoch_tested == 0 or epoch_tested == '0':
checkpoint = torch.load(self.config.corr_weights)
dict = {k.replace('module.', ''): checkpoint['model'][k] for k in checkpoint['model']}
model.module.corr.load_state_dict(dict)
else:
ckpt_path = os.path.join(self.ckptdir, f"epoch_{epoch_tested}.pt")
# self.device = torch.device('cuda' if config.cuda else 'cpu')
print("Evaluation...")
checkpoint = torch.load(ckpt_path)
model.load_state_dict(checkpoint['model'])
model.eval()
if not os.path.exists(os.path.join(self.evaldir, 'epoch' + str(epoch_tested))):
os.mkdir(os.path.join(self.evaldir, 'epoch' + str(epoch_tested)))
if not os.path.exists(os.path.join(self.evaldir, 'epoch' + str(epoch_tested), 'jsons')):
os.mkdir(os.path.join(self.evaldir, 'epoch' + str(epoch_tested), 'jsons'))
eval_output_dir = os.path.join(self.evaldir, 'epoch' + str(epoch_tested))
test_loader = self.test_loader
print(len(test_loader))
mean_acc = []
mean_valid_acc = []
mean_invalid_acc = []
# 144 data 10x10 is for training , 2x10 (unseen model) + 10x2 (unseen action) + 2x2 (unseen model unseen action) is for test
# record the accuracy for
mean_model_acc = []
mean_model_epe = []
mean_action_acc = []
mean_action_epe = []
mean_none_acc = []
mean_none_epe = []
mean_acc = []
mean_epe = []
mean_cd = []
model.eval()
# for i_eval, data in enumerate(tqdm(test_loader, desc='Refining motion and visibility...')):
# pred = model(data)
# # for k, v in data.items():
# # pred[k] = v[0]
# # pred = {**pred, **data}
# mean_acc.append(pred['Visibility Acc'].mean().item())
# mean_epe.append(pred['EPE'].mean().item())
# log.log_eval({
# 'updates': opt.epoch,
# 'Visibility Accuracy': np.mean(mean_acc),
# 'EPE': np.mean(mean_epe),
# })
for i_eval, data in enumerate(tqdm(test_loader, desc='Predicting Vtx Corr...')):
# if i_eval == 34:
# continue
pred = model(data)
for k, v in pred.items():
# print(k, flush=True)
pred[k] = v
pred = {**pred, **data}
mean_acc.append(pred['Visibility Acc'].mean().item())
mean_epe.append(pred['EPE'].mean().item())
unmarked = True
for model_name in train_model:
if model_name in pred['file_name']:
mean_model_acc.append(pred['Visibility Acc'])
mean_model_epe.append(pred['EPE'])
unmarked = False
break
for action_name in train_action:
if action_name in pred['file_name']:
mean_action_acc.append(pred['Visibility Acc'])
mean_action_epe.append(pred['EPE'])
unmarked = False
break
if unmarked:
mean_none_acc.append(pred['Visibility Acc'])
mean_action_epe.append(pred['EPE'])
# if 'invalid_accuracy' in pred and pred['invalid_accuracy'] is not None:
# mean_invalid_acc.append(pred['invalid_accuracy'])
img_vis = visualize(pred)
# mean_cd.append(cd.item())
file_name = pred['file_name'][0].split('/')
cv2.imwrite(os.path.join(config.imwrite_dir, (file_name[-2] + '_' + file_name[-1]) + 'png'), img_vis)
# cv2.imwrite(os.path.join(eval_output_dir, pred['file_name'][0].replace('/', '_') + '.jpg'), img_vis)
log.log_eval({
'updates': self.config.testing.ckpt_epoch,
# 'mean CD': np.mean(mean_cd),
# 'Visibility Accuracy': np.mean(mean_acc),
# 'EPE': np.mean(mean_epe),
# 'Unseen Action Accuracy': np.mean(mean_model_acc),
# 'Unseen Action EPE': np.mean(mean_model_epe),
# 'Unseen Model Accuracy': np.mean(mean_action_acc),
# 'Unseen Model EPE': np.mean(mean_action_epe),
# 'Unseen Both Accuracy': np.mean(mean_none_acc),
# 'Unseen Both Valid Accuracy': np.mean(mean_none_epe)
})
# print ('Epoch [{}/{}]], Acc.: {:.4f}, Valid Acc.{:.4f}'
# .format(epoch, opt.epoch, np.mean(mean_acc), np.mean(mean_valid_acc)) )
sys.stdout.flush()
def gen(self):
log = Logger(self.config, self.viddir)
with torch.no_grad():
model = self.model.eval()
config = self.config
epoch_tested = self.config.testing.ckpt_epoch
if epoch_tested == 0 or epoch_tested == '0':
checkpoint = torch.load(self.config.corr_weights)
dict = {k.replace('module.', ''): checkpoint['model'][k] for k in checkpoint['model']}
model.module.corr.load_state_dict(dict)
else:
ckpt_path = os.path.join(self.ckptdir, f"epoch_{epoch_tested}.pt")
# self.device = torch.device('cuda' if config.cuda else 'cpu')
print("Evaluation...")
checkpoint = torch.load(ckpt_path)
model.load_state_dict(checkpoint['model'])
model.eval()
if not os.path.exists(os.path.join(self.viddir, 'epoch' + str(epoch_tested))):
os.mkdir(os.path.join(self.viddir, 'epoch' + str(epoch_tested)))
if not os.path.exists(os.path.join(self.viddir, 'epoch' + str(epoch_tested), 'frames')):
os.mkdir(os.path.join(self.viddir, 'epoch' + str(epoch_tested), 'frames'))
if not os.path.exists(os.path.join(self.viddir, 'epoch' + str(epoch_tested), 'videos')):
os.mkdir(os.path.join(self.viddir, 'epoch' + str(epoch_tested), 'videos'))
gen_frame_dir = os.path.join(self.viddir, 'epoch' + str(epoch_tested), 'frames')
gen_video_dir = os.path.join(self.viddir, 'epoch' + str(epoch_tested), 'videos')
vid_loader = self.vid_loader
print(len(vid_loader))
mean_acc = []
mean_valid_acc = []
mean_invalid_acc = []
model.eval()
for i_eval, data in enumerate(tqdm(vid_loader, desc='Gen Video...')):
pred = model(data)
for k, v in pred.items():
pred[k] = v
pred = {**pred, **data}
img_vis = visgen(pred, config.inter_frames)
if not os.path.exists(os.path.join(gen_frame_dir, pred['folder_name0'][0])):
os.mkdir(os.path.join(gen_frame_dir, pred['folder_name0'][0]))
cv2.imwrite(os.path.join(gen_frame_dir, pred['folder_name0'][0], pred['file_name0'][0] + '_000.jpg'),img_vis[0])
for tt in range(config.inter_frames):
cv2.imwrite(os.path.join(gen_frame_dir, pred['folder_name0'][0], pred['file_name0'][0] + '_' + '{:03d}'.format(tt + 1) + '.jpg'), img_vis[tt + 1])
cv2.imwrite(os.path.join(gen_frame_dir, pred['folder_name0'][0], pred['file_name1'][0] + '_000.jpg'),img_vis[-1])
for ff in os.listdir(gen_frame_dir):
frame_dir = os.path.join(gen_frame_dir, ff)
video_file = os.path.join(gen_video_dir, f"{ff}.mp4")
cmd = f"ffmpeg -r {config.fps} -pattern_type glob -i '{frame_dir}/*.jpg' -vb 20M -vcodec mpeg4 -y '{video_file}'"
print(cmd, flush=True)
os.system(cmd)
log.log_eval({
'updates': self.config.testing.ckpt_epoch,
})
sys.stdout.flush()
def _build(self):
config = self.config
self.start_epoch = 0
self._dir_setting()
self._build_model()
if not(hasattr(config, 'need_not_train_data') and config.need_not_train_data):
self._build_train_loader()
if not(hasattr(config, 'need_not_test_data') and config.need_not_train_data):
self._build_test_loader()
if hasattr(config, 'gen_video') and config.gen_video:
self._build_video_loader()
self._build_optimizer()
def _build_model(self):
""" Define Model """
config = self.config
if hasattr(config.model, 'name'):
print(f'Experiment Using {config.model.name}')
model_class = getattr(models, config.model.name)
model = model_class(config.model)
else:
raise NotImplementedError("Wrong Model Selection")
model = nn.DataParallel(model)
self.model = model.cuda()
def _build_train_loader(self):
config = self.config
self.train_loader = fetch_dataloader(config.data.train, type='train')
def _build_test_loader(self):
config = self.config
self.test_loader = fetch_dataloader(config.data.test, type='test')
def _build_video_loader(self):
config = self.config
self.vid_loader = fetch_videoloader(config.video)
def _build_optimizer(self):
#model = nn.DataParallel(model).to(device)
config = self.config.optimizer
try:
optim = getattr(torch.optim, config.type)
except Exception:
raise NotImplementedError('not implemented optim method ' + config.type)
self.optimizer = optim(itertools.chain(self.model.module.parameters(),
),
**config.kwargs)
self.schedular = torch.optim.lr_scheduler.MultiStepLR(self.optimizer, **config.schedular_kwargs)
def _dir_setting(self):
self.expname = self.config.expname
# self.experiment_dir = os.path.join("/mnt/cache/syli/inbetween", "experiments")
self.experiment_dir = 'experiments'
self.expdir = os.path.join(self.experiment_dir, self.expname)
if not os.path.exists(self.expdir):
os.mkdir(self.expdir)
self.visdir = os.path.join(self.expdir, "vis") # -- imgs, videos, jsons
if not os.path.exists(self.visdir):
os.mkdir(self.visdir)
self.ckptdir = os.path.join(self.expdir, "ckpt")
if not os.path.exists(self.ckptdir):
os.mkdir(self.ckptdir)
self.evaldir = os.path.join(self.expdir, "eval")
if not os.path.exists(self.evaldir):
os.mkdir(self.evaldir)
self.viddir = os.path.join(self.expdir, "video")
if not os.path.exists(self.viddir):
os.mkdir(self.viddir)
# self.ckptdir = os.path.join(self.expdir, "ckpt")
# if not os.path.exists(self.ckptdir):
# os.mkdir(self.ckptdir)