-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTestVisual.py
326 lines (282 loc) · 11.3 KB
/
TestVisual.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
import tensorflow as tf
import numpy as np
import Model
import csv
from TestLoader import TestLoader
import os
import simplekml
# network parameters
n_lstm = 128
lstm_step = 6
batch_size = 1
# Restore LSTM model, seq2seq model and attention-seq2seq model.
lstm_restored = Model.LSTM(n_lstm, lstm_step, batch_size)
checkpoint1 = tf.train.Checkpoint(LSTM_network = lstm_restored)
checkpoint1.restore(tf.train.latest_checkpoint('./SaveLSTM'))
encoder = Model.Encoder(n_lstm, batch_size)
checkpoint2 = tf.train.Checkpoint(Encoder = encoder)
checkpoint2.restore(tf.train.latest_checkpoint('./SaveEncoder'))
decoder = Model.Decoder(n_lstm, batch_size)
checkpoint3 = tf.train.Checkpoint(Decoder = decoder)
checkpoint3.restore(tf.train.latest_checkpoint('./SaveDecoder'))
encoder_a = Model.Encoder(n_lstm, batch_size)
checkpoint4 = tf.train.Checkpoint(EncoderAttention = encoder_a)
checkpoint4.restore(tf.train.latest_checkpoint('./SaveEncoderAttention'))
decoder_a = Model.DecoderAttention(n_lstm, batch_size, 'general')
checkpoint5 = tf.train.Checkpoint(DecoderAttention = decoder_a)
checkpoint5.restore(tf.train.latest_checkpoint('./SaveDecoderAttention'))
# Test functions for models: TestSeq2Seq, TestSeq2SeqAttention, TestLSTM.
def TestSeq2Seq(source_seq, target_seq_in, target_seq_out):
"""Test restored seq2seq model.
Args:
source_seq (shape of [batch_size, source_length, 5])
target_seq_in (shape of [batch_size, pred_length, 5])
target_seq_out (shape of [batch_size, pred_length, 5])
Returns:
pred [np.array(pred)]: The prediction of points. shape of [seq_length, 5].
loss [tensor]: Root Mean Squre Error loss of prediction of points.
"""
loss = 0
pred = []
decoder_length = target_seq_out.shape[1]
# Encode the source.
encoder_outputs = encoder(source_seq)
states = encoder_outputs[1:]
# Decoder predicts the target_seq. decoder_in shape of [batch_size, 1, 5]
decoder_in = tf.expand_dims(target_seq_in[:, 0], 1)
for t in range(decoder_length):
logit, de_state_h, de_state_c= decoder(decoder_in, states)
# logit shape of [batch_size, 5]
decoder_in = tf.expand_dims(logit, 1)
states = de_state_h, de_state_c
# loss function : RSME TODO
loss_0 = tf.keras.losses.MSE(target_seq_out[:, t, 1:3], logit[:, 1:3])
loss += tf.sqrt(loss_0)# TODO
pred.append(logit[0, :])
loss = tf.reduce_mean(loss)
loss = loss / decoder_length
pred = np.array(pred)
return pred, loss.numpy()
'''
def TestAttentionSeq2SeqOld(source_seq, target_seq_in, target_seq_out):
"""Test restored attention_seq2seq model.
Args:
source_seq (shape of [batch_size, source_length, 5])
target_seq_in (shape of [batch_size, pred_length, 5])
target_seq_out (shape of [batch_size, pred_length, 5])
Returns:
pred [np.array(pred)]: The prediction of points. shape of [seq_length, 5].
loss [tensor]: Root Mean Squre Error loss of prediction of points.
"""
loss = 0
pred = []
decoder_length = target_seq_out.shape[1]
# Encode the source.
encoder_outputs = encoder_a(source_seq)
states = encoder_outputs[1:]
# Decoder predicts the target_seq. decoder_in shape of [batch_size, 1, 5]
decoder_in = tf.expand_dims(target_seq_in[:, 0], 1)
for t in range(decoder_length):
logit, _, de_state_h, de_state_c, _= decoder_a(decoder_in, states, encoder_outputs[0])
# logit shape of [batch_size, 5]
decoder_in = tf.expand_dims(logit, 1)
states = de_state_h, de_state_c
# loss function : RSME TODO
loss_0 = tf.keras.losses.MSE(target_seq_out[:, t, 1:3], logit[:, 1:3])
loss += tf.sqrt(loss_0)# TODO
pred.append(logit[0, :])
loss = tf.reduce_mean(loss)
loss = loss / decoder_length
pred = np.array(pred)
return pred, loss.numpy()
'''
def TestAttentionSeq2Seq(source_seq, target_seq_in, target_seq_out):
"""Test restored attention_seq2seq model.
Args:
source_seq (shape of [batch_size, source_length, 5])
target_seq_in (shape of [batch_size, pred_length, 5])
target_seq_out (shape of [batch_size, pred_length, 5])
Returns:
pred [np.array(pred)]: The prediction of points. shape of [seq_length, 5].
loss [tensor]: Root Mean Squre Error loss of prediction of points.
"""
loss = 0
pred = []
decoder_length = target_seq_out.shape[1]
# Encode the source.
encoder_outputs = encoder_a(source_seq)
states = encoder_outputs[1:]
history = encoder_outputs[0]
# Decoder predicts the target_seq. decoder_in shape of [batch_size, 1, 5]
decoder_in = tf.expand_dims(target_seq_in[:, 0], 1)
for t in range(decoder_length):
logit, lstm_out, de_state_h, de_state_c, _= decoder_a(decoder_in, states, history)
# logit shape of [batch_size, 5]
decoder_in = tf.expand_dims(logit, 1)
history_new = tf.expand_dims(lstm_out, 1)
history = tf.concat([history[:, 1:], history_new], 1)
states = de_state_h, de_state_c
# loss function : RSME
loss_0 = tf.keras.losses.MSE(target_seq_out[:, t, 1:3], logit[:, 1:3])
loss += tf.sqrt(loss_0)
pred.append(logit[0, :])
loss = tf.reduce_mean(loss)
loss = loss / decoder_length
pred = np.array(pred)
return pred, loss.numpy()
def TestLSTM(test_x, test_y):
"""Test restored lstm model.
Args:
test_x (shape of [batch_size, source_length, 5])
test_y ([batch_size, pred_length, 5])
Returns:
pred [np.array(pred)]: The prediction of points. shape of [seq_length, 5].
loss [tensor]: Root Mean Squre Error loss of prediction of points.
"""
loss = 0.0
pred = []
seq_length = test_y.shape[1]
for t in range(seq_length):
lstm_in = StepProcess(test_x, batch_size, source_length, lstm_step)
logit = lstm_restored(lstm_in)
# loss function : RSME TODO
loss_0 = tf.keras.losses.MSE(test_y[:, t, 1:3], logit[:, 1:3])
loss += tf.sqrt(loss_0)# TODO
pred_point = np.reshape(logit.numpy(), [batch_size, 1, 5])
test_x = np.concatenate((test_x[:, 1:source_length, :], pred_point), axis=1)
pred.append(logit[0,:])
loss = tf.reduce_mean(loss)
loss = loss / seq_length
pred = np.array(pred)
return pred, loss.numpy()
def StepProcess(input, batch_size, seq_length, lstm_step):
if lstm_step == 1:
return input
else:
seq_length_new = seq_length - lstm_step + 1
output = []
for i in range(lstm_step):
seq_sub = input[:, i:seq_length_new+i, :]
output.append(seq_sub)
return np.concatenate(output, axis=2)
# Load test data
test_loader = TestLoader()
test_loader.loadTestTrajectory("./DataSet/test_fix.csv")
source_length = 120
target_length = 60
source_seq, source_coordinates, target_seq, target_coordinates= test_loader.getTestSeq2Seq(batch_size, source_length, target_length)
# LSTM
test_x = source_seq
test_y = target_seq[:, 1:target_length+1, :]
pred_lstm, loss = TestLSTM(test_x, test_y)
print("Result of LSTM_%d: %f" % (target_length, loss))
# Seq2Seq
target_seq_in = target_seq[:, :target_length, :]
target_seq_out = target_seq[:, 1:target_length+1, :]
pred_seq2seq, loss = TestSeq2Seq(source_seq, target_seq_in, target_seq_out)
print("Result of Seq2Seq_%d: %f" % (target_length, loss))
# Seq2SeqAttention
pred_aseq2seq, loss = TestAttentionSeq2Seq(source_seq, target_seq_in, target_seq_out)
print("Result of Seq2SeqAttention_%d: %f" % (target_length, loss))
# Coordinates recovery: denormalize and convert to list.
# source coordinates
lng_source = source_coordinates[0, :, 0]
lat_source = source_coordinates[0, :, 1]
lng_source = lng_source * (test_loader.max_train_data[5] - test_loader.min_train_data[5]) + test_loader.min_train_data[5]
lat_source = lat_source * (test_loader.max_train_data[6] - test_loader.min_train_data[6]) + test_loader.min_train_data[6]
lng_source = lng_source.tolist()
lat_source = lat_source.tolist()
kml = simplekml.Kml(open=1)
lng1 = lng_source[0]
lat1 = lat_source[0]
for i in range(1, len(lng_source)):
lng2 = lng_source[i]
lat2 = lat_source[i]
name = '%d' %i
linestring = kml.newlinestring(name=name)
linestring.coords = [(lng1, lat1), (lng2, lat2)]
lng1 = lng2
lat1 = lat2
kml.save('./Visualization/source.kml')
# true coordinates
lng_true = target_coordinates[0, :, 0]
lat_true = target_coordinates[0, :, 1]
lng_true = lng_true * (test_loader.max_train_data[5] - test_loader.min_train_data[5]) + test_loader.min_train_data[5]
lat_true = lat_true * (test_loader.max_train_data[6] - test_loader.min_train_data[6]) + test_loader.min_train_data[6]
lng_true = lng_true.tolist()
lat_true = lat_true.tolist()
lng0 = lng_true[0]
lat0 = lat_true[0]
kml = simplekml.Kml(open=1)
lng1 = lng_true[0]
lat1 = lat_true[0]
for i in range(1, len(lng_true)):
lng2 = lng_true[i]
lat2 = lat_true[i]
name = '%d' %i
linestring = kml.newlinestring(name=name)
linestring.coords = [(lng1, lat1), (lng2, lat2)]
lng1 = lng2
lat1 = lat2
kml.save('./Visualization/true.kml')
# pred coordomates
delta_lng = pred_aseq2seq[:, 1]
delta_lat =pred_aseq2seq[:, 2]
delta_lng = delta_lng * (test_loader.max_train_data[1] - test_loader.min_train_data[1]) + test_loader.min_train_data[1]
delta_lat = delta_lat * (test_loader.max_train_data[2] - test_loader.min_train_data[2]) + test_loader.min_train_data[2]
delta_lng = delta_lng.tolist()
delta_lat = delta_lat.tolist()
# delta_lng, delta_lat to lng, lat
lng0 = lng_true[0]
lat0 = lat_true[0]
lng_pred = []
lat_pred = []
for i in range(len(delta_lng)):
lng = lng0 + delta_lng[i]
lat = lat0 + delta_lat[i]
lng_pred.append(lng)
lat_pred.append(lat)
lng0 = lng
lat0 = lat
kml = simplekml.Kml(open=1)
lng1 = lng_true[0]
lat1 = lat_true[0]
for i in range(len(lng_pred)):
lng2 = lng_pred[i]
lat2 = lat_pred[i]
name = '%d' %i
linestring = kml.newlinestring(name=name)
linestring.coords = [(lng1, lat1), (lng2, lat2)]
lng1 = lng2
lat1 = lat2
kml.save('./Visualization/pred_a.kml')
# pred coordomates
delta_lng = pred_seq2seq[:, 1]
delta_lat = pred_seq2seq[:, 2]
delta_lng = delta_lng * (test_loader.max_train_data[1] - test_loader.min_train_data[1]) + test_loader.min_train_data[1]
delta_lat = delta_lat * (test_loader.max_train_data[2] - test_loader.min_train_data[2]) + test_loader.min_train_data[2]
delta_lng = delta_lng.tolist()
delta_lat = delta_lat.tolist()
lng0 = lng_true[0]
lat0 = lat_true[0]
lng_pred = []
lat_pred = []
for i in range(len(delta_lng)):
lng = lng0 + delta_lng[i]
lat = lat0 + delta_lat[i]
lng_pred.append(lng)
lat_pred.append(lat)
lng0 = lng
lat0 = lat
kml = simplekml.Kml(open=1)
lng1 = lng_true[0]
lat1 = lat_true[0]
for i in range(len(lng_pred)):
lng2 = lng_pred[i]
lat2 = lat_pred[i]
name = '%d' %i
linestring = kml.newlinestring(name=name)
linestring.coords = [(lng1, lat1), (lng2, lat2)]
lng1 = lng2
lat1 = lat2
kml.save('./Visualization/pred_seq.kml')