-
Notifications
You must be signed in to change notification settings - Fork 18
/
model.py
262 lines (225 loc) · 10.5 KB
/
model.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
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.nn.init import kaiming_normal_, orthogonal_
import numpy as np
from torch.distributions.utils import broadcast_all, probs_to_logits, logits_to_probs, lazy_property, clamp_probs
import torch.nn.functional as F
def conv(batchNorm, in_planes, out_planes, kernel_size=3, stride=1, dropout=0):
if batchNorm:
return nn.Sequential(
nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=(kernel_size - 1) // 2, bias=False),
nn.BatchNorm2d(out_planes),
nn.LeakyReLU(0.1, inplace=True),
nn.Dropout(dropout) # , inplace=True)
)
else:
return nn.Sequential(
nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=(kernel_size - 1) // 2, bias=True),
nn.LeakyReLU(0.1, inplace=True),
nn.Dropout(dropout) # , inplace=True)
)
# The inertial encoder for raw imu data
class Inertial_encoder(nn.Module):
def __init__(self, opt):
super(Inertial_encoder, self).__init__()
self.encoder_conv = nn.Sequential(
nn.Conv1d(6, 64, kernel_size=3, padding=1),
nn.BatchNorm1d(64),
nn.LeakyReLU(0.1, inplace=True),
nn.Dropout(opt.imu_dropout),
nn.Conv1d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm1d(128),
nn.LeakyReLU(0.1, inplace=True),
nn.Dropout(opt.imu_dropout),
nn.Conv1d(128, 256, kernel_size=3, padding=1),
nn.BatchNorm1d(256),
nn.LeakyReLU(0.1, inplace=True),
nn.Dropout(opt.imu_dropout))
self.proj = nn.Linear(256 * 1 * 11, opt.i_f_len)
def forward(self, x):
# x: (N, seq_len, 11, 6)
batch_size = x.shape[0]
seq_len = x.shape[1]
x = x.view(batch_size * seq_len, x.size(2), x.size(3)) # x: (N x seq_len, 11, 6)
x = self.encoder_conv(x.permute(0, 2, 1)) # x: (N x seq_len, 64, 11)
out = self.proj(x.view(x.shape[0], -1)) # out: (N x seq_len, 256)
return out.view(batch_size, seq_len, 256)
class Encoder(nn.Module):
def __init__(self, opt):
super(Encoder, self).__init__()
# CNN
self.opt = opt
self.conv1 = conv(True, 6, 64, kernel_size=7, stride=2, dropout=0.2)
self.conv2 = conv(True, 64, 128, kernel_size=5, stride=2, dropout=0.2)
self.conv3 = conv(True, 128, 256, kernel_size=5, stride=2, dropout=0.2)
self.conv3_1 = conv(True, 256, 256, kernel_size=3, stride=1, dropout=0.2)
self.conv4 = conv(True, 256, 512, kernel_size=3, stride=2, dropout=0.2)
self.conv4_1 = conv(True, 512, 512, kernel_size=3, stride=1, dropout=0.2)
self.conv5 = conv(True, 512, 512, kernel_size=3, stride=2, dropout=0.2)
self.conv5_1 = conv(True, 512, 512, kernel_size=3, stride=1, dropout=0.2)
self.conv6 = conv(True, 512, 1024, kernel_size=3, stride=2, dropout=0.5)
# Comput the shape based on diff image size
__tmp = Variable(torch.zeros(1, 6, opt.img_w, opt.img_h))
__tmp = self.encode_image(__tmp)
self.visual_head = nn.Linear(int(np.prod(__tmp.size())), opt.v_f_len)
self.inertial_encoder = Inertial_encoder(opt)
def forward(self, img, imu):
v = torch.cat((img[:, :-1], img[:, 1:]), dim=2)
batch_size = v.size(0)
seq_len = v.size(1)
# image CNN
v = v.view(batch_size * seq_len, v.size(2), v.size(3), v.size(4))
v = self.encode_image(v)
v = v.view(batch_size, seq_len, -1) # (batch, seq_len, fv)
v = self.visual_head(v) # (batch, seq_len, 256)
# IMU CNN
imu = torch.cat([imu[:, i * 10:i * 10 + 11, :].unsqueeze(1) for i in range(seq_len)], dim=1)
imu = self.inertial_encoder(imu)
return v, imu
def encode_image(self, x):
out_conv2 = self.conv2(self.conv1(x))
out_conv3 = self.conv3_1(self.conv3(out_conv2))
out_conv4 = self.conv4_1(self.conv4(out_conv3))
out_conv5 = self.conv5_1(self.conv5(out_conv4))
out_conv6 = self.conv6(out_conv5)
return out_conv6
# The fusion module
class Fusion_module(nn.Module):
def __init__(self, opt):
super(Fusion_module, self).__init__()
self.fuse_method = opt.fuse_method
self.f_len = opt.i_f_len + opt.v_f_len
if self.fuse_method == 'soft':
self.net = nn.Sequential(
nn.Linear(self.f_len, self.f_len))
elif self.fuse_method == 'hard':
self.net = nn.Sequential(
nn.Linear(self.f_len, 2 * self.f_len))
def forward(self, v, i):
if self.fuse_method == 'cat':
return torch.cat((v, i), -1)
elif self.fuse_method == 'soft':
feat_cat = torch.cat((v, i), -1)
weights = self.net(feat_cat)
return feat_cat * weights
elif self.fuse_method == 'hard':
feat_cat = torch.cat((v, i), -1)
weights = self.net(feat_cat)
weights = weights.view(v.shape[0], v.shape[1], self.f_len, 2)
mask = F.gumbel_softmax(weights, tau=1, hard=True, dim=-1)
return feat_cat * mask[:, :, :, 0]
# The policy network module
class PolicyNet(nn.Module):
def __init__(self, opt):
super(PolicyNet, self).__init__()
in_dim = opt.rnn_hidden_size + opt.i_f_len
self.net = nn.Sequential(
nn.Linear(in_dim, 256),
nn.LeakyReLU(0.1, inplace=True),
nn.BatchNorm1d(256),
nn.Linear(256, 32),
nn.LeakyReLU(0.1, inplace=True),
nn.BatchNorm1d(32),
nn.Linear(32, 2))
def forward(self, x, temp):
logits = self.net(x)
hard_mask = F.gumbel_softmax(logits, tau=temp, hard=True, dim=-1)
return logits, hard_mask
# The pose estimation network
class Pose_RNN(nn.Module):
def __init__(self, opt):
super(Pose_RNN, self).__init__()
# The main RNN network
f_len = opt.v_f_len + opt.i_f_len
self.rnn = nn.LSTM(
input_size=f_len,
hidden_size=opt.rnn_hidden_size,
num_layers=2,
dropout=opt.rnn_dropout_between,
batch_first=True)
self.fuse = Fusion_module(opt)
# The output networks
self.rnn_drop_out = nn.Dropout(opt.rnn_dropout_out)
self.regressor = nn.Sequential(
nn.Linear(opt.rnn_hidden_size, 128),
nn.LeakyReLU(0.1, inplace=True),
nn.Linear(128, 6))
def forward(self, fv, fv_alter, fi, dec, prev=None):
if prev is not None:
prev = (prev[0].transpose(1, 0).contiguous(), prev[1].transpose(1, 0).contiguous())
# Select between fv and fv_alter
v_in = fv * dec[:, :, :1] + fv_alter * dec[:, :, -1:] if fv_alter is not None else fv
fused = self.fuse(v_in, fi)
out, hc = self.rnn(fused) if prev is None else self.rnn(fused, prev)
out = self.rnn_drop_out(out)
pose = self.regressor(out)
hc = (hc[0].transpose(1, 0).contiguous(), hc[1].transpose(1, 0).contiguous())
return pose, hc
class DeepVIO(nn.Module):
def __init__(self, opt):
super(DeepVIO, self).__init__()
self.Feature_net = Encoder(opt)
self.Pose_net = Pose_RNN(opt)
self.Policy_net = PolicyNet(opt)
self.opt = opt
initialization(self)
def forward(self, img, imu, is_first=True, hc=None, temp=5, selection='gumbel-softmax', p=0.5):
fv, fi = self.Feature_net(img, imu)
batch_size = fv.shape[0]
seq_len = fv.shape[1]
poses, decisions, logits= [], [], []
hidden = torch.zeros(batch_size, self.opt.rnn_hidden_size).to(fv.device) if hc is None else hc[0].contiguous()[:, -1, :]
fv_alter = torch.zeros_like(fv) # zero padding in the paper, can be replaced by other
for i in range(seq_len):
if i == 0 and is_first:
# The first relative pose is estimated by both images and imu by default
pose, hc = self.Pose_net(fv[:, i:i+1, :], None, fi[:, i:i+1, :], None, hc)
else:
if selection == 'gumbel-softmax':
# Otherwise, sample the decision from the policy network
p_in = torch.cat((fi[:, i, :], hidden), -1)
logit, decision = self.Policy_net(p_in.detach(), temp)
decision = decision.unsqueeze(1)
logit = logit.unsqueeze(1)
pose, hc = self.Pose_net(fv[:, i:i+1, :], fv_alter[:, i:i+1, :], fi[:, i:i+1, :], decision, hc)
decisions.append(decision)
logits.append(logit)
elif selection == 'random':
decision = (torch.rand(fv.shape[0], 1, 2) < p).float()
decision[:,:,1] = 1-decision[:,:,0]
decision = decision.to(fv.device)
logit = 0.5*torch.ones((fv.shape[0], 1, 2)).to(fv.device)
pose, hc = self.Pose_net(fv[:, i:i+1, :], fv_alter[:, i:i+1, :], fi[:, i:i+1, :], decision, hc)
decisions.append(decision)
logits.append(logit)
poses.append(pose)
hidden = hc[0].contiguous()[:, -1, :]
poses = torch.cat(poses, dim=1)
decisions = torch.cat(decisions, dim=1)
logits = torch.cat(logits, dim=1)
probs = torch.nn.functional.softmax(logits, dim=-1)
return poses, decisions, probs, hc
def initialization(net):
#Initilization
for m in net.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Conv1d) or isinstance(m, nn.ConvTranspose2d) or isinstance(m, nn.Linear):
kaiming_normal_(m.weight.data)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.LSTM):
for name, param in m.named_parameters():
if 'weight_ih' in name:
torch.nn.init.kaiming_normal_(param.data)
elif 'weight_hh' in name:
torch.nn.init.kaiming_normal_(param.data)
elif 'bias_ih' in name:
param.data.fill_(0)
elif 'bias_hh' in name:
param.data.fill_(0)
n = param.size(0)
start, end = n//4, n//2
param.data[start:end].fill_(1.)
elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.BatchNorm1d):
m.weight.data.fill_(1)
m.bias.data.zero_()