-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataLoader.py
378 lines (311 loc) · 12.6 KB
/
dataLoader.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import torch
from torch.utils.data import Dataset, DataLoader
import os
from PIL import Image
from torchvision import transforms
import numpy as np
import math
class CustomDataSet(Dataset):
def __init__(self, main_dir, curr_index, batch_size):
self.main_dir = main_dir
self.image_dir = '/sequences/'
self.curr_index = curr_index
self.batch_size = batch_size
#all_imgs = os.listdir(main_dir+self.image_dir)
all_imgs = os.listdir(self.main_dir+self.image_dir+str(self.curr_index).zfill(2)+'/image_2/')
self.total_imgs = np.array(all_imgs[:-1])
self.total_imgs_second = np.array(all_imgs[1:])
self.preprocess = transforms.Compose([
transforms.Resize((256, 256)),
# transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
def __len__(self):
return len(self.total_imgs)
def __getitem__(self, idx):
img_loc1 = os.path.join(self.main_dir+self.image_dir+str(self.curr_index).zfill(2)+'/image_2/', self.total_imgs[idx])
img_loc2 = os.path.join(self.main_dir+self.image_dir+str(self.curr_index).zfill(2)+'/image_2/', self.total_imgs_second[idx])
img1 = Image.open(img_loc1)
image1 = Image.new("RGB", img1.size)
image1.paste(img1)
img2 = Image.open(img_loc2)
image2 = Image.new("RGB", img2.size)
image2.paste(img2)
tensor_image1 = self.preprocess(image1)
tensor_image2 = self.preprocess(image2)
# if tensor_image1[0] == 1:
# tensor_image1 = torch.cat([tensor_image1, tensor_image1, tensor_image1], dim=0)
# tensor_image2 = torch.cat([tensor_image2, tensor_image2, tensor_image2], dim=0)
return [tensor_image1, tensor_image2]
class PositioningDataset():
def __init__(self, main_dir,curr_index, batch_size):
self.main_dir = main_dir
self.pos_dir = '/poses/'
self.curr_index = curr_index
self.batch_size = batch_size
self.positioning = []
self.euler = []
self.make_dataset()
def __len__(self):
return len(self.positioning)
def __getitem__(self, idx):
#position = self.positioning[idx]
return self.positioning[idx]
def make_dataset(self):
#data part
positioning_path = self.main_dir + self.pos_dir
positioning = open(positioning_path + str(self.curr_index).zfill(2)+'.txt',"r")
positioning_3x4 = positioning.readlines()
positioning.close()
self.positioning = []
positioning_temp = []
self.rot_matrix = []
self.transitions = []
transitions_temp = []
self.euler = []
for pos in positioning_3x4:
pos = pos.split()
for j in range(len(pos)):
#word = word[1:-1]
pos[j] = float(pos[j])
rot = np.array([[pos[0],pos[1],pos[2]],
[pos[4],pos[5],pos[6]],
[pos[8],pos[9],pos[10]]])
#self.transitions.append(np.reshape(np.transpose(np.transpose(rot) @ np.transpose(np.array([[pos[3],pos[7],pos[11]]]))),-1))
transitions_temp.append(np.array([pos[3],pos[7],pos[11]]))
#self.transitions.append(np.reshape(np.transpose(np.transpose(positioning_temp[-1]) @ np.transpose(np.array(transitions_temp[-1]))),-1))
self.positioning += [pos]
positioning_temp += [rot]
transitions_temp = np.diff(transitions_temp, axis=0)
self.rotation_abs = positioning_temp.copy()
#generating relative transitions and rotations
for i in range(len(positioning_temp)-1):
self.transitions.append(np.reshape(np.transpose(np.transpose(positioning_temp[i])) @ np.transpose(transitions_temp[i]),-1))
positioning_temp[i] = positioning_temp[i+1] @ np.transpose(positioning_temp[i])
self.euler += [euler_angles_from_rotation_matrix(positioning_temp[i])]
#self.transitions.append(np.reshape(np.transpose(np.transpose(positioning_temp[-1]) @ np.transpose(np.array(transitions_temp[-1]))),-1))
self.rot_matrix = positioning_temp
positioning_temp = np.array(positioning_temp).reshape(len(positioning_temp), 9)
#end
#print(self.transitions)
self.positioning = np.concatenate((np.array(self.euler), self.transitions), axis=1)
self.positioning = torch.Tensor(self.positioning)
"""qw= √(1 + m00 + m11 + m22) /2
qx = (m21 - m12)/( 4 *qw)
qy = (m02 - m20)/( 4 *qw)
qz = (m10 - m01)/( 4 *qw)"""
# def isclose(x, y, rtol=1.e-5, atol=1.e-8):
# return abs(x-y) <= atol + rtol * abs(y)
# def euler_angles_from_rotation_matrix(R):
# phi = 0.0
# if isclose(R[2,0],-1.0):
# theta = math.pi/2.0
# psi = math.atan2(R[0,1],R[0,2])
# elif isclose(R[2,0],1.0):
# theta = -math.pi/2.0
# psi = math.atan2(-R[0,1],-R[0,2])
# else:
# theta = -math.asin(R[2,0])
# cos_theta = math.cos(theta)
# psi = math.atan2(R[2,1]/cos_theta, R[2,2]/cos_theta)
# phi = math.atan2(R[1,0]/cos_theta, R[0,0]/cos_theta)
# return [psi, theta, phi]
# Calculates Rotation Matrix given euler angles
# Checks if a matrix is a valid rotation matrix.
def isRotationMatrix(R) :
Rt = np.transpose(R)
shouldBeIdentity = np.dot(Rt, R)
I = np.identity(3, dtype = R.dtype)
n = np.linalg.norm(I - shouldBeIdentity)
return n < 1e-6
# Calculates rotation matrix to euler angles
# The result is the same as MATLAB except the order
# of the euler angles ( x and z are swapped ).
def euler_angles_from_rotation_matrix(R):
assert(isRotationMatrix(R))
sy = math.sqrt(R[0,0] * R[0,0] + R[1,0] * R[1,0])
singular = sy < 1e-6
if not singular :
x = math.atan2(R[2,1] , R[2,2])
y = math.atan2(-R[2,0], sy)
z = math.atan2(R[1,0], R[0,0])
else :
x = math.atan2(-R[1,2], R[1,1])
y = math.atan2(-R[2,0], sy)
z = 0
return [x, y, z]
# def Euler2Rot(euler):
# euler = euler.flatten()
# x = euler[0]
# y = euler[1]
# z = euler[2]
# Rx = torch.tensor(
# [
# [np.cos(x), -np.sin(x), 0],
# [np.sin(x), np.cos(x), 0],
# [0, 0, 1],
# ]
# )
# Ry = torch.tensor(
# [
# [np.cos(y), 0, np.sin(y)],
# [0, 1, 0],
# [-np.sin(y), 0, np.cos(y)],
# ]
# )
# Rz = torch.tensor(
# [
# [1, 0, 0],
# [0, np.cos(z), -np.sin(z)],
# [0, np.sin(z), np.cos(z)],
# ]
# )
# return Rx @ Ry @ Rz
# Calculates Rotation Matrix given euler angles.
def Euler2Rot(theta) :
R_x = np.array([[1, 0, 0 ],
[0, math.cos(theta[0]), -math.sin(theta[0]) ],
[0, math.sin(theta[0]), math.cos(theta[0]) ]
])
R_y = np.array([[math.cos(theta[1]), 0, math.sin(theta[1]) ],
[0, 1, 0 ],
[-math.sin(theta[1]), 0, math.cos(theta[1]) ]
])
R_z = np.array([[math.cos(theta[2]), -math.sin(theta[2]), 0],
[math.sin(theta[2]), math.cos(theta[2]), 0],
[0, 0, 1]
])
R = np.dot(R_z, np.dot( R_y, R_x ))
return R
class DataGetter():
def __init__(self, main_dir, batch_size, start_index, end_index, sampling = 1, randomize_data=True):
self.main_dir = main_dir
self.start_index = start_index
self.curr_index = start_index - 1
self.end_index = end_index
self.index = 0
self.pos_dir = '/poses/'
self.batch_size = batch_size * sampling
self.sampling = sampling
self.image_dataset = None
self.train_loader = None
self.train_loader_iterator1 = None
self.pos_dataset = None
self.pos_loader = None
self.pos_loader_iterator = None
self.randomize_data = randomize_data
self.make_datasets()
def __len__(self):
return 0
def __getitem__(self, idx):
img_batches = None
try:
img_batches = next(self.train_loader_iterator1)
except:
if self.curr_index == self.end_index:
raise StopIteration
self.make_datasets()
img_batches = next(self.train_loader_iterator1)
quaternion_batch=0
transitions_batch=0
all_data = next(self.pos_loader_iterator)
quaternion_batch = all_data[:,:3]
transitions_batch = all_data[:,3:]
return img_batches[0][0::self.sampling],img_batches[1][0::self.sampling], quaternion_batch[0::self.sampling], transitions_batch[0::self.sampling]
def make_datasets(self):
self.curr_index += 1
self.image_dataset = CustomDataSet(self.main_dir, self.curr_index, self.batch_size)
self.train_loader = DataLoader(self.image_dataset , batch_size=self.batch_size, shuffle=False)
self.train_loader_iterator1 = iter(self.train_loader)
self.pos_dataset = PositioningDataset(self.main_dir, self.curr_index, self.batch_size)
self.pos_loader = DataLoader(self.pos_dataset , batch_size=self.batch_size, shuffle=False)
self.pos_loader_iterator = iter(self.pos_loader)
self.shake_baby()
def shake_baby(self):
randomize = np.arange(len(self.image_dataset.total_imgs))
if self.randomize_data:
np.random.shuffle(randomize)
self.pos_dataset.positioning = self.pos_dataset.positioning[randomize]
self.image_dataset.total_imgs = self.image_dataset.total_imgs[randomize]
self.image_dataset.total_imgs_second = self.image_dataset.total_imgs_second[randomize]
def refresh(self):
self.curr_index = self.start_index - 1
self.index = 0
self.image_dataset = None
self.train_loader = None
self.train_loader_iterator1 = None
self.pos_dataset = None
self.pos_loader = None
self.pos_loader_iterator = None
self.make_datasets()
def __iter__(self):
return self
def __next__(self):
self.index +=1
return self[self.index]
"""
#image part
img_folder_path = 'D:/data_odometry_gray/dataset/sequences/00/image_0'
batch_size = 64
my_dataset = CustomDataSet(img_folder_path, transform =False)
train_loader = DataLoader(my_dataset , batch_size=batch_size, shuffle=False)
train_loader_iterator = iter(train_loader)
#end image part
#data part
positioning_path = 'D:/data_odometry_gray/dataset/poses/'
positioning = open(positioning_path+'00.txt',"r")
positioning_3x4 = positioning.readlines()
positioning_final = [[]]
transition = []
quaternion = []
for pos in positioning_3x4:
pos = pos.split()
for j in range(len(pos)):
#word = word[1:-1]
pos[j] = float(pos[j])
#quaternions
qw = np.sqrt(1+pos[0]+pos[5]+pos[10])/2 # matrix diagonal
qx = pos[9] - pos[6] / (4*qw)
qy = pos[2] - pos[8] / (4*qw)
qz = pos[4] - pos[1] / (4*qw)
quaternion += [[qw,qx,qy,qz]]
transition += [[pos[3], pos[7], pos[11]]]
qw= √(1 + m00 + m11 + m22) /2
qx = (m21 - m12)/( 4 *qw)
qy = (m02 - m20)/( 4 *qw)
qz = (m10 - m01)/( 4 *qw)
positioning_final.append(pos)
#print([words for segments in positioning_3x4 for words in segments.split()])
#positioning_3x4 = np.reshape(positioning_3x4, len(positioning_3x4)/12, 12)
print(positioning_final)
print('+++++++++++++++++++++++++++++++++++')
print(quaternion)
print('+++++++++++++++++++++++++++++++++++')
print(transition)
not_done = True
i = 0
while not_done:
batch = next(train_loader_iterator)
if(len(batch)<64):
break
not_done = False
dataset1 = batch.narrow(0,0,batch_size-2)
dataset2 = batch.narrow(0,1,batch_size-1)
print(len(batch))
print(i)
i+=1
#print(batch)"""
### Primer kako radi
if __name__ == "__main__":
#main_dir = 'D:\\data_odometry_gray\\dataset'
main_dir = 'C:/Users/DELL/Documents/Python/PSI ML/dataset'
batch_size = 32
all_data = DataGetter(main_dir, batch_size, 0, 0)
i = 0
for img_batch1, img_batch2, quaternions, transitions in all_data:
print(str(len(img_batch1)) + str(len(quaternions)+ len(transitions)))
print(i)
print(img_batch1[0,0,0,0])
print(img_batch2[0,0,0,0])
i+=1