-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataset_wnn.py
172 lines (142 loc) · 7.72 KB
/
dataset_wnn.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
import os
import numpy as np
from dataset_util import *
import matplotlib.pyplot as plt
def base_datasetname(dir, year_base, year_curr, offset_base, offset_curr):
return dir + "basepos-{0}-{1}-{2}m-{3}m.txt".format(year_base, year_curr, offset_base, offset_curr)
def curr_datasetname(dir, year_base, year_curr, offset_base, offset_curr):
return dir + "livepos-{0}-{1}-{2}m-{3}m.txt".format(year_base, year_curr, offset_base, offset_curr)
def find_closest_in_space(curr_pose, base_poses, curr_time, base_times, base_offset):
nearest_index = -1
smallest_distance = base_offset
shortest_interval = np.float('inf')
for j in range(len(base_poses)):
interval = np.abs(curr_time - base_times[j])
distance = LA.norm(curr_pose[[0,1]]-base_poses[j][[0,1]])
# remove pontos na contra-mao
orientation = np.abs(curr_pose[2] - base_poses[j][2])
if (orientation <= math.pi/2) \
and (distance >= 0) and (distance <= smallest_distance) \
and (interval >= 0) and (interval <= shortest_interval):
smallest_distance = distance
shortest_interval = interval
nearest_index = j
return nearest_index
def detect_closure_loop(data, min_distance):
index = -1
first = np.array((data['x'][0], data['y'][0]))
previous_distance = 0
for i in range(1, len(data)):
current = np.array((data['x'][i], data['y'][i]))
current_distance = LA.norm(first - current)
if (previous_distance - current_distance) > 0.1 and current_distance <= min_distance:
index = i
previous_distance = current_distance
return index
def plot_dataset(data1, data2, labels):
plt.figure(figsize=(10, 6), dpi=100)
data1_scatter = plt.scatter(data1[:,0], data1[:,1], facecolors='g', edgecolors='g', alpha=.5, s=5)
data2_scatter = plt.scatter(data2[:,0], data2[:,1], facecolors='r', edgecolors='r', alpha=.5, s=5)
for x2, x1 in enumerate(labels):
plt.plot([data1[x1,0], data2[x2,0]], [data1[x1,1], data2[x2,1]])
plt.xlabel('x (m)')
plt.ylabel('y (m)')
plt.legend((data1_scatter, data2_scatter), ('Base', 'Live'), loc='upper left')
plt.show()
def save_dataset_base(data, labels, dataset):
sample_file = open(dataset, "w")
sample_file.write("image label x y z rx ry rz timestamp\n")
for i in range(len(data)):
sample_file.write("{0} {1} {2} {3} {4} {5} {6} {7} {8}\n".format(
data['left_image'][i], labels[i],
data['x'][i], data['y'][i], data['z'][i],
data['rx'][i], data['ry'][i], data['rz'][i],
data['timestamp'][i])
)
sample_file.close()
def create_dataset(datasetname_base, datasetname_curr, datasetname_base_out, datasetname_curr_out, offset_base, offset_curr):
data_curr_label = []
data_curr_index = []
data_curr_label2 = []
data_curr_index2 = []
data_base = np.genfromtxt(datasetname_base, delimiter=' ', names=True, dtype=np.dtype(columns))
data_curr = np.genfromtxt(datasetname_curr, delimiter=' ', names=True, dtype=np.dtype(columns))
data_base_loop = detect_closure_loop(data_base, offset_base)
data_curr_loop = detect_closure_loop(data_curr, offset_curr)
data_base = data_base[:data_base_loop]
data_curr = data_curr[:data_curr_loop]
data_base_index = get_indices_of_sampled_data(data_base, offset_base)
data_base = data_base[data_base_index]
data_base_label = np.arange(data_base.shape[0])
# data_base_label = np.random.permutation(data_base.shape[0])
save_dataset_base(data_base, data_base_label, datasetname_base_out)
data_base_pose_2d = np.dstack([data_base['x'], data_base['y'], data_base['rz']])[0] # x, y, yaw
data_curr_pose_2d = np.dstack([data_curr['x'], data_curr['y'], data_curr['rz']])[0] # x, y, yaw
curr_start, base_start = find_start_point(data_curr_pose_2d, data_base_pose_2d)
data_curr_time = build_spacial_index(data_curr_pose_2d, curr_start)
data_base_time = build_spacial_index(data_base_pose_2d, base_start)
for index_curr in range(len(data_curr)):
index_base = find_closest_in_space(data_curr_pose_2d[index_curr], data_base_pose_2d,
data_curr_time[index_curr], data_base_time,
offset_base if index_curr == 0 else 5.0)
if index_base < 0: # get only frames ahead in space/time
# print 'live frame with no match: ', index_curr
continue
data_curr_label.append(data_base_label[index_base])
data_curr_index.append(index_curr)
if offset_base == offset_curr:
for index_base in range(len(data_base)):
nearest_index = -1
smallest_distance = 5 # offset_base/2
for index_curr in data_curr_index:
distance = LA.norm(data_curr_pose_2d[index_curr][[0, 1]] - data_base_pose_2d[index_base][[0, 1]])
if distance < smallest_distance:
smallest_distance = distance
nearest_index = index_curr
if nearest_index >= 0:
data_curr_label2.append(data_base_label[index_base])
data_curr_index2.append(nearest_index)
data_curr_index = data_curr_index2
data_curr_label = np.array(data_curr_label2)
data_curr = data_curr[data_curr_index]
else:
data_curr_label = np.array(data_curr_label)
data_curr = data_curr[data_curr_index]
data_curr_index = get_indices_of_sampled_data(data_curr, offset_curr)
data_curr = data_curr[data_curr_index]
data_curr_label = data_curr_label[data_curr_index]
save_dataset_base(data_curr, data_curr_label, datasetname_curr_out)
#data_curr_pose_2d = np.dstack([data_curr['x'], data_curr['y'], data_curr['rz']])[0]
#plot_dataset(data_base_pose_2d, data_curr_pose_2d, data_curr_label)
if __name__ == '__main__':
input_dir = '/dados/ufes/'
# output_dir = '/home/avelino/deepslam/data/ufes_wnn/'
# output_dir = '/Users/avelino/Sources/deepslam/data/ufes_wnn/'
output_dir = '/home/likewise-open/LCAD/avelino/deepslam/data/ufes_wnn/'
# offset_base_list = [1, 5, 10, 15, 30]
offset_base_list = [1]
offset_curr = 1
# os.system('rm -rf ' + output_dir + '*')
# datasets = ['20161021', '20171122']
# terceira ponte (camera 3)
# datasets = ['20160906-02', '20161228', '20170220', '20170220-02']
# datasets = ['20160830', '20170119']
# volta da ufes (camera 3)
datasets = ['20160825', '20160825-01', '20160825-02', '20161021', '20171205', '20171122']
# volta da ufes (camera 8)
# datasets += ['20140418', '20160902', '20160906-01']
for k in range(0, len(offset_base_list)):
offset_base = offset_base_list[k]
for i in range(0, len(datasets)): # base datasets
for j in range(len(datasets)-1, len(datasets)): # curr datasets
# if i != j: continue # skips building base and curr datasets with different data
if i == j: continue # skips building base and curr datasets with same data
basefilename_in = logfilename(input_dir, datasets[i])
currfilename_in = logfilename(input_dir, datasets[j])
basefilename_out = base_datasetname(output_dir, datasets[i], datasets[j], offset_base, offset_curr)
currfilename_out = curr_datasetname(output_dir, datasets[i], datasets[j], offset_base, offset_curr)
if not os.path.isfile(currfilename_out):
print 'building ', basefilename_out, currfilename_out
create_dataset(basefilename_in, currfilename_in, basefilename_out, currfilename_out, offset_base, offset_curr)
else:
print 'skipping ', basefilename_out, currfilename_out