-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
136 lines (120 loc) · 5.85 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
import torch
import torch.nn as nn
from params import par
from torch.autograd import Variable
from torch.nn.init import kaiming_normal_, orthogonal_
import numpy as np
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)
)
class DeepVO(nn.Module):
def __init__(self, imsize1, imsize2, batchNorm=True):
super(DeepVO,self).__init__()
# CNN
self.batchNorm = batchNorm
self.clip = par.clip
self.conv1 = conv(self.batchNorm, 6, 64, kernel_size=7, stride=2, dropout=par.conv_dropout[0])
self.conv2 = conv(self.batchNorm, 64, 128, kernel_size=5, stride=2, dropout=par.conv_dropout[1])
self.conv3 = conv(self.batchNorm, 128, 256, kernel_size=5, stride=2, dropout=par.conv_dropout[2])
self.conv3_1 = conv(self.batchNorm, 256, 256, kernel_size=3, stride=1, dropout=par.conv_dropout[3])
self.conv4 = conv(self.batchNorm, 256, 512, kernel_size=3, stride=2, dropout=par.conv_dropout[4])
self.conv4_1 = conv(self.batchNorm, 512, 512, kernel_size=3, stride=1, dropout=par.conv_dropout[5])
self.conv5 = conv(self.batchNorm, 512, 512, kernel_size=3, stride=2, dropout=par.conv_dropout[6])
self.conv5_1 = conv(self.batchNorm, 512, 512, kernel_size=3, stride=1, dropout=par.conv_dropout[7])
self.conv6 = conv(self.batchNorm, 512, 1024, kernel_size=3, stride=2, dropout=par.conv_dropout[8])
# Comput the shape based on diff image size
# this creates a tensor of zeros of shape (1,6,384,640) - batch size, 2 3-channels stacked, image width, height
__tmp = Variable(torch.zeros(1, 6, imsize1, imsize2))
# feed the zeros through the CNN and store in tmp
__tmp = self.encode_image(__tmp)
# RNN
self.rnn = nn.LSTM(
#np.prod multiplies the numbers in the array shape. eg if x has shape (2,3,5) np.prod(x) will give 2*3*5 = 30. here it will be size of o/p of cnn = 20*6*1024
input_size=int(np.prod(__tmp.size())),
hidden_size=par.rnn_hidden_size,
num_layers=2,
dropout=par.rnn_dropout_between,
batch_first=True)
self.rnn_drop_out = nn.Dropout(par.rnn_dropout_out)
# create a Fully connected layer at the end for getting 6 values
self.linear = nn.Linear(in_features=par.rnn_hidden_size, out_features=6)
# Initilization
for m in self.modules():
if isinstance(m, nn.Conv2d) 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):
# layer 1
kaiming_normal_(m.weight_ih_l0) #orthogonal_(m.weight_ih_l0)
kaiming_normal_(m.weight_hh_l0)
m.bias_ih_l0.data.zero_()
m.bias_hh_l0.data.zero_()
# Set forget gate bias to 1 (remember)
n = m.bias_hh_l0.size(0)
start, end = n//4, n//2
m.bias_hh_l0.data[start:end].fill_(1.)
# layer 2
kaiming_normal_(m.weight_ih_l1) #orthogonal_(m.weight_ih_l1)
kaiming_normal_(m.weight_hh_l1)
m.bias_ih_l1.data.zero_()
m.bias_hh_l1.data.zero_()
n = m.bias_hh_l1.size(0)
start, end = n//4, n//2
m.bias_hh_l1.data[start:end].fill_(1.)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward(self, x):
# x: (batch, seq_len, channel, width, height)
# stack_image
x = torch.cat(( x[:, :-1], x[:, 1:]), dim=2)
batch_size = x.size(0)
seq_len = x.size(1)
# CNN
x = x.view(batch_size*seq_len, x.size(2), x.size(3), x.size(4))
x = self.encode_image(x)
x = x.view(batch_size, seq_len, -1)
# RNN
out, hc = self.rnn(x)
out = self.rnn_drop_out(out)
out = self.linear(out)
return out
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
def weight_parameters(self):
return [param for name, param in self.named_parameters() if 'weight' in name]
def bias_parameters(self):
return [param for name, param in self.named_parameters() if 'bias' in name]
def get_loss(self, x, y):
predicted = self.forward(x)
y = y[:, 1:, :] # (batch, seq, dim_pose)
# Weighted MSE Loss
angle_loss = torch.nn.functional.mse_loss(predicted[:,:,:3], y[:,:,:3])
translation_loss = torch.nn.functional.mse_loss(predicted[:,:,3:], y[:,:,3:])
loss = (100 * angle_loss + translation_loss)
return loss
def step(self, x, y, optimizer):
optimizer.zero_grad()
loss = self.get_loss(x, y)
loss.backward()
if self.clip != None:
torch.nn.utils.clip_grad_norm(self.rnn.parameters(), self.clip)
optimizer.step()
return loss