-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_helper.py
286 lines (251 loc) · 12.2 KB
/
data_helper.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
import os
import glob
import pandas as pd
import numpy as np
from PIL import Image
import torch
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.sampler import Sampler
from torchvision import transforms
import time
from params import par
from helper import normalize_angle_delta
def get_data_info(folder_list, seq_len_range, overlap, sample_times=1, pad_y=False, shuffle=False, sort=True):
X_path, Y = [], []
X_len = []
for folder in folder_list:
start_t = time.time()
poses = np.load('{}{}.npy'.format(par.pose_dir, folder)) # (n_images, 6)
fpaths = glob.glob('{}{}/image_3/*.png'.format(par.image_dir, folder))
fpaths.sort()
print (fpaths)
# Fixed seq_len
if seq_len_range[0] == seq_len_range[1]:
print("inside first")
if sample_times > 1:
sample_interval = int(np.ceil(seq_len_range[0] / sample_times))
start_frames = list(range(0, seq_len_range[0], sample_interval))
print('Sample start from frame {}'.format(start_frames))
else:
start_frames = [0]
for st in start_frames:
seq_len = seq_len_range[0]
n_frames = len(fpaths) - st
jump = seq_len - overlap
res = n_frames % seq_len
if res != 0:
n_frames = n_frames - res
x_segs = [fpaths[i:i+seq_len] for i in range(st, n_frames, jump)]
y_segs = [poses[i:i+seq_len] for i in range(st, n_frames, jump)]
Y += y_segs
X_path += x_segs
X_len += [len(xs) for xs in x_segs]
# Random segment to sequences with diff lengths
else:
assert(overlap < min(seq_len_range))
n_frames = len(fpaths)
min_len, max_len = seq_len_range[0], seq_len_range[1]
for i in range(sample_times):
start = 0
while True:
n = np.random.random_integers(min_len, max_len)
if start + n < n_frames:
x_seg = fpaths[start:start+n]
X_path.append(x_seg)
print("lennnn: ", len(X_path))
if not pad_y:
Y.append(poses[start:start+n])
else:
pad_zero = np.zeros((max_len-n, 15))
padded = np.concatenate((poses[start:start+n], pad_zero))
Y.append(padded.tolist())
else:
print('Last %d frames is not used' %(start+n-n_frames))
break
start += n - overlap
X_len.append(len(x_seg))
print('Folder {} finish in {} sec'.format(folder, time.time()-start_t))
# Convert to pandas dataframes
data = {'seq_len': X_len, 'image_path': X_path, 'pose': Y}
#print("within dictionary: length ", data[seq_len])
df = pd.DataFrame(data, columns = ['seq_len', 'image_path', 'pose'])
# Shuffle through all videos
if shuffle:
df = df.sample(frac=1)
# Sort dataframe by seq_len
if sort:
df = df.sort_values(by=['seq_len'], ascending=False)
return df
def get_partition_data_info(partition, folder_list, seq_len_range, overlap, sample_times=1, pad_y=False, shuffle=False, sort=True):
X_path = [[], []]
Y = [[], []]
X_len = [[], []]
df_list = []
#do till images of the folder get over. repeat it for number of sample time, the seq would be diff since seq length is randomly chosen
# between 5 and 7. do this for each folder of images. if partition is not None, this is done for both trg and validn. but here partition=None
for part in range(2):
for folder in folder_list:
start_t = time.time()
poses = np.load('{}{}.npy'.format(par.pose_dir, folder)) # (n_images, 6)
fpaths = glob.glob('{}{}/*.png'.format(par.image_dir, folder))
fpaths.sort()
# Get the middle section as validation set
n_val = int((1-partition)*len(fpaths))
st_val = int((len(fpaths)-n_val)/2)
ed_val = st_val + n_val
print('st_val: {}, ed_val:{}'.format(st_val, ed_val))
if part == 1:
fpaths = fpaths[st_val:ed_val]
poses = poses[st_val:ed_val]
else:
fpaths = fpaths[:st_val] + fpaths[ed_val:]
poses = np.concatenate((poses[:st_val], poses[ed_val:]), axis=0)
# Random Segment
assert(overlap < min(seq_len_range))
n_frames = len(fpaths)
min_len, max_len = seq_len_range[0], seq_len_range[1]
for i in range(sample_times):
start = 0
while True:
n = np.random.random_integers(min_len, max_len)
if start + n < n_frames:
x_seg = fpaths[start:start+n]
X_path[part].append(x_seg)
if not pad_y:
Y[part].append(poses[start:start+n])
else:
pad_zero = np.zeros((max_len-n, 6))
padded = np.concatenate((poses[start:start+n], pad_zero))
Y[part].append(padded.tolist())
else:
print('Last %d frames is not used' %(start+n-n_frames))
break
start += n - overlap
X_len[part].append(len(x_seg))
print('Folder {} finish in {} sec'.format(folder, time.time()-start_t))
# Convert to pandas dataframes
data = {'seq_len': X_len[part], 'image_path': X_path[part], 'pose': Y[part]}
df = pd.DataFrame(data, columns = ['seq_len', 'image_path', 'pose'])
# Shuffle through all videos
if shuffle:
df = df.sample(frac=1)
# Sort dataframe by seq_len
if sort:
df = df.sort_values(by=['seq_len'], ascending=False)
df_list.append(df)
return df_list
class SortedRandomBatchSampler(Sampler):
def __init__(self, info_dataframe, batch_size, drop_last=False):
self.df = info_dataframe
self.batch_size = batch_size
self.drop_last = drop_last
#unique_seq_lens will contain any combination of 5,6,7 like just 5/5,6/5,6,7/6/6,7/...
self.unique_seq_lens = sorted(self.df.iloc[:].seq_len.unique(), reverse=True)
# Calculate len (num of batches, not num of samples)
self.len = 0
for v in self.unique_seq_lens:
n_sample = len(self.df.loc[self.df.seq_len == v])
n_batch = int(n_sample / self.batch_size)
if not self.drop_last and n_sample % self.batch_size != 0:
n_batch += 1
self.len += n_batch
def __iter__(self):
# Calculate number of sameples in each group (grouped by seq_len)
list_batch_indexes = []
start_idx = 0
for v in self.unique_seq_lens:
n_sample = len(self.df.loc[self.df.seq_len == v])
n_batch = int(n_sample / self.batch_size)
if not self.drop_last and n_sample % self.batch_size != 0:
n_batch += 1
rand_idxs = (start_idx + torch.randperm(n_sample)).tolist()
tmp = [rand_idxs[s*self.batch_size: s*self.batch_size+self.batch_size] for s in range(0, n_batch)]
list_batch_indexes += tmp
start_idx += n_sample
return iter(list_batch_indexes)
def __len__(self):
return self.len
class ImageSequenceDataset(Dataset):
def __init__(self, info_dataframe, resize_mode='crop', new_sizeize=None, img_mean=None, img_std=(1,1,1), minus_point_5=False):
# Transforms
transform_ops = []
if resize_mode == 'crop':
transform_ops.append(transforms.CenterCrop((new_sizeize[0], new_sizeize[1])))
elif resize_mode == 'rescale':
transform_ops.append(transforms.Resize((new_sizeize[0], new_sizeize[1])))
transform_ops.append(transforms.ToTensor())
#transform_ops.append(transforms.Normalize(mean=img_mean, std=img_std))
self.transformer = transforms.Compose(transform_ops)
self.minus_point_5 = minus_point_5
self.normalizer = transforms.Normalize(mean=img_mean, std=img_std)
self.data_info = info_dataframe
self.seq_len_list = list(self.data_info.seq_len)
self.image_arr = np.asarray(self.data_info.image_path) # image paths
print ("shape of poses: ", self.data_info.pose.shape)
self.groundtruth_arr = np.asarray(self.data_info.pose)
def __getitem__(self, index):
raw_groundtruth = np.hsplit(self.groundtruth_arr[index], np.array([6]))
print ("grndtruth_arr[index] ",self.groundtruth_arr[index])
print ("raw grndtruth: ")
print (raw_groundtruth)
print ("index: ",index)
print ("shape : ",raw_groundtruth[0].shape)
print ("")
print ("test: ",raw_groundtruth[0][1])
groundtruth_sequence = raw_groundtruth[0]
groundtruth_rotation = raw_groundtruth[1][0].reshape((3, 3)).T # opposite rotation of the first frame
groundtruth_sequence = torch.FloatTensor(groundtruth_sequence)
# groundtruth_sequence[1:] = groundtruth_sequence[1:] - groundtruth_sequence[0:-1] # get relative pose w.r.t. previois frame
groundtruth_sequence[1:] = groundtruth_sequence[1:] - groundtruth_sequence[0] # get relative pose w.r.t. the first frame in the sequence
# print('Item before transform: ' + str(index) + ' ' + str(groundtruth_sequence))
# here we rotate the sequence relative to the first frame
for gt_seq in groundtruth_sequence[1:]:
location = torch.FloatTensor(groundtruth_rotation.dot(gt_seq[3:].numpy()))
gt_seq[3:] = location[:]
# print(location)
# get relative pose w.r.t. previous frame
groundtruth_sequence[2:] = groundtruth_sequence[2:] - groundtruth_sequence[1:-1]
# here we consider cases when rotation angles over Y axis go through PI -PI discontinuity
for gt_seq in groundtruth_sequence[1:]:
gt_seq[0] = normalize_angle_delta(gt_seq[0])
# print('Item after transform: ' + str(index) + ' ' + str(groundtruth_sequence))
image_path_sequence = self.image_arr[index]
sequence_len = torch.tensor(self.seq_len_list[index]) #sequence_len = torch.tensor(len(image_path_sequence))
image_sequence = []
for img_path in image_path_sequence:
img_as_img = Image.open(img_path)
img_as_tensor = self.transformer(img_as_img)
if self.minus_point_5:
img_as_tensor = img_as_tensor - 0.5 # from [0, 1] -> [-0.5, 0.5]
img_as_tensor = self.normalizer(img_as_tensor)
img_as_tensor = img_as_tensor.unsqueeze(0)
image_sequence.append(img_as_tensor)
image_sequence = torch.cat(image_sequence, 0)
return (sequence_len, image_sequence, groundtruth_sequence)
def __len__(self):
return len(self.data_info.index)
# Example of usage
if __name__ == '__main__':
start_t = time.time()
# Gernerate info dataframe
overlap = 1
sample_times = 1
folder_list = ['00']
seq_len_range = [5, 7]
df = get_data_info(folder_list, seq_len_range, overlap, sample_times)
print('Elapsed Time (get_data_info): {} sec'.format(time.time()-start_t))
# Customized Dataset, Sampler
n_workers = 4
resize_mode = 'crop'
new_size = (150, 600)
img_mean = (-0.14968217427134656, -0.12941663107068363, -0.1320610301921484)
dataset = ImageSequenceDataset(df, resize_mode, new_size, img_mean)
sorted_sampler = SortedRandomBatchSampler(df, batch_size=4, drop_last=True)
dataloader = DataLoader(dataset, batch_sampler=sorted_sampler, num_workers=n_workers)
print('Elapsed Time (dataloader): {} sec'.format(time.time()-start_t))
for batch in dataloader:
s, x, y = batch
print('='*50)
print('len:{}\nx:{}\ny:{}'.format(s, x.shape, y.shape))
print('Elapsed Time: {} sec'.format(time.time()-start_t))
print('Number of workers = ', n_workers)