-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_utils.py
572 lines (447 loc) · 19.5 KB
/
data_utils.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
"""Functions that help with data processing for human3.6m"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from six.moves import xrange # pylint: disable=redefined-builtin
import copy
import tensorflow as tf
import torch
def rotmat2euler( R ):
"""
Converts a rotation matrix to Euler angles
Matlab port to python for evaluation purposes
https://github.com/asheshjain399/RNNexp/blob/srnn/structural_rnn/CRFProblems/H3.6m/mhmublv/Motion/RotMat2Euler.m#L1
Args
R: a 3x3 rotation matrix
Returns
eul: a 3x1 Euler angle representation of R
"""
if R[0,2] == 1 or R[0,2] == -1:
# special case
E3 = 0 # set arbitrarily
dlta = np.arctan2( R[0,1], R[0,2] );
if R[0,2] == -1:
E2 = np.pi/2;
E1 = E3 + dlta;
else:
E2 = -np.pi/2;
E1 = -E3 + dlta;
else:
E2 = -np.arcsin( R[0,2] )
E1 = np.arctan2( R[1,2]/np.cos(E2), R[2,2]/np.cos(E2) )
E3 = np.arctan2( R[0,1]/np.cos(E2), R[0,0]/np.cos(E2) )
eul = np.array([E1, E2, E3]);
return eul
def quat2expmap(q):
"""
Converts a quaternion to an exponential map
Matlab port to python for evaluation purposes
https://github.com/asheshjain399/RNNexp/blob/srnn/structural_rnn/CRFProblems/H3.6m/mhmublv/Motion/quat2expmap.m#L1
Args
q: 1x4 quaternion
Returns
r: 1x3 exponential map
Raises
ValueError if the l2 norm of the quaternion is not close to 1
"""
if (np.abs(np.linalg.norm(q)-1)>1e-3):
raise(ValueError, "quat2expmap: input quaternion is not norm 1")
sinhalftheta = np.linalg.norm(q[1:])
coshalftheta = q[0]
r0 = np.divide( q[1:], (np.linalg.norm(q[1:]) + np.finfo(np.float32).eps));
theta = 2 * np.arctan2( sinhalftheta, coshalftheta )
theta = np.mod( theta + 2*np.pi, 2*np.pi )
if theta > np.pi:
theta = 2 * np.pi - theta
r0 = -r0
r = r0 * theta
return r
def rotmat2quat(R):
"""
Converts a rotation matrix to a quaternion
Matlab port to python for evaluation purposes
https://github.com/asheshjain399/RNNexp/blob/srnn/structural_rnn/CRFProblems/H3.6m/mhmublv/Motion/rotmat2quat.m#L4
Args
R: 3x3 rotation matrix
Returns
q: 1x4 quaternion
"""
rotdiff = R - R.T;
r = np.zeros(3)
r[0] = -rotdiff[1,2]
r[1] = rotdiff[0,2]
r[2] = -rotdiff[0,1]
sintheta = np.linalg.norm(r) / 2;
r0 = np.divide(r, np.linalg.norm(r) + np.finfo(np.float32).eps );
costheta = (np.trace(R)-1) / 2;
theta = np.arctan2( sintheta, costheta );
q = np.zeros(4)
q[0] = np.cos(theta/2)
q[1:] = r0*np.sin(theta/2)
return q
def rotmat2expmap(R):
return quat2expmap( rotmat2quat(R) );
def expmap2rotmat(r):
"""
Converts an exponential map angle to a rotation matrix
Matlab port to python for evaluation purposes
I believe this is also called Rodrigues' formula
https://github.com/asheshjain399/RNNexp/blob/srnn/structural_rnn/CRFProblems/H3.6m/mhmublv/Motion/expmap2rotmat.m
Args
r: 1x3 exponential map
Returns
R: 3x3 rotation matrix
"""
theta = np.linalg.norm( r )
r0 = np.divide( r, theta + np.finfo(np.float32).eps )
r0x = np.array([0, -r0[2], r0[1], 0, 0, -r0[0], 0, 0, 0]).reshape(3,3)
r0x = r0x - r0x.T
# R = np.eye(3,3) + np.sin(theta)*r0x + (1-np.cos(theta))*(r0x).dot(r0x);
R = np.eye(3) + np.sin(theta) * r0x + (1 - np.cos(theta)) * tf.tensordot(r0x, r0x, 1)
return R
def unNormalizeData(normalizedData, data_mean, data_std, dimensions_to_ignore, actions, one_hot ):
"""Borrowed from SRNN code. Reads a csv file and returns a float32 matrix.
https://github.com/asheshjain399/RNNexp/blob/srnn/structural_rnn/CRFProblems/H3.6m/generateMotionData.py#L12
Args
normalizedData: nxd matrix with normalized data
data_mean: vector of mean used to normalize the data
data_std: vector of standard deviation used to normalize the data
dimensions_to_ignore: vector with dimensions not used by the model
actions: list of strings with the encoded actions
one_hot: whether the data comes with one-hot encoding
Returns
origData: data originally used to
"""
T = normalizedData.shape[0]
D = data_mean.shape[0]
origData = np.zeros((T, D), dtype=np.float32)
dimensions_to_use = []
for i in range(D):
if i in dimensions_to_ignore:
continue
dimensions_to_use.append(i)
dimensions_to_use = np.array(dimensions_to_use)
if one_hot:
# print(normalizedData.shape) (64 54)
origData[:, dimensions_to_use] = normalizedData[:, :-6] # normalizedData[:, :-len(actions)]
else:
if normalizedData.shape[1] == 54:
origData[:, dimensions_to_use] = normalizedData[:, :-6] # for no one hot encode -1
else:
origData[:, dimensions_to_use] = normalizedData[:, :-1] # for no one hot encode -1
# potentially ineficient, but only done once per experiment
stdMat = data_std.reshape((1, D))
stdMat = np.repeat(stdMat, T, axis=0)
meanMat = data_mean.reshape((1, D))
meanMat = np.repeat(meanMat, T, axis=0)
origData = np.multiply(origData, stdMat) + meanMat
return origData
def revert_output_format(poses, data_mean, data_std, dim_to_ignore, actions, one_hot):
"""
Converts the output of the neural network to a format that is more easy to
manipulate for, e.g. conversion to other format or visualization
Args
poses: The output from the TF model. A list with (seq_length) entries,
each with a (batch_size, dim) output
Returns
poses_out: A tensor of size (batch_size, seq_length, dim) output. Each
batch is an n-by-d sequence of poses.
"""
seq_len = len(poses)
if seq_len == 0:
return []
batch_size, dim = poses[0].shape
poses = poses.detach().numpy()
poses_out = np.concatenate([poses])
poses_out = np.reshape(poses_out, (seq_len, batch_size, dim))
poses_out = np.transpose(poses_out, [1, 0, 2])
poses_out_list = []
for i in xrange(poses_out.shape[0]):
poses_out_list.append(
unNormalizeData(poses_out[i, :, :], data_mean, data_std, dim_to_ignore, actions, one_hot))
return poses_out_list
def readCSVasFloat(filename):
"""
Borrowed from SRNN code. Reads a csv and returns a float matrix.
https://github.com/asheshjain399/NeuralModels/blob/master/neuralmodels/utils.py#L34
Args
filename: string. Path to the csv file
Returns
returnArray: the read data in a float32 matrix
"""
returnArray = []
lines = open(filename).readlines()
for line in lines:
line = line.strip().split(',')
if len(line) > 0:
returnArray.append(np.array([np.float32(x) for x in line]))
returnArray = np.array(returnArray)
return returnArray
def load_data(path_to_dataset, subjects, actions, one_hot):
"""
Borrowed from SRNN code. This is how the SRNN code reads the provided .txt files
https://github.com/asheshjain399/RNNexp/blob/srnn/structural_rnn/CRFProblems/H3.6m/processdata.py#L270
Args
path_to_dataset: string. directory where the data resides
subjects: list of numbers. The subjects to load
actions: list of string. The actions to load
one_hot: Whether to add a one-hot encoding to the data
Returns
trainData: dictionary with k:v
k=(subject, action, subaction, 'even'), v=(nxd) un-normalized data
completeData: nxd matrix with all the data. Used to normlization stats
"""
nactions = len( actions )
trainData = {}
completeData = []
for subj in subjects:
for action_idx in np.arange(len(actions)):
action = actions[ action_idx ]
for subact in [1, 2]: # subactions
print("Reading subject {0}, action {1}, subaction {2}".format(subj, action, subact))
filename = '{0}/S{1}/{2}_{3}.txt'.format( path_to_dataset, subj, action, subact)
action_sequence = readCSVasFloat(filename)
n, d = action_sequence.shape
even_list = range(0, n, 2)
if one_hot:
# Add a one-hot encoding at the end of the representation
the_sequence = np.zeros( (len(even_list), d + nactions), dtype=float )
the_sequence[ :, 0:d ] = action_sequence[even_list, :]
the_sequence[ :, d+action_idx ] = 1
trainData[(subj, action, subact, 'even')] = the_sequence
else:
trainData[(subj, action, subact, 'even')] = action_sequence[even_list, :]
if len(completeData) == 0:
completeData = copy.deepcopy(action_sequence)
else:
completeData = np.append(completeData, action_sequence, axis=0)
return trainData, completeData
def normalize_data( data, data_mean, data_std, dim_to_use, actions, one_hot ):
"""
Normalize input data by removing unused dimensions, subtracting the mean and
dividing by the standard deviation
Args
data: nx99 matrix with data to normalize
data_mean: vector of mean used to normalize the data
data_std: vector of standard deviation used to normalize the data
dim_to_use: vector with dimensions used by the model
actions: list of strings with the encoded actions
one_hot: whether the data comes with one-hot encoding
Returns
data_out: the passed data matrix, but normalized
"""
data_out = {}
nactions = len(actions)
if not one_hot:
# No one-hot encoding... no need to do anything special
for key in data.keys():
data_out[ key ] = np.divide( (data[key] - data_mean), data_std )
data_out[ key ] = data_out[ key ][ :, dim_to_use ]
else:
# TODO hard-coding 99 dimensions for un-normalized human poses
for key in data.keys():
data_out[ key ] = np.divide( (data[key][:, 0:48] - data_mean), data_std )
data_out[ key ] = data_out[ key ][ :, dim_to_use ]
data_out[ key ] = np.hstack( (data_out[key], data[key][:,-nactions:]) )
return data_out
def normalization_stats(completeData):
""""
Also borrowed for SRNN code. Computes mean, stdev and dimensions to ignore.
https://github.com/asheshjain399/RNNexp/blob/srnn/structural_rnn/CRFProblems/H3.6m/processdata.py#L33
Args
completeData: nx99 matrix with data to normalize
Returns
data_mean: vector of mean used to normalize the data
data_std: vector of standard deviation used to normalize the data
dimensions_to_ignore: vector with dimensions not used by the model
dimensions_to_use: vector with dimensions used by the model
"""
data_mean = np.mean(completeData, axis=0)
data_std = np.std(completeData, axis=0)
dimensions_to_ignore = []
dimensions_to_use = []
dimensions_to_ignore.extend( list(np.where(data_std < 1e-4)[0]) )
dimensions_to_use.extend( list(np.where(data_std >= 1e-4)[0]) )
data_std[dimensions_to_ignore] = 1.0
return data_mean, data_std, dimensions_to_ignore, dimensions_to_use
def define_actions( action ):
"""
Define the list of actions we are using.
Args
action: String with the passed action. Could be "all"
Returns
actions: List of strings of actions
Raises
ValueError if the action is not included in H3.6M
"""
actions = ["walking", "eating", "smoking", "discussion", "directions",
"greeting", "phoning", "posing", "purchases", "sitting",
"sittingdown", "photo", "waiting", "walkdog",
"walktogether"]
if action in actions:
return [action]
if action == "all":
return actions
if action == "all_srnn":
return ["walking", "eating", "smoking", "discussion"]
raise( ValueError, "Unrecognized action: %d" % action )
def read_all_data( actions, seq_length_in, seq_length_out, data_dir, one_hot ):
"""
Loads data for training/testing and normalizes it.
Args
actions: list of strings (actions) to load
seq_length_in: number of frames to use in the burn-in sequence
seq_length_out: number of frames to use in the output sequence
data_dir: directory to load the data from
one_hot: whether to use one-hot encoding per action
Returns
train_set: dictionary with normalized training data
test_set: dictionary with test data
data_mean: d-long vector with the mean of the training data
data_std: d-long vector with the standard dev of the training data
dim_to_ignore: dimensions that are not used becaused stdev is too small
dim_to_use: dimensions that we are actually using in the model
"""
# === Read training data ===
print ("Reading training data (seq_len_in: {0}, seq_len_out {1}).".format(
seq_length_in, seq_length_out))
train_subject_ids = [1,6,7,8,9,11]
test_subject_ids = [5]
train_set, complete_train = load_data( data_dir, train_subject_ids, actions, one_hot )
test_set, complete_test = load_data( data_dir, test_subject_ids, actions, one_hot )
# Compute normalization stats
data_mean, data_std, dim_to_ignore, dim_to_use = normalization_stats(complete_train)
# Normalize -- subtract mean, divide by stdev
train_set = normalize_data( train_set, data_mean, data_std, dim_to_use, actions, one_hot )
test_set = normalize_data( test_set, data_mean, data_std, dim_to_use, actions, one_hot )
print("done reading data.")
return train_set, test_set, data_mean, data_std, dim_to_ignore, dim_to_use
def find_indices(data, action ):
"""
Find the same action indices as in SRNN.
See https://github.com/asheshjain399/RNNexp/blob/master/structural_rnn/CRFProblems/H3.6m/processdata.py#L325
"""
# Used a fixed dummy seed, following
# https://github.com/asheshjain399/RNNexp/blob/srnn/structural_rnn/forecastTrajectories.py#L29
SEED = 1234567890
rng = np.random.RandomState( SEED )
subject = 5
subaction1 = 1
subaction2 = 2
T1 = data[ (subject, action, subaction1, 'even') ].shape[0]
T2 = data[ (subject, action, subaction2, 'even') ].shape[0]
prefix, suffix = 50, 100
idx = []
idx.append( rng.randint( 16,T1-prefix-suffix ))
idx.append( rng.randint( 16,T2-prefix-suffix ))
idx.append( rng.randint( 16,T1-prefix-suffix ))
idx.append( rng.randint( 16,T2-prefix-suffix ))
idx.append( rng.randint( 16,T1-prefix-suffix ))
idx.append( rng.randint( 16,T2-prefix-suffix ))
idx.append( rng.randint( 16,T1-prefix-suffix ))
idx.append( rng.randint( 16,T2-prefix-suffix ))
return idx
def get_batch_test(data, action, source_seq_len, target_seq_len, input_size):
"""
Get a random batch of data from the specified bucket, prepare for step.
Args
data: dictionary with k:v, k=((subject, action, subsequence, 'even')),
v=nxd matrix with a sequence of poses
action: the action to load data from
Returns
The tuple (encoder_inputs, decoder_inputs, decoder_outputs);
the constructed batches have the proper format to call step(...) later.
"""
actions = ["walking", "eating", "smoking", "discussion", "directions",
"greeting", "phoning", "posing", "purchases", "sitting",
"sittingdown", "photo", "waiting", "walkdog",
"walktogether"]
if not action in actions:
raise ValueError("Unrecognized action {0}".format(action))
frames = {}
frames[ action ] = find_indices( data, action )
batch_size = 8 # we always evaluate 8 seeds
subject = 5 # we always evaluate on subject 5
seeds = [( action, (i%2)+1, frames[action][i] ) for i in range(batch_size)]
encoder_inputs = np.zeros( (batch_size, source_seq_len-1, input_size), dtype=float )
decoder_inputs = np.zeros( (batch_size, target_seq_len, input_size), dtype=float )
decoder_outputs = np.zeros( (batch_size, target_seq_len, input_size), dtype=float )
# Compute the number of frames needed
total_frames = source_seq_len + target_seq_len
# Reproducing SRNN's sequence subsequence selection as done in
# https://github.com/asheshjain399/RNNexp/blob/master/structural_rnn/CRFProblems/H3.6m/processdata.py#L343
for i in xrange( batch_size ):
_, subsequence, idx = seeds[i]
idx = idx + 50
data_sel = data[ (subject, action, subsequence, 'even') ]
data_sel = data_sel[(idx-source_seq_len):(idx+target_seq_len) ,:]
encoder_inputs[i, :, :] = data_sel[0:source_seq_len-1, :input_size]
decoder_inputs[i, :, :] = data_sel[source_seq_len-1:(source_seq_len+target_seq_len-1), :input_size]
decoder_outputs[i, :, :] = data_sel[source_seq_len:, :input_size]
return encoder_inputs, decoder_inputs, decoder_outputs
def get_gts( actions, model, test_set, data_mean, data_std, dim_to_ignore, one_hot, source_seq_len, target_seq_len, input_size, to_euler=True):
"""
Get the ground truths for srnn's sequences, and convert to Euler angles.
(the error is always computed in Euler angles).
Args
actions: a list of actions to get ground truths for.
model: training model we are using (we only use the "get_batch" method).
test_set: dictionary with normalized training data.
data_mean: d-long vector with the mean of the training data.
data_std: d-long vector with the standard deviation of the training data.
dim_to_ignore: dimensions that we are not using to train/predict.
one_hot: whether the data comes with one-hot encoding indicating action.
to_euler: whether to convert the angles to Euler format or keep thm in exponential map
Returns
srnn_gts_euler: a dictionary where the keys are actions, and the values
are the ground_truth, denormalized expected outputs of srnns's seeds.
"""
gts_euler = {}
for action in actions:
gt_euler = []
_, _, expmap = get_batch_test( test_set, action, source_seq_len, target_seq_len, input_size )
# expmap -> rotmat -> euler
for i in np.arange( expmap.shape[0] ):
denormed = unNormalizeData(expmap[i,:,:], data_mean, data_std, dim_to_ignore, actions, one_hot )
if to_euler:
for j in np.arange( denormed.shape[0] ):
for k in np.arange(3,97,3):
denormeded[j,k:k+3] = rotmat2euler(expmap2rotmat( denormed[j,k:k+3] ))
gt_euler.append( denormed );
# Put back in the dictionary
gts_euler[action] = gt_euler
return gts_euler
def get_batch( data, actions, source_seq_len, target_seq_len, input_size, batch_size):
"""Get a random batch of data from the specified bucket, prepare for step.
Args
data: a list of sequences of size n-by-d to fit the model to.
actions: a list of the actions we are using
Returns
The tuple (encoder_inputs, decoder_inputs, decoder_outputs);
the constructed batches have the proper format to call step(...) later.
"""
# Select entries at random
all_keys = list(data.keys())
chosen_keys = np.random.choice( len(all_keys), batch_size )
# How many frames in total do we need?
total_frames = source_seq_len + target_seq_len
encoder_inputs = np.zeros((batch_size, source_seq_len-1, input_size), dtype=np.float32)
decoder_inputs = np.zeros((batch_size, target_seq_len, input_size), dtype=np.float32)
decoder_outputs = np.zeros((batch_size, target_seq_len, input_size), dtype=np.float32)
for i in xrange( batch_size ):
the_key = all_keys[ chosen_keys[i] ]
# Get the number of frames
n, _ = data[ the_key ].shape
# Sample somewherein the middle
idx = np.random.randint( 16, n-total_frames )
# Select the data around the sampled points
data_sel = data[ the_key ][idx:idx+total_frames ,:]
# Add the data
encoder_inputs[i,:,0:input_size] = data_sel[0:source_seq_len-1, :input_size]
decoder_inputs[i,:,0:input_size] = data_sel[source_seq_len-1:source_seq_len+target_seq_len-1, :input_size]
decoder_outputs[i,:,0:input_size] = data_sel[source_seq_len:, 0:input_size]
encoder_inputs = torch.from_numpy(encoder_inputs)
decoder_inputs = torch.from_numpy(decoder_inputs)
decoder_outputs = torch.from_numpy(decoder_outputs)
return encoder_inputs, decoder_inputs, decoder_outputs