forked from svip-lab/CIDNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
216 lines (161 loc) · 8.55 KB
/
train.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
# -*- coding: utf-8 -*-
# @Time : 2018/1/20 下午5:01
# @Author : Zhixin Piao
# @Email : [email protected]
from base.base_network import *
import visdom
class Trace2Trace_LN:
def __init__(self):
self.pedestrian_num = 20
self.hidden_size = 128
# input
self.input_frame = 5
self.input_size = 2 # * self.input_frame
self.n_layers = 1
# target
self.target_frame = 5
self.target_size = 2
self.window_size = 1
# learn
self.lr = 2e-3
self.weight_decay = 5e-3
self.batch_size = 1000
self.n_epochs = 10000
# show
self.vis = None
self.train_loss_list = []
self.test_loss_list = []
def load_data(self, data_path):
# load data
data = np.load(data_path)
train_X, train_Y = data['train_X'], data['train_Y']
test_X, test_Y = data['test_X'], data['test_Y']
if self.batch_size <= 0:
self.batch_size = train_X.shape[0]
self.test_input_traces = Variable(torch.from_numpy(test_X).cuda())
self.test_target_traces = Variable(torch.from_numpy(test_Y).cuda())
# (B, pedestrian_num, frame_size, 2)
train_input_traces = torch.from_numpy(train_X)
# (B, pedestrian_num, frame_size, 2)
train_target_traces = torch.from_numpy(train_Y)
self.train_input_traces = Variable(train_input_traces.cuda())
self.train_target_traces = Variable(train_target_traces.cuda())
# data loader
train = torch.utils.data.TensorDataset(train_input_traces, train_target_traces)
self.train_loader = torch.utils.data.DataLoader(train, batch_size=self.batch_size, shuffle=False, num_workers=4)
def main_compute_step(self, batch_input_traces, batch_target_traces):
batch_size = batch_input_traces.size(0)
target_traces = batch_input_traces[:, :, self.input_frame - 1]
encoder_hidden = self.encoder_net.init_hidden(batch_size)
# run LSTM in observation frame
for i in xrange(self.input_frame - 1):
input_hidden_traces, encoder_hidden = self.encoder_net(batch_input_traces[:, :, i], encoder_hidden)
regression_list = []
for i in xrange(self.target_frame):
# encode LSTM
input_hidden_traces, encoder_hidden = self.encoder_net(target_traces, encoder_hidden)
# NN with Attention
target_hidden_traces = self.decoder_net(target_traces)
Attn_nn = self.attn(target_hidden_traces, target_hidden_traces)
c_traces = torch.bmm(Attn_nn, input_hidden_traces)
# predict next frame traces
regression_traces = self.regression_net(c_traces, target_hidden_traces, target_traces)
# decoder --> location
target_traces = regression_traces
regression_list.append(regression_traces)
regression_traces = torch.stack(regression_list, 2)
# compute loss
L2_square_loss = ((batch_target_traces - regression_traces) ** 2).sum() / self.pedestrian_num
MSE_loss = ((batch_target_traces - regression_traces) ** 2).sum(3).sqrt().mean()
self.loss = L2_square_loss
return L2_square_loss.data[0], MSE_loss.data[0], regression_traces
def train(self, train_input_traces, train_target_traces):
# Zero gradients of both optimizers
self.encoder_net.zero_grad()
self.decoder_net.zero_grad()
self.regression_net.zero_grad()
L2_square_loss, MSE_loss, _ = self.main_compute_step(train_input_traces, train_target_traces)
self.loss.backward()
# MSE_loss.backward()
# Update parameters with optimizers
self.encoder_optimizer.step()
self.decoder_optimizer.step()
self.regression_optimizer.step()
return MSE_loss, L2_square_loss
def test(self):
L2_square_loss, MSE_loss, _ = self.main_compute_step(self.test_input_traces, self.test_target_traces)
return MSE_loss
def predict(self, input_traces, ground_truth_traces, dir_name, batch_size=20):
_, _, regression_traces = self.main_compute_step(input_traces[:batch_size], ground_truth_traces[:batch_size])
# draw predict image
format_input_traces = input_traces[:batch_size].data.cpu().numpy()
format_ground_truth_traces = ground_truth_traces[:batch_size].data.cpu().numpy()
format_predict_traces = regression_traces.data.cpu().numpy()
for k in xrange(batch_size):
fig = plt.figure(figsize=(10, 10))
for i in xrange(20):
plt.scatter(format_input_traces[k, i, :, 1], format_input_traces[k, i, :, 0], color='r')
plt.scatter(format_ground_truth_traces[k, i, :, 1], format_ground_truth_traces[k, i, :, 0], color='b')
plt.scatter(format_predict_traces[k, i, :, 1], format_predict_traces[k, i, :, 0], color='g')
fig.savefig('%s/%d.png' % (dir_name, k))
fig.close()
def save_model(self, model_name, epoch):
dir_path = '%s/lr_%s_iter_%d' % (self.model_path, self.lr, epoch)
if not os.path.exists(dir_path):
os.system('mkdir -p %s' % dir_path)
torch.save(self.encoder_net.state_dict(), '%s/encoder_net.pkl' % dir_path)
torch.save(self.decoder_net.state_dict(), '%s/decoder_net.pkl' % dir_path)
torch.save(self.regression_net.state_dict(), '%s/regression_net.pkl' % dir_path)
def main(self, model_name, log):
cur_time = time.strftime('%Y-%m-%d-%X', time.localtime())
self.model_path = 'model/%s/%s' % (model_name, cur_time)
if log:
if not os.path.exists(self.model_path):
os.system('mkdir -p %s' % self.model_path)
self.log_file = open('%s/message.log' % self.model_path, 'a')
self.log_file.write('lr: %s, n_epochs: %d\n' % (self.lr, self.n_epochs) + '-' * 40 + '\n')
self.log_file.write('model LSTM layers number: %d\n' % self.n_layers)
self.log_file.flush()
self.encoder_net = EncoderNetWithLSTM(self.pedestrian_num, self.input_size, self.hidden_size, n_layers=self.n_layers)
self.decoder_net = DecoderNet(self.pedestrian_num, self.target_size, self.hidden_size, self.window_size)
self.regression_net = RegressionNet(self.pedestrian_num, self.target_size, self.hidden_size)
self.attn = Attention()
self.encoder_optimizer = optim.Adam(self.encoder_net.parameters(), lr=self.lr, weight_decay=self.weight_decay)
self.decoder_optimizer = optim.Adam(self.decoder_net.parameters(), lr=self.lr, weight_decay=self.weight_decay)
self.regression_optimizer = optim.Adam(self.regression_net.parameters(), lr=self.lr, weight_decay=self.weight_decay)
self.encoder_net.cuda()
self.decoder_net.cuda()
self.regression_net.cuda()
for epoch in xrange(1, self.n_epochs + 1):
for i, (train_input_traces, train_target_traces) in enumerate(self.train_loader):
train_input_traces = Variable(train_input_traces.cuda())
train_target_traces = Variable(train_target_traces.cuda())
MSE_loss, L2_square_loss = self.train(train_input_traces, train_target_traces)
loss_msg = 'Epoch: [%d/%d], L2_suqare_loss: %.9f, MSE_loss: %.9f\n' % (epoch, self.n_epochs, L2_square_loss, MSE_loss)
self.train_loss_list.append(MSE_loss)
self.vis.line(np.array(self.train_loss_list), win='train', opts={'title': 'train loss'})
print loss_msg
if log:
self.log_file.write(loss_msg)
self.log_file.flush()
test_loss = self.test()
test_loss_msg = '----TEST----\n' + 'MSE Loss:%s\n\n' % test_loss
self.test_loss_list.append(test_loss)
self.vis.line(np.array(self.test_loss_list), win='test', opts={'title': 'test loss'})
print test_loss_msg
if log:
self.log_file.write(test_loss_msg)
self.log_file.flush()
if epoch % 100 is 0:
self.save_model(model_name, epoch)
self.save_model(model_name, self.n_epochs)
self.predict(self.train_input_traces, self.train_target_traces, 'train_predict')
self.predict(self.test_input_traces, self.test_target_traces, 'test_predict')
def main():
model = Trace2Trace_LN()
model.load_data('data/GC/xy_data_set.npz')
model.n_layers = 2
model.vis = visdom.Visdom(env='LSTM-NN-GC')
model.main('LSTM-NN-GC', log=True)
if __name__ == '__main__':
main()