-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_gd_kitti.py
More file actions
800 lines (644 loc) · 31.4 KB
/
Copy pathtrain_gd_kitti.py
File metadata and controls
800 lines (644 loc) · 31.4 KB
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
'''
Single-GPU training code
'''
import argparse
from datetime import datetime
from model_concat_upsa_gd import chamfer_loss, computesmooth
import numpy as np
import tensorflow as tf
import importlib
import os
import sys
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
import kitti_dataset
import kitti_dataset_self_supervised_cycle
import pickle
sys.path.append(os.path.join(BASE_DIR, 'utils'))
from pointnet_util import *
from tf_grouping import knn_point,group_point
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=3, help='GPU to use [default: GPU 0]')
parser.add_argument('--model', default='model_concat_upsa_gd', help='Model name [default: model_concat_upsa]')
parser.add_argument('--data', default='data_preprocessing/data_processed_maxcut_35_20k_2k_8192', help='Dataset directory [default: data_preprocessing/data_processed_maxcut_35_20k_2k_8192]')
parser.add_argument('--data_kitti', default='../kitti_self_supervised_flow')
parser.add_argument('--log_dir', default='log_train', help='Log dir [default: log_train]')
parser.add_argument('--num_point', type=int, default=8192, help='Point Number [default: 2048]')
parser.add_argument('--max_epoch', type=int, default=151, help='Epoch to run [default: 151]')
parser.add_argument('--batch_size', type=int, default=16, help='Batch Size during training [default: 16]')
parser.add_argument('--learning_rate_g', type=float, default=0.001, help='Initial learning rate [default: 0.001]')
parser.add_argument('--learning_rate_d', type=float, default=0.001, help='Initial learning rate [default: 0.001]')
parser.add_argument('--momentum', type=float, default=0.9, help='Initial learning rate [default: 0.9]')
parser.add_argument('--optimizer', default='adam', help='adam or momentum [default: adam]')
parser.add_argument('--decay_step', type=int, default=200000, help='Decay step for lr decay [default: 200000]')
parser.add_argument('--nn_decay_step', type=int, default=200000, help='Decay step for lr decay [default: 200000]')
parser.add_argument('--decay_rate', type=float, default=0.7, help='Decay rate for lr decay [default: 0.7]')
parser.add_argument('--pre_trained_dir', default='', help='pretrained_model')
parser.add_argument('--lamda', type=float, default=0.5, help='Weight for anchor point [default: 0.5]')
parser.add_argument('--gd_frequency', type=float, default=1.0, help='g:d')
parser.add_argument('--weight_loss', type=float, default=1.0)
parser.add_argument('--weight_g', type=float, default=1.0)
FLAGS = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = str(FLAGS.gpu)
EPOCH_CNT = 0
EPOCH_TRAIN_CNT = 0
NUMBER_GPUS = FLAGS.gpu
BATCH_SIZE = FLAGS.batch_size
NUM_POINT = FLAGS.num_point
DATA = FLAGS.data
DATA_KITTI = FLAGS.data_kitti
MAX_EPOCH = FLAGS.max_epoch
BASE_LEARNING_RATE_G = FLAGS.learning_rate_g
BASE_LEARNING_RATE_D = FLAGS.learning_rate_d
GPU_INDEX = FLAGS.gpu
MOMENTUM = FLAGS.momentum
OPTIMIZER = FLAGS.optimizer
DECAY_STEP = FLAGS.decay_step
DECAY_RATE = FLAGS.decay_rate
PRETRAINED_DIR = FLAGS.pre_trained_dir
LAMDA = FLAGS.lamda
GD_F = FLAGS.gd_frequency
WEIGHT_LOSS = FLAGS.weight_loss
NN = FLAGS.nn_decay_step
W_G = FLAGS.weight_g
MODEL = importlib.import_module(FLAGS.model) # import network module
MODEL_FILE = os.path.join(BASE_DIR, FLAGS.model+'.py')
LOG_DIR = FLAGS.log_dir
if not os.path.exists(LOG_DIR): os.mkdir(LOG_DIR)
os.system('cp %s %s' % (FLAGS.model+'.py', LOG_DIR))
os.system('cp %s %s' % (__file__, LOG_DIR)) # bkp of train procedure
os.system('cp %s %s' % ('flying_things_dataset.py', LOG_DIR))
os.system('cp %s %s' % ('kitti_dataset.py', LOG_DIR))
LOG_FOUT = open(os.path.join(LOG_DIR, 'log_train.txt'), 'w')
LOG_FOUT.write(str(FLAGS)+'\n')
BN_INIT_DECAY = 0.5
BN_DECAY_DECAY_RATE = 0.5
BN_DECAY_DECAY_STEP = float(DECAY_STEP)
BN_DECAY_CLIP = 0.99
TRAIN_DATASET_KITTI = kitti_dataset_self_supervised_cycle.SceneflowDataset(DATA_KITTI, npoints=NUM_POINT, train=True)
TEST_DATASET_KITTI = kitti_dataset.SceneflowDataset(DATA_KITTI, npoints=NUM_POINT, train=False)
def log_string(out_str):
LOG_FOUT.write(out_str+'\n')
LOG_FOUT.flush()
print(out_str)
def get_learning_rate_g(batch):
learning_rate = tf.train.exponential_decay(
BASE_LEARNING_RATE_G, # Base learning rate.
batch * BATCH_SIZE, # Current index into the dataset.
DECAY_STEP, # Decay step.
DECAY_RATE, # Decay rate.
staircase=True)
learning_rate = tf.maximum(learning_rate, 0.00001) # CLIP THE LEARNING RATE!
return learning_rate
def get_learning_rate_d(batch):
learning_rate = tf.train.exponential_decay(
BASE_LEARNING_RATE_D, # Base learning rate.
batch * BATCH_SIZE, # Current index into the dataset.
DECAY_STEP, # Decay step.
DECAY_RATE, # Decay rate.
staircase=True)
learning_rate = tf.maximum(learning_rate, 0.00001) # CLIP THE LEARNING RATE!
return learning_rate
def get_bn_decay(batch):
bn_momentum = tf.train.exponential_decay(
BN_INIT_DECAY,
batch*BATCH_SIZE,
BN_DECAY_DECAY_STEP,
BN_DECAY_DECAY_RATE,
staircase=True)
bn_decay = tf.minimum(BN_DECAY_CLIP, 1 - bn_momentum)
return bn_decay
def get_nn(batch):
w_nn = tf.train.exponential_decay(
1.0,
batch * BATCH_SIZE, # Current index into the dataset.
NN, # Decay step.
DECAY_RATE, # Decay rate.
staircase=True)
return w_nn
def train():
tf.reset_default_graph()
with tf.Graph().as_default():
with tf.device('/gpu:'+str(GPU_INDEX)):
with tf.variable_scope('const', reuse=tf.AUTO_REUSE) as scope:
pointclouds_pl, labels_pl, masks_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_POINT)
is_training_pl_g = tf.placeholder(tf.bool, shape=())
is_training_pl_d = tf.placeholder(tf.bool, shape=())
# Note the global_step=batch parameter to minimize.
# That tells the optimizer to helpfully increment the 'batch' parameter for you every time it trains.
batch = tf.Variable(0)
bn_decay = get_bn_decay(batch)
tf.summary.scalar('bn_decay', bn_decay)
with tf.variable_scope('', reuse=tf.AUTO_REUSE) as scope:
print("--- Get model and loss")
# Get model and loss
pred, end_points = MODEL.Generator(pointclouds_pl, is_training_pl_g, bn_decay=bn_decay)
d_model_fake, d_logits_fake, d_model_real, d_logits_real = MODEL.Discriminator(pointclouds_pl, pred, is_training=is_training_pl_d, bn_decay=bn_decay)
pc1 = pointclouds_pl[:,:NUM_POINT,0:3]
color1 = pointclouds_pl[:, :NUM_POINT, 3:]
pc2 = pointclouds_pl[:, NUM_POINT:, 0:3]
color2 = pointclouds_pl[:, NUM_POINT:, 3:]
pred_pc2 = pc1 + pred
val, idx = knn_point(1, pc2, pred_pc2)
neighbor_pc2 = group_point(pc2, idx)
neighbor_pc2 = tf.squeeze(neighbor_pc2, axis=2)
neighbor_color2 = group_point(color1, idx)
neighbor_color2 = tf.squeeze(neighbor_color2, axis=2)
anchor_pc2 = LAMDA * pred_pc2 + (1 - LAMDA) * neighbor_pc2
anchor_color2 = LAMDA * color1 + (1 - LAMDA) * neighbor_color2
anchor_p2 = tf.concat((anchor_pc2, anchor_color2), axis=2)
p1 = tf.concat((pc1, color1), axis=2)
pointclouds_b = tf.concat((anchor_p2, p1), axis=1)
backward_sf, end_points_b = MODEL.Generator(pointclouds_b, is_training_pl_g, bn_decay=bn_decay)
Chamfer_loss = MODEL.chamfer_loss(pc1, pc2, pred)
CC_loss = MODEL.cycle_consistency_loss(pred, backward_sf)
Curvature_loss = MODEL.curvature_loss(pc1, pc2, pred)
smooth_loss = MODEL.computesmooth(pc1, pred)
loss = CC_loss + 10*smooth_loss + Chamfer_loss + Curvature_loss
#grads = optimizer.compute_gradients(loss)
g_loss = MODEL.GLoss(d_model_real, d_logits_real, d_model_fake, d_logits_fake, is_training_pl_g)
g_loss_total = W_G*g_loss + WEIGHT_LOSS*loss
d_loss = MODEL.DLoss(d_model_real, d_logits_real, d_model_fake, d_logits_fake, is_training_pl_d)
tf.summary.scalar('d_loss', d_loss)
tf.summary.scalar('g_loss', g_loss)
# tf.summary.scalar('NN_loss', NN_loss)
tf.summary.scalar('CC_loss', CC_loss)
tf.summary.scalar('cf_loss', Chamfer_loss)
tf.summary.scalar('curvature_loss', Curvature_loss)
tf.summary.scalar('smooth_loss', smooth_loss)
tf.summary.scalar('loss', loss)
tf.summary.scalar('g_loss_total', g_loss_total)
#with tf.variable_scope('op', reuse=tf.AUTO_REUSE) as scope:
print("--- Get training operator")
# Get training operator
with tf.variable_scope('op', reuse=tf.AUTO_REUSE) as scope:
learning_rate_g = get_learning_rate_g(batch)
learning_rate_d = get_learning_rate_d(batch)
tf.summary.scalar('learning_rate_g', learning_rate_g)
tf.summary.scalar('learning_rate_d', learning_rate_d)
if OPTIMIZER == 'momentum':
optimizer_g = tf.train.MomentumOptimizer(learning_rate_g, momentum=MOMENTUM)
optimizer_d = tf.train.MomentumOptimizer(learning_rate_d, momentum=MOMENTUM)
elif OPTIMIZER == 'adam':
optimizer_g = tf.train.AdamOptimizer(learning_rate_g)
optimizer_d = tf.train.AdamOptimizer(learning_rate_d)
g_train_op = optimizer_g.minimize(g_loss_total, global_step=batch,
var_list=tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES,
scope='sa1|flow_embedding|layer3|layer4|up_sa_layer1|up_sa_layer2|up_sa_layer3|fa_layer4|fc1|fc2'))
d_train_op = optimizer_d.minimize(d_loss, global_step=batch,
var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='dis'))
# Add ops to save and restore all the variables.
# Create a session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.allow_soft_placement = True
config.log_device_placement = False
sess = tf.Session(config=config)
saver = tf.train.Saver(var_list=tf.get_collection(
tf.GraphKeys.GLOBAL_VARIABLES,
scope='sa1|flow_embedding|layer3|layer4|up_sa_layer1|up_sa_layer2|up_sa_layer3|fa_layer4|fc1|fc2|dis'))
saver.restore(sess, PRETRAINED_DIR)
saver_global = tf.train.Saver()
log_string("Model restored.")
'''
init = tf.global_variables_initializer()
sess.run(init)
'''
init = tf.variables_initializer(var_list=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='const|op'))
sess.run(init)
'''
model_variables = slim.get_variables()
restore_variables = [var for var in model_variables]
for var in restore_variables:
print(var.name)
'''
# Add summary writers
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'train'), sess.graph)
test_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'test'), sess.graph)
ops = {'pointclouds_pl': pointclouds_pl,
'labels_pl': labels_pl,
#'masks_pl': masks_pl,
'is_training_pl_g': is_training_pl_g,
'is_training_pl_d': is_training_pl_d,
'pred': pred,
'backward_sf': backward_sf,
#'NN_loss': NN_loss,
'chamfer_loss': Chamfer_loss,
'curvature_loss': Curvature_loss,
'smooth_loss': smooth_loss,
'CC_loss': CC_loss,
'loss': loss,
'g_loss_total': g_loss_total,
'd_logits_real': d_logits_real,
'd_logits_fake': d_logits_fake,
'g_loss': g_loss,
'd_loss': d_loss,
'g_train_op': g_train_op,
'd_train_op': d_train_op,
'merged': merged,
'step': batch,
'end_points': end_points}
epe3d_min = 10000.0
for epoch in range(MAX_EPOCH):
log_string('**** EPOCH %03d ****' % (epoch))
sys.stdout.flush()
# epe3d, acc3d_1, acc3d_2, outlier, gsum, dsum = eval_one_epoch(sess, ops, test_writer)
epe3d, acc3d_1, acc3d_2, outlier, gsum, dsum = eval_one_epoch_kitti(sess, ops, test_writer)
if epe3d < epe3d_min:
epe3d_min = epe3d
save_path_epe3d = saver.save(sess, os.path.join(LOG_DIR, "model_epe3d_%03d.ckpt" % (epoch)))
log_string("Model epe3d saved in file: %s" % save_path_epe3d)
save_path_latest = saver_global.save(sess, os.path.join(LOG_DIR, "model_latest.ckpt" ))
log_string("Model latest saved in file: %s" % save_path_latest)
if GD_F >= 1:
if epoch % GD_F == 0:
train_one_epoch_d(sess, ops, train_writer)
train_one_epoch_g(sess, ops, train_writer)
else:
gd_f = 1 / GD_F
train_one_epoch_d(sess, ops, train_writer)
if epoch % gd_f == 0:
train_one_epoch_g(sess, ops, train_writer)
# Save the variables to disk.
if epoch % 10 == 0:
save_path = saver_global.save(sess, os.path.join(LOG_DIR, "model.ckpt"))
log_string("Model saved in file: %s" % save_path)
def get_batch(dataset, idxs, start_idx, end_idx):
bsize = end_idx-start_idx
batch_data = np.zeros((bsize, NUM_POINT*2, 6))
batch_label = np.zeros((bsize, NUM_POINT, 3))
# shuffle idx to change point order (change FPS behavior)
shuffle_idx = np.arange(NUM_POINT)
np.random.shuffle(shuffle_idx)
for i in range(bsize):
pc1, pc2, flow= dataset[idxs[i+start_idx]]
# move pc1 to center
pc1_center = np.mean(pc1, 0)
pc1 -= pc1_center
pc2 -= pc1_center
batch_data[i,:NUM_POINT,:3] = pc1[shuffle_idx]
batch_data[i,NUM_POINT:,:3] = pc2[shuffle_idx]
batch_label[i] = flow[shuffle_idx]
return batch_data, batch_label
def get_eval_batch(dataset, idxs, start_idx, end_idx):
bsize = end_idx - start_idx
batch_data = np.zeros((bsize, NUM_POINT * 2, 6))
batch_label = np.zeros((bsize, NUM_POINT, 3))
shuffle_idx = np.arange(NUM_POINT)
np.random.shuffle(shuffle_idx)
for i in range(bsize):
pc1, pc2, color1, color2, flow = dataset[idxs[i + start_idx]]
# move pc1 to center
pc1_center = np.mean(pc1, 0)
pc1 -= pc1_center
pc2 -= pc1_center
batch_data[i, :NUM_POINT, :3] = pc1[shuffle_idx]
batch_data[i, :NUM_POINT, 3:] = color1[shuffle_idx]
batch_data[i, NUM_POINT:, :3] = pc2[shuffle_idx]
batch_data[i, NUM_POINT:, 3:] = color1[shuffle_idx]
batch_label[i] = flow[shuffle_idx]
# batch_mask[i] = mask1[shuffle_idx]
return batch_data, batch_label # , batch_mask
def get_cycle_batch(dataset, idxs, start_idx, end_idx):
bsize = end_idx - start_idx
# change here, numpoint *(5, 3)
batch_data = np.zeros((bsize, NUM_POINT * 2, 6))
shuffle_idx = np.arange(NUM_POINT)
for i in range(bsize):
pos, color = dataset[idxs[i + start_idx]]
pos1_center = np.mean(pos[0], 0) # 1 * 3
for frame_idx in range(2):
np.random.shuffle(shuffle_idx)
batch_data[i, NUM_POINT*frame_idx:NUM_POINT*(frame_idx+1), :3] = \
pos[frame_idx, shuffle_idx, :] - pos1_center
batch_data[i, NUM_POINT*frame_idx:NUM_POINT*(frame_idx+1), 3:] = \
color[frame_idx, shuffle_idx, :]
return batch_data
def train_one_epoch_d(sess, ops, train_writer):
""" ops: dict mapping from string to tf ops """
is_training_d = True
is_training_g = False
# Shuffle train samples
# train_idxs = np.arange(0, len(TRAIN_DATASET_KITTI))
train_idxs = np.arange(0, 100)
np.random.shuffle(train_idxs)
num_batches = 100 // BATCH_SIZE
log_string(str(datetime.now()))
g_loss_sum = 0
d_loss_sum = 0
smooth_loss_sum = 0
CC_loss_sum = 0
cf_loss_sum = 0
cv_loss_sum = 0
loss_sum = 0
g_loss_total_sum = 0
for batch_idx in range(num_batches):
start_idx = batch_idx * BATCH_SIZE
end_idx = min(100, (batch_idx+1) * BATCH_SIZE)
batch_data = get_cycle_batch(TRAIN_DATASET_KITTI, train_idxs, start_idx, end_idx)
feed_dict = {ops['pointclouds_pl']: batch_data,
# ops['labels_pl']: batch_label,
#ops['masks_pl']: batch_mask,
ops['is_training_pl_g']: is_training_g,
ops['is_training_pl_d']: is_training_d}
summary, step, _, g_loss_val, d_loss_val, smooth_loss, cf_loss, CC_loss, cv_loss, loss, g_loss_total, pred_val, d_logits_real_val, d_logits_fake_val = sess.run(
[ops['merged'], ops['step'],
ops['d_train_op'],
ops['g_loss'], ops['d_loss'],
ops['smooth_loss'], ops['chamfer_loss'], ops['CC_loss'], ops['curvature_loss'], ops['loss'], ops['g_loss_total'],
ops['pred'],
ops['d_logits_real'], ops['d_logits_fake']], feed_dict=feed_dict)
train_writer.add_summary(summary, step)
g_loss_sum += g_loss_val
d_loss_sum += d_loss_val
smooth_loss_sum += smooth_loss
cf_loss_sum += cf_loss
CC_loss_sum += CC_loss
cv_loss_sum += cv_loss
loss_sum += loss
g_loss_total_sum += g_loss_total
if (batch_idx+1)%10 == 0:
log_string(' -- %03d / %03d --' % (batch_idx+1, num_batches))
log_string('g mean loss: %f' % (g_loss_sum / 10))
log_string('d mean loss: %f' % (d_loss_sum / 10))
log_string('mean loss: %f' % (loss_sum / 10))
log_string('mean smooth loss: %f' % (smooth_loss_sum / 10))
log_string('mean cf loss: %f' % (cf_loss_sum / 10))
log_string('mean CC loss: %f' % (CC_loss_sum / 10))
log_string('mean cv loss: %f' % (cv_loss_sum / 10))
log_string('g total mean loss: %f' % (g_loss_total_sum / 10))
g_loss_sum = 0
d_loss_sum = 0
smooth_loss_sum = 0
cf_loss_sum = 0
CC_loss_sum = 0
cv_loss_sum = 0
loss_sum = 0
g_loss_total_sum = 0
def train_one_epoch_g(sess, ops, train_writer):
""" ops: dict mapping from string to tf ops """
is_training_d = False
is_training_g = True
# Shuffle train samples
train_idxs = np.arange(0, 100)
np.random.shuffle(train_idxs)
num_batches = 100 // BATCH_SIZE
log_string(str(datetime.now()))
g_loss_sum = 0
d_loss_sum = 0
smooth_loss_sum = 0
cf_loss_sum = 0
CC_loss_sum = 0
cv_loss_sum = 0
loss_sum = 0
g_loss_total_sum = 0
for batch_idx in range(num_batches):
start_idx = batch_idx * BATCH_SIZE
end_idx = min(100, (batch_idx+1) * BATCH_SIZE)
batch_data = get_cycle_batch(TRAIN_DATASET_KITTI, train_idxs, start_idx, end_idx)
feed_dict = {ops['pointclouds_pl']: batch_data,
ops['is_training_pl_g']: is_training_g,
ops['is_training_pl_d']: is_training_d}
summary, step, _, g_loss_val, d_loss_val, smooth_loss, cf_loss, CC_loss, cv_loss, loss, g_loss_total, pred_val, d_logits_real_val, d_logits_fake_val = sess.run(
[ops['merged'], ops['step'],
ops['g_train_op'],
ops['g_loss'], ops['d_loss'],
ops['smooth_loss'], ops['chamfer_loss'], ops['CC_loss'], ops['curvature_loss'], ops['loss'], ops['g_loss_total'],
ops['pred'],
ops['d_logits_real'], ops['d_logits_fake']], feed_dict=feed_dict)
train_writer.add_summary(summary, step)
g_loss_sum += g_loss_val
d_loss_sum += d_loss_val
smooth_loss_sum += smooth_loss
cf_loss_sum += cf_loss
CC_loss_sum += CC_loss
cv_loss_sum += cv_loss
loss_sum += loss
g_loss_total_sum += g_loss_total
if (batch_idx+1)%10 == 0:
log_string(' -- %03d / %03d --' % (batch_idx+1, num_batches))
log_string('g mean loss: %f' % (g_loss_sum / 10))
log_string('d mean loss: %f' % (d_loss_sum / 10))
log_string('mean loss: %f' % (loss_sum / 10))
log_string('mean smooth loss: %f' % (smooth_loss_sum / 10))
log_string('mean cf loss: %f' % (cf_loss_sum / 10))
log_string('mean CC loss: %f' % (CC_loss_sum / 10))
log_string('mean cv loss: %f' % (cv_loss_sum / 10))
log_string('g total mean loss: %f' % (g_loss_total_sum / 10))
g_loss_sum = 0
d_loss_sum = 0
smooth_loss_sum = 0
cf_loss_sum = 0
CC_loss_sum = 0
cv_loss_sum = 0
loss_sum = 0
g_loss_total_sum = 0
def eval_one_epoch(sess, ops, test_writer):
global EPOCH_CNT
is_training_g = False
is_training_d = False
test_idxs = np.arange(0, 3824)
num_batches = (3824+BATCH_SIZE-1) // BATCH_SIZE
g_loss_sum = 0
loss_sum_l2 = 0
d_loss_sum = 0
sum_epe3d = 0
sum_acc3d_1 = 0
sum_acc3d_2 = 0
sum_outlier = 0
loss_sum = 0
log_string(str(datetime.now()))
log_string('---- EPOCH %03d EVALUATION ----'%(EPOCH_CNT))
batch_data = np.zeros((BATCH_SIZE, NUM_POINT*2, 3))
batch_label = np.zeros((BATCH_SIZE, NUM_POINT, 3))
for batch_idx in range(num_batches):
if batch_idx %20==0:
log_string('%03d/%03d'%(batch_idx, num_batches))
start_idx = batch_idx * BATCH_SIZE
end_idx = min(3824, (batch_idx+1) * BATCH_SIZE)
cur_batch_size = end_idx-start_idx
cur_batch_data, cur_batch_label = get_batch(TEST_DATASET, test_idxs, start_idx, end_idx)
if cur_batch_size == BATCH_SIZE:
batch_data = cur_batch_data
batch_label = cur_batch_label
else:
batch_data[0:cur_batch_size] = cur_batch_data
batch_label[0:cur_batch_size] = cur_batch_label
# ---------------------------------------------------------------------
# ---- INFERENCE BELOW ----
feed_dict = {ops['pointclouds_pl']: batch_data,
ops['labels_pl']: batch_label,
#ops['masks_pl']: batch_mask,
ops['is_training_pl_g']: is_training_g,
ops['is_training_pl_d']: is_training_d}
summary, step, g_loss_val, d_loss_val, pred_val, d_logits_real_val, d_logits_fake_val = sess.run(
[ops['merged'], ops['step'],
#ops['loss'],
ops['g_loss'], ops['d_loss'], ops['pred'],
ops['d_logits_real'], ops['d_logits_fake']], feed_dict=feed_dict)
test_writer.add_summary(summary, step)
# ---- INFERENCE ABOVE ----
# ---------------------------------------------------------------------
pc1 = batch_data[:, :NUM_POINT, :3]
color1 = batch_data[:, :NUM_POINT, 3:]
pc2 = batch_data[:, NUM_POINT:, :3]
color2 = batch_data[:, NUM_POINT:, 3:]
pc1 = change_axis(pc1)
batch_label = change_axis(batch_label)
pred_val = change_axis(pred_val)
error = np.linalg.norm(pred_val - batch_label, axis=-1)
num = pred_val.shape[1]
sf_gt_len = np.linalg.norm(batch_label, axis=-1) + 1e-20
acc3d_1 = np.sum(np.logical_or((error <= 0.05), (error / sf_gt_len <= 0.05)), axis=1) ###note the range
acc3d_2 = np.sum(np.logical_or((error <= 0.1), (error / sf_gt_len <= 0.1)), axis=1)
outlier = np.sum(np.logical_or((error > 0.3), (error / sf_gt_len > 0.1)), axis=1)
# mask_sum = np.sum(mask, 1)
acc3d_1 = acc3d_1 / num
acc3d_1 = np.mean(acc3d_1)
acc3d_2 = acc3d_2 / num
acc3d_2 = np.mean(acc3d_2)
outlier = outlier / num
outlier = np.mean(outlier)
EPE3D = np.sum(error, axis=-1) / num
EPE3D = np.mean(EPE3D)
tmp = np.sum((pred_val - batch_label)**2, 2) / 2.0
#loss_val_np = np.mean(batch_mask * tmp)
loss_val_np = np.mean(tmp)
if cur_batch_size==BATCH_SIZE:
g_loss_sum += g_loss_val
loss_sum_l2 += loss_val_np
d_loss_sum += d_loss_val
sum_epe3d += EPE3D
sum_acc3d_1 += acc3d_1
sum_acc3d_2 += acc3d_2
sum_outlier += outlier
# Dump some results
if batch_idx == 0:
with open('test_results.pkl', 'wb') as fp:
pickle.dump([batch_data, batch_label, pred_val], fp)
log_string('g eval mean loss: %f' % (g_loss_sum / float(3824/BATCH_SIZE)))
log_string('d eval mean loss: %f' % (d_loss_sum / float(3824/BATCH_SIZE)))
log_string('eval mean loss: %f' % (loss_sum_l2 / float(3824/BATCH_SIZE)))
epe3d = sum_epe3d / float(3824 // BATCH_SIZE)
acc3d_1 = sum_acc3d_1 / float(3824 // BATCH_SIZE)
acc3d_2 = sum_acc3d_2 / float(3824 // BATCH_SIZE)
outlier = sum_outlier / float(3824 // BATCH_SIZE)
log_string('eval mean EPE 3D: %f' % (epe3d))
log_string('eval mean acc3d_1: %f' % (acc3d_1))
log_string('eval mean acc3d_2 : %f' % (acc3d_2))
log_string('eval mean outlier : %f' % (outlier))
EPOCH_CNT += 1
return epe3d, acc3d_1, acc3d_2, outlier, g_loss_sum/float(len(TEST_DATASET)/BATCH_SIZE), d_loss_sum/float(len(TEST_DATASET)/BATCH_SIZE)
def eval_one_epoch_kitti(sess, ops, test_writer):
# ops: dict mapping from string to tf ops
global EPOCH_CNT
is_training_g = False
is_training_d = False
test_idxs = np.arange(0, 50)
# Test on all data: last batch might be smaller than BATCH_SIZE
num_batches = (50+BATCH_SIZE-1) // BATCH_SIZE
g_loss_sum = 0
loss_sum_l2 = 0
d_loss_sum = 0
sum_epe3d = 0
sum_acc3d_1 = 0
sum_acc3d_2 = 0
sum_outlier = 0
loss_sum = 0
log_string(str(datetime.now()))
log_string('---- EPOCH %03d EVALUATION KITII ----'%(EPOCH_CNT))
batch_data = np.zeros((BATCH_SIZE, NUM_POINT*2, 3))
batch_label = np.zeros((BATCH_SIZE, NUM_POINT, 3))
#batch_mask = np.zeros((BATCH_SIZE, NUM_POINT))
for batch_idx in range(num_batches):
if batch_idx %20==0:
log_string('%03d/%03d'%(batch_idx, num_batches))
start_idx = batch_idx * BATCH_SIZE
end_idx = min(50, (batch_idx+1) * BATCH_SIZE)
cur_batch_size = end_idx-start_idx
cur_batch_data, cur_batch_label = get_eval_batch(TEST_DATASET_KITTI, test_idxs, start_idx, end_idx)
if cur_batch_size == BATCH_SIZE:
batch_data = cur_batch_data
batch_label = cur_batch_label
#batch_mask = cur_batch_mask
else:
batch_data[0:cur_batch_size] = cur_batch_data
batch_label[0:cur_batch_size] = cur_batch_label
#batch_mask[0:cur_batch_size] = cur_batch_mask
# ---------------------------------------------------------------------
# ---- INFERENCE BELOW ----
feed_dict = {ops['pointclouds_pl']: batch_data,
ops['labels_pl']: batch_label,
#ops['masks_pl']: batch_mask,
ops['is_training_pl_g']: is_training_g,
ops['is_training_pl_d']: is_training_d}
summary, step, g_loss_val, d_loss_val, pred_val, d_logits_real_val, d_logits_fake_val = sess.run(
[ops['merged'], ops['step'],
#ops['loss'],
ops['g_loss'], ops['d_loss'], ops['pred'],
ops['d_logits_real'], ops['d_logits_fake']], feed_dict=feed_dict)
test_writer.add_summary(summary, step)
# ---- INFERENCE ABOVE ----
# ---------------------------------------------------------------------
pc1 = batch_data[:, :NUM_POINT, :3]
color1 = batch_data[:, :NUM_POINT, 3:]
pc2 = batch_data[:, NUM_POINT:, :3]
color2 = batch_data[:, NUM_POINT:, 3:]
pc1 = change_axis(pc1)
batch_label = change_axis(batch_label)
pred_val = change_axis(pred_val)
error = np.linalg.norm(pred_val - batch_label, axis=-1)
num = pred_val.shape[1]
sf_gt_len = np.linalg.norm(batch_label, axis=-1) + 1e-20
acc3d_1 = np.sum(np.logical_or((error <= 0.05), (error / sf_gt_len <= 0.05)), axis=1) ###note the range
acc3d_2 = np.sum(np.logical_or((error <= 0.1), (error / sf_gt_len <= 0.1)), axis=1)
outlier = np.sum(np.logical_or((error > 0.3), (error / sf_gt_len > 0.1)), axis=1)
# mask_sum = np.sum(mask, 1)
acc3d_1 = acc3d_1 / num
acc3d_1 = np.mean(acc3d_1)
acc3d_2 = acc3d_2 / num
acc3d_2 = np.mean(acc3d_2)
outlier = outlier / num
outlier = np.mean(outlier)
EPE3D = np.sum(error, axis=-1) / num
EPE3D = np.mean(EPE3D)
tmp = np.sum((pred_val - batch_label)**2, 2) / 2.0
#loss_val_np = np.mean(batch_mask * tmp)
loss_val_np = np.mean(tmp)
if cur_batch_size==BATCH_SIZE:
g_loss_sum += g_loss_val
loss_sum_l2 += loss_val_np
d_loss_sum += d_loss_val
sum_epe3d += EPE3D
sum_acc3d_1 += acc3d_1
sum_acc3d_2 += acc3d_2
sum_outlier += outlier
# Dump some results
if batch_idx == 0:
with open('test_results.pkl', 'wb') as fp:
pickle.dump([batch_data, batch_label, pred_val], fp)
log_string('g eval mean loss: %f' % (g_loss_sum / float(50/BATCH_SIZE)))
log_string('d eval mean loss: %f' % (d_loss_sum / float(50/BATCH_SIZE)))
log_string('eval mean loss: %f' % (loss_sum_l2 / float(50/BATCH_SIZE)))
epe3d = sum_epe3d / float(50 // BATCH_SIZE)
acc3d_1 = sum_acc3d_1 / float(50 // BATCH_SIZE)
acc3d_2 = sum_acc3d_2 / float(50 // BATCH_SIZE)
outlier = sum_outlier / float(50 // BATCH_SIZE)
log_string('eval mean EPE 3D: %f' % (epe3d))
log_string('eval mean acc3d_1: %f' % (acc3d_1))
log_string('eval mean acc3d_2 : %f' % (acc3d_2))
log_string('eval mean outlier : %f' % (outlier))
EPOCH_CNT += 1
return epe3d, acc3d_1, acc3d_2, outlier, g_loss_sum/float(len(TEST_DATASET_KITTI)/BATCH_SIZE), d_loss_sum/float(len(TEST_DATASET_KITTI)/BATCH_SIZE)
def change_axis(pos1):
pos1_x = pos1[:,:, 2]
pos1_y = -pos1[:,:, 1]
pos1_z = pos1[:,:, 0]
pos1 = np.stack([pos1_x, pos1_y, pos1_z], axis=-1)
return pos1
if __name__ == "__main__":
log_string('pid: %s'%(str(os.getpid())))
train()
LOG_FOUT.close()