-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumm.py
executable file
·374 lines (268 loc) · 10.8 KB
/
dumm.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
# ==============================================================
# Copyright (C) 2021 whubaichuan. All rights reserved.
# function: Demo of Vessel Trajectory Prediction by sequence-to-sequence model (LSTM)
# ==============================================================
# Create by whubaichuan at 2021.05.02
# Version 1.0
# whubaichuan [[email protected]]
# ==============================================================
import pandas as pd
import numpy as np
import matplotlib
import glob, os
import seaborn as sns
import sys
from sklearn.preprocessing import MinMaxScaler
import sys
import random
from pylab import mpl, plt
from datetime import datetime
import math, time
import itertools
import datetime
from operator import itemgetter
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from math import sqrt
import torch
import torch.nn as nn
from torch.autograd import Variable
import pickle
matplotlib.rcParams['font.family'] ='Malgun Gothic'
matplotlib.rcParams['axes.unicode_minus'] =False
#[전체 데이터 개수,300][2 - XY][2 - 0 : x, 1 : y]
# print(len(data.iloc[0]))
# data.iloc[0][:][0][0]
# print(data.iloc[0][:][0][1])
# print(len(data.iloc[0][:][0][0]))
# print(len(data.iloc[0][:][0][1]))
# print(len(data.iloc[0][:]))
# print(data.iloc[0][:][1])
def loadData(data) :
x_x = data.iloc[:][0][0]
x_y = data.iloc[:][0][1]
y = data.iloc[:][1]
i = 0
data = {"x_x": x_x,
"x_y": x_y, }
dataPd = pd.DataFrame(data)
dataPd.loc[len(dataPd)] = y
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(dataPd[['x_x', 'x_y']].values)
reframed = series_to_supervised(scaled_data, 5,1) # t = 50 ; # 12 -> step = 5 + predict = 1 <- feature = x_pos, y_pos
train_days = 50 # 50
# valid_days = 2
values = reframed.values
train = values[:train_days + 1, :, ]
# valid = values[-valid_days:, :] #<-전체 데이터에서 분류할 것
# return values, train, valid
return values, train, scaler
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
n_vars = 1 if type(data) is list else data.shape[1]
df = pd.DataFrame(data)
cols, names = list(), list()
# input sequence (t-n, ... t-1)
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
# forecast sequence (t, t+1, ... t+n)
for i in range(0, n_out):
cols.append(df.shift(-i))
if i == 0:
names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
else:
names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
# put it all together
agg = pd.concat(cols, axis=1)
agg.columns = names
# drop rows with NaN values
if dropnan:
agg.dropna(inplace=True)
return agg
# Here we define our model as a class
class LSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, num_layers,
output_dim): # num_layers : 2, hidden_dim : 32, input_dim : 1, self : LSTM(1,32,2,batch_firsttrue)
super(LSTM, self).__init__()
# Hidden dimensions
self.hidden_dim = hidden_dim
# Number of hidden layers
self.num_layers = num_layers
# batch_first=True causes input/output tensors to be of shape
# (batch_dim, seq_dim, feature_dim)
self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True)
# Readout layer
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
# Initialize hidden state with zeros
# fc = nn.Linear(hidden_dim, output_dim)
h0 = torch.zeros(self.num_layers, x.size(1), self.hidden_dim).requires_grad_()
# Initialize cell state
c0 = torch.zeros(self.num_layers, x.size(1), self.hidden_dim).requires_grad_()
# We need to detach as we are doing truncated backpropagation through time (BPTT)
# If we don't, we'll backprop all the way to the start even after going through another batch
# out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))
out, (hn, cn) = self.lstm(x)
# Index hidden state of last time step
# out.size() --> 100, 32, 100
# out[:, -1, :] --> 100, 100 --> just want last time step hidden states!
# out = self.fc(out[:, -1, :])
out = self.fc(out[:, :])
# out.size() --> 100, 10
return out
def train(trainData) :
loss_fn = torch.nn.MSELoss()
optimiser = torch.optim.Adam(model.parameters(), lr=0.01)
# 데이터 하나 당 epoch 씩 학습
# for i in range(len(trainData)) :
for i in range(1) :
train_values, train_data, scaler = loadData(trainData.iloc[i])
train_X_, train_y_ = train_data[:, :-2], train_data[:, -2:] # 끝에 두 개가 Y의 x,y에 대한 예측값
print(train_X_)
print(len(train_X_))
sys.exit()
train_X = torch.Tensor(train_X_)
train_y = torch.Tensor(train_y_)
print(train_X.shape, train_y.shape) #(46, 10) (46, 2)
print("train data Num : ",i)
for t in range(num_epochs):
train_X = torch.Tensor(train_X)
train_y = torch.Tensor(train_y)
y_train_pred = model(train_X)
loss = loss_fn(y_train_pred, train_y)
x_loss = loss_fn(y_train_pred[:, 0], train_y[:, 0])
y_loss = loss_fn(y_train_pred[:, 1], train_y[:, 1])
if t % 10 == 0 and t != 0:
print("Epoch ", t, "MSE: ", loss.item())
print("x_loss : ", x_loss.item())
print("y_loss : ", y_loss.item())
hist[t] = loss.item()
# Zero out gradient, else they will accumulate between epochs
optimiser.zero_grad()
# Backward pass
loss.backward()
# Update parameters
optimiser.step()
train_predict = model(train_X)
plt.figure(figsize=(24, 8))
plt.xlabel('x')
plt.ylabel('y')
# train-values의 X값 비교
plt.title(label="train-values의 X값 비교")
plt.plot(list(range(len(train_values[:, 0]))), train_values[:, 0], label='raw_trajectory', c='b')
plt.plot(list(range(len(train_predict[:, 0]))), train_predict[:, 0].detach().numpy(), label='test_predict', c='r')
plt.legend()
plt.show()
plt.gca()
# train-values의 Y값 비교
plt.title(label="train-values의 Y값 비교")
plt.plot(list(range(len(train_values[:, 1]))), train_values[:, 1], label='raw_trajectory', c='b')
plt.plot(list(range(len(train_predict[:, 1]))), train_predict[:, 1].detach().numpy(), label='test_predict', c='r')
plt.legend()
plt.show()
#
# x_loss = loss_fn(train_values[:,0],train_predict[:,0])
# y_loss = loss_fn(train_values[:,1],train_predict[:,1])
#
# print(x_loss)
def test(testData) :
# 데이터 하나 당 epoch 씩 학습
# for i in range(len(testData)):
for i in range(1):
test_values, test_data, scaler = loadData(testData.iloc[i])
test_X, test_y = test_data[:, :-2], test_data[:, -2:] # 끝에 두 개가 Y의 x,y에 대한 예측값
test_X = torch.Tensor(test_X)
test_y = torch.Tensor(test_y)
#todo - loss 가 이 위치 또는 더 상위에 있어야 하나?
test_X = torch.Tensor(test_X).to(device)
test_y = torch.Tensor(test_y).to(device)
y_test_pred = model(test_X)
loss = loss_fn(y_test_pred, test_y)
print("test loss : ", loss.item())
# x_loss = loss_fn(test_values[0], test_y[0])
# y_loss = loss_fn(test_values[1], test_y[1])
#
# print("x_loss : ",x_loss.item())
# print("y_loss : ",y_loss.item())
test_predict = model(test_X)
# plt.figure(figsize=(24, 8))
# plt.xlabel('x')
# plt.ylabel('y')
# # for LSTM
# #test-values의 X값 비교
# plt.title(label="test-values의 X값 비교")
# plt.plot(list(range(len(test_values[:, 0]))),test_values[:, 0], label='raw_trajectory', c='b')
# plt.plot(list(range(len(test_values[:, 0]))), test_predict[:, 0].detach().numpy(), label='test_predict', c='r')
# plt.legend()
# plt.show()
# plt.gca()
# # test-values의 Y값 비교
# plt.title(label="test-values의 Y값 비교")
# plt.plot(list(range(len(test_values[:, 1]))), test_values[:, 1], label='raw_trajectory', c='b')
# plt.plot(list(range(len(test_values[:, 1]))), test_predict[:, 1].detach().numpy(), label='test_predict', c='r')
# plt.legend()
# plt.show()
if __name__ == '__main__':
# with open('./data/listTrainData.pickle', 'rb') as f:
# data1 = pickle.load(f)
# print(data1.head(10))
# print(data1['feature'].head(10))
# print(data1['feature'].iloc[0])
# print(len(data1['feature'].iloc[0]))
# sys.exit()
#[x값 50개 주르륵, y값 50개 주르륵],[예측해야하는 좌표 x,y]
with open('./data/total_3921.pickle', 'rb') as f:
data1 = pickle.load(f)
# 각 값 뽑아야함..
# with open('./data/rawToData1107_30frame.pickle', 'rb') as f:
# data1 = pickle.load(f)
# with open('./data/rawToData1107_60frame.pickle', 'rb') as f:
# data2 = pickle.load(f)
# pd.set_option('display.max_rows', None)
# pd.set_option('display.max_olumns', None)
# total_data = pd.concat([data1, data2])
# total_data = total_data['xyPos']
# print(total_data.info())
# print(total_data.head(10))
# total_data.to_pickle('./data/total_3921.pickle')
sys.exit()
print(torch.cuda.is_available())
# device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
device = torch.device("cpu")
print(device)
# with open('./data/data_prof.pickle', 'rb') as f :
with open('./data/rawToData1107_30frame.pickle', 'rb') as f:
data = pickle.load(f)
print(data.head(10))
sys.exit()
print(data.iloc[0][1])
print(data.info())
print(len(data.iloc[0][1]))
print(data.head())
sys.exit()
flag = int(len(data) * 0.7) # 210
print(flag)
trainData = data.iloc[:flag]
testData = data.iloc[flag:]
#INIT - model
#####################
num_epochs = 200
hist = np.zeros(num_epochs)
# Number of steps to unroll
# seq_dim = look_back - 1
input_dim = 10
hidden_dim = 128
num_layers = 2
output_dim = 2
model = LSTM(input_dim=input_dim, hidden_dim=hidden_dim, output_dim=output_dim, num_layers=num_layers)
loss_fn = torch.nn.MSELoss()
optimiser = torch.optim.Adam(model.parameters(), lr=0.01)
train(trainData)
torch.save(model, './model/model_200_221111.pt')
model = torch.load('./model/model_200_221111.pt').to(device)
start = time.time()
print(start)
test(testData)
end = time.time()
print(end)
print(end-start)