forked from hbing-l/PoSynDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h36m_3dhp_transfer.py
1356 lines (1092 loc) · 62.6 KB
/
h36m_3dhp_transfer.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import itertools
import numpy as np
import random
from common.arguments import parse_args
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import os
import sys
import errno
import math
from einops import rearrange, repeat
from copy import deepcopy
from common.camera import *
import collections
from common.diffusionpose_3dhp_transfer import *
from common.loss import *
from common.generators_3dhp import ChunkedGenerator_Seq, UnchunkedGenerator_Seq
from common.generators import ChunkedGenerator_Seq1, UnchunkedGenerator_Seq1
from common.weight import TransformerModel
from common.lora import inject_trainable_lora, extract_lora_ups_down
from time import time
from common.utils import *
from common.logging import Logger
from torch.utils.tensorboard import SummaryWriter
from datetime import datetime
from itertools import cycle
import scipy.io as scio
#cudnn.benchmark = True
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
args = parse_args()
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
if args.evaluate != '':
description = "Evaluate!"
elif args.evaluate == '':
description = "Train!"
# initial setting
TIMESTAMP = "{0:%Y%m%dT%H-%M-%S/}".format(datetime.now())
# tensorboard
if not args.nolog:
writer = SummaryWriter(args.log+'_'+TIMESTAMP)
writer.add_text('description', description)
writer.add_text('command', 'python ' + ' '.join(sys.argv))
# logging setting
logfile = os.path.join(args.log+'_'+TIMESTAMP, 'logging.log')
sys.stdout = Logger(logfile)
print(description)
print('python ' + ' '.join(sys.argv))
print("CUDA Device Count: ", torch.cuda.device_count())
print(args)
manualSeed = 1
random.seed(manualSeed)
torch.manual_seed(manualSeed)
np.random.seed(manualSeed)
torch.cuda.manual_seed_all(manualSeed)
# if not assign checkpoint path, Save checkpoint file into log folder
if args.checkpoint=='':
args.checkpoint = args.log+'_'+TIMESTAMP
try:
# Create checkpoint directory if it does not exist
os.makedirs(args.checkpoint)
except OSError as e:
if e.errno != errno.EEXIST:
raise RuntimeError('Unable to create checkpoint directory:', args.checkpoint)
# dataset loading
print('Loading dataset...')
dataset_path = 'data/data_3d_' + args.dataset + '.npz'
if args.dataset == 'h36m':
from common.h36m_dataset import Human36mDataset
dataset = Human36mDataset(dataset_path)
elif args.dataset.startswith('humaneva'):
from common.humaneva_dataset import HumanEvaDataset
dataset = HumanEvaDataset(dataset_path)
elif args.dataset.startswith('custom'):
from common.custom_dataset import CustomDataset
dataset = CustomDataset('data/data_2d_' + args.dataset + '_' + args.keypoints + '.npz')
else:
raise KeyError('Invalid dataset')
print('Preparing target data...')
out_poses_3d_train = {}
out_poses_2d_train = {}
out_poses_3d_test = {}
out_poses_2d_test = {}
valid_frame = {}
kps_left, kps_right = [5, 6, 7, 11, 12, 13], [2, 3, 4, 8, 9, 10]
joints_left, joints_right = [5, 6, 7, 11, 12, 13], [2, 3, 4, 8, 9, 10]
data_train = np.load("./data/data_train_3dhp_ori.npz", allow_pickle=True)['data'].item()
for seq in data_train.keys():
for cam in data_train[seq][0].keys():
anim = data_train[seq][0][cam]
subject_name, seq_name = seq.split(" ")
data_3d = anim['data_3d']
data_3d[:, :14] -= data_3d[:, 14:15]
data_3d[:, 15:] -= data_3d[:, 14:15]
out_poses_3d_train[(subject_name, seq_name, cam)] = data_3d
data_2d = anim['data_2d']
data_2d[..., :2] = normalize_screen_coordinates(data_2d[..., :2], w=2048, h=2048)
out_poses_2d_train[(subject_name, seq_name, cam)] = data_2d
data_test = np.load("./data/data_test_3dhp_ori.npz", allow_pickle=True)['data'].item()
for seq in data_test.keys():
anim = data_test[seq]
valid_frame[seq] = anim["valid"]
data_3d = anim['data_3d']
data_3d[:, :14] -= data_3d[:, 14:15]
data_3d[:, 15:] -= data_3d[:, 14:15]
out_poses_3d_test[seq] = data_3d
data_2d = anim['data_2d']
if seq == "TS5" or seq == "TS6":
width = 1920
height = 1080
else:
width = 2048
height = 2048
data_2d[..., :2] = normalize_screen_coordinates(data_2d[..., :2], w=width, h=height)
out_poses_2d_test[seq] = data_2d
print('Preparing source data...')
for subject in dataset.subjects():
for action in dataset[subject].keys():
anim = dataset[subject][action]
if 'positions' in anim:
positions_3d = []
for cam in anim['cameras']:
pos_3d = world_to_camera(anim['positions'], R=cam['orientation'], t=cam['translation'])
pos_3d[:, 1:] -= pos_3d[:, :1] # Remove global offset, but keep trajectory in first position
positions_3d.append(pos_3d)
anim['positions_3d'] = positions_3d
print('Loading 2D detections...')
keypoints = np.load('data/data_2d_' + args.dataset + '_' + args.keypoints + '.npz', allow_pickle=True)
keypoints_metadata = keypoints['metadata'].item()
keypoints_symmetry = keypoints_metadata['keypoints_symmetry']
source_kps_left, source_kps_right = list(keypoints_symmetry[0]), list(keypoints_symmetry[1])
source_joints_left, source_joints_right = list(dataset.skeleton().joints_left()), list(dataset.skeleton().joints_right())
keypoints = keypoints['positions_2d'].item()
###################
for subject in dataset.subjects():
assert subject in keypoints, 'Subject {} is missing from the 2D detections dataset'.format(subject)
for action in dataset[subject].keys():
assert action in keypoints[subject], 'Action {} of subject {} is missing from the 2D detections dataset'.format(action, subject)
if 'positions_3d' not in dataset[subject][action]:
continue
for cam_idx in range(len(keypoints[subject][action])):
# We check for >= instead of == because some videos in H3.6M contain extra frames
mocap_length = dataset[subject][action]['positions_3d'][cam_idx].shape[0]
assert keypoints[subject][action][cam_idx].shape[0] >= mocap_length
if keypoints[subject][action][cam_idx].shape[0] > mocap_length:
# Shorten sequence
keypoints[subject][action][cam_idx] = keypoints[subject][action][cam_idx][:mocap_length]
assert len(keypoints[subject][action]) == len(dataset[subject][action]['positions_3d'])
for subject in keypoints.keys():
for action in keypoints[subject]:
for cam_idx, kps in enumerate(keypoints[subject][action]):
# Normalize camera frame
cam = dataset.cameras()[subject][cam_idx]
kps[..., :2] = normalize_screen_coordinates(kps[..., :2], w=cam['res_w'], h=cam['res_h'])
keypoints[subject][action][cam_idx] = kps
subjects_train = args.subjects_train.split(',')
subjects_semi = [] if not args.subjects_unlabeled else args.subjects_unlabeled.split(',')
if not args.render:
subjects_test = args.subjects_test.split(',')
else:
subjects_test = [args.viz_subject]
def fetch(subjects, action_filter=None, subset=1, parse_3d_poses=True):
out_poses_3d = []
out_poses_2d = []
out_camera_params = []
for subject in subjects:
for action in keypoints[subject].keys():
if action_filter is not None:
found = False
for a in action_filter:
if action.startswith(a):
found = True
break
if not found:
continue
poses_2d = keypoints[subject][action]
for i in range(len(poses_2d)): # Iterate across cameras
out_poses_2d.append(poses_2d[i])
if subject in dataset.cameras():
cams = dataset.cameras()[subject]
assert len(cams) == len(poses_2d), 'Camera count mismatch'
for cam in cams:
if 'intrinsic' in cam:
out_camera_params.append(cam['intrinsic'])
if parse_3d_poses and 'positions_3d' in dataset[subject][action]:
poses_3d = dataset[subject][action]['positions_3d']
assert len(poses_3d) == len(poses_2d), 'Camera count mismatch'
for i in range(len(poses_3d)): # Iterate across cameras
out_poses_3d.append(poses_3d[i])
if len(out_camera_params) == 0:
out_camera_params = None
if len(out_poses_3d) == 0:
out_poses_3d = None
stride = args.downsample
if subset < 1:
for i in range(len(out_poses_2d)):
n_frames = int(round(len(out_poses_2d[i])//stride * subset)*stride)
start = deterministic_random(0, len(out_poses_2d[i]) - n_frames + 1, str(len(out_poses_2d[i])))
out_poses_2d[i] = out_poses_2d[i][start:start+n_frames:stride]
if out_poses_3d is not None:
out_poses_3d[i] = out_poses_3d[i][start:start+n_frames:stride]
elif stride > 1:
# Downsample as requested
for i in range(len(out_poses_2d)):
out_poses_2d[i] = out_poses_2d[i][::stride]
if out_poses_3d is not None:
out_poses_3d[i] = out_poses_3d[i][::stride]
return out_camera_params, out_poses_3d, out_poses_2d
action_filter = None if args.actions == '*' else args.actions.split(',')
if action_filter is not None:
print('Selected actions:', action_filter)
cameras_train, poses_train, poses_train_2d = fetch(subjects_train, action_filter, subset=args.subset) # poses_train: list len 600 poses_train[0].shape=[1383, 17, 3]
# set receptive_field as number assigned
receptive_field = args.number_of_frames
print('INFO: Receptive field: {} frames'.format(receptive_field))
if not args.nolog:
writer.add_text(args.log+'_'+TIMESTAMP + '/Receptive field', str(receptive_field))
pad = (receptive_field -1) // 2 # Padding on each side
min_loss = args.min_loss
model_pos_train = D3DP(args, joints_left, joints_right, is_train=True, num_proposals=args.num_proposals, sampling_timesteps=args.sampling_timesteps)
model_weight_train = TransformerModel(ntoken=3, ninp=512, nhead=8, nhid=512, nlayers=8)
model_pos_test_temp = D3DP(args,joints_left, joints_right, is_train=False)
model_weight_temp = TransformerModel(ntoken=3, ninp=512, nhead=8, nhid=512, nlayers=8)
model_pos = D3DP(args,joints_left, joints_right, is_train=False)
model_weight = TransformerModel(ntoken=3, ninp=512, nhead=8, nhid=512, nlayers=8)
#################
causal_shift = 0
if not args.nolog:
writer.add_text(args.log+'_'+TIMESTAMP + '/Trainable parameter count', str(model_params/1000000) + ' Million')
# make model parallel
if torch.cuda.is_available():
model_pos = nn.DataParallel(model_pos)
model_pos = model_pos.cuda()
model_pos_train = nn.DataParallel(model_pos_train)
model_pos_train = model_pos_train.cuda()
model_pos_test_temp = nn.DataParallel(model_pos_test_temp)
model_pos_test_temp = model_pos_test_temp.cuda()
model_weight = nn.DataParallel(model_weight)
model_weight = model_weight.cuda()
model_weight_train = nn.DataParallel(model_weight_train)
model_weight_train = model_weight_train.cuda()
model_weight_temp = nn.DataParallel(model_weight_temp)
model_weight_temp = model_weight_temp.cuda()
checkpoint = torch.load('checkpoint/model_h36m/h36m_best_epoch.bin', map_location=lambda storage, loc: storage)
print('This model was trained for {} epochs'.format(checkpoint['epoch']))
model_pos_train.load_state_dict(checkpoint['model_pos'], strict=False)
# freeze the denoiser parameters, inject the lora parameters
model_pos_train.requires_grad_(False)
lora_params, train_names = inject_trainable_lora(model_pos_train)
model_pos_test_temp.requires_grad_(False)
lora_params2, train_names2 = inject_trainable_lora(model_pos_test_temp)
model_pos.requires_grad_(False)
lora_params3, train_names3 = inject_trainable_lora(model_pos)
if args.resume or args.evaluate:
chk_filename = os.path.join(args.checkpoint, args.resume if args.resume else args.evaluate)
# chk_filename = args.resume or args.evaluate
print('Loading checkpoint', chk_filename)
checkpoint = torch.load(chk_filename, map_location=lambda storage, loc: storage)
print('This model was trained for {} epochs'.format(checkpoint['epoch']))
model_pos_train.load_state_dict(checkpoint['model_pos'], strict=False)
model_pos.load_state_dict(checkpoint['model_pos'], strict=False)
# get target training data
train_test_generator = UnchunkedGenerator_Seq(None, out_poses_3d_test, out_poses_2d_test,
pad=pad, causal_shift=causal_shift, augment=args.data_augmentation,
kps_left=kps_left, kps_right=kps_right, joints_left=joints_left, joints_right=joints_right, valid_frame=valid_frame)
test_generator = UnchunkedGenerator_Seq(None, out_poses_3d_test, out_poses_2d_test,
pad=pad, causal_shift=causal_shift, augment=False,
kps_left=kps_left, kps_right=kps_right, joints_left=joints_left, joints_right=joints_right, valid_frame=valid_frame)
print('INFO: Testing on {} frames'.format(test_generator.num_frames()))
if not args.nolog:
writer.add_text(args.log+'_'+TIMESTAMP + '/Testing Frames', str(test_generator.num_frames()))
# get source training data
source_train_generator = UnchunkedGenerator_Seq1(cameras_train, poses_train, poses_train_2d,
pad=pad, causal_shift=causal_shift, augment=False)
def eval_data_prepare(receptive_field, inputs_2d, inputs_3d, valid_frame):
assert inputs_2d.shape[:-1] == inputs_3d.shape[:-1], "2d and 3d inputs shape must be same! "+str(inputs_2d.shape)+str(inputs_3d.shape)
inputs_2d_p = torch.squeeze(inputs_2d)
inputs_3d_p = torch.squeeze(inputs_3d)
valid_frame = valid_frame.unsqueeze(1)
if inputs_2d_p.shape[0] / receptive_field > inputs_2d_p.shape[0] // receptive_field:
out_num = inputs_2d_p.shape[0] // receptive_field+1
elif inputs_2d_p.shape[0] / receptive_field == inputs_2d_p.shape[0] // receptive_field:
out_num = inputs_2d_p.shape[0] // receptive_field
eval_input_2d = torch.empty(out_num, receptive_field, inputs_2d_p.shape[1], inputs_2d_p.shape[2])
eval_input_3d = torch.empty(out_num, receptive_field, inputs_3d_p.shape[1], inputs_3d_p.shape[2])
eval_valid_frame = torch.empty(out_num, receptive_field, 1)
for i in range(out_num-1):
eval_input_2d[i,:,:,:] = inputs_2d_p[i*receptive_field:i*receptive_field+receptive_field,:,:]
eval_input_3d[i,:,:,:] = inputs_3d_p[i*receptive_field:i*receptive_field+receptive_field,:,:]
eval_valid_frame[i, :, :] = valid_frame[i * receptive_field:i * receptive_field + receptive_field, :]
if inputs_2d_p.shape[0] < receptive_field:
from torch.nn import functional as F
pad_right = receptive_field-inputs_2d_p.shape[0]
inputs_2d_p = rearrange(inputs_2d_p, 'b f c -> f c b')
inputs_2d_p = F.pad(inputs_2d_p, (0,pad_right), mode='replicate')
# inputs_2d_p = np.pad(inputs_2d_p, ((0, receptive_field-inputs_2d_p.shape[0]), (0, 0), (0, 0)), 'edge')
inputs_2d_p = rearrange(inputs_2d_p, 'f c b -> b f c')
if inputs_3d_p.shape[0] < receptive_field:
pad_right = receptive_field-inputs_3d_p.shape[0]
inputs_3d_p = rearrange(inputs_3d_p, 'b f c -> f c b')
inputs_3d_p = F.pad(inputs_3d_p, (0,pad_right), mode='replicate')
inputs_3d_p = rearrange(inputs_3d_p, 'f c b -> b f c')
if valid_frame.shape[0] < receptive_field:
pad_right = receptive_field-valid_frame.shape[0]
valid_frame = rearrange(valid_frame, 'f c -> c f')
valid_frame = F.pad(valid_frame, (0,pad_right), mode='replicate')
valid_frame = rearrange(valid_frame, 'c f -> f c')
eval_input_2d[-1,:,:,:] = inputs_2d_p[-receptive_field:,:,:]
eval_input_3d[-1,:,:,:] = inputs_3d_p[-receptive_field:,:,:]
eval_valid_frame[-1, :, :] = valid_frame[-receptive_field:, :]
return eval_input_2d, eval_input_3d, eval_valid_frame
def source_eval_data_prepare(receptive_field, inputs_2d, inputs_3d):
assert inputs_2d.shape[:-1] == inputs_3d.shape[:-1], "2d and 3d inputs shape must be same! "+str(inputs_2d.shape)+str(inputs_3d.shape)
inputs_2d_p = torch.squeeze(inputs_2d)
inputs_3d_p = torch.squeeze(inputs_3d)
if inputs_2d_p.shape[0] / receptive_field > inputs_2d_p.shape[0] // receptive_field:
out_num = inputs_2d_p.shape[0] // receptive_field+1
elif inputs_2d_p.shape[0] / receptive_field == inputs_2d_p.shape[0] // receptive_field:
out_num = inputs_2d_p.shape[0] // receptive_field
eval_input_2d = torch.empty(out_num, receptive_field, inputs_2d_p.shape[1], inputs_2d_p.shape[2])
eval_input_3d = torch.empty(out_num, receptive_field, inputs_3d_p.shape[1], inputs_3d_p.shape[2])
for i in range(out_num-1):
eval_input_2d[i,:,:,:] = inputs_2d_p[i*receptive_field:i*receptive_field+receptive_field,:,:]
eval_input_3d[i,:,:,:] = inputs_3d_p[i*receptive_field:i*receptive_field+receptive_field,:,:]
if inputs_2d_p.shape[0] < receptive_field:
from torch.nn import functional as F
pad_right = receptive_field-inputs_2d_p.shape[0]
inputs_2d_p = rearrange(inputs_2d_p, 'b f c -> f c b')
inputs_2d_p = F.pad(inputs_2d_p, (0,pad_right), mode='replicate')
# inputs_2d_p = np.pad(inputs_2d_p, ((0, receptive_field-inputs_2d_p.shape[0]), (0, 0), (0, 0)), 'edge')
inputs_2d_p = rearrange(inputs_2d_p, 'f c b -> b f c')
if inputs_3d_p.shape[0] < receptive_field:
pad_right = receptive_field-inputs_3d_p.shape[0]
inputs_3d_p = rearrange(inputs_3d_p, 'b f c -> f c b')
inputs_3d_p = F.pad(inputs_3d_p, (0,pad_right), mode='replicate')
inputs_3d_p = rearrange(inputs_3d_p, 'f c b -> b f c')
eval_input_2d[-1,:,:,:] = inputs_2d_p[-receptive_field:,:,:]
eval_input_3d[-1,:,:,:] = inputs_3d_p[-receptive_field:,:,:]
return eval_input_2d, eval_input_3d
def pose_post_process(pose_pred, data_list, keys, receptive_field):
for ii in range(pose_pred.shape[0] - 1):
data_list[keys][:, ii * receptive_field:(ii + 1) * receptive_field] = pose_pred[ii]
data_list[keys][:, -receptive_field:] = pose_pred[-1]
data_list[keys] = data_list[keys].transpose(3, 2, 1, 0)
return data_list
def cam_mm_to_pix(cam, cam_data):
# w, h, ss_x, ss_y
mx = cam_data[0] / cam_data[2]
my = cam_data[1] / cam_data[3]
cam[0] = cam[0] * mx
cam[1] = cam[1] * my
cam[2] = cam[2] * mx + cam_data[0]/2
cam[3] = cam[3] * my + cam_data[1]/2
return cam
###################
def global_position_redistribution(source_3d, target_2d, camera_params):
"""
global position re-distribution torch version
"""
source_3d = source_3d.detach()
fx, fy = camera_params[0, :2]
if source_3d.shape[0] <= target_2d.shape[0]:
index = torch.randperm(source_3d.shape[0])
target_2d = target_2d[index]
camera_params = camera_params.repeat(source_3d.shape[0], 1)
else:
index = torch.randperm(target_2d.shape[0])
source_3d = source_3d[index]
camera_params = camera_params.repeat(target_2d.shape[0], 1)
# calculate 2d scale
w = torch.max(target_2d[..., 0], dim=-1)[0] - torch.min(target_2d[..., 0], dim=-1)[0]
h = torch.max(target_2d[..., 1], dim=-1)[0] - torch.min(target_2d[..., 1], dim=-1)[0]
s = (w + h) / 2
# calculate 3d range
dx = torch.max(source_3d[..., 0], dim=-1)[0] - torch.min(source_3d[..., 0], dim=-1)[0]
dy = torch.max(source_3d[..., 1], dim=-1)[0] - torch.min(source_3d[..., 1], dim=-1)[0]
# calculate z
z = (fx * dx + fy * dy) / (2 * s)
# process with camera params
target_2d[..., 0, :] -= camera_params[..., 2:4] # c
target_2d[..., 0, :] /= camera_params[..., :2] # f
u, v = target_2d[..., 0, 0], target_2d[..., 0, 1]
# calculate x
x, y = z * u, z * v
x = x.reshape(-1, 1)
y = y.reshape(-1, 1)
z = z.reshape(-1, 1)
position = torch.stack([x,y,z], axis=1).reshape(-1, 1, 3)
source_3d = source_3d - source_3d[:, :1, :] + position
return source_3d, camera_params
# Training start
if not args.evaluate:
lr = args.learning_rate
optimizer = optim.Adam(itertools.chain(*lora_params, model_weight_train.parameters()), lr=1e-4)
lr_decay = args.lr_decay
losses_3d_train = []
losses_3d_pos_train = []
losses_2d_pos_train = []
losses_3d_train_eval = []
losses_3d_valid = []
losses_3d_depth_valid = []
epoch = 0
best_epoch = 0
initial_momentum = 0.1
final_momentum = 0.001
print('INFO: Training on {} frames'.format(train_test_generator.num_frames()))
if not args.nolog:
writer.add_text(args.log+'_'+TIMESTAMP + '/Training Frames', str(train_test_generator.num_frames()))
if args.resume:
epoch = checkpoint['epoch']
if 'optimizer' in checkpoint and checkpoint['optimizer'] is not None:
optimizer.load_state_dict(checkpoint['optimizer'])
else:
print('WARNING: this checkpoint does not contain an optimizer state. The optimizer will be reinitialized.')
if not args.coverlr:
lr = checkpoint['lr']
print('** Note: reported losses are averaged over all frames.')
print('** The final evaluation will be carried out after the last training epoch.')
# Pos model only
while epoch < args.epochs:
start_time = time()
epoch_loss_3d_train = 0
epoch_loss_3d_pos_train = 0
epoch_loss_2d_pos_train = 0
epoch_loss_3d_pos_true = 0
model_pos_train.train()
model_weight_train.train()
iteration = 0
num_frames = train_test_generator.num_frames()
N = 0
data_inference_all = {}
data_inference_mean = {}
data_inference_h_min = {}
data_inference_joint_min = {}
data_inference_reproj_min = {}
cam_1 = torch.tensor([7.32506, 7.32506, -0.0322884, 0.0929296, 0, 0, 0, 0, 0])
cam_data_1 = [2048, 2048, 10, 10] #width, height, sensorSize_x, sensorSize_y
cam_2 = torch.tensor([8.770747185, 8.770747185, -0.104908645, 0.104899704, 0, 0, 0, 0, 0])
cam_data_2 = [1920, 1080, 10, 5.625] # width, height, sensorSize_x, sensorSize_y
cam_1 = cam_mm_to_pix(cam_1, cam_data_1)
cam_2 = cam_mm_to_pix(cam_2, cam_data_2)
quickdebug=args.debug
for ((_, batch, batch_2d, batch_valid, keys ), (_, src_batch_3d, src_batch_2d))in zip(train_test_generator.next_epoch(), cycle(source_train_generator.next_epoch())):
if iteration % 1000 == 0:
print("%d/%d"% (iteration, num_frames))
# target
tar_inputs_2d = torch.from_numpy(batch_2d.astype('float32'))
tar_inputs_3d = torch.from_numpy(batch.astype('float32'))
tar_inputs_valid = torch.from_numpy(batch_valid.astype('float32'))
_, f_sz, j_sz, c_sz = tar_inputs_3d.shape
print(keys)
##### apply test-time-augmentation (following Videopose3d)
tar_inputs_2d_flip = tar_inputs_2d.clone()
tar_inputs_2d_flip [:, :, :, 0] *= -1
tar_inputs_2d_flip[:, :, kps_left + kps_right,:] = tar_inputs_2d_flip[:, :, kps_right + kps_left,:]
##### convert size
tar_inputs_3d_p = tar_inputs_3d
tar_inputs_2d, tar_inputs_3d, tar_valid_frame = eval_data_prepare(receptive_field, tar_inputs_2d, tar_inputs_3d_p, inputs_valid)
tar_inputs_2d_flip, _, _ = eval_data_prepare(receptive_field, tar_inputs_2d_flip, tar_inputs_3d_p, tar_inputs_valid)
if torch.cuda.is_available():
tar_inputs_2d = tar_inputs_2d.cuda()
tar_inputs_2d_flip = tar_inputs_2d_flip.cuda()
tar_inputs_3d = tar_inputs_3d.cuda() # [26, 243, 17, 3]
bs = 2
total_batch = (tar_inputs_3d.shape[0] + bs - 1) // bs
for batch_cnt in range(total_batch):
if (batch_cnt + 1) * bs > tar_inputs_3d.shape[0]:
tar_inputs_2d_single = tar_inputs_2d[batch_cnt * bs:]
tar_inputs_2d_flip_single = tar_inputs_2d_flip[batch_cnt * bs:]
tar_inputs_3d_single = tar_inputs_3d[batch_cnt * bs:]
# valid_frame_single = valid_frame[batch_cnt * bs:]
else:
tar_inputs_2d_single = tar_inputs_2d[batch_cnt * bs:(batch_cnt+1) * bs]
tar_inputs_2d_flip_single = tar_inputs_2d_flip[batch_cnt * bs:(batch_cnt+1) * bs]
tar_inputs_3d_single = tar_inputs_3d[batch_cnt * bs:(batch_cnt+1) * bs]
# valid_frame_single = valid_frame[batch_cnt * bs:(batch_cnt + 1) * bs]
traj = tar_inputs_3d_single[:, :, 14:15].clone()
tar_inputs_3d_single[:, :, 14] = 0 # [2, 243, 17, 3]
optimizer.zero_grad()
predicted_3d_pos_single = model_pos_train(tar_inputs_2d_single, tar_inputs_3d_single, input_2d_flip=tar_inputs_2d_flip_single) #b, t, h, f, j, c
b,t,h,f,j,c = predicted_3d_pos_single.shape
predicted_3d_pos_single = predicted_3d_pos_single.reshape(-1, j, c)
predicted_3d_pos_single = model_weight_train(predicted_3d_pos_single)
predicted_3d_pos_single = predicted_3d_pos_single.reshape(b, t, h, f, j, c)
predicted_3d_pos_single[:, :, :, :, 14] = 0
# find the best 3d pose for target input
inputs_traj_single_all = traj.unsqueeze(1).unsqueeze(1).repeat(1, t, h, 1, 1, 1)
predicted_3d_pos_abs_single = predicted_3d_pos_single + inputs_traj_single_all
#predicted_3d_pos_abs_single = predicted_3d_pos_abs_single/1000
predicted_3d_pos_abs_single = predicted_3d_pos_abs_single.reshape(b * t * h * f, j_sz, c_sz)
if keys == "TS5" or keys == "TS6":
cam = cam_2.clone()
cam_data = cam_data_2.copy()
reproject_func = project_to_2d
else:
cam = cam_1.clone()
cam_data = cam_data_1.copy()
reproject_func = project_to_2d_linear
cam_single_all = cam.unsqueeze(0).repeat(b * t * h * f, 1).cuda()
reproj_2d = reproject_func(predicted_3d_pos_abs_single, cam_single_all)
reproj_2d = reproj_2d.reshape(b, t, h, f, j_sz, 2)
cam_single_gt = cam.unsqueeze(0).repeat(b * f, 1).cuda()
input_3d_reproj = inputs_3d_single + traj
input_3d_reproj = input_3d_reproj.reshape(b * f, 17, 3)
reproj_2d_gt = reproject_func(input_3d_reproj, cam_single_gt)
reproj_2d_gt = reproj_2d_gt.reshape(b, f, 17, 2)
target_2d = torch.from_numpy(image_coordinates(tar_inputs_2d_single[..., :2].cpu().numpy(), w=cam_data[0], h=cam_data[1])).cuda()
target_2d = target_2d.unsqueeze(1).unsqueeze(1).repeat(1, t, h, 1, 1, 1)
errors_2d = torch.norm(reproj_2d - target_2d, dim=len(target_2d.shape) - 1) # b, t, h, f, n
reproj_min_indices = torch.min(errors_2d, dim=2, keepdim=True).indices # b,t,1,f,n
reproj_min_indices = reproj_min_indices.unsqueeze(-1).repeat(1, 1, 1, 1, 1, c_sz)
reproj_min_pose = torch.gather(predicted_3d_pos_single, 2, reproj_min_indices).squeeze(2)
reproj_all_min_pose = reproj_min_pose.unsqueeze(2).repeat(1, 1, h, 1, 1, 1)
loss_2d = torch.mean(torch.min(errors_2d, dim=2, keepdim=True).values)
loss_3d = mpjpe(reproj_all_min_pose, predicted_3d_pos_single)
lambd1 = 0.5
lambd2 = 1
loss_total = lambd1 * loss_2d + lambd2 * loss_3d
loss_total.backward(loss_total.clone().detach())
loss_total = torch.mean(loss_total)
epoch_loss_3d_train += inputs_3d_single.shape[0] * inputs_3d_single.shape[1] * loss_total.item()
epoch_loss_3d_pos_train += inputs_3d_single.shape[0] * inputs_3d_single.shape[1] * loss_3d.item()
epoch_loss_2d_pos_train += inputs_3d_single.shape[0] * inputs_3d_single.shape[1] * loss_2d.item()
N += inputs_3d_single.shape[0] * inputs_3d_single.shape[1]
optimizer.step()
# source
src_inputs_3d = torch.from_numpy(src_batch_3d.astype('float32'))
src_inputs_2d = torch.from_numpy(src_batch_2d.astype('float32'))
# print("source shape ", src_inputs_3d.shape)
b, f, n, c = src_inputs_3d.shape
source_3d = src_inputs_3d.reshape(b*f, n, c)
tb, tf, tn, tc = inputs_2d.shape
target_2d = inputs_2d.reshape(tb*tf, tn, tc).cpu()
if keys == "TS5" or keys == "TS6":
cam = cam_2.clone()
cam_data = cam_data_2.copy()
reproject_func = project_to_2d
else:
cam = cam_1.clone()
cam_data = cam_data_1.copy()
reproject_func = project_to_2d_linear
camera_params = cam.unsqueeze(0)
source_3d, camera_params = global_position_redistribution(source_3d, target_2d, camera_params)
reproj_2d = reproject_func(source_3d, camera_params)
src_inputs_2d = reproj_2d.reshape(b, -1, n, 2).float()
src_inputs_3d = source_3d.reshape(b, -1, n, 3).float()
# print("transform source shape ", src_inputs_3d.shape)
##### apply test-time-augmentation (following Videopose3d)
src_inputs_2d_flip = src_inputs_2d.clone()
src_inputs_2d_flip[:, :, :, 0] *= -1
src_inputs_2d_flip[:, :, source_kps_left + source_kps_right, :] = src_inputs_2d_flip[:, :, source_kps_right + source_kps_left, :]
##### convert size
src_inputs_3d_p = src_inputs_3d
src_inputs_2d, src_inputs_3d = source_eval_data_prepare(receptive_field, src_inputs_2d, src_inputs_3d_p)
src_inputs_2d_flip, _ = source_eval_data_prepare(receptive_field, src_inputs_2d_flip, src_inputs_3d_p)
if torch.cuda.is_available():
src_inputs_3d = src_inputs_3d.cuda() # [6, 243, 17, 3]
src_inputs_2d = src_inputs_2d.cuda() # [6, 243, 17, 2]
src_inputs_2d_flip = src_inputs_2d_flip.cuda()
src_inputs_3d[:, :, 0] = 0
inputs_3d = src_inputs_3d
inputs_2d = src_inputs_2d
inputs_2d_flip = src_inputs_2d_flip
bs = 2
total_batch = (inputs_3d.shape[0] + bs - 1) // bs
for batch_cnt in range(total_batch):
if (batch_cnt + 1) * bs > inputs_3d.shape[0]:
inputs_2d_single = inputs_2d[batch_cnt * bs:]
inputs_2d_flip_single = inputs_2d_flip[batch_cnt * bs:]
inputs_3d_single = inputs_3d[batch_cnt * bs:]
# valid_frame_single = valid_frame[batch_cnt * bs:]
else:
inputs_2d_single = inputs_2d[batch_cnt * bs:(batch_cnt+1) * bs]
inputs_2d_flip_single = inputs_2d_flip[batch_cnt * bs:(batch_cnt+1) * bs]
inputs_3d_single = inputs_3d[batch_cnt * bs:(batch_cnt+1) * bs]
# valid_frame_single = valid_frame[batch_cnt * bs:(batch_cnt + 1) * bs]
traj = inputs_3d_single[:, :, 14:15].clone()
inputs_3d_single[:, :, 14] = 0 # [2, 243, 17, 3]
optimizer.zero_grad()
predicted_3d_pos_single = model_pos_train(inputs_2d_single, inputs_3d_single, input_2d_flip=inputs_2d_flip_single) #b, t, h, f, j, c
b,t,h,f,j,c = predicted_3d_pos_single.shape
predicted_3d_pos_single = predicted_3d_pos_single.reshape(-1, j, c)
predicted_3d_pos_single = model_weight_train(predicted_3d_pos_single)
predicted_3d_pos_single = predicted_3d_pos_single.reshape(b, t, h, f, j, c)
predicted_3d_pos_single[:, :, :, :, 14] = 0
inputs_traj_single_all = traj.unsqueeze(1).unsqueeze(1).repeat(1, t, h, 1, 1, 1)
predicted_3d_pos_abs_single = predicted_3d_pos_single + inputs_traj_single_all
predicted_3d_pos_abs_single = predicted_3d_pos_abs_single.reshape(b * t * h * f, j_sz, c_sz)
if keys == "TS5" or keys == "TS6":
cam = cam_2.clone()
cam_data = cam_data_2.copy()
reproject_func = project_to_2d
else:
cam = cam_1.clone()
cam_data = cam_data_1.copy()
reproject_func = project_to_2d_linear
cam_single_all = cam.unsqueeze(0).repeat(b * t * h * f, 1).cuda()
reproj_2d = reproject_func(predicted_3d_pos_abs_single, cam_single_all)
reproj_2d = reproj_2d.reshape(b, t, h, f, j_sz, 2)
#reproj_2d[..., :2] = torch.from_numpy(normalize_screen_coordinates(reproj_2d[..., :2].cpu().numpy(), w=cam_data[0],h=cam_data[1])).cuda()
cam_single_gt = cam.unsqueeze(0).repeat(b * f, 1).cuda()
input_3d_reproj = inputs_3d_single + traj
input_3d_reproj = input_3d_reproj.reshape(b * f, 17, 3)
reproj_2d_gt = reproject_func(input_3d_reproj, cam_single_gt)
reproj_2d_gt = reproj_2d_gt.reshape(b, f, 17, 2)
src_2d = torch.from_numpy(image_coordinates(inputs_2d_single[..., :2].cpu().numpy(), w=cam_data[0], h=cam_data[1])).cuda()
src_2d = src_2d.unsqueeze(1).unsqueeze(1).repeat(1, t, h, 1, 1, 1)
errors_2d = torch.norm(reproj_2d - src_2d, dim=len(src_2d.shape) - 1) # b, t, h, f, n
reproj_min_indices = torch.min(errors_2d, dim=2, keepdim=True).indices # b,t,1,f,n
reproj_min_indices = reproj_min_indices.unsqueeze(-1).repeat(1, 1, 1, 1, 1, c_sz)
reproj_min_pose = torch.gather(predicted_3d_pos_single, 2, reproj_min_indices).squeeze(2)
reproj_all_min_pose = reproj_min_pose.unsqueeze(2).repeat(1, 1, h, 1, 1, 1)
loss_2d = torch.mean(torch.min(errors_2d, dim=2, keepdim=True).values)
label = inputs_3d_single.unsqueeze(1).unsqueeze(1).repeat(1, t, h, 1, 1, 1)
loss_3d = mpjpe(label, predicted_3d_pos_single)
lambd1 = 0.1
lambd2 = 1
loss_total = lambd1 * loss_2d + lambd2 * loss_3d
loss_total.backward(loss_total.clone().detach())
loss_total = torch.mean(loss_total)
epoch_loss_3d_train += inputs_3d_single.shape[0] * inputs_3d_single.shape[1] * loss_total.item()
epoch_loss_3d_pos_train += inputs_3d_single.shape[0] * inputs_3d_single.shape[1] * loss_3d.item()
epoch_loss_2d_pos_train += inputs_3d_single.shape[0] * inputs_3d_single.shape[1] * loss_2d.item()
N += inputs_3d_single.shape[0] * inputs_3d_single.shape[1]
optimizer.step()
iteration += 1
losses_3d_train.append(epoch_loss_3d_train / N)
losses_3d_pos_train.append(epoch_loss_3d_pos_train / N)
losses_2d_pos_train.append(epoch_loss_2d_pos_train / N)
with torch.no_grad():
model_pos_test_temp.load_state_dict(model_pos_train.state_dict(), strict=False)
model_pos_test_temp.eval()
model_weight_temp.load_state_dict(model_weight_train.state_dict(), strict=False)
model_weight_temp.eval()
epoch_loss_3d_valid = None
epoch_loss_3d_depth_valid = 0
epoch_loss_traj_valid = 0
epoch_loss_2d_valid = 0
epoch_loss_3d_vel = 0
N = 0
iteration = 0
if not args.no_eval:
# Evaluate on test set
for cam, batch, batch_2d, batch_valid, _ in test_generator.next_epoch():
inputs_3d = torch.from_numpy(batch.astype('float32'))
inputs_2d = torch.from_numpy(batch_2d.astype('float32'))
inputs_valid = torch.from_numpy(batch_valid.astype('float32'))
##### apply test-time-augmentation (following Videopose3d)
inputs_2d_flip = inputs_2d.clone()
inputs_2d_flip[:, :, :, 0] *= -1
inputs_2d_flip[:, :, kps_left + kps_right, :] = inputs_2d_flip[:, :, kps_right + kps_left, :]
##### convert size
inputs_3d_p = inputs_3d
inputs_2d, inputs_3d, valid_frame = eval_data_prepare(receptive_field, inputs_2d, inputs_3d_p, inputs_valid)
inputs_2d_flip, _, _ = eval_data_prepare(receptive_field, inputs_2d_flip, inputs_3d_p, inputs_valid)
if torch.cuda.is_available():
inputs_3d = inputs_3d.cuda()
inputs_2d = inputs_2d.cuda()
inputs_2d_flip = inputs_2d_flip.cuda()
inputs_3d[:, :, 14] = 0
bs = 4
total_batch = (inputs_3d.shape[0] + bs - 1) // bs
for batch_cnt in range(total_batch):
if (batch_cnt + 1) * bs > inputs_3d.shape[0]:
inputs_2d_single = inputs_2d[batch_cnt * bs:]
inputs_2d_flip_single = inputs_2d_flip[batch_cnt * bs:]
inputs_3d_single = inputs_3d[batch_cnt * bs:]
valid_frame_single = valid_frame[batch_cnt * bs:]
else:
inputs_2d_single = inputs_2d[batch_cnt * bs:(batch_cnt + 1) * bs]
inputs_2d_flip_single = inputs_2d_flip[batch_cnt * bs:(batch_cnt + 1) * bs]
inputs_3d_single = inputs_3d[batch_cnt * bs:(batch_cnt + 1) * bs]
valid_frame_single = valid_frame[batch_cnt * bs:(batch_cnt + 1) * bs]
predicted_3d_pos_single = model_pos_test_temp(inputs_2d_single, inputs_3d_single,
input_2d_flip=inputs_2d_flip_single) # b, t, h, f, j, c
b,t,h,f,j,c = predicted_3d_pos_single.shape
predicted_3d_pos_single = predicted_3d_pos_single.reshape(-1, j, c)
predicted_3d_pos_single = model_weight_temp(predicted_3d_pos_single)
predicted_3d_pos_single = predicted_3d_pos_single.reshape(b, t, h, f, j, c)
predicted_3d_pos_single[:, :, :, :, 14] = 0
error = mpjpe_diffusion_3dhp(predicted_3d_pos_single, inputs_3d_single, valid_frame_single.type(torch.bool))
if iteration == 0:
epoch_loss_3d_valid = inputs_3d_single.shape[0] * inputs_3d_single.shape[1] * error.clone()
else:
epoch_loss_3d_valid += inputs_3d_single.shape[0] * inputs_3d_single.shape[1] * error.clone()
N += inputs_3d_single.shape[0] * inputs_3d_single.shape[1]
iteration += 1
if quickdebug:
if N == inputs_3d_single.shape[0] * inputs_3d_single.shape[1]:
break
if quickdebug:
if N == inputs_3d_single.shape[0] * inputs_3d_single.shape[1]:
break
losses_3d_valid.append(epoch_loss_3d_valid / N)
elapsed = (time() - start_time) / 60
if args.no_eval:
print('[%d] time %.2f lr %f 3d_train %f 3d_pos_train %f 2d_pos_trainn %f' % (
epoch + 1,
elapsed,
lr,
losses_3d_train[-1] * 1000,
losses_3d_pos_train[-1] * 1000,
losses_2d_pos_train[-1] * 1000
))
log_path = os.path.join(args.checkpoint, 'training_log.txt')
f = open(log_path, mode='a')
f.write('[%d] time %.2f lr %f 3d_train %f 3d_pos_train %f 2d_pos_train %f\n' % (
epoch + 1,
elapsed,
lr,
losses_3d_train[-1] * 1000,
losses_3d_pos_train[-1] * 1000,
losses_2d_pos_train[-1] * 1000
))
f.close()
else:
print('[%d] time %.2f lr %f 3d_train %f 3d_pos_train %f 2d_pos_train %f 3d_pos_valid %f' % (
epoch + 1,
elapsed,
lr,
losses_3d_train[-1],
losses_3d_pos_train[-1],
losses_2d_pos_train[-1],
losses_3d_valid[-1][0]
))
log_path = os.path.join(args.checkpoint, 'training_log.txt')
f = open(log_path, mode='a')
f.write('[%d] time %.2f lr %f 3d_train %f 3d_pos_train %f 2d_pos_train %f 3d_pos_valid %f\n' % (
epoch + 1,
elapsed,
lr,
losses_3d_train[-1],
losses_3d_pos_train[-1],
losses_2d_pos_train[-1],
losses_3d_valid[-1][0]
))
f.close()
if not args.nolog:
#writer.add_scalar("Loss/3d training eval loss", losses_3d_train_eval[-1] * 1000, epoch+1)
writer.add_scalar("Loss/3d validation loss", losses_3d_valid[-1] * 1000, epoch+1)
if not args.nolog:
writer.add_scalar("Loss/3d training loss", losses_3d_train[-1] * 1000, epoch+1)
writer.add_scalar("Parameters/learing rate", lr, epoch+1)
writer.add_scalar('Parameters/training time per epoch', elapsed, epoch+1)
# Decay learning rate exponentially
lr *= lr_decay
for param_group in optimizer.param_groups:
param_group['lr'] *= lr_decay
epoch += 1
# Decay BatchNorm momentum
# momentum = initial_momentum * np.exp(-epoch/args.epochs * np.log(initial_momentum/final_momentum))
# model_pos_train.set_bn_momentum(momentum)
# Save checkpoint if necessary
if epoch % args.checkpoint_frequency == 0:
chk_path = os.path.join(args.checkpoint, 'epoch_{}.bin'.format(epoch))
print('Saving checkpoint to', chk_path)
torch.save({
'epoch': epoch,
'lr': lr,
'optimizer': optimizer.state_dict(),
'model_pos': model_pos_train.state_dict(),
'model_weight': model_weight_train.state_dict(),
# 'min_loss': min_loss
# 'model_traj': model_traj_train.state_dict() if semi_supervised else None,
# 'random_state_semi': semi_generator.random_state() if semi_supervised else None,
}, chk_path)
#### save best checkpoint
best_chk_path = os.path.join(args.checkpoint, 'best_epoch.bin')
# min_loss = 41.65
if losses_3d_valid[-1][0] < min_loss:
min_loss = losses_3d_valid[-1]
best_epoch = epoch
print("save best checkpoint")
torch.save({
'epoch': epoch,
'lr': lr,
'optimizer': optimizer.state_dict(),
'model_pos': model_pos_train.state_dict(),
'model_weight': model_weight_train.state_dict(),
# 'model_traj': model_traj_train.state_dict() if semi_supervised else None,
# 'random_state_semi': semi_generator.random_state() if semi_supervised else None,
}, best_chk_path)
f = open(log_path, mode='a')
f.write('best epoch\n')
f.close()
# Save training curves after every epoch, as .png images (if requested)
if args.export_training_curves and epoch > 3:
if 'matplotlib' not in sys.modules:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.figure()
epoch_x = np.arange(3, len(losses_3d_train)) + 1
plt.plot(epoch_x, losses_3d_train[3:], '--', color='C0')
plt.plot(epoch_x, losses_3d_train_eval[3:], color='C0')