forked from ElliotHYLee/Deep_Visual_Inertial_Odometry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_KF.py
174 lines (144 loc) · 4.95 KB
/
main_KF.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
from src.DataReader.KF_Data.KF_PrepData import DataManager
from scipy import signal
from src.Params import *
from src.Models.KF_Model.KF_BLock import *
from src.Models.KF_Model.KF_Model import *
import torch.optim as optim
import matplotlib.pyplot as plt
from src.Params import getNoiseLevel
dsName, subType, seq = 'kitti', 'none', [0, 2, 7, 10]
isTrain = True
wName = 'Weights/' + branchName() + '_' + dsName + '_' + subType + '_KF'
def preClamp(data):
if dsName=='kitti':
return data
N = data.shape[0]
for i in range(0, N):
row = data[i, :]
for j in range(0, 3):
val = row[j]
if val > 1:
val = 1
elif val < -1:
val = -1
row[j] = val
data[i] = row
return data
def filtfilt(data):
y = np.zeros_like(data)
b, a = signal.butter(8, 0.1)
for i in range(0, 3):
y[:, i] = signal.filtfilt(b, a, data[:, i], padlen=100)
return y
def plotter(filt, gt):
plt.figure()
plt.subplot(311)
plt.plot(gt[:, 0], 'r.')
plt.plot(filt[:, 0], 'b.')
plt.subplot(312)
plt.plot(gt[:, 1], 'r')
plt.plot(filt[:, 1], 'b.')
plt.subplot(313)
plt.plot(gt[:, 2], 'r')
plt.plot(filt[:, 2], 'b.')
posFilt = integrate(filt)
posGT = integrate(gt)
plt.figure()
plt.subplot(311)
plt.plot(posGT[:, 0], 'r')
plt.plot(posFilt[:, 0], 'g')
plt.subplot(312)
plt.plot(posGT[:, 1], 'r')
plt.plot(posFilt[:, 1], 'g')
plt.subplot(313)
plt.plot(posGT[:, 2], 'r')
plt.plot(posFilt[:, 2], 'g')
plt.figure()
plt.plot(posGT[:, 0], posGT[:, 2], 'r')
plt.plot(posFilt[:, 0], posFilt[:, 2], 'g')
return posFilt, posGT
def prepData(seqLocal = seq):
dm = DataManager()
dm.initHelper(dsName, subType, seqLocal)
dt = dm.dt
pSignal = dm.accdt_gnd
pSignal = preClamp(pSignal)
mSignal = dm.pr_dtr_gnd
mSignal = preClamp((mSignal))
mCov = dm.dtr_cov_gnd
gtSignal = preClamp(dm.gt_dtr_gnd)
gtSignal = filtfilt(gtSignal)
return gtSignal, dt, pSignal, mSignal, mCov
def main():
kfNumpy = KFBlock()
gtSignal, dt, pSignal, mSignal, mCov = prepData(seqLocal=seq)
posGT = np.cumsum(gtSignal, axis=0)
gnet = GuessNet()
if not isTrain:
gnet.train()
checkPoint = torch.load(wName + '.pt')
gnet.load_state_dict(checkPoint['model_state_dict'])
gnet.load_state_dict(checkPoint['optimizer_state_dict'])
else:
gnet.eval()
kf = TorchKFBLock(gtSignal, dt, pSignal, mSignal, mCov)
rmser = GetRMSE()
optimizer = optim.RMSprop(gnet.parameters(), lr=10 ** -4)
fig = plt.gcf()
fig.show()
fig.canvas.draw()
iterN = 50 if isTrain else 1
for epoch in range(0, iterN):
guess, sign = gnet()
filt = kf(guess, sign)
velRMSE, posRMSE = rmser(filt, gtSignal)
params = guess.data.numpy()
paramsSign = sign.data.numpy()
loss = posRMSE.data.numpy() + velRMSE.data.numpy()
theLOss = velRMSE + posRMSE
if isTrain:
if epoch == 10:
optimizer = optim.RMSprop(gnet.parameters(), lr=10 ** -4)
optimizer.zero_grad()
theLOss.backward(torch.ones_like(posRMSE))
optimizer.step()
temp = filt.data.numpy()
posKF = np.cumsum(temp, axis=0)
fig.clear()
plt.subplot(311)
plt.plot(posGT[:, 0], 'r')
plt.plot(posKF[:, 0], 'b')
plt.subplot(312)
plt.plot(posGT[:, 1], 'r')
plt.plot(posKF[:, 1], 'b')
plt.subplot(313)
plt.plot(posGT[:, 2], 'r')
plt.plot(posKF[:, 2], 'b')
plt.pause(0.001)
fig.canvas.draw()
plt.savefig('KFOptimHistory/'+dsName +' ' + subType + ' temp ' + str(epoch) + '.png')
#if np.mod(epoch, 10):
print('epoch: %d' % epoch)
print('params: ')
print(params)
print(paramsSign)
print('posRMSE: %.4f, %.4f, %.4f' %(loss[0], loss[1], loss[2]))
torch.save({
'model_state_dict': gnet.state_dict(),
'optimizer_state_dict': gnet.state_dict(),
}, wName + '.pt')
if isTrain:
kfRes = filt.data.numpy()
_, _ = plotter(kfRes, gtSignal)
else:
noise = getNoiseLevel()
for ii in range(5, 6):
gtSignal, dt, pSignal, mSignal, mCov = prepData(seqLocal=[ii])
kfNumpy.setR(params, paramsSign)
kfRes = kfNumpy.runKF(dt, pSignal, mSignal, mCov)
posFilt, posGT = plotter(kfRes, gtSignal)
np.savetxt('Results/Data/posFilt' + str(ii) + '_' + str(noise) + '.txt', posFilt)
np.savetxt('Results/Data/posGT' + str(ii) + '_' + str(noise) + '.txt', posGT)
plt.show()
if __name__ == '__main__':
main()