-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm_keras.py
489 lines (376 loc) · 18 KB
/
lstm_keras.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
"""
single LSTM (not seqence modeling)
"""
from __future__ import print_function
from keras.models import Model
from keras.layers import Input, LSTM, Dense
from keras.layers import Lambda,Concatenate,Flatten,Reshape
from keras.callbacks import ModelCheckpoint,ReduceLROnPlateau,EarlyStopping
from keras import backend as K
from keras.models import load_model
import sys,glob,io,random
from dataLayer import DataLayer
import cost as costfunc
from config_11_22 import cfg
from dataIO import clip_xyz0
import utility_single as util
from random import shuffle
import matplotlib.pyplot as plt
import _pickle as pickle
import numpy as np
import pdb
batch_size = 32 # Batch size for training.
epochs = 2 # Number of epochs to train for.
latent_dim = 64 # Latent dimensionality of the encoding space.
fps = 30
num_encoder_tokens = 3*fps
num_decoder_tokens = 6
max_encoder_seq_length = cfg.running_length
max_decoder_seq_length = cfg.predict_step
expand_dim_layer = Lambda(lambda x: K.expand_dims(x,1))
Concatenatelayer1 = Concatenate(axis=-1)
def generate_fake_batch(x):
"""generate new data for 1 second using predicted mean and variance"""
mu = x[0]
var = x[1]
temp = K.random_normal(shape = (batch_size,fps), mean=mu,stddev=var)
return temp
generate_fake_batch_layer = Lambda(lambda x: generate_fake_batch(x))
# ************************************
# **
# 1st part: 10-step input **
# 1-step loss during training **
# **
# ************************************
### ====================Graph def====================
# single layer LSTM
if not cfg.input_mean_var:
inputs = Input(shape=(None, num_encoder_tokens))
else:
inputs = Input(shape=(None, num_decoder_tokens))
lstm = LSTM(latent_dim, return_state=True)
# encoder_outputs, state_h, state_c = lstm(inputs)
# states = [state_h, state_c]
output_dense = Dense(num_decoder_tokens,activation='tanh')
all_outputs = []
for time_ind in range(max_decoder_seq_length):
this_inputs = util.slice_layer(1,time_ind,time_ind+1)(inputs)
if time_ind==0:
decoder_states, state_h, state_c = lstm(this_inputs)#no initial states
else:
decoder_states, state_h, state_c = lstm(this_inputs,
initial_state=states)
outputs = output_dense(decoder_states)
all_outputs.append(expand_dim_layer(outputs))
# this_inputs = outputs
states = [state_h, state_c]
all_outputs = Lambda(lambda x: K.concatenate(x, axis=1))(all_outputs)
model = Model(inputs, all_outputs)
model.compile(optimizer='Adam', loss='mean_squared_error',metrics=['accuracy'])
#### ========================================data============================================================
video_data_train = pickle.load(open('./data/shanghai_dataset_xyz_train.p','rb'),encoding='latin1')
video_data_train = clip_xyz0(video_data_train)
datadb = video_data_train.copy()
# assert cfg.data_chunk_stride=1
_video_db,_video_db_future,_video_db_future_input = util.get_data(datadb,pick_user=False,num_user=34)
if cfg.input_mean_var:
input_data = util.get_gt_target_xyz(_video_db_future_input)
else:
input_data = _video_db_future_input
target_data = util.get_gt_target_xyz(_video_db_future)
# ### ====================Training====================
tag = 'single_LSTM_keras_sep24'
model_checkpoint = ModelCheckpoint(tag+'{epoch:02d}-{val_loss:.4f}.h5', monitor='val_loss', save_best_only=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
patience=3, min_lr=1e-6)
stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='auto')
model.fit(input_data, target_data,
batch_size=batch_size,
epochs=epochs,
validation_split=0.1,
shuffle=False,
initial_epoch=0,
callbacks=[model_checkpoint, reduce_lr, stopping])
### ====================Testing====================
# =================1-step input during testing======================
# define sampling model. note the "this_inputs = outputs"
if not cfg.input_mean_var:
inputs = Input(shape=(1, num_encoder_tokens))
else:
inputs = Input(shape=(1, num_decoder_tokens))
lstm = LSTM(latent_dim, return_state=True)
output_dense = Dense(num_decoder_tokens,activation='tanh')
all_outputs = []
this_inputs = inputs
for time_ind in range(max_decoder_seq_length):
if time_ind==0:
decoder_states, state_h, state_c = lstm(this_inputs)#no initial states
else:
decoder_states, state_h, state_c = lstm(this_inputs,
initial_state=states)
outputs = output_dense(decoder_states)
if cfg.predict_mean_var and cfg.sample_and_refeed:
ux_temp = util.slice_layer(1,0,1)(outputs)
uy_temp = util.slice_layer(1,1,2)(outputs)
uz_temp = util.slice_layer(1,2,3)(outputs)
varx_temp = util.slice_layer(1,3,4)(outputs)
vary_temp = util.slice_layer(1,4,5)(outputs)
varz_temp = util.slice_layer(1,5,6)(outputs)
temp_newdata = expand_dim_layer(Concatenatelayer1([generate_fake_batch_layer([ux_temp,varx_temp]),
generate_fake_batch_layer([uy_temp,vary_temp]),
generate_fake_batch_layer([uz_temp,varz_temp])]))
this_inputs = temp_newdata
all_outputs.append(expand_dim_layer(outputs))
states = [state_h, state_c]
all_outputs = Lambda(lambda x: K.concatenate(x, axis=1))(all_outputs)
model = Model(inputs, all_outputs)
# model.load_weights('single_LSTM_keras_sep24200-0.0122.h5')
# data
video_data_test = pickle.load(open('./data/shanghai_dataset_xyz_test.p','rb'),encoding='latin1')
video_data_test = clip_xyz0(video_data_test)
datadb = video_data_test.copy()
_,_video_db_future,_video_db_future_input = util.get_data(datadb,pick_user=False)
if cfg.input_mean_var:
_video_db = util.get_gt_target_xyz(_video_db_future_input)
else:
_video_db = _video_db_future_input #use this as input
def decode_sequence_fov(input_seq):
last_location = input_seq[:,0,:][:,np.newaxis,:] #1-step input during testing
if cfg.input_mean_var:
last_mu_var = util.get_gt_target_xyz(last_location)
else:
last_mu_var = last_location
decoded_sentence = model.predict(last_mu_var)
return last_mu_var, decoded_sentence
testing_input_list = []
gt_sentence_list = []
decoded_sentence_list = []
for seq_index in range(0,_video_db.shape[0],batch_size):
input_seq = _video_db[seq_index: seq_index + batch_size,:,:]
if input_seq.shape[0]<batch_size:
break
input_seq1, decoded_sentence = decode_sequence_fov(input_seq)
testing_input_list+= [input_seq1] #model input
decoded_sentence_list+=[decoded_sentence]
gt_sentence = _video_db_future[seq_index: seq_index + batch_size,:,:]
gt_sentence_list+=[gt_sentence]
decoder_target = util.get_gt_target_xyz(gt_sentence)
# print('-')
# print('Decoded sentence - decoder_target:', np.squeeze(np.array(decoded_sentence))[:,:3]-np.squeeze(decoder_target)[:,:3])
pickle.dump(decoded_sentence_list,open('decoded_sentence'+tag+'.p','wb'))
pickle.dump(gt_sentence_list,open('gt_sentence_list'+tag+'.p','wb'))
pickle.dump(testing_input_list,open('testing_input_list'+tag+'.p','wb'))
print('Testing finished!')
# # ************************************
# # **
# # 2nd part: 1-step input **
# # 10-step loss during training **
# # **
# # ************************************
# if not cfg.input_mean_var:
# inputs = Input(shape=(1, num_encoder_tokens))
# else:
# inputs = Input(shape=(1, num_decoder_tokens))
# lstm = LSTM(latent_dim, return_state=True)
# output_dense = Dense(num_decoder_tokens,activation='tanh')
# all_outputs = []
# this_inputs = inputs
# for time_ind in range(max_decoder_seq_length):
# if time_ind==0:
# decoder_states, state_h, state_c = lstm(this_inputs)#no initial states
# else:
# decoder_states, state_h, state_c = lstm(this_inputs,
# initial_state=states)
# outputs = output_dense(decoder_states)
# if cfg.predict_mean_var and cfg.sample_and_refeed:
# ux_temp = util.slice_layer(1,0,1)(outputs)
# uy_temp = util.slice_layer(1,1,2)(outputs)
# uz_temp = util.slice_layer(1,2,3)(outputs)
# varx_temp = util.slice_layer(1,3,4)(outputs)
# vary_temp = util.slice_layer(1,4,5)(outputs)
# varz_temp = util.slice_layer(1,5,6)(outputs)
# temp_newdata = expand_dim_layer(Concatenatelayer1([generate_fake_batch_layer([ux_temp,varx_temp]),
# generate_fake_batch_layer([uy_temp,vary_temp]),
# generate_fake_batch_layer([uz_temp,varz_temp])]))
# this_inputs = temp_newdata
# all_outputs.append(expand_dim_layer(outputs))
# states = [state_h, state_c]
# all_outputs = Lambda(lambda x: K.concatenate(x, axis=1))(all_outputs)
# model = Model(inputs, all_outputs)
# model.compile(optimizer='Adam', loss='mean_squared_error',metrics=['accuracy'])
# #### ========================================data============================================================
# video_data_train = pickle.load(open('./360video/data/shanghai_dataset_xyz_train.p','rb'),encoding='latin1') #76414,10,90
# video_data_train = clip_xyz(video_data_train)
# datadb = video_data_train.copy()
# # assert cfg.data_chunk_stride=1
# _video_db,_video_db_future,_video_db_future_input = util.get_data(datadb,pick_user=False,num_user=34)
# if cfg.input_mean_var:
# input_data = util.get_gt_target_xyz(_video_db_future_input)
# else:
# input_data = _video_db_future_input
# input_data = input_data[:,0,:][:,np.newaxis,:] #1-step input
# target_data = util.get_gt_target_xyz(_video_db_future)
# # if using the generate fake batch layer, the dataset size has to
# # be dividable by the batch size
# validation_ratio=0.1
# if cfg.sample_and_refeed or cfg.stateful_across_batch:
# sample_ind = util.rand_sample_ind(input_data.shape[0],0,batch_size,validation_ratio=validation_ratio)
# if not cfg.shuffle_data:
# sample_ind = sorted(sample_ind)
# input_data = util.rand_sample(input_data,sample_ind)
# target_data = util.rand_sample(target_data,sample_ind)
# ### ====================Training====================
# tag = 'single_LSTM_keras_sep24'
# model_checkpoint = ModelCheckpoint(tag+'{epoch:02d}-{val_loss:.4f}.h5', monitor='val_loss', save_best_only=True)
# reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
# patience=3, min_lr=1e-6)
# stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='auto')
# model.fit(input_data, target_data,
# batch_size=batch_size,
# epochs=epochs,
# validation_split=0.1,
# shuffle=False,
# initial_epoch=0,
# callbacks=[model_checkpoint, reduce_lr, stopping])
# ### ====================Testing====================
# # model.load_weights('single_LSTM_keras_10steploss_sep2540-0.1204.h5')
# # data
# # video_data_test = pickle.load(open('./360video/data/shanghai_dataset_xyz_test.p','rb'),encoding='latin1') #20528,10,90
# # video_data_test = clip_xyz(video_data_test)
# # datadb = video_data_test.copy()
# # _,_video_db_future,_video_db_future_input = util.get_data(datadb,pick_user=False)
# dataformat = 'format5_tsinghua_by_sec_interp' #tsinghua
# option=''
# # _video_db_tar = util.load_h5('./cache/'+dataformat+'/test/'+option+'_video_db_tar.h5','_video_db_tar')
# _video_db_future = util.load_h5('./cache/'+dataformat+'/test/'+option+'_video_db_future_tar.h5','_video_db_future_tar') #(6768, 10, 90)
# _video_db_future_input = util.load_h5('./cache/'+dataformat+'/test/'+option+'_video_db_future_input_tar.h5','_video_db_future_input_tar')
# thu_tag='_thu_'
# if cfg.input_mean_var:
# _video_db = util.get_gt_target_xyz(_video_db_future_input)
# else:
# _video_db = _video_db_future_input #use this as input
# def decode_sequence_fov(input_seq):
# last_location = input_seq[:,0,:][:,np.newaxis,:] #1-step input during testing
# if cfg.input_mean_var:
# last_mu_var = util.get_gt_target_xyz(last_location)
# else:
# last_mu_var = last_location
# decoded_sentence = model.predict(last_mu_var)
# return decoded_sentence
# gt_sentence_list = []
# decoded_sentence_list = []
# for seq_index in range(0,_video_db.shape[0],batch_size):
# input_seq = _video_db[seq_index: seq_index + batch_size,:,:]
# if input_seq.shape[0]<batch_size:
# break
# decoded_sentence = decode_sequence_fov(input_seq)
# decoded_sentence_list+=[decoded_sentence]
# gt_sentence = _video_db_future[seq_index: seq_index + batch_size,:,:]
# gt_sentence_list+=[gt_sentence]
# decoder_target = util.get_gt_target_xyz(gt_sentence)
# # print('-')
# # print('Decoded sentence - decoder_target:', np.squeeze(np.array(decoded_sentence))[:,:3]-np.squeeze(decoder_target)[:,:3])
# pickle.dump(decoded_sentence_list,open('decoded_sentence'+thu_tag+tag+'.p','wb'))
# pickle.dump(gt_sentence_list,open('gt_sentence_list'+thu_tag+tag+'.p','wb'))
# print('Testing finished!')
# ************************************
# **
# 3rd part: 10-step input **
# 10-step loss during training **
# **
# ************************************
if not cfg.input_mean_var:
inputs = Input(shape=(None, num_encoder_tokens))
else:
inputs = Input(shape=(None, num_decoder_tokens))
lstm = LSTM(latent_dim, return_state=True)
encoder_outputs, state_h, state_c = lstm(inputs)
states = [state_h, state_c]
output_dense = Dense(num_decoder_tokens,activation='tanh')
all_outputs = []
for time_ind in range(max_decoder_seq_length):
if time_ind==0:
decoder_states = encoder_outputs
else:
decoder_states, state_h, state_c = lstm(this_inputs)
outputs = output_dense(decoder_states)
all_outputs.append(expand_dim_layer(outputs))
if cfg.predict_mean_var and cfg.sample_and_refeed:
ux_temp = util.slice_layer(1,0,1)(outputs)
uy_temp = util.slice_layer(1,1,2)(outputs)
uz_temp = util.slice_layer(1,2,3)(outputs)
varx_temp = util.slice_layer(1,3,4)(outputs)
vary_temp = util.slice_layer(1,4,5)(outputs)
varz_temp = util.slice_layer(1,5,6)(outputs)
temp_newdata = expand_dim_layer(Concatenatelayer1([generate_fake_batch_layer([ux_temp,varx_temp]),
generate_fake_batch_layer([uy_temp,vary_temp]),
generate_fake_batch_layer([uz_temp,varz_temp])]))
this_inputs = temp_newdata
all_outputs = Lambda(lambda x: K.concatenate(x, axis=1))(all_outputs)
model = Model(inputs, all_outputs)
model.compile(optimizer='Adam', loss='mean_squared_error',metrics=['accuracy'])
#### ========================================data============================================================
video_data_train = pickle.load(open('./360video/data/shanghai_dataset_xyz_train.p','rb'),encoding='latin1')
video_data_train = clip_xyz(video_data_train)
datadb = video_data_train.copy()
# assert cfg.data_chunk_stride=1
_video_db,_video_db_future,_video_db_future_input = util.get_data(datadb,pick_user=False,num_user=34)
if cfg.input_mean_var:
input_data = util.get_gt_target_xyz(_video_db)
else:
input_data = _video_db
target_data = util.get_gt_target_xyz(_video_db_future)
# if using the generate fake batch layer, the dataset size has to
# be dividable by the batch size
validation_ratio=0.1
if cfg.sample_and_refeed or cfg.stateful_across_batch:
sample_ind = util.rand_sample_ind(input_data.shape[0],0,batch_size,validation_ratio=validation_ratio)
if not cfg.shuffle_data:
sample_ind = sorted(sample_ind)
input_data = util.rand_sample(input_data,sample_ind)
target_data = util.rand_sample(target_data,sample_ind)
### ====================Training====================
tag = 'single_LSTM_keras_10input_10steploss_model3_sep25'
model_checkpoint = ModelCheckpoint(tag+'{epoch:02d}-{val_loss:.4f}.h5', monitor='val_loss', save_best_only=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
patience=3, min_lr=1e-6)
stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='auto')
model.fit(input_data, target_data,
batch_size=batch_size,
epochs=epochs,
validation_split=validation_ratio,
shuffle=False,
initial_epoch=0,
callbacks=[model_checkpoint, reduce_lr, stopping])
### ====================Testing====================
# data
video_data_test = pickle.load(open('./360video/data/shanghai_dataset_xyz_test.p','rb'),encoding='latin1')
video_data_test = clip_xyz(video_data_test)
datadb = video_data_test.copy()
_video_db,_video_db_future,_video_db_future_input = util.get_data(datadb,pick_user=False)
if cfg.input_mean_var:
_video_db = util.get_gt_target_xyz(_video_db)
def decode_sequence_fov(input_seq):
last_location = input_seq #10-step input during testing (last 10 sec)
if cfg.input_mean_var:
last_mu_var = util.get_gt_target_xyz(last_location)
else:
last_mu_var = last_location
decoded_sentence = model.predict(last_mu_var)
return decoded_sentence
gt_sentence_list = []
decoded_sentence_list = []
for seq_index in range(0,_video_db.shape[0],batch_size):
input_seq = _video_db[seq_index: seq_index + batch_size,:,:]
if input_seq.shape[0]<batch_size:
break
decoded_sentence = decode_sequence_fov(input_seq)
decoded_sentence_list+=[decoded_sentence]
gt_sentence = _video_db_future[seq_index: seq_index + batch_size,:,:]
gt_sentence_list+=[gt_sentence]
decoder_target = util.get_gt_target_xyz(gt_sentence)
# print('-')
# print('Decoded sentence - decoder_target:', np.squeeze(np.array(decoded_sentence))[:,:3]-np.squeeze(decoder_target)[:,:3])
pickle.dump(decoded_sentence_list,open('decoded_sentence'+tag+'.p','wb'))
pickle.dump(gt_sentence_list,open('gt_sentence_list'+tag+'.p','wb'))
print('Testing finished!')