-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tracking.py
208 lines (140 loc) · 6.15 KB
/
test_tracking.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# %load_ext autoreload
# %autoreload 2
# In[2]:
import jax
jax.config.update("jax_enable_x64", True)
# In[14]:
import jax
import jax.numpy as jnp
import numpy as nnp
import os
import sys
import pickle
import matplotlib.pyplot as plt
from tqdm import tqdm
from core.optimiser import Optimiser, RiemannSGDOptimiser
from core.cost import cost_function
from core.utils import imu_preprocessing, read_data, vicon_processing, get_eulers_from_quaternions, generate_panaroma
from core.model import euler_integration
# In[4]:
def test_orientation_tracking(data_path, imu_file_name):
"""
Runs the orientation tracking algorithm on the given data
Args:
data_path (str): path to the data folder
imu_file_name (str): name of the IMU data file
"""
imu_path = os.path.join(data_path, "imu")
cam_path = os.path.join(data_path, "cam")
# In[5]:
file_number = imu_file_name.split("imuRaw")[1].split(".")[0]
# In[61]:
# make director if not exists set_name + file_number
result_path = "results/{}_{}".format(os.path.basename(data_path), file_number)
if not os.path.exists(result_path):
os.makedirs(result_path)
# In[6]:
print(imu_file_name)
imu_file_path = os.path.join(imu_path, imu_file_name)
cam_file_path = os.path.join(cam_path, "cam" + file_number + ".p")
assert os.path.exists(imu_file_path), "File not found: {}".format(imu_file_path)
imu_vals, imu_ts = imu_preprocessing(imu_file_path)
qs = euler_integration(imu_vals, imu_ts)
# In[7]:
q = qs.copy()
optimiser = RiemannSGDOptimiser(q, imu_vals, imu_ts, learning_rate=0.01)
iter_num = 0
logs = {
"cost": [],
"cost_motion": [],
"cost_observation": [],
}
for i in range(50):
optimiser.update()
optimiser.project()
q = optimiser.params
cost, (cost_logs) = optimiser.cost, optimiser.cost_logs
cost_motion, cost_observation = cost_logs["cost_motion"]/1000000000.0, cost_logs["cost_observation"]/10000.0
print("iter: {}, cost: {:.4f}, cost_motion: {:.4f}, cost_observation: {:.4f}".format(i, cost, cost_motion, cost_observation))
logs["cost"].append(cost)
logs["cost_motion"].append(cost_motion)
logs["cost_observation"].append(cost_observation)
# In[104]:
# save logs to file
import pickle
with open(os.path.join(result_path, "logs.json"), "wb") as f:
pickle.dump(logs, f)
# In[83]:
# plot between cost and iterations
plt.plot(logs["cost"], label="Cost", color = "red")
plt.xlabel("Iterations")
plt.ylabel("Cost")
plt.title("Cost vs Iterations")
plt.tight_layout()
plt.savefig(os.path.join(result_path, "cost_vs_iterations.png"))
# In[39]:
euler_from_q = get_eulers_from_quaternions(q)
euler_from_qs = get_eulers_from_quaternions(qs)
# In[78]:
fig, axs = plt.subplots(3, 1, figsize=(15, 10))
# plot the first column
axs[0].plot(imu_ts, euler_from_qs[:, 0], label="Integration", color = "red", linestyle = "dotted", linewidth = 2)
axs[0].plot(imu_ts, euler_from_q[:, 0], label="Optimised", color = "green", linewidth = 2.5)
axs[0].set_xlabel('Time (s)')
axs[0].set_ylabel('Roll (rad)')
axs[0].title.set_text("Orientation Tracking - Roll")
axs[0].legend()
# plot the second column
axs[1].plot(imu_ts, euler_from_qs[:, 1], label="Integration", color = "red", linestyle = "dotted", linewidth = 2)
axs[1].plot(imu_ts, euler_from_q[:, 1], label="Optimised", color = "green", linewidth = 2.5)
axs[1].set_xlabel('Time (s)')
axs[1].set_ylabel('Pitch (rad)')
axs[1].title.set_text("Orientation Tracking - Pitch")
axs[1].legend()
# plot the third column
axs[2].plot(imu_ts, euler_from_qs[:, 2], label="Integration", color = "red", linestyle = "dotted", linewidth = 2)
axs[2].plot(imu_ts, euler_from_q[:, 2], label="Optimised", color = "green", linewidth = 2.5)
axs[2].set_xlabel('Time (s)')
axs[2].set_ylabel('Yaw (rad)')
axs[2].title.set_text("Orientation Tracking - Yaw")
axs[2].legend()
# very tight layout and save the figure
plt.tight_layout()
plt.savefig("{}/orientation_horizontal.png".format(result_path))
plt.show(block=False)
fig, axs = plt.subplots(3, 1, figsize=(5, 10))
# plot the first column
axs[0].plot(imu_ts, euler_from_qs[:, 0], label="Integration", color = "red", linestyle = "dotted", linewidth = 2)
axs[0].plot(imu_ts, euler_from_q[:, 0], label="Optimised", color = "green", linewidth = 2.5)
axs[0].set_xlabel('Time (s)')
axs[0].title.set_text("Orientation Tracking - Roll (rad)")
axs[0].legend()
# plot the second column
axs[1].plot(imu_ts, euler_from_qs[:, 1], label="Integration", color = "red", linestyle = "dotted", linewidth = 2)
axs[1].plot(imu_ts, euler_from_q[:, 1], label="Optimised", color = "green", linewidth = 2.5)
axs[1].set_xlabel('Time (s)')
axs[1].title.set_text("Orientation Tracking - Pitch (rad)")
axs[1].legend()
# plot the third column
axs[2].plot(imu_ts, euler_from_qs[:, 2], label="Integration", color = "red", linestyle = "dotted", linewidth = 2)
axs[2].plot(imu_ts, euler_from_q[:, 2], label="Optimised", color = "green", linewidth = 2.5)
axs[2].set_xlabel('Time (s)')
axs[2].title.set_text("Orientation Tracking - Yaw (rad)")
axs[2].legend()
# very tight layout and save the figure
plt.tight_layout()
plt.savefig("{}/orientation_vertical.png".format(result_path))
plt.show(block=False)
# In[45]:
if os.path.exists(cam_file_path):
panorama_image_optimised = generate_panaroma(cam_file_path, q, imu_ts)
panorama_image_integration = generate_panaroma(cam_file_path, qs, imu_ts)
plt.tight_layout()
plt.imsave("{}/pan_img_optimised.png".format(result_path), panorama_image_optimised.astype(nnp.uint8))
plt.imshow(panorama_image_optimised.astype(nnp.uint8))
plt.tight_layout()
plt.imsave("{}/pan_img_integration.png".format(result_path), panorama_image_integration.astype(nnp.uint8))
plt.imshow(panorama_image_integration.astype(nnp.uint8))