This repository was archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathmodels.py
executable file
·1364 lines (1079 loc) · 46.9 KB
/
models.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
# Copyright 2019 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
"""Model graph definitions and other functions for training and testing."""
import functools
import math
import operator
import os
import random
import re
import numpy as np
import tensorflow as tf
def get_model(config, gpuid):
"""Make model instance and pin to one gpu.
Args:
config: arguments.
gpuid: gpu id to use
Returns:
Model instance.
"""
with tf.name_scope(config.modelname), tf.device('/gpu:%d' % gpuid):
model = Model(config, '%s' % config.modelname)
return model
class Model(object):
"""Model graph definitions.
"""
def __init__(self, config, scope):
self.scope = scope
self.config = config
self.global_step = tf.get_variable('global_step', shape=[],
dtype='int32',
initializer=tf.constant_initializer(0),
trainable=False)
# get all the dimension here
# Tensor dimensions, so pylint: disable=g-bad-name
N = self.N = config.batch_size
KP = self.KP = config.kp_size
SH = self.SH = config.scene_h
SW = self.SW = config.scene_w
SC = self.SC = config.scene_class
K = self.K = config.max_other
self.P = P = 2 # traj coordinate dimension
# all the inputs
# the trajactory sequence,
# in training, it is the obs+pred combined,
# in testing, only obs is fed and the rest is zeros
# [N,T1,2] # T1 is the obs_len
# mask is used for variable length input extension
self.traj_obs_gt = tf.placeholder(
'float', [N, None, P], name='traj_obs_gt')
self.traj_obs_gt_mask = tf.placeholder(
'bool', [N, None], name='traj_obs_gt_mask')
# [N,T2,2]
self.traj_pred_gt = tf.placeholder(
'float', [N, None, P], name='traj_pred_gt')
self.traj_pred_gt_mask = tf.placeholder(
'bool', [N, None], name='traj_pred_gt_mask')
self.obs_kp = tf.placeholder('float', [N, None, KP, 2], name='obs_kp')
# used for drop out switch
self.is_train = tf.placeholder('bool', [], name='is_train')
# scene semantic segmentation features
# the index to the feature
self.obs_scene = tf.placeholder('int32', [N, None], name='obs_scene')
self.obs_scene_mask = tf.placeholder(
'bool', [N, None], name='obs_scene_mask')
# the actual feature
self.scene_feat = tf.placeholder(
'float32', [None, SH, SW, SC], name='scene_feat')
# [N, obs_len, 5, 9, 2048]
self.obs_person_features = tf.placeholder('float32', [
N, None, config.person_h, config.person_w,
config.person_feat_dim], name='obs_boxes_features')
# other box
# the box input is the relative coordinates
# [N,obs_len, K, 4]
self.obs_other_boxes = tf.placeholder(
'float32', [N, None, K, 4], name='other_boxes')
# [N,obs_len, K, num_class]
self.obs_other_boxes_class = tf.placeholder(
'float32', [N, None, K, config.num_box_class], name='other_boxes_class')
# [N,obs_len, K]
self.obs_other_boxes_mask = tf.placeholder(
'bool', [N, None, K], name='other_boxes_mask')
# grid loss
self.grid_pred_labels = []
self.grid_pred_targets = []
self.grid_obs_labels = []
self.grid_obs_targets = []
for _ in config.scene_grids:
# [N, seq_len]
# currently only the destination
self.grid_pred_labels.append(
tf.placeholder('int32', [N])) # grid class
self.grid_pred_targets.append(tf.placeholder('float32', [N, 2]))
self.grid_obs_labels.append(
tf.placeholder('int32', [N, None])) # grid class
self.grid_obs_targets.append(
tf.placeholder('float32', [N, None, 2]))
# traj class loss
self.traj_class_gt = tf.placeholder('int64', [N], name='traj_class')
self.future_act_label = tf.placeholder(
'uint8', [N, config.num_act], name='future_act')
self.loss = None
self.build_forward()
self.build_loss()
def build_forward(self):
"""Build the forward model graph."""
config = self.config
# Tensor dimensions, so pylint: disable=g-bad-name
N = self.N
KP = self.KP
# add dropout
keep_prob = tf.cond(self.is_train,
lambda: tf.constant(config.keep_prob),
lambda: tf.constant(1.0))
# ------------------------- encoder ------------------------
enc_cell_traj = tf.nn.rnn_cell.LSTMCell(
config.enc_hidden_size, state_is_tuple=True, name='enc_traj')
enc_cell_traj = tf.nn.rnn_cell.DropoutWrapper(enc_cell_traj, keep_prob)
# scene encoder
enc_cell_personscene = tf.nn.rnn_cell.LSTMCell(
config.enc_hidden_size, state_is_tuple=True, name='enc_scene')
enc_cell_personscene = tf.nn.rnn_cell.DropoutWrapper(
enc_cell_personscene, keep_prob)
# person pose encoder
if config.add_kp:
enc_cell_kp = tf.nn.rnn_cell.LSTMCell(
config.enc_hidden_size, state_is_tuple=True, name='enc_kp')
enc_cell_kp = tf.nn.rnn_cell.DropoutWrapper(enc_cell_kp, keep_prob)
# person appearance encoder
enc_cell_person = tf.nn.rnn_cell.LSTMCell(
config.enc_hidden_size, state_is_tuple=True, name='enc_person')
# enc_cell_person = tf.contrib.rnn.ConvLSTMCell(conv_ndims=2,
# input_shape=[person_h, person_w, person_feat_dim],
# output_channels=config.enc_hidden_size, kernel_shape=[3,3])
enc_cell_person = tf.nn.rnn_cell.DropoutWrapper(
enc_cell_person, keep_prob)
# other box encoder
enc_cell_other = tf.nn.rnn_cell.LSTMCell(
config.enc_hidden_size, state_is_tuple=True, name='enc_other')
enc_cell_other = tf.nn.rnn_cell.DropoutWrapper(
enc_cell_other, keep_prob)
# activity location/grid loss
enc_cell_gridclass = []
for i, _ in enumerate(config.scene_grids):
enc_cell_gridclass_this = tf.nn.rnn_cell.LSTMCell(
config.enc_hidden_size, state_is_tuple=True,
name='enc_gridclass_%s' % i)
enc_cell_gridclass_this = tf.nn.rnn_cell.DropoutWrapper(
enc_cell_gridclass_this, keep_prob)
enc_cell_gridclass.append(enc_cell_gridclass_this)
# ------------------------ decoder
if config.multi_decoder:
dec_cell_traj = [tf.nn.rnn_cell.LSTMCell(
config.dec_hidden_size, state_is_tuple=True, name='dec_traj_%s' % i)
for i in range(len(config.traj_cats))]
dec_cell_traj = [tf.nn.rnn_cell.DropoutWrapper(
one, keep_prob) for one in dec_cell_traj]
else:
dec_cell_traj = tf.nn.rnn_cell.LSTMCell(
config.dec_hidden_size, state_is_tuple=True, name='dec_traj')
dec_cell_traj = tf.nn.rnn_cell.DropoutWrapper(
dec_cell_traj, keep_prob)
# ----------------------------------------------------------
# the obs part is the same for training and testing
# obs_out is only used in training
# encoder, decoder
# top_scope is used for variable inside
# encode and decode if want to share variable across
with tf.variable_scope('person_pred') as top_scope:
# [N,T1,h_dim]
# xy encoder
obs_length = tf.reduce_sum(
tf.cast(self.traj_obs_gt_mask, 'int32'), 1)
traj_xy_emb_enc = linear(self.traj_obs_gt,
output_size=config.emb_size,
activation=config.activation_func,
add_bias=True,
scope='enc_xy_emb')
traj_obs_enc_h, traj_obs_enc_last_state = tf.nn.dynamic_rnn(
enc_cell_traj, traj_xy_emb_enc, sequence_length=obs_length,
dtype='float', scope='encoder_traj')
enc_h_list = [traj_obs_enc_h]
enc_last_state_list = [traj_obs_enc_last_state]
# grid class and grid regression encoder
# multi-scale
grid_obs_enc_h = []
grid_obs_enc_last_state = []
for i, (h, w) in enumerate(config.scene_grids):
# [N, T] -> [N, T, h*w]
obs_gridclass_onehot = tf.one_hot(self.grid_obs_labels[i], h*w)
obs_gridclass_encode_h, obs_gridclass_encode_last_state = \
tf.nn.dynamic_rnn(enc_cell_gridclass[i], obs_gridclass_onehot,
sequence_length=obs_length, dtype='float',
scope='encoder_gridclass_%s' % i)
grid_obs_enc_h.append(obs_gridclass_encode_h)
grid_obs_enc_last_state.append(obs_gridclass_encode_last_state)
enc_h_list.extend(grid_obs_enc_h)
enc_last_state_list.extend(grid_obs_enc_last_state)
# gather all visual observation encoder
# ------------------------------------------------------------
with tf.variable_scope('scene'):
# [N,obs_len, SH, SW, SC]
obs_scene = tf.nn.embedding_lookup(
self.scene_feat, self.obs_scene)
obs_scene = tf.reduce_mean(obs_scene, axis=1) # [N,SH,SW,SC]
with tf.variable_scope('scene_conv'):
# [N, SH, SW, dim]
# resnet structure?
conv_dim = config.scene_conv_dim
scene_conv1 = obs_scene
# [N, SH/2, SW/2, dim]
scene_conv2 = conv2d(scene_conv1, out_channel=conv_dim,
kernel=config.scene_conv_kernel,
stride=2, activation=config.activation_func,
add_bias=True, scope='conv2')
# [N, SH/4, SW/4, dim]
scene_conv3 = conv2d(scene_conv2, out_channel=conv_dim,
kernel=config.scene_conv_kernel,
stride=2, activation=config.activation_func,
add_bias=True, scope='conv3')
self.scene_convs = [scene_conv2, scene_conv3]
# pool the scene features for each trajectory, for different scale
# currently only used single scale conv
pool_scale_idx = config.pool_scale_idx
scene_h, scene_w = config.scene_grids[pool_scale_idx]
# [N, num_grid_class, conv_dim]
scene_conv_full = tf.reshape(
self.scene_convs[pool_scale_idx], (N, scene_h*scene_w, conv_dim))
# [N, seq_len]
obs_grid = self.grid_obs_labels[pool_scale_idx]
obs_grid = tf.reshape(obs_grid, [-1]) # [N*seq_len]
# [N*seq_len, 2]
indices = tf.stack(
[tf.range(tf.shape(obs_grid)[0]), tf.to_int32(obs_grid)], axis=-1)
# [N, seq_len, num_grid_class, conv_dim]
scene_conv_full_tile = tf.tile(tf.expand_dims(
scene_conv_full, 1), [1, config.obs_len, 1, 1])
# [N*seq_len, num_grid_class, conv_dim]
scene_conv_full_tile = tf.reshape(
scene_conv_full_tile, (-1, scene_h*scene_w, conv_dim))
# [N*seq_len, h*w, feat_dim] + [N*seq_len,2] -> # [N*seq_len, feat_dim]
obs_personscene = tf.gather_nd(scene_conv_full_tile, indices)
obs_personscene = tf.reshape(
obs_personscene, (N, config.obs_len, conv_dim))
# obs_personscene [N, seq_len, conv_dim]
personscene_obs_enc_h, personscene_obs_enc_last_state = \
tf.nn.dynamic_rnn(enc_cell_personscene, obs_personscene,
sequence_length=obs_length, dtype='float',
scope='encoder_personscene')
enc_h_list.append(personscene_obs_enc_h)
enc_last_state_list.append(personscene_obs_enc_last_state)
# person pose
if config.add_kp:
obs_kp = tf.reshape(self.obs_kp, [N, -1, KP*2])
obs_kp = linear(obs_kp, output_size=config.emb_size, add_bias=True,
activation=config.activation_func, scope='kp_emb')
kp_obs_enc_h, kp_obs_enc_last_state = tf.nn.dynamic_rnn(
enc_cell_kp, obs_kp, sequence_length=obs_length, dtype='float',
scope='encoder_kp')
enc_h_list.append(kp_obs_enc_h)
enc_last_state_list.append(kp_obs_enc_last_state)
# person appearance
# average and then normal lstm
obs_person_features = tf.reduce_mean(
self.obs_person_features, axis=[2, 3])
# [N,T,hdim]
person_obs_enc_h, person_obs_enc_last_state = tf.nn.dynamic_rnn(
enc_cell_person, obs_person_features, sequence_length=obs_length,
dtype='float', scope='encoder_person')
enc_h_list.append(person_obs_enc_h)
enc_last_state_list.append(person_obs_enc_last_state)
# extract features from other boxes
# obs_other_boxes [N, obs_len, K, 4]
# obs_other_boxes_class [N, obs_len, K, num_class]
# obs_other_boxes_mask [N, obs_len, K]
with tf.variable_scope('other_box'):
# [N, obs_len, K, box_emb_size]
obs_other_boxes_geo_features = linear(
self.obs_other_boxes, add_bias=True,
activation=config.activation_func, output_size=config.box_emb_size,
scope='other_box_geo_emb')
obs_other_boxes_class_features = linear(
self.obs_other_boxes_class, add_bias=True,
activation=config.activation_func, output_size=config.box_emb_size,
scope='other_box_class_emb')
obs_other_boxes_features = tf.concat(
[obs_other_boxes_geo_features, obs_other_boxes_class_features],
axis=3)
# cosine simi
obs_other_boxes_geo_features = tf.nn.l2_normalize(
obs_other_boxes_geo_features, -1)
obs_other_boxes_class_features = tf.nn.l2_normalize(
obs_other_boxes_class_features, -1)
# [N, T,K]
other_attention = tf.reduce_sum(tf.multiply(
obs_other_boxes_geo_features, obs_other_boxes_class_features), 3)
other_attention = exp_mask(
other_attention, self.obs_other_boxes_mask)
other_attention = tf.nn.softmax(other_attention)
# [N, obs_len, K, 1] * [N, obs_len, K, feat_dim]
# -> [N, obs_len, feat_dim]
other_box_features_attended = tf.reduce_sum(tf.expand_dims(
other_attention, -1)*obs_other_boxes_features, axis=2)
other_obs_enc_h, other_obs_enc_last_state = tf.nn.dynamic_rnn(
enc_cell_other, other_box_features_attended,
sequence_length=obs_length, dtype='float', scope='encoder_other')
enc_h_list.append(other_obs_enc_h)
enc_last_state_list.append(other_obs_enc_last_state)
# pack all observed hidden states
obs_enc_h = tf.stack(enc_h_list, axis=1)
# .h is [N,h_dim*k]
obs_enc_last_state = concat_states(enc_last_state_list, axis=1)
# -------------------------------------------------- xy decoder
traj_obs_last = self.traj_obs_gt[:, -1]
pred_length = tf.reduce_sum(
tf.cast(self.traj_pred_gt_mask, 'int32'), 1) # N
if config.multi_decoder:
# [N, num_traj_cat] # each is num_traj_cat classification
self.traj_class_logits = self.traj_class_head(
obs_enc_h, obs_enc_last_state, scope='traj_class_predict')
# [N]
traj_class = tf.argmax(self.traj_class_logits, axis=1)
traj_class_gated = tf.cond(
self.is_train,
lambda: self.traj_class_gt,
lambda: traj_class,
)
traj_pred_outs = [
self.decoder(
traj_obs_last,
traj_obs_enc_last_state,
obs_enc_h,
pred_length,
dec_cell_traj[traj_cat],
top_scope=top_scope,
scope='decoder_%s' % traj_cat)
for _, traj_cat in config.traj_cats
]
# [N, num_decoder, T, 2]
self.traj_pred_outs = tf.stack(traj_pred_outs, axis=1)
# [N, 2]
indices = tf.stack(
[tf.range(N), tf.to_int32(traj_class_gated)], axis=1)
# [N, T, 2]
traj_pred_out = tf.gather_nd(self.traj_pred_outs, indices)
else:
traj_pred_out = self.decoder(traj_obs_last, traj_obs_enc_last_state,
obs_enc_h, pred_length, dec_cell_traj,
top_scope=top_scope, scope='decoder')
if config.add_activity:
# activity decoder
self.future_act_logits = self.activity_head(
obs_enc_h, obs_enc_last_state, scope='activity_predict')
# predict the activity destination
with tf.variable_scope('grid_head', reuse=tf.AUTO_REUSE):
conv_dim = config.scene_conv_dim
assert len(config.scene_grids) == 2
# grid class and grid target output
self.grid_class_logits = []
self.grid_target_logits = []
for i, (h, w) in enumerate(config.scene_grids):
# [h,w,c]
this_scene_conv = self.scene_convs[i]
this_scene_conv = tf.reshape(
this_scene_conv, [N, h*w, conv_dim])
# tile
# [N, h*w, h_dim*k]
h_tile = tf.tile(tf.expand_dims(
obs_enc_last_state.h, axis=1), [1, h*w, 1])
# [N, h*w, conv_dim + h_dim + emb]
scene_feature = tf.concat(
[h_tile, this_scene_conv], axis=-1)
# add the occupation map, grid obs input is already in the h_tile
# [N, T, h*w]
obs_gridclass_onehot = tf.one_hot(
self.grid_obs_labels[i], h*w)
obs_gridclass_occupy = tf.reduce_sum(
obs_gridclass_onehot, axis=1)
obs_gridclass = tf.cast(
obs_gridclass_occupy, 'float32') # [N,h*w]
obs_gridclass = tf.reshape(obs_gridclass, [N, h*w, 1])
# [N, h*w, 1] -> [N, h*w, emb]
obs_grid_class_emb = linear(obs_gridclass,
output_size=config.emb_size,
activation=config.activation_func,
add_bias=True,
scope='obs_grid_class_emb_%d' % i)
scene_feature = tf.concat(
[scene_feature, obs_grid_class_emb], axis=-1)
grid_class_logit = conv2d(tf.reshape(scene_feature, [N, h, w, -1]),
out_channel=1, kernel=1, stride=1,
activation=config.activation_func,
add_bias=True, scope='grid_class_%d' % i)
grid_target_logit_all = conv2d(tf.reshape(scene_feature,
[N, h, w, -1]),
out_channel=2, kernel=1, stride=1,
activation=config.activation_func,
add_bias=True,
scope='grid_target_%d' % i)
grid_class_logit = tf.reshape(
grid_class_logit, [N, h*w, 1])
grid_target_logit_all = tf.reshape(
grid_target_logit_all, [N, h*w, 2])
grid_class_logit = tf.squeeze(grid_class_logit, axis=-1)
# [N]
target_class = tf.argmax(grid_class_logit, axis=-1)
# [N,2]
indices = tf.stack(
[tf.range(N), tf.to_int32(target_class)], axis=-1)
# [N,h*w,2] + [N,2] -> # [N,2]
grid_target_logit = tf.gather_nd(
grid_target_logit_all, indices)
self.grid_class_logits.append(grid_class_logit)
self.grid_target_logits.append(grid_target_logit)
# for loss and forward
self.traj_pred_out = traj_pred_out
# output [N, num_decoder]
# enc_h for future extension, so pylint: disable=unused-argument
def traj_class_head(self, enc_h, enc_last_state, scope='predict_traj_cat'):
"""Trajectory classification branch."""
config = self.config
with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
# [N, hdim*num_enc]
feature = enc_last_state.h
# [N, num_traj_class]
logits = linear(feature, output_size=len(config.traj_cats),
add_bias=False, activation=tf.identity,
scope='traj_cat_logits')
return logits
def activity_head(self, enc_h, enc_last_state, scope='activity_predict'):
"""Activity prediction branch."""
config = self.config
with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
feature = enc_last_state.h
future_act = linear(feature, output_size=config.num_act, add_bias=False,
activation=tf.identity, scope='future_act')
return future_act
def decoder(self, first_input, enc_last_state, enc_h, pred_length, rnn_cell,
top_scope, scope):
"""Decoder definition."""
config = self.config
# Tensor dimensions, so pylint: disable=g-bad-name
N = self.N
P = self.P
with tf.variable_scope(scope):
# this is only used for training
with tf.name_scope('prepare_pred_gt_training'):
# these input only used during training
time_1st_traj_pred = tf.transpose(
self.traj_pred_gt, perm=[1, 0, 2]) # [N,T2,W] -> [T2,N,W]
T2 = tf.shape(time_1st_traj_pred)[0] # T2
traj_pred_gt = tf.TensorArray(size=T2, dtype='float')
traj_pred_gt = traj_pred_gt.unstack(
time_1st_traj_pred) # [T2] , [N,W]
# all None for first call
with tf.name_scope('decoder_rnn'):
def decoder_loop_fn(time, cell_output, cell_state, loop_state):
"""RNN loop function for the decoder."""
emit_output = cell_output # == None for time==0
elements_finished = time >= pred_length
finished = tf.reduce_all(elements_finished)
# h_{t-1}
with tf.name_scope('prepare_next_cell_state'):
if cell_output is None:
next_cell_state = enc_last_state
else:
next_cell_state = cell_state
# x_t
with tf.name_scope('prepare_next_input'):
if cell_output is None: # first time
next_input_xy = first_input # the last observed x,y as input
else:
# for testing, construct from this output to be next input
next_input_xy = tf.cond(
# first check the sequence finished or not
finished,
lambda: tf.zeros([N, P], dtype='float'),
# pylint: disable=g-long-lambda
lambda: tf.cond(
self.is_train,
# this will make training faster than testing
lambda: traj_pred_gt.read(time),
# hidden vector from last step to coordinates
lambda: self.hidden2xy(cell_output, scope=top_scope,
additional_scope='hidden2xy'))
)
# spatial embedding
# [N,emb]
xy_emb = linear(next_input_xy, output_size=config.emb_size,
activation=config.activation_func, add_bias=True,
scope='xy_emb_dec')
next_input = xy_emb
with tf.name_scope('attend_enc'):
# [N,h_dim]
attended_encode_states = focal_attention(
next_cell_state.h, enc_h, use_sigmoid=False,
scope='decoder_attend_encoders')
next_input = tf.concat(
[xy_emb, attended_encode_states], axis=1)
return elements_finished, next_input, next_cell_state, \
emit_output, None # next_loop_state
decoder_out_ta, _, _ = tf.nn.raw_rnn(
rnn_cell, decoder_loop_fn, scope='decoder_rnn')
with tf.name_scope('reconstruct_output'):
decoder_out_h = decoder_out_ta.stack() # [T2,N,h_dim]
# [N,T2,h_dim]
decoder_out_h = tf.transpose(decoder_out_h, perm=[1, 0, 2])
# recompute the output;
# if use loop_state to save the output, will 10x slower
# use the same hidden2xy for different decoder
decoder_out = self.hidden2xy(
decoder_out_h, scope=top_scope, additional_scope='hidden2xy')
return decoder_out
def hidden2xy(self, lstm_h, return_scope=False, scope='hidden2xy',
additional_scope=None):
"""Hiddent states to xy coordinates."""
# Tensor dimensions, so pylint: disable=g-bad-name
P = self.P
with tf.variable_scope(scope, reuse=tf.AUTO_REUSE) as this_scope:
if additional_scope is not None:
return self.hidden2xy(lstm_h, return_scope=return_scope,
scope=additional_scope, additional_scope=None)
out_xy = linear(lstm_h, output_size=P, activation=tf.identity,
add_bias=False, scope='out_xy_mlp2')
if return_scope:
return out_xy, this_scope
return out_xy
def build_loss(self):
"""Model loss."""
config = self.config
losses = []
# N,T,W
# L2 loss
# [N,T2,W]
traj_pred_out = self.traj_pred_out
traj_pred_gt = self.traj_pred_gt
diff = traj_pred_out - traj_pred_gt
xyloss = tf.pow(diff, 2) # [N,T2,2]
xyloss = tf.reduce_mean(xyloss)
self.xyloss = xyloss
losses.append(xyloss)
# trajectory classification loss
if config.multi_decoder:
traj_class_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=self.traj_class_gt, logits=self.traj_class_logits)
traj_class_loss = tf.reduce_mean(
traj_class_loss)*tf.constant(config.traj_class_loss_weight,
dtype='float')
self.traj_class_loss = traj_class_loss
losses.append(traj_class_loss)
# ------------------------ activity destination loss
self.grid_loss = []
grid_loss_weight = config.grid_loss_weight
for i, _ in enumerate(config.scene_grids):
grid_pred_label = self.grid_pred_labels[i] # [N]
grid_pred_target = self.grid_pred_targets[i] # [N,2]
grid_class_logit = self.grid_class_logits[i] # [N,h*w]
grid_target_logit = self.grid_target_logits[i] # [N,2]
# classification loss
class_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=grid_pred_label, logits=grid_class_logit)
class_loss = tf.reduce_mean(class_loss)
# regression loss
regression_loss = tf.losses.huber_loss(
labels=grid_pred_target, predictions=grid_target_logit,
reduction=tf.losses.Reduction.MEAN)
class_loss = class_loss * \
tf.constant(grid_loss_weight, dtype='float')
regression_loss = regression_loss * \
tf.constant(grid_loss_weight, dtype='float')
self.grid_loss.extend([class_loss, regression_loss])
losses.extend([class_loss, regression_loss])
# --------- activity class loss
if config.add_activity:
act_loss_weight = config.act_loss_weight
future_act_logits = self.future_act_logits # [N,num_act]
future_act_label = self.future_act_label # [N,num_act]
activity_loss = tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.cast(future_act_label, 'float32'), logits=future_act_logits)
activity_loss = tf.reduce_mean(activity_loss)
activity_loss = activity_loss * \
tf.constant(act_loss_weight, dtype='float')
self.activity_loss = activity_loss
losses.extend([activity_loss])
if config.wd is not None:
wd = wd_cost('.*/W', config.wd, scope='wd_cost')
if wd:
wd = tf.add_n(wd)
losses.append(wd)
# there might be l2 weight loss in some layer
self.loss = tf.add_n(losses, name='total_losses')
def get_feed_dict(self, batch, is_train=False):
"""Givng a batch of data, construct the feed dict."""
# get the cap for each kind of step first
config = self.config
# Tensor dimensions, so pylint: disable=g-bad-name
N = self.N
P = self.P
KP = self.KP
T_in = config.obs_len
T_pred = config.pred_len
feed_dict = {}
# initial all the placeholder
traj_obs_gt = np.zeros([N, T_in, P], dtype='float')
traj_obs_gt_mask = np.zeros([N, T_in], dtype='bool')
# link the feed_dict
feed_dict[self.traj_obs_gt] = traj_obs_gt
feed_dict[self.traj_obs_gt_mask] = traj_obs_gt_mask
# for getting pred length during test time
traj_pred_gt_mask = np.zeros([N, T_pred], dtype='bool')
feed_dict[self.traj_pred_gt_mask] = traj_pred_gt_mask
# this is needed since it is in tf.conf?
traj_pred_gt = np.zeros([N, T_pred, P], dtype='float')
feed_dict[self.traj_pred_gt] = traj_pred_gt # all zero when testing,
feed_dict[self.is_train] = is_train
data = batch.data
# encoder features
# ------------------------------------- xy input
assert len(data['obs_traj_rel']) == N
for i, (obs_data, pred_data) in enumerate(zip(data['obs_traj_rel'],
data['pred_traj_rel'])):
for j, xy in enumerate(obs_data):
traj_obs_gt[i, j, :] = xy
traj_obs_gt_mask[i, j] = True
for j in range(config.pred_len):
# used in testing to get the prediction length
traj_pred_gt_mask[i, j] = True
# ---------------------------------------
# scene input
obs_scene = np.zeros((N, T_in), dtype='int32')
obs_scene_mask = np.zeros((N, T_in), dtype='bool')
feed_dict[self.obs_scene] = obs_scene
feed_dict[self.obs_scene_mask] = obs_scene_mask
feed_dict[self.scene_feat] = data['batch_scene_feat']
# each bacth
for i in range(len(data['batch_obs_scene'])):
for j in range(len(data['batch_obs_scene'][i])):
# it was (1) shaped
obs_scene[i, j] = data['batch_obs_scene'][i][j][0]
obs_scene_mask[i, j] = True
# [N,num_scale, T] # each is int to num_grid_class
for j, _ in enumerate(config.scene_grids):
this_grid_label = np.zeros([N, T_in], dtype='int32')
for i in range(len(data['obs_grid_class'])):
this_grid_label[i, :] = data['obs_grid_class'][i][j, :]
feed_dict[self.grid_obs_labels[j]] = this_grid_label
# person pose input
if config.add_kp:
obs_kp = np.zeros((N, T_in, KP, 2), dtype='float')
feed_dict[self.obs_kp] = obs_kp
# each bacth
for i, obs_kp_rel in enumerate(data['obs_kp_rel']):
for j, obs_kp_step in enumerate(obs_kp_rel):
obs_kp[i, j, :, :] = obs_kp_step
split = 'train'
if not is_train:
split = 'val'
if config.is_test:
split = 'test'
# this is the h/w the bounding box is based on
person_h = config.person_h
person_w = config.person_w
person_feat_dim = config.person_feat_dim
obs_person_features = np.zeros(
(N, T_in, person_h, person_w, person_feat_dim), dtype='float32')
for i in range(len(data['obs_boxid'])):
for j in range(len(data['obs_boxid'][i])):
boxid = data['obs_boxid'][i][j]
featfile = os.path.join(
config.person_feat_path, split, '%s.npy' % boxid)
obs_person_features[i, j] = np.squeeze(
np.load(featfile), axis=0)
feed_dict[self.obs_person_features] = obs_person_features
# add other boxes,
K = self.K # max_other boxes
other_boxes_class = np.zeros(
(N, T_in, K, config.num_box_class), dtype='float32')
other_boxes = np.zeros((N, T_in, K, 4), dtype='float32')
other_boxes_mask = np.zeros((N, T_in, K), dtype='bool')
for i in range(len(data['obs_other_box'])):
for j in range(len(data['obs_other_box'][i])): # -> seq_len
this_other_boxes = data['obs_other_box'][i][j]
this_other_boxes_class = data['obs_other_box_class'][i][j]
other_box_idxs = range(len(this_other_boxes))
if config.random_other:
random.shuffle(other_box_idxs)
other_box_idxs = other_box_idxs[:K]
# get the current person box
this_person_x1y1x2y2 = data['obs_box'][i][j] # (4)
for k, idx in enumerate(other_box_idxs):
other_boxes_mask[i, j, k] = True
other_box_x1y1x2y2 = this_other_boxes[idx]
other_boxes[i, j, k, :] = self.encode_other_boxes(
this_person_x1y1x2y2, other_box_x1y1x2y2)
# one-hot representation
box_class = this_other_boxes_class[idx]
other_boxes_class[i, j, k, box_class] = 1
feed_dict[self.obs_other_boxes] = other_boxes
feed_dict[self.obs_other_boxes_class] = other_boxes_class
feed_dict[self.obs_other_boxes_mask] = other_boxes_mask
# -----------------------------------------------------------
# ----------------------------training
if is_train:
for i, (obs_data, pred_data) in enumerate(zip(data['obs_traj_rel'],
data['pred_traj_rel'])):
for j, xy in enumerate(pred_data):
traj_pred_gt[i, j, :] = xy
traj_pred_gt_mask[i, j] = True
for j, _ in enumerate(config.scene_grids):
this_grid_label = np.zeros([N], dtype='int32')
this_grid_target = np.zeros([N, 2], dtype='float32')
for i in range(len(data['pred_grid_class'])):
# last pred timestep
this_grid_label[i] = data['pred_grid_class'][i][j, -1]
# last pred timestep
this_grid_target[i] = data['pred_grid_target'][i][j, -1]
# add new label as kxk for more target loss?
feed_dict[self.grid_pred_labels[j]] = this_grid_label
feed_dict[self.grid_pred_targets[j]] = this_grid_target
if config.add_activity:
future_act = np.zeros((N, config.num_act), dtype='uint8')
# for experiment, training activity detection model
for i in range(len(data['future_activity_onehot'])):
future_act[i, :] = data['future_activity_onehot'][i]
feed_dict[self.future_act_label] = future_act
# needed since it is in tf.conf, but all zero in testing
feed_dict[self.traj_class_gt] = np.zeros((N), dtype='int32')
if config.multi_decoder and is_train:
traj_class = np.zeros((N), dtype='int32')
for i in range(len(data['traj_cat'])):
traj_class[i] = data['traj_cat'][i]
feed_dict[self.traj_class_gt] = traj_class
return feed_dict
def encode_other_boxes(self, person_box, other_box):
"""Encoder other boxes."""
# get relative geometric feature
x1, y1, x2, y2 = person_box
xx1, yy1, xx2, yy2 = other_box
x_m = x1
y_m = y1
w_m = x2 - x1
h_m = y2 - y1
x_n = xx1
y_n = yy1
w_n = xx2 - xx1
h_n = yy2 - yy1
return [
math.log(max((x_m - x_n), 1e-3)/w_m),
math.log(max((y_m - y_n), 1e-3)/h_m),
math.log(w_n/w_m),
math.log(h_n/h_m),
]
def wd_cost(regex, wd, scope):
"""Given regex to get the parameter to do regularization.
Args:
regex: regular expression
wd: weight decay factor
scope: variable scope
Returns:
Tensor
"""
params = tf.trainable_variables()
with tf.name_scope(scope):
costs = []
for p in params:
para_name = p.op.name
if re.search(regex, para_name):
regloss = tf.multiply(tf.nn.l2_loss(p), wd, name='%s/wd' % p.op.name)
assert regloss.dtype.is_floating, regloss
if regloss.dtype != tf.float32:
regloss = tf.cast(regloss, tf.float32)
costs.append(regloss)
return costs
def reconstruct(tensor, ref, keep):
"""Reverse the flatten function.
Args:
tensor: the tensor to operate on
ref: reference tensor to get original shape
keep: index of dim to keep