-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLMDBTools.lua
More file actions
195 lines (141 loc) · 5.28 KB
/
Copy pathLMDBTools.lua
File metadata and controls
195 lines (141 loc) · 5.28 KB
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
local LMDBTools = {}
function LMDBTools.loadH(theFile)
--adapted from http://blog.aicry.com/torch7-reading-csv-into-tensor/
local csvFile = io.open(theFile, 'r')
if(csvFile == nil) then
local theMat = torch.Tensor(9)
theMat = theMat:reshape(3,3);
theMat = theMat:zero();
theMat[1][1] = 1.0;
theMat[2][2] = 1.0;
theMat[3][3] = 1.0;
return theMat
end
local header = csvFile:read()
local theMat = torch.Tensor(9)
local l = header:split(' ')
local i = 1;
for key, val in ipairs(l) do
theMat[i] = val
i = i + 1;
end
csvFile:close()
return theMat:reshape(3,3);
end
function LMDBTools.toLMDB(self, theDir, txn, count)
local isFile = io.open(theDir.."/smoothPoses.mat", "r");
if(isFile == nil) then
return count
end
isFile.close();
local datafile = hdf5.open(theDir.."/smoothPoses.mat", "r");
local thePoseList = datafile:read('/thePoses'):all();
if(thePoseList:max() < 1) then
return count
end
thePoseList = thePoseList:transpose(3,1);
local numPos = thePoseList:size()[1];
datafile:close();
for kk = 1,numPos do
local thePoses = thePoseList[kk];
local poseConf = ((thePoses:reshape(thePoses:size()[1],3,18)):transpose(2,1))[3];
if (poseConf:max() > -1000000 and poseConf[poseConf:ge(0)]:mean() > opt.confThresh) then
print(poseConf[poseConf:ge(0)]:mean());
local numJpgs = 1
local jpgFiles = {}
for file in paths.files(theDir) do
if file:find(".jpg" .. '$') then
jpgFiles[numJpgs] = file
numJpgs = numJpgs + 1;
end
end
table.sort(jpgFiles);
numJpgs = numJpgs - 1;
theChunk = opt.timestep/opt.numSteps;
for i = 1,(thePoses:size()[1]-(opt.timestep+opt.numDecSteps*theChunk + 1)),15 do
local theDiffs = torch.Tensor(opt.timestep+opt.numDecSteps*theChunk,18*3);
theDiffs = theDiffs:float();
theDiffs = theDiffs:zero();
local contPoses = torch.Tensor(opt.timestep+opt.numDecSteps*theChunk,18*3);
contPoses = contPoses:float();
contPoses = contPoses:zero();
local chunkPoses = torch.Tensor(opt.numSteps+opt.numDecSteps,18*3);
chunkPoses = chunkPoses:float();
chunkPoses = chunkPoses:zero();
local theCompDiffs = torch.Tensor(36,opt.numSteps+opt.numDecSteps);
theCompDiffs = theCompDiffs:zero();
local firstPose = thePoses[i + 1];
firstPose = firstPose:reshape(3,18);
firstPose[1] = firstPose[1]/240.0;
firstPose[2] = firstPose[2]/320.0;
chunkPoses[1] = firstPose:clone();
local curH = torch.Tensor(3,3);
curH = curH:zero();
curH[1][1] = 1.0;
curH[2][2] = 1.0;
curH[3][3] = 1.0;
local prevPose = firstPose:clone();
for j = (i + 2), (i+opt.timestep+opt.numDecSteps*theChunk + 1) do
print({i, j, jpgFiles[j]})
local nextH = self.loadH(theDir..string.sub(jpgFiles[j], 1, -5)..".txt");
nextH = torch.inverse(nextH);
curH = torch.mm(nextH, curH);
local onePose = thePoses[j];
onePose = onePose:reshape(3,18);
onePose[1] = onePose[1]/240.0;
onePose[2] = onePose[2]/320.0;
prevPose = thePoses[torch.floor((j - i - 1)/theChunk - 0.001)*theChunk + i + 1]:clone();
prevPose = prevPose:reshape(3,18);
prevPose[1] = prevPose[1]/240.0;
prevPose[2] = prevPose[2]/320.0;
local bigPose = onePose:narrow(1,1,2);
bigPose[1] = bigPose[1]*240.0;
bigPose[2] = bigPose[2]*320.0;
local rectPose = torch.cat(bigPose:double(), torch.zeros(1,18)+1.0,1);
rectPose = torch.mm(curH:float(), rectPose:float()):clone();
rectPose[1] = torch.cdiv(rectPose[1], rectPose[3]);
rectPose[2] = torch.cdiv(rectPose[2], rectPose[3]);
onePose[1] = rectPose[1]:clone()/240.0;
onePose[2] = rectPose[2]:clone()/320.0;
bigPose = prevPose:narrow(1,1,2);
bigPose[1] = bigPose[1]*240.0;
bigPose[2] = bigPose[2]*320.0;
rectPose = torch.cat(bigPose:double(), torch.zeros(1,18)+1.0,1);
rectPose = torch.mm(curH:float(), rectPose:float()):clone();
rectPose[1] = torch.cdiv(rectPose[1], rectPose[3]);
rectPose[2] = torch.cdiv(rectPose[2], rectPose[3]);
prevPose[1] = rectPose[1]:clone()/240.0;
prevPose[2] = rectPose[2]:clone()/320.0;
theDiffs[j - i - 1] = (onePose:float()):reshape(18*3):float() - prevPose:reshape(18*3):float();
contPoses[j - i - 1] = prevPose:clone();
end
theDiffs[theDiffs:lt(-1000)] = 0;
theDiffs = theDiffs:transpose(2,1);
for k = 1,(opt.numSteps+opt.numDecSteps) do
chunkPoses[k] = contPoses[k*(opt.timestep/opt.numSteps)]:clone();
end
for j = 1, 36 do
for k = 1,(opt.numSteps+opt.numDecSteps) do
local temp = theDiffs[j][k*(opt.timestep/opt.numSteps)];
theCompDiffs[j][k] = temp;
end
end
local im = image.load(theDir .. jpgFiles[i+opt.numDecSteps*theChunk]);
im = image.scale(im, 320,240);
im = im*255.0;
local paintPose = image.load(theDir .. '/../lmdb/pose_' ..
string.format('%06i', i+opt.numDecSteps*theChunk - 1)..".png");
paintPose = image.scale(paintPose, 160,120);
paintPose = paintPose*255.0;
chunkPoses = chunkPoses:reshape(opt.numSteps+opt.numDecSteps,3,18);
txn:put(count, theCompDiffs:float());
txn:put(count+1, im:byte());
txn:put(count+2, paintPose:byte());
txn:put(count+3, chunkPoses:float());
count = count + 4
end
end
end
return count;
end
return LMDBTools;