-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmupots_dataset.py
202 lines (163 loc) · 8.24 KB
/
mupots_dataset.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
from lib.dataset import BaseDataset
import numpy as np
import os
import bisect
class MuPoTSDataset(BaseDataset):
def __init__(self, cfg, estimator='tposenet', return_type='3D', phase='test'):
BaseDataset.__init__(self, cfg)
self.dataset_name = "mupots"
if phase == 'test':
self.phase = phase # 'test'
else:
raise NotImplementedError(
"Unknown phase type! Valid phase: [\'test\']. You can edit the code for additional implements"
)
if return_type in ['3D']: # no 2D, 'smpl'
self.return_type = return_type # '3D'
else:
raise NotImplementedError(
"Unknown return type! Valid phase: [\'3D\']. You can edit the code for additional implement"
)
if estimator in ['tposenetrefinenet','tposenet']:
self.estimator = estimator # 'tposenet','tposenetrefinenet'
else:
raise NotImplementedError(
"Unknown estimator! Valid phase: [\'tposenetrefinenet\',\'tposenet\']. You can edit the code for additional implement"
)
print('#############################################################')
print('You are loading the [' + self.phase + 'ing set] of dataset [' +
self.dataset_name + ']')
print('You are using pose esimator [' + str(self.estimator) + ']')
print('The type of the data is [' + str(self.return_type) + ']')
self.slide_window_size = cfg.MODEL.SLIDE_WINDOW_SIZE
self.evaluate_slide_window_step = cfg.EVALUATE.SLIDE_WINDOW_STEP_SIZE
self.base_data_path = cfg.DATASET.BASE_DIR
try:
ground_truth_data = np.load(os.path.join(
self.base_data_path,
self.dataset_name+"_"+self.estimator+"_"+self.return_type,
"groundtruth",
self.dataset_name + "_" + "gt"+"_"+self.return_type + "_" + self.phase + ".npz"),
allow_pickle=True)
except:
raise ImportError("Ground-truth data do not exist!")
try:
detected_data = np.load(os.path.join(
self.base_data_path,
self.dataset_name+"_"+self.estimator+"_"+self.return_type,
"detected",
self.dataset_name + "_" + self.estimator+"_"+self.return_type + "_" + self.phase + ".npz"),
allow_pickle=True)
except:
raise ImportError("Detected data do not exist!")
ground_truth_data_len = sum(
len(seq) for seq in ground_truth_data["imgname"])
detected_data_len = sum(len(seq) for seq in detected_data["imgname"])
if ground_truth_data_len != detected_data_len:
raise ImportError(
"Detected data is not the same size with ground_truth data!")
self.data_len = [len(seq)-self.slide_window_size if (len(seq)-self.slide_window_size)>0 else 0 for seq in ground_truth_data["imgname"]]
self.data_start_num = [
sum(self.data_len[0:i]) for i in range(len(self.data_len))
]
for i in range(len(self.data_start_num)-2,1):
if self.data_start_num[i]==self.data_start_num[i-1]:
self.data_start_num[i]=self.data_start_num[i+1]
self.frame_num = ground_truth_data_len
print('The frame number is [' + str(self.frame_num) + ']')
self.sequence_num = len(ground_truth_data["imgname"])
print('The sequence number is [' + str(self.sequence_num) + ']')
print('#############################################################')
if self.return_type == '3D':
self.ground_truth_data_imgname = ground_truth_data["imgname"]
self.ground_truth_data_joints_3d = ground_truth_data["joints_3d"]
self.detected_data_imgname = detected_data["imgname"]
self.detected_data_joints_3d = detected_data["joints_3d"]
self.input_dimension =ground_truth_data["joints_3d"][0].shape[1]
def __len__(self):
if self.phase == "train":
return self.frame_num
elif self.phase == "test":
return self.sequence_num
def __getitem__(self, index):
if self.phase == "train":
return self.get_data(index)
elif self.phase == "test":
return self.get_test_data(index)
def get_data(self, index):
position = bisect.bisect(self.data_start_num, index)-1
ground_truth_data_len = len(self.ground_truth_data_imgname[position])
detected_data_len = len(self.detected_data_imgname[position])
if ground_truth_data_len != detected_data_len:
raise ImportError(
"Detected data is not the same size with ground_truth data!")
if self.return_type == '3D':
gt_data = self.ground_truth_data_joints_3d[position].reshape(
ground_truth_data_len, -1, 3)
pred_data = self.detected_data_joints_3d[position].reshape(
ground_truth_data_len, -1, 3)
gt_data = gt_data.reshape(ground_truth_data_len, -1)
pred_data = pred_data.reshape(ground_truth_data_len, -1)
if self.slide_window_size <= ground_truth_data_len:
gt_data = np.concatenate(
(gt_data, np.zeros(tuple((1, )) + tuple(gt_data.shape[1:]))),
axis=0)
pred_data = np.concatenate(
(pred_data,
np.zeros(tuple((1, )) + tuple(pred_data.shape[1:]))),
axis=0)
start_idx = (index - self.data_start_num[position]) % (
ground_truth_data_len - self.slide_window_size + 1)
end_idx = start_idx + self.slide_window_size
gt_data = gt_data[start_idx:end_idx, :]
pred_data = pred_data[start_idx:end_idx, :]
else:
gt_data = np.concatenate((
gt_data,
np.zeros(
tuple((self.slide_window_size - ground_truth_data_len, )) +
tuple(gt_data.shape[1:]))),
axis=0)
pred_data = np.concatenate((
pred_data,
np.zeros(
tuple((self.slide_window_size - ground_truth_data_len, )) +
tuple(pred_data.shape[1:]))),
axis=0)
return {"gt": gt_data, "pred": pred_data}
def get_test_data(self, index):
ground_truth_data_len = len(self.ground_truth_data_imgname[index])
detected_data_len = len(self.detected_data_imgname[index])
if ground_truth_data_len != detected_data_len:
raise ImportError(
"Detected data is not the same size with ground_truth data!")
if self.return_type == '3D':
gt_data = self.ground_truth_data_joints_3d[index].reshape(
ground_truth_data_len, -1, 3)
pred_data = self.detected_data_joints_3d[index].reshape(
ground_truth_data_len, -1, 3)
gt_data = gt_data.reshape(ground_truth_data_len, -1)
pred_data = pred_data.reshape(ground_truth_data_len, -1)
if self.slide_window_size <= ground_truth_data_len:
start_idx=np.arange(0,ground_truth_data_len-self.slide_window_size+1,self.evaluate_slide_window_step)
gt_data_=[]
pred_data_=[]
for idx in start_idx:
gt_data_.append(gt_data[idx:idx+self.slide_window_size,:])
pred_data_.append(pred_data[idx:idx+self.slide_window_size,:])
gt_data=np.array(gt_data_)
pred_data=np.array(pred_data_)
else:
gt_data = np.concatenate((
gt_data,
np.zeros(
tuple((self.slide_window_size - ground_truth_data_len, )) +
tuple(gt_data.shape[1:]))),
axis=0)[np.newaxis, :]
pred_data = np.concatenate((
pred_data,
np.zeros(
tuple((self.slide_window_size - ground_truth_data_len, )) +
tuple(pred_data.shape[1:]))),
axis=0)[np.newaxis, :]
return {"gt": gt_data, "pred": pred_data}