-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeed.py
executable file
·136 lines (109 loc) · 6.07 KB
/
Speed.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
import utils
import torch
import torch.nn as nn
import torch.nn.functional as F
from attention import *
class ShortSpeed(nn.Module):
def __init__(self):
super(ShortSpeed, self).__init__()
self.build()
def build(self):
# self.process_shortspeeds = nn.Linear(48, 16)
self.short_kernel_size = 2
self.short_cnn = nn.Conv1d(3, 4, kernel_size = self.short_kernel_size, stride = 1)
self.short_rnn = nn.RNN(
input_size = 4, \
hidden_size = 16, \
num_layers = 1, \
batch_first = True
)
self.attn = SelfAttention(n_head=8, d_k=48, d_v=24, d_x=12, d_o=12)
# nn.init.uniform_(self.short_rnn.state_dict()['weight_hh_l0'], a=-0.05, b=0.05)
def forward(self, traj):
# short-term travel speed features
n_batchs = traj['speeds_0'].size()[0]
speeds_forward = traj['speeds_0'].reshape(-1, 4)
speeds_adjacent1 = traj['speeds_1'].reshape(-1, 4)
speeds_adjacent2 = traj['speeds_2'].reshape(-1, 4)
grid_len = traj['grid_len'].reshape(-1, 1)
speeds_forward = torch.unsqueeze(speeds_forward, dim =2)
speeds_adjacent1 = torch.unsqueeze(speeds_adjacent1, dim = 2)
speeds_adjacent2 = torch.unsqueeze(speeds_adjacent2, dim = 2)
grid_len = torch.unsqueeze(grid_len, dim = 2)
grid_len_short = grid_len.expand(speeds_forward.size()[:2] + (grid_len.size()[-1], ))
times_forward = speeds_forward.clone()
times_forward[times_forward==0] = 0.2
times_forward = grid_len_short / times_forward * 3600
times_adjacent1 = speeds_adjacent1.clone()
times_adjacent1[times_adjacent1==0] = 0.2
times_adjacent1 = grid_len_short / times_adjacent1 * 3600
times_adjacent2 = speeds_adjacent2.clone()
times_adjacent2[times_adjacent2==0] = 0.2
times_adjacent2 = grid_len_short / times_adjacent2 * 3600
speeds_forward = utils.normalize(speeds_forward, 'speeds_0')
speeds_adjacent1 = utils.normalize(speeds_adjacent1, 'speeds_1')
speeds_adjacent2 = utils.normalize(speeds_adjacent2, 'speeds_2')
grid_len_short = utils.normalize(grid_len_short, 'grid_len')
times_forward = utils.normalize(times_forward, 'time_gap')
times_adjacent1 = utils.normalize(times_adjacent1, 'time_gap')
times_adjacent2 = utils.normalize(times_adjacent2, 'time_gap')
inputs_0 = torch.cat([speeds_forward, grid_len_short, times_forward], dim = 2)
inputs_1 = torch.cat([speeds_adjacent1, grid_len_short, times_adjacent1], dim = 2)
inputs_2 = torch.cat([speeds_adjacent2, grid_len_short, times_adjacent2], dim = 2)
cnn_outputs_0 = F.tanh(self.short_cnn(inputs_0.permute(0, 2, 1)))
cnn_outputs_0 = cnn_outputs_0.permute(0, 2, 1)
cnn_outputs_1 = F.tanh(self.short_cnn(inputs_1.permute(0, 2, 1)))
cnn_outputs_1 = cnn_outputs_1.permute(0, 2, 1)
cnn_outputs_2 = F.tanh(self.short_cnn(inputs_2.permute(0, 2, 1)))
cnn_outputs_2 = cnn_outputs_2.permute(0, 2, 1)
rnn_outputs_0, h_n = self.short_rnn(cnn_outputs_0)
rnn_outputs_1, h_n = self.short_rnn(cnn_outputs_1)
rnn_outputs_2, h_n = self.short_rnn(cnn_outputs_2)
rnn_outputs_0 = rnn_outputs_0.reshape(n_batchs, -1, 4-self.short_kernel_size+1, 16)
rnn_outputs_1 = rnn_outputs_1.reshape(n_batchs, -1, 4-self.short_kernel_size+1, 16)
rnn_outputs_2 = rnn_outputs_2.reshape(n_batchs, -1, 4-self.short_kernel_size+1, 16)
cnn_outputs_0 = cnn_outputs_0.reshape(n_batchs, -1, 4 - self.short_kernel_size + 1, 4)
cnn_outputs_1 = cnn_outputs_1.reshape(n_batchs, -1, 4 - self.short_kernel_size + 1, 4)
cnn_outputs_2 = cnn_outputs_2.reshape(n_batchs, -1, 4 - self.short_kernel_size + 1, 4)
V_cnn = torch.cat([cnn_outputs_0[:, :, -1], cnn_outputs_1[:, :, -1], cnn_outputs_2[:, :, -1]], dim = 2)
_, V_cnn = self.attn(V_cnn)
V_rnn = torch.cat([rnn_outputs_0[:, :, -1], rnn_outputs_1[:, :, -1], rnn_outputs_2[:, :, -1]], dim=2)
V_short = torch.cat([V_cnn, V_rnn], dim=2)
return V_short
class LongSpeed(nn.Module):
def __init__(self):
super(LongSpeed, self).__init__()
self.build()
def build(self):
# self.process_longspeeds = nn.Linear(16, 16)
self.long_kernel_size = 3
self.long_cnn = nn.Conv1d(3, 4, kernel_size = self.long_kernel_size, stride = 1)
self.long_rnn = nn.RNN(
input_size = 4, \
hidden_size = 16, \
num_layers = 1, \
batch_first = True
)
# nn.init.uniform_(self.long_rnn.state_dict()['weight_hh_l0'], a=-0.05, b=0.05)
def forward(self, traj):
# long-term travel speed features
n_batchs = traj['speeds_long'].size()[0]
speeds_history = traj['speeds_long'].reshape(-1, 7)
grid_len = traj['grid_len'].reshape(-1, 1)
speeds_history = torch.unsqueeze(speeds_history, dim = 2)
grid_len = torch.unsqueeze(grid_len, dim = 2)
grid_len_long = grid_len.expand(speeds_history.size()[:2] + (grid_len.size()[-1], ))
times_history = speeds_history.clone()
times_history[times_history==0] = 0.2
times_history = grid_len_long / times_history * 3600
speeds_history = utils.normalize(speeds_history, 'speeds_long')
grid_len_long = utils.normalize(grid_len_long, 'grid_len')
times_history = utils.normalize(times_history, 'time_gap')
inputs_3 = torch.cat([speeds_history, grid_len_long, times_history], dim = 2)
cnn_outputs_3 = self.long_cnn(inputs_3.permute(0, 2, 1))
cnn_outputs_3 = cnn_outputs_3.permute(0, 2, 1)
rnn_outputs_3, h_n = self.long_rnn(cnn_outputs_3)
cnn_outputs_3 = cnn_outputs_3.reshape(n_batchs, -1, 7 - self.long_kernel_size + 1, 4)
rnn_outputs_3 = rnn_outputs_3.reshape(n_batchs, -1, 7-self.long_kernel_size+1, 16)
V_long = torch.cat([cnn_outputs_3[:, :, -1], rnn_outputs_3[:, :, -1]], dim=2)
return V_long