-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_utils.lua
More file actions
381 lines (337 loc) · 14.7 KB
/
data_utils.lua
File metadata and controls
381 lines (337 loc) · 14.7 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
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
require 'image'
require 'distributions'
torch.setdefaulttensortype('torch.FloatTensor')
function point2newFileLocation(oldFileName, pattern, replace)
local newFileName, changedFlag = string.gsub(oldFileName, pattern, replace)
assert(changedFlag == 1)
return newFileName
end
--function shallowCopy(src)
-- local src_type = type(src)
-- local dst
-- if src_type == 'table' then
-- dst = {}
-- for key, value in pairs(src) do
-- dst[key] = value
-- end
-- else -- number, string, boolean, etc
-- dst = src
-- end
-- return dst
--end
function deepCopy(src)
local src_type = type(src)
local dst
if src_type == 'table' then
dst = {}
for key, value in next, src, nil do
dst[deepCopy(key)] = deepCopy(value)
end
else -- number, string, boolean, etc
dst = src
end
return dst
end
function preProcess(imgsRGB, inputWidth, inputHeight, meanRGBValue)
-- convert to input image size
local imgs_scaled
if torch.type(imgsRGB) == 'table' then
imgs_scaled = torch.FloatTensor(#imgsRGB, 3, inputHeight, inputWidth)
for i=1, #imgsRGB do
imgs_scaled[i] = image.scale(imgsRGB[i], inputWidth, inputHeight)
end
else
imgs_scaled = image.scale(imgsRGB, inputWidth, inputHeight)
end
-- -- convert RGB to BGR
-- local perm = torch.LongTensor{3, 2, 1 }
-- local imgsBGR = imgs_scaled:index(2, perm):float()
if meanRGBValue == nil then
meanRGBValue = torch.FloatTensor({123, 117, 104})
end
-- -- substract mean values
-- for i=1, 3 do
-- imgs_scaled:select(2, i):add(-1*meanRGBValue[i])
-- end
return imgs_scaled
end
-- [discard, use genJointMapNew] generate binary joint map
function genJointMap(ptx, pty, radius, frame)
local height = frame:size(2)
local width = frame:size(3)
local jointmap = torch.zeros(1, height, width)
for i=torch.round(ptx-radius), torch.round(ptx+radius) do
for j=torch.round(pty-radius), torch.round(pty+radius) do
if math.sqrt(math.pow(i-ptx,2)+math.pow(j-pty,2)) <= radius and i>=1 and i<=width and j>=1 and j<=height then
jointmap[1][j][i] = 1
end
end
end
return jointmap
end
--toolJointNames = {'LeftClasperPoint=2', 'RightClasperPoint=3',
-- 'HeadPoint=4', 'ShaftPoint=5',
-- 'TrackedPoint=6', 'EndPoint=7' } -- joint number = 6
-- background=1
-- note: annotations is normalized
function genJointMapNew(annotations, jointNames, radius, frame, scale)
if scale == nil or scale == 0 then
scale = 1
end
radius = radius / scale
local frame_height = frame:size(2)
local frame_width = frame:size(3)
local jm_height = torch.floor(frame_height / scale)
local jm_width = torch.floor(frame_width / scale)
local jointmap = torch.ByteTensor(1, jm_height, jm_width):fill(1)
for joint_idx=1, #jointNames do
local joint_anno = annotations[jointNames[joint_idx]]
if joint_anno ~= nil then
for tool_idx=1, #joint_anno do
local joint_x = joint_anno[tool_idx].x * frame_width /scale
local joint_y = joint_anno[tool_idx].y * frame_height/scale
local x_min = math.max(1, torch.round( joint_x - radius))
local x_max = math.min(jm_width, torch.round(joint_x + radius))
local y_min = math.max(1, torch.round(joint_y - radius))
local y_max = math.min(jm_height, torch.round(joint_y + radius))
for i=x_min, x_max do
for j=y_min, y_max do
if math.sqrt(math.pow(i-joint_x, 2) + math.pow(j-joint_y, 2)) <= radius then
if jointmap[1][j][i] ~= 1 then
-- dist between new class and old_class
local old_class = jointmap[1][j][i]-1
local old_dist = 1e+8 -- large number
for old_tool_idx=1, #annotations[jointNames[old_class]] do
local old_toolj_dist= math.sqrt(math.pow(i-annotations[jointNames[old_class]][old_tool_idx].x*frame_width/scale, 2) +
math.pow(j-annotations[jointNames[old_class]][old_tool_idx].y*frame_height/scale, 2))
if old_toolj_dist < old_dist then old_dist = old_toolj_dist end
end
local new_dist = math.sqrt(math.pow(i-joint_x, 2) + math.pow(j-joint_y, 2))
-- print(old_class, joint_idx)
-- print(old_dist, new_dist)
if new_dist < old_dist then
jointmap[1][j][i] = joint_idx+1
end
else
jointmap[1][j][i] = joint_idx+1 -- plus 1 because of background
end
end
end
end
end
end
end
return jointmap
end
function genSepJointMap(annotations, jointNames, radius, frame, scale)
if scale == nil or scale == 0 then
scale = 1
end
radius = radius / scale
local frame_height = frame:size(2)
local frame_width = frame:size(3)
local jm_height = torch.floor(frame_height / scale)
local jm_width = torch.floor(frame_width / scale)
local joint_num = #jointNames
local jointmap = torch.ByteTensor(joint_num, jm_height, jm_width):fill(0)
for joint_idx=1, #jointNames do
local joint_anno = annotations[jointNames[joint_idx]]
if joint_anno ~= nil then
for tool_idx=1, #joint_anno do
local joint_x = joint_anno[tool_idx].x * frame_width /scale
local joint_y = joint_anno[tool_idx].y * frame_height/scale
local x_min = math.max(1, torch.round( joint_x - radius))
local x_max = math.min(jm_width, torch.round(joint_x + radius))
local y_min = math.max(1, torch.round(joint_y - radius))
local y_max = math.min(jm_height, torch.round(joint_y + radius))
for i=x_min, x_max do
for j=y_min, y_max do
if math.sqrt(math.pow(i-joint_x, 2) + math.pow(j-joint_y, 2)) <= radius then
jointmap[joint_idx][j][i] = 1
end
end
end
end
end
end
return jointmap
end
-- [discard, use genHeatMapFast] generate regression heatmap
function genHeatMap(ptx, pty, sigma, frame)
local mu = torch.Tensor({ptx, pty})
local sigma_matrix = torch.eye(2) * sigma
local height = frame:size(2)
local width = frame:size(3)
local heatmap = torch.zeros(1, height, width)
for i=1, width do
for j=1, height do
local prob = distributions.mvn.pdf(torch.Tensor({i,j}), mu, sigma_matrix)
heatmap[1][j][i] = prob
end
end
return heatmap
end
-- normalized ptx and pty
function genHeatMapFast(ptx, pty, sigma, frame, scale)
if scale == nil or scale == 0 then
scale = 1
end
local frame_height = frame:size(2)
local frame_width = frame:size(3)
sigma = sigma / scale
ptx = ptx * frame_width / scale
pty = pty * frame_height/ scale
local mu = torch.Tensor({ptx, pty})
local sigma_matrix = torch.eye(2) * sigma
local hm_height = torch.floor(frame_height / scale)
local hm_width = torch.floor(frame_width / scale)
local heatmap = torch.zeros(1, hm_height, hm_width)
local r = 2
local x_min = math.max(1, torch.floor(ptx - r * sigma))
local x_max = math.min(width, torch.ceil(ptx + r * sigma))
local y_min = math.max(1, torch.floor(pty - r * sigma))
local y_max = math.min(height, torch.ceil(pty + r * sigma))
for i = x_min, x_max do
for j= y_min, y_max do
heatmap[1][j][i] = distributions.mvn.pdf(torch.Tensor({i,j}), mu, sigma_matrix)
end
end
return heatmap
end
-- flip (for normalized annotations)
function flipToolPosData(frame, flip, annotations)
local flipped_frame, flipped_annos
flipped_annos = deepCopy(annotations)
if flip == 0 then
flipped_frame = frame:clone()
else
local frame_width = frame:size(3)
flipped_frame = image.hflip(frame)
for joint_class, __ in pairs(flipped_annos) do
for i=1, #flipped_annos[joint_class] do
-- x,y -> flip x, not y
flipped_annos[joint_class][i].x = 1 - annotations[joint_class][i].x + 1/frame_width
-- flipped_annos[joint_class][i].y = annotations[joint_class][i].y
if joint_class == 'LeftClasperPoint' then
-- class -> flip left and right clasper
flipped_annos['LeftClasperPoint'][i].x = 1 - annotations['RightClasperPoint'][i].x + 1/frame_width
flipped_annos['LeftClasperPoint'][i].y = annotations['RightClasperPoint'][i].y
elseif joint_class == 'RightClasperPoint' then
flipped_annos['RightClasperPoint'][i].x = 1 - annotations['LeftClasperPoint'][i].x + 1/frame_width
flipped_annos['RightClasperPoint'][i].y = annotations['LeftClasperPoint'][i].y
end
end
end
end
return flipped_frame, flipped_annos
end
-- rotate frame to simulate small camera roll movement (counter-clockwise)
function rotateToolPos(frame, degree, annotations)
local rotated_annos = deepCopy(annotations)
degree = degree % 360 -- [0, 360)
local radian = degree * math.pi / 180 -- [0, 2pi)
local frame_width = frame:size(3)
local frame_height = frame:size(2)
local rotated_frame = image.rotate(frame, radian, 'bilinear')
-- rotate the position (visualize to check)
local cx, cy = 0.5, 0.5
for joint_class, __ in pairs(annotations) do
for i=1, #annotations[joint_class] do
local rx = annotations[joint_class][i].x - cx
local ry = annotations[joint_class][i].y - cy
-- print('rel x=' .. rx)
-- print('rel y=' .. ry)
local rl = math.sqrt(math.pow(rx,2)+math.pow(ry,2))
local rad = torch.atan2(ry, rx) % (2*math.pi) -- [0, 2pi]
local rotated_rad = (rad - radian) % (2*math.pi) -- [0, 2pi)
-- print('rotated_degree=' .. rotated_rad)
rx = torch.cos(rotated_rad) * rl
ry = torch.sin(rotated_rad) * rl
-- print('rot cos=' .. torch.cos(rotated_rad))
-- print('rot sin=' .. torch.sin(rotated_rad))
-- print('rot rel x=' .. rx)
-- print('rot rel y=' .. ry)
rotated_annos[joint_class][i].x = cx + rx
rotated_annos[joint_class][i].y = cy + ry
-- check if the rotated annotations are still inside the image
if rotated_annos[joint_class][i].x * frame_width < 1 or rotated_annos[joint_class][i].x > 1 or
rotated_annos[joint_class][i].y * frame_height< 1 or rotated_annos[joint_class][i].y > 1 then
-- print('Rotate out of frame')
-- print(rotated_annos[joint_class][i])
rotated_annos[joint_class][i] = nil
end
end
end
return rotated_frame, rotated_annos
end
function rotateToolPosOrigin(frame, degree, annotations)
local rotated_annos = deepCopy(annotations)
degree = degree % 360 -- [0, 360)
local radian = degree * math.pi / 180 -- [0, 2pi)
local frame_width = frame:size(3)
local frame_height = frame:size(2)
local rotated_frame = image.rotate(frame, radian, 'bilinear')
-- rotate the position (visualize to check)
local cx = frame_width / 2
local cy = frame_height / 2
for joint_class, __ in pairs(annotations) do
for i=1, #annotations[joint_class] do
local rx = annotations[joint_class][i].x - cx
local ry = annotations[joint_class][i].y - cy
-- print('rel x=' .. rx)
-- print('rel y=' .. ry)
local rl = math.sqrt(math.pow(rx,2)+math.pow(ry,2))
local rad = torch.atan2(ry, rx) % (2*math.pi) -- [0, 2pi]
local rotated_rad = (rad - radian) % (2*math.pi) -- [0, 2pi)
-- print('rotated_degree=' .. rotated_rad)
rx = torch.cos(rotated_rad) * rl
ry = torch.sin(rotated_rad) * rl
-- print('rot cos=' .. torch.cos(rotated_rad))
-- print('rot sin=' .. torch.sin(rotated_rad))
-- print('rot rel x=' .. rx)
-- print('rot rel y=' .. ry)
rotated_annos[joint_class][i].x = cx + rx
rotated_annos[joint_class][i].y = cy + ry
-- check if the rotated annotations are still inside the image
if rotated_annos[joint_class][i].x < 1 or rotated_annos[joint_class][i].x > frame_width or
rotated_annos[joint_class][i].y < 1 or rotated_annos[joint_class][i].y > frame_height then
-- print('Rotate out of frame')
-- print(rotated_annos[joint_class][i])
rotated_annos[joint_class][i] = nil
end
end
end
return rotated_frame, rotated_annos
end
-- normalize pose to (-0.5, 0.5]
function normalizeToolPosWithNeg(frame_width, frame_height, annotations)
local normalized_annos = deepCopy(annotations)
for joint_class, __ in pairs(normalized_annos) do
for i=1, #normalized_annos[joint_class] do
normalized_annos[joint_class][i].x = (annotations[joint_class][i].x - frame_width*0.5) / frame_width
normalized_annos[joint_class][i].y = (annotations[joint_class][i].y - frame_height*0.5) / frame_height
end
end
return normalized_annos
end
-- normalize pose to (0,1]
function normalizeToolPos01(frame_width, frame_height, annotations)
local normalized_annos = deepCopy(annotations)
for joint_class, __ in pairs(normalized_annos) do
for i=1, #normalized_annos[joint_class] do
normalized_annos[joint_class][i].x = annotations[joint_class][i].x / frame_width
normalized_annos[joint_class][i].y = annotations[joint_class][i].y / frame_height
end
end
return normalized_annos
end
function unNormalizeToolPos01(frame_width, frame_height, normalized_annotations)
local annos = deepCopy(normalized_annotations)
for joint_class, __ in pairs(annos) do
for i=1, #annos[joint_class] do
annos[joint_class][i].x = normalized_annotations[joint_class][i].x * frame_width
annos[joint_class][i].y = normalized_annotations[joint_class][i].y * frame_height
end
end
return annos
end