-
Notifications
You must be signed in to change notification settings - Fork 9
/
Step_1_preproc_hdf5_skeleton.py
171 lines (135 loc) · 5.08 KB
/
Step_1_preproc_hdf5_skeleton.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
from numpy import *
import h5py
import gzip
from glob import glob
from scipy import misc
from numpy.random import permutation
import cv2
raw_input("Press Enter to continue...")
pc = "wudi"
pc = "wudi_linux"
if pc=="wudi":
src = r"D:\Chalearn2014\Data_processed/"
dst = r"J:\Chalearn2014\dummy/"
elif pc=="wudi_linux":
src = '/idiap/temp/dwu/chalearn2014_data/Data_processed/'
dst = '/idiap/user/dwu/chalearn/'
elif pc=="lio":
src = "/mnt/wd/chalearn/preproc/"
def main():
if pc=="wudi":
files_train = glob(src+"train/batch_*.zip")
files_valid = glob(src+"valid/batch_*.zip")
elif pc=="wudi_linux":
files_train = glob(src+"train/batch_*.zip")
files_valid = glob(src+"valid/batch_*.zip")
elif pc=="lio":
files_train = glob(src+"train/batch_*.zip")
files_valid = glob(src+"valid/batch_*.zip")
print len(files_train), "train files"
print len(files_valid), "valid files"
f = h5py.File(dst+"data%d.hdf5", "w", driver="family", memb_size=2**32-1)
x_train = f.create_dataset("x_train", (400*1050,2,2,4,64,64), dtype='uint8', chunks=True)
x_valid = f.create_dataset("x_valid", (29*1050,2,2,4,64,64), dtype='uint8', chunks=True)
y_train = f.create_dataset("y_train", (400*1050,), dtype='uint8', chunks=True)
y_valid = f.create_dataset("y_valid", (29*1050,), dtype='uint8', chunks=True)
x_train_skeleton_feature = f.create_dataset("x_train_skeleton_feature", (400*1050,891), dtype='float32',chunks=True)
x_valid_skeleton_feature = f.create_dataset("x_valid_skeleton_feature", (29*1050,891), dtype='float32', chunks=True)
l = 0
pos = 0
for path in files_train:
print path
vid, lbl, skeleton_feature = load_data(path)
print vid.shape[0], lbl.shape[0], skeleton_feature.shape[0]
assert vid.shape[0] == lbl.shape[0] == skeleton_feature.shape[0]
l += vid.shape[0]
x_train.resize(l, axis=0)
y_train.resize(l, axis=0)
x_train_skeleton_feature.resize(l, axis=0)
x_train[pos:pos+vid.shape[0]] = vid
y_train[pos:pos+vid.shape[0]] = lbl
x_train_skeleton_feature[pos:pos+vid.shape[0]] = skeleton_feature
pos += vid.shape[0]
print x_train.shape
l = 0
pos = 0
for path in files_valid:
vid, lbl, skeleton_feature = load_data(path)
print path
print vid.shape[0], lbl.shape[0]
assert vid.shape[0] == lbl.shape[0]
l += vid.shape[0]
x_valid.resize(l, axis=0)
y_valid.resize(l, axis=0)
x_valid_skeleton_feature.resize(l, axis=0)
x_valid[pos:pos+vid.shape[0]] = vid
y_valid[pos:pos+vid.shape[0]] = lbl
x_valid_skeleton_feature[pos:pos+vid.shape[0]] = skeleton_feature
pos += vid.shape[0]
print x_valid.shape
f.close()
print "done"
def load_gzip(path):
file = gzip.GzipFile(path, 'rb')
video, skeleton_feature, label, sk_trajectory = load(file)
file.close()
#wudi made an error in Step 1 extracting skelet, so
# the following interwined, gesture frames, before frames, after frames
pheight = empty(shape=(video.shape[2]))
count = 0
for i in range(len(sk_trajectory)):
pheight[count: count+sk_trajectory[i][0].shape[1]-12] \
= ones(shape=(sk_trajectory[i][0].shape[1]-12)) \
*sk_trajectory[i][2]
count = count+sk_trajectory[i][0].shape[1]-12
#label start from 0 REMEMBER!!!
video = video.swapaxes(0,2)
video = video.swapaxes(1,2)
return video,pheight,skeleton_feature,label
def cut_img(img,s):
if s==0: return img
return img[s:-s,s:-s]
ratio = 0.25
res_shape=[100,2,2,5,64,64]
h = res_shape[-1]
def load_data(path):
""" load data into shared variables """
v,p,skeleton_feature,l = load_gzip(path)
v = v[:,:,:2]
# print v.shape[0]
res_shape[0] = v.shape[0]
v_new = empty(res_shape,dtype="uint8")
for i in xrange(v.shape[0]): #batch
if p[i] < 10: p[i] = 100
ofs = p[i]*ratio
mid = v.shape[-1]/2.
sli = None
if ofs < mid:
start = int(round(mid-ofs))
end = int(round(mid+ofs))
sli = slice(start,end)
for j in xrange(v.shape[2]): #maps
for k in xrange(v.shape[3]): #frames
#body
img = v[i,0,j,k]
img = cut_img(img,5)
img = misc.imresize(img,(h,h))
# if j==0: img = 255-misc.imfilter(img,"contour")
v_new[i,0,j,k] = img
#hand
img = v[i,1,j,k]
img = img[sli,sli]
img = misc.imresize(img,(h,h))
v_new[i,1,j,k] = img
vid, lbl = v_new,l
# shuffle data
ind = permutation(l.shape[0])
# ind = ind[:1000]
vid = vid[:,:,:,:4,:,:]
vid, skeleton_feature, lbl = vid[ind].astype("float32"), skeleton_feature[ind].astype("float32"),lbl[ind].astype("float32")
# set value
# x_.set_value(vid, borrow=True)
# y_.set_value(lbl, borrow=True)
return vid, lbl, skeleton_feature
if __name__ == '__main__':
main()