-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner_invivo_det.lua
518 lines (457 loc) · 19.8 KB
/
runner_invivo_det.lua
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
require 'nn'
require 'cunn'
require 'cudnn'
require 'cutorch'
require 'xlua'
require 'image'
local optim = require('optim')
local matio = require 'matio'
local DataLoader = require 'dataloader_invivo_det'
torch.setdefaulttensortype('torch.FloatTensor')
local ORIGIN_FRAME_HEIGHT = 1080 --576
local ORIGIN_FRAME_WIDTH = 1920 --720
-- frames [b,c=3,H,W]
-- gt_jtmaps [b,c=1,h,w]
-- outputs [b*h*w,9]
local function visualResult(frames, gt_maps, outputs_map, joint_names, compo_names, saveDir)
-- outputs_map:clamp(0,1)
local batch_size = frames:size(1)
local rand_idx = torch.ceil(batch_size * math.random())
image.save(paths.concat(saveDir, 'frame_raw.png'), (255*frames[rand_idx]):byte())
local joint_num = #joint_names
for i=1, joint_num do
-- print(gt_maps[rand_idx][i]:min(), gt_maps[rand_idx][i]:max())
-- print(outputs_map[rand_idx][i]:min(), outputs_map[rand_idx][i]:max())
if gt_maps ~= nil then
image.save(paths.concat(saveDir, string.format('frame_%s_gt.png', joint_names[i])), (255*gt_maps[rand_idx][i]):byte())
end
image.save(paths.concat(saveDir, string.format('frame_%s_result.png', joint_names[i])), (255*outputs_map[rand_idx][i]):byte())
end
local compo_num = #compo_names
for i=1, compo_num do
-- print(gt_maps[rand_idx][joint_num+i]:min(), gt_maps[rand_idx][joint_num+i]:max())
-- print(outputs_map[rand_idx][joint_num+i]:min(), outputs_map[rand_idx][joint_num+i]:max())
if gt_maps ~= nil then
image.save(paths.concat(saveDir, string.format('frame_{%s_%s}_gt.png', compo_names[i][1], compo_names[i][2])), (255*gt_maps[rand_idx][joint_num+i]):byte())
end
image.save(paths.concat(saveDir, string.format('frame_{%s_%s}_result.png', compo_names[i][1], compo_names[i][2])), (255*outputs_map[rand_idx][joint_num+i]):byte())
end
end
local function saveMatResult(frames, gt_maps, outputs_map, joint_names, compo_names, saveDir)
outputs_map:clamp(0,1)
local batch_size = frames:size(1)
local rand_idx = torch.ceil(batch_size * math.random())
local saved_mat = {}
saved_mat['frame'] = frames[rand_idx]:float()
local joint_num = #joint_names
for i=1, joint_num do
if gt_maps ~= nil then saved_mat[string.format('conf_%s_gt', joint_names[i])] = gt_maps[rand_idx][i]:float() end
saved_mat[string.format('conf_%s_result', joint_names[i])] = outputs_map[rand_idx][i]:float()
end
local compo_num = #compo_names
for i=1, compo_num do
if gt_maps ~= nil then saved_mat[string.format('compo_%s_%s_gt', compo_names[i][1], compo_names[i][2])] = gt_maps[rand_idx][joint_num+i]:float() end
saved_mat[string.format('compo_%s_%s_result', compo_names[i][1], compo_names[i][2])] = outputs_map[rand_idx][joint_num+i]:float()
end
matio.save(paths.concat(saveDir, 'output.mat'), saved_mat)
end
local function JointPrecision(gt_joints_anno, outputs_map, joint_names, dist_thres)
local batch_size = outputs_map:size(1)
local output_height = outputs_map:size(3)
local output_width = outputs_map:size(4)
local batch_output_peaks_tab = {}
dist_thres = dist_thres or 10
local score_factor_thres = 0.3
-- nms
for bidx = 1, batch_size do
local output_peaks_tab = {}
for i=1, #joint_names do
local joint_map = outputs_map[bidx][i]
local output_peaks = nmsPt(joint_map, dist_thres, score_factor_thres)
for pidx=1, #output_peaks do
-- print(output_peaks[pidx][1], output_peaks[pidx][2])
output_peaks[pidx][1] = output_peaks[pidx][1] / output_height * ORIGIN_FRAME_HEIGHT
output_peaks[pidx][2] = output_peaks[pidx][2] / output_width * ORIGIN_FRAME_WIDTH
end
table.insert(output_peaks_tab, output_peaks)
end
table.insert(batch_output_peaks_tab, output_peaks_tab)
end
local dist_tab
local old_dist_tab = {}
local new_dist_tab = {}
for i=1, #joint_names do
table.insert(old_dist_tab, {})
table.insert(new_dist_tab, {})
end
for bidx=1, batch_size do
local frame_anno = gt_joints_anno[bidx].anno
local frame_class = gt_joints_anno[bidx].class
if frame_class == 1 then
dist_tab = old_dist_tab
elseif frame_class == 2 then
dist_tab = new_dist_tab
end
for i=1, #joint_names do
local joint_anno = frame_anno[joint_names[i]]
if joint_anno ~= nil then
local output_peaks = batch_output_peaks_tab[bidx][i]
for tool_idx=1, #joint_anno do
local gt_joint_x = joint_anno[tool_idx].x * ORIGIN_FRAME_WIDTH
local gt_joint_y = joint_anno[tool_idx].y * ORIGIN_FRAME_HEIGHT
-- print(string.format('gtx=%f, gty=%f', gt_joint_x, gt_joint_y))
-- compute the distance
local dist = 1e+8
local chosen_result_x, chosen_result_y = -1, -1
for pidx=1, #output_peaks do
local result_joint_x = output_peaks[pidx][2]
local result_joint_y = output_peaks[pidx][1]
-- print(string.format('rex=%f, rey=%f', result_joint_x, result_joint_y))
local d = math.sqrt(math.pow(gt_joint_x-result_joint_x,2)+math.pow(gt_joint_y-result_joint_y,2))
if d < dist then
dist = d
chosen_result_x = result_joint_x
chosen_result_y = result_joint_y
end
end
-- if dist > 20 and dist < 1e+8 then
-- print('')
-- print(joint_names[i])
-- for pidx=1, #output_peaks do
-- print(string.format('candis:[%d, %d]', output_peaks[pidx][2], output_peaks[pidx][1]))
-- end
-- print(string.format('gt: [%d, %d]', gt_joint_x, gt_joint_y))
-- print(string.format('result:[%d, %d]', chosen_result_x, chosen_result_y))
-- print(string.format('dist = %.2f', dist))
-- end
table.insert(dist_tab[i], dist)
end
end
end
end
-- average dist
local old_avg_dist_tab = {}
for i=1, #old_dist_tab do
local avg_dist = 0.0
local joint_disttab = old_dist_tab[i]
for j=1, #joint_disttab do
avg_dist = avg_dist + joint_disttab[j]
end
if #joint_disttab ~= 0 then avg_dist = avg_dist / #joint_disttab end
table.insert(old_avg_dist_tab, avg_dist)
-- print(string.format('%s average precision dist = %.2f',joint_names[i], avg_dist))
end
local new_avg_dist_tab = {}
for i=1, #new_dist_tab do
local avg_dist = 0.0
local joint_disttab = new_dist_tab[i]
for j=1, #joint_disttab do
avg_dist = avg_dist + joint_disttab[j]
end
if #joint_disttab ~= 0 then avg_dist = avg_dist / #joint_disttab end
table.insert(new_avg_dist_tab, avg_dist)
-- print(string.format('%s average precision dist = %.2f',joint_names[i], avg_dist))
end
-- local avgdist = 0.0
-- for i=1, #avg_dist_tab do
-- avgdist = avgdist + avg_dist_tab[i]
-- end
-- avgdist = avgdist / #avg_dist_tab
-- return avg_dist_tab
return old_avg_dist_tab, new_avg_dist_tab
end
local M = {}
local Runner = torch.class('Runner', M)
function Runner:__init(net_path, opt, optimState)
-- load network
print('Loading network ...')
self.model = torch.load(net_path)
-- print(self.model)
self.model:cuda()
-- opt
self.opt = opt
self.optimState = optimState
self.trainBatchSize = opt.trainBatchSize or opt.batchSize or 1
self.valBatchSize = opt.valBatchSize or opt.batchSize or 1
self.batchSize = opt.batchSize or 1
self.toolJointNames = opt.toolJointNames
self.toolCompoNames = opt.toolCompoNames
self.toolJointNum = #self.toolJointNames
self.toolCompoNum = #self.toolCompoNames
self.jointRadius = opt.jointRadius or 10
self.nGPU = #opt.gpus
local nGPU = #opt.gpus
if nGPU > 1 then
print('converting module to nn.DataParallelTable')
assert(nGPU <= cutorch.getDeviceCount(), 'number of GPUs less than nGPU specified')
local dpt = nn.DataParallelTable(1, true, true):add(self.extractModel, opt.gpus):threads(function()
require('cudnn')
end)
dpt.gradInput = nil
self.model = dpt
else
cudnn.fastest = true
cudnn.benchmark = true
end
-- data related
self.dataLoader = DataLoader(opt)
self.inputWidth = opt.inputWidth
self.inputHeight = opt.inputHeight
self.framesGPU = nil
self.mapsGPU = nil
if nGPU > 1 then
self.framesGPU = cutorch.createCudaHostTensor(self.batchSize, 3, self.inputHeight, self.inputWidth)
self.mapsGPU = cutorch.createCudaHostTensor(self.batchSize, self.toolJointNum+2*self.toolCompoNum, self.inputHeight, self.inputWidth)
else
-- self.framesGPU = torch.CudaTensor(self.batchSize, 3, self.inputHeight, self.inputWidth)
-- self.mapsGPU = torch.CudaTensor(self.batchSize, 1, self.inputHeight, self.inputWidth)
self.framesGPU = torch.CudaTensor()
self.mapsGPU = torch.CudaTensor()
end
self.params, self.gradParams = self.model:getParameters()
print('model #params = ' .. tostring(#self.params))
self.criterion = nn.BCECriterion()
-- self.criterion = nn.MSECriterion()
-- self.criterion.sizeAverage = false
self.criterion:cuda()
end
function Runner:getModel()
return self.model
end
function Runner:train(epoch)
local timer = torch.Timer()
local dataTimer = torch.Timer()
local dataTime = 0
local size = self.dataLoader:trainSize()
local loss = 0.0
local acc = 0.0
local old_acc = 0.0
local new_acc = 0.0
local old_prec_tab = {}
local new_prec_tab = {}
for i=1, #self.toolJointNames do
table.insert(old_prec_tab, 0.0)
table.insert(new_prec_tab, 0.0)
end
local N = 0
self.model:training()
local function feval()
return self.criterion.output, self.gradParams
end
for n, framesCPU, mapsCPU, jointAnnoTab in self.dataLoader:load(1) do
-- load data
dataTime = dataTime + dataTimer:time().real
-- transfer over to GPU
self.framesGPU:resize(framesCPU:size()):copy(framesCPU)
self.mapsGPU:resize(mapsCPU:size()):copy(mapsCPU)
-- reset gradparameters
self.gradParams:zero()
-- forward
-- print(self.framesGPU:size())
-- print(self.mapsGPU:size())
local outputsGPU = self.model:forward(self.framesGPU)
local loss_batch = self.criterion:forward(outputsGPU, self.mapsGPU)
loss = loss + loss_batch
-- backward
local grad_output = self.criterion:backward(outputsGPU, self.mapsGPU)
self.model:backward(self.framesGPU, grad_output)
-- update parameters
optim.sgd(feval, self.params, self.optimState)
-- todo: accumulate accuracy
local old_batch_prec, new_batch_prec = {}, {}
for i=1, #self.toolJointNames do table.insert(old_batch_prec, -1) table.insert(new_batch_prec, -1) end
old_batch_prec, new_batch_prec = JointPrecision(jointAnnoTab, outputsGPU, self.toolJointNames, self.jointRadius)
for i=1, #self.toolJointNames do
old_prec_tab[i] = old_batch_prec[i] + old_batch_prec[i]
new_prec_tab[i] = new_prec_tab[i] + new_batch_prec[i]
end
local batch_acc = torch.eq(torch.round(outputsGPU), torch.round(self.mapsGPU)):sum() / self.mapsGPU:nElement()
acc = acc + batch_acc
-- acc for old or new data
local old_batch_acc, new_batch_acc = 0.0, 0.0
local old_num, new_num = 0, 0
for idx = 1, #jointAnnoTab do
if jointAnnoTab[idx].class == 1 then -- old
old_batch_acc = old_batch_acc + torch.eq(torch.round(outputsGPU[idx]), torch.round(self.mapsGPU[idx])):sum() / self.mapsGPU[idx]:nElement()
old_num = old_num + 1
else
new_batch_acc = new_batch_acc + torch.eq(torch.round(outputsGPU[idx]), torch.round(self.mapsGPU[idx])):sum() / self.mapsGPU[idx]:nElement()
new_num = new_num + 1
end
end
old_acc = old_acc + old_batch_acc/old_num
new_acc = new_acc + new_batch_acc/new_num
N = N + 1
-- visualize result for debugging
if n == framesCPU:size(1) then
print(mapsCPU:max())
print(outputsGPU:max())
saveMatResult(framesCPU, mapsCPU, outputsGPU, self.toolJointNames, self.toolCompoNames,'/home/xiaofei/workspace/toolPose/finetune_det_results/train')
end
-- check that the storage didn't get changed due to an unfortunate getParameters call
assert(self.params:storage() == self.model:parameters()[1]:storage())
xlua.progress(n, size)
collectgarbage()
collectgarbage()
dataTimer:reset()
end
-- update optimState
self.optimState.epoch = self.optimState.epoch + 1
if epoch % self.opt.updateIternal == 0 then
self.optimState.learningRate = self.optimState.learningRate * self.opt.decayRatio
self.optimState.weightDecay = self.optimState.weightDecay * self.opt.decayRatio
end
-- calculate loss, acc
loss = loss / N
acc = acc * 100 / N
old_acc = old_acc * 100 / N
new_acc = new_acc * 100 / N
local old_prec = 0.0 -- average joint prec
for i=1, #self.toolJointNames do
old_prec_tab[i] = old_prec_tab[i] / N
-- print(string.format('OLD %s average precision dist = %.2f',self.toolJointNames[i], old_prec_tab[i]))
old_prec = old_prec + old_prec_tab[i]
end
old_prec = old_prec / #self.toolJointNames
local new_prec = 0.0 -- average joint prec
for i=1, #self.toolJointNames do
new_prec_tab[i] = new_prec_tab[i] / N
-- print(string.format('NEW %s average precision dist = %.2f',self.toolJointNames[i], new_prec_tab[i]))
new_prec = new_prec + new_prec_tab[i]
end
new_prec = new_prec / #self.toolJointNames
-- acc = 1 / (loss + 1e-5)
print("\nTrain : time to learn = " .. timer:time().real .. ' sec')
print("Train : time to load data = " .. dataTime .. ' sec')
return acc, loss, old_acc, new_acc
end
function Runner:val(epoch)
local timer = torch.Timer()
local dataTimer = torch.Timer()
local dataTime = 0
local size = self.dataLoader:valSize()
local loss = 0.0
local acc = 0.0
local old_acc = 0.0
local new_acc = 0.0
local old_prec_tab = {}
local new_prec_tab = {}
for i=1, #self.toolJointNames do
table.insert(old_prec_tab, 0.0)
table.insert(new_prec_tab, 0.0)
end
local N = 0
self.model:evaluate()
for n, framesCPU, mapsCPU, jointAnnoTab in self.dataLoader:load(2) do
-- load data
dataTime = dataTime + dataTimer:time().real
-- transfer over to GPU
self.framesGPU:resize(framesCPU:size()):copy(framesCPU)
self.mapsGPU:resize(mapsCPU:size()):copy(mapsCPU)
-- forward
-- print(self.framesGPU:size())
-- print(self.mapsGPU:size())
local outputsGPU = self.model:forward(self.framesGPU)
local loss_batch = self.criterion:forward(outputsGPU, self.mapsGPU)
loss = loss + loss_batch
-- todo: accumulate accuracy
local old_batch_prec, new_batch_prec = {}, {}
for i=1, #self.toolJointNames do table.insert(old_batch_prec, -1) table.insert(new_prec_tab, -1) end
old_batch_prec, new_batch_prec = JointPrecision(jointAnnoTab, outputsGPU, self.toolJointNames, self.jointRadius)
for i=1, #self.toolJointNames do
old_prec_tab[i] = old_prec_tab[i] + old_batch_prec[i]
new_prec_tab[i] = new_prec_tab[i] + new_batch_prec[i]
end
local batch_acc = torch.eq(torch.round(outputsGPU), torch.round(self.mapsGPU)):sum() / self.mapsGPU:nElement()
acc = acc + batch_acc
-- acc for old or new data
local old_batch_acc, new_batch_acc = 0.0, 0.0
local old_num, new_num = 0, 0
for idx = 1, #jointAnnoTab do
if jointAnnoTab[idx].class == 1 then -- old
old_batch_acc = old_batch_acc + torch.eq(torch.round(outputsGPU[idx]), torch.round(self.mapsGPU[idx])):sum() / self.mapsGPU[idx]:nElement()
old_num = old_num + 1
else
new_batch_acc = new_batch_acc + torch.eq(torch.round(outputsGPU[idx]), torch.round(self.mapsGPU[idx])):sum() / self.mapsGPU[idx]:nElement()
new_num = new_num + 1
end
end
old_acc = old_acc + old_batch_acc/old_num
new_acc = new_acc + new_batch_acc/new_num
N = N + 1
-- visualize result for debugging
if n == framesCPU:size(1) then
print(mapsCPU:max())
print(outputsGPU:max())
saveMatResult(framesCPU, mapsCPU, outputsGPU, self.toolJointNames, self.toolCompoNames, '/home/xiaofei/workspace/toolPose/finetune_det_results/val')
end
xlua.progress(n, size)
collectgarbage()
collectgarbage()
dataTimer:reset()
end
-- calculate loss, acc
loss = loss / N
acc = acc * 100 / N
old_acc = old_acc * 100 / N
new_acc = new_acc * 100 / N
local old_prec = 0.0 -- average joint prec
for i=1, #self.toolJointNames do
old_prec_tab[i] = old_prec_tab[i] / N
-- print(string.format('OLD %s average precision dist = %.2f',self.toolJointNames[i], old_prec_tab[i]))
old_prec = old_prec + old_prec_tab[i]
end
old_prec = old_prec / #self.toolJointNames
local new_prec = 0.0 -- average joint prec
for i=1, #self.toolJointNames do
new_prec_tab[i] = new_prec_tab[i] / N
-- print(string.format('NEW %s average precision dist = %.2f',self.toolJointNames[i], new_prec_tab[i]))
new_prec = new_prec + new_prec_tab[i]
end
new_prec = new_prec / #self.toolJointNames
-- acc = 1 / (loss + 1e-5)
print("\nVal : time to predict = " .. timer:time().real .. ' sec')
print("Val : time to load data = " .. dataTime .. ' sec')
return acc, loss, old_acc, new_acc
end
function Runner:test(epoch)
local timer = torch.Timer()
local dataTimer = torch.Timer()
local dataTime = 0
local size = self.dataLoader:testSize()
local loss = 0.0
local acc = 0.0
local N = 0
self.model:evaluate()
for n, framesCPU, mapsCPU in self.dataLoader:load(3) do
-- load data
dataTime = dataTime + dataTimer:time().real
-- transfer over to GPU
self.framesGPU:resize(framesCPU:size()):copy(framesCPU)
-- forward
-- print(self.framesGPU:size())
local outputsGPU = self.model:forward(self.framesGPU)
-- local loss_batch = self.criterion:forward(outputsGPU, self.mapsGPU)
-- loss = loss + loss_batch
-- accumulate accuracy
-- local batch_acc = torch.eq(torch.round(outputsGPU), torch.round(self.mapsGPU)):sum() / self.mapsGPU:nElement()
-- acc = acc + batch_acc
N = N + 1
-- visualize result for debugging
if n == framesCPU:size(1) then
saveMatResult(framesCPU, mapsCPU, outputsGPU, self.toolJointNames, self.toolCompoNames, '/home/xiaofei/workspace/toolPose/finetune_det_results/test')
end
xlua.progress(n, size)
collectgarbage()
collectgarbage()
dataTimer:reset()
end
-- calculate loss, acc
loss = loss / N
acc = acc * 100 / N
-- acc = 1 / (loss + 1e-5)
print("\nTest : time to predict = " .. timer:time().real .. ' sec')
print("Test : time to load data = " .. dataTime .. ' sec')
return acc, loss
end
return M.Runner