-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJointsDataset.py
73 lines (57 loc) · 2.09 KB
/
JointsDataset.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
import os
import json
import pickle
import numpy as np
from torch.utils.data import Dataset
class JointsDataset(Dataset):
"""Joints Dataset"""
def __init__(self, directory, FLAGS):
self.input = directory
self.meta = os.path.join(directory.split('/')[0], 'stats')
# Load the mean and std filenames
mean_file = os.path.join(self.meta, 'mean.pkl')
std_file = os.path.join(self.meta, 'std.pkl')
with open(mean_file, "rb") as f:
self.mean = pickle.load(f, encoding='Latin-1')
self.mean = np.array(self.mean['joints21'])
with open(std_file, "rb") as f:
self.std = pickle.load(f, encoding='Latin-1')
self.std = np.array(self.std['joints21'])
self.std[self.std == 0.0] = 1.0
def __len__(self):
"""
:return: Number of input data examples
"""
return len(os.listdir(self.input))
def __getitem__(self, idx):
"""
Returns a training example
:param idx: index of the training example
:return:
"""
# read the file
filename = os.path.join(self.input, str(idx) + '.pkl')
with open(filename, "rb") as f:
data = pickle.load(f, encoding='Latin-1')
# transformed data
transformed_data = {}
for subject in data.keys():
# normalize the joint vectors
joints21 = np.array(data[subject]['joints21'])
positions = data[subject]['positions']
joints21 = joints21 - self.mean
joints21 = np.divide(joints21, self.std)
transformed_data[subject] = {
'directions': joints21,
'positions': positions,
}
return transformed_data
def denormalize_data(self, joints21):
"""
Denormalizes the input data
:param joints21: Input joints data of shape (F, output_shape)
:return: denormalized joint data of shape (F, output_shape)
"""
x = joints21 * self.std
x = x + self.mean
return x