-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_utils_new.lua
1251 lines (1120 loc) · 52.6 KB
/
data_utils_new.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'image'
require 'distributions'
local toolCompo = require 'toolComponent'
torch.setdefaulttensortype('torch.FloatTensor')
local ORIGIN_FRAME_HEIGHT = 576
local ORIGIN_FRAME_WIDTH = 720
local toolJointNames = {'LeftClasperPoint', 'RightClasperPoint',
'HeadPoint', 'ShaftPoint', 'EndPoint' } -- joint number = 5
local toolCompoNames = {{'LeftClasperPoint', 'HeadPoint'},
{'RightClasperPoint', 'HeadPoint'},
{'HeadPoint', 'ShaftPoint'},
{'ShaftPoint', 'EndPoint'} }
local toolTreeStructure = {{1, 3},{2, 3},{3, 4},{4, 5}}
function point2newFileLocation(oldFileName, pattern, replace)
local newFileName, changedFlag = string.gsub(oldFileName, pattern, replace)
assert(changedFlag == 1)
return newFileName
end
function changeFrameFormat(oldFileName, new_string_format)
local fileDir = paths.dirname(oldFileName)
local extname = paths.extname(oldFileName)
local filename = paths.basename(oldFileName, extname)
local frame_idx = string.match(filename, "%d+")
local frame_name = string.format(new_string_format, frame_idx)
local newfileName = paths.concat(fileDir, frame_name)
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 nmsBox(boxes, overlap)
-- todo
end
-- for 2D heatmap, perform the non-maximum suppression by point-wise distanace,
-- return point position (row, column)
function nmsPt(map, dist_thres, score_factor_thres, max_pt_num)
assert(map:dim() == 2)
max_pt_num = max_pt_num or 100
map = map:float()
local pick = {}
local max_score = map:max()
local map_height = map:size(1)
local map_width = map:size(2)
map[torch.lt(map, score_factor_thres*max_score)] = 0
local map_aug = torch.zeros(map_height+2, map_width+2)
local map_aug1 = map_aug:clone()
local map_aug2 = map_aug:clone()
local map_aug3 = map_aug:clone()
local map_aug4 = map_aug:clone()
map_aug[{{2,-2},{2,-2}}] = map:clone()
map_aug1[{{2,-2},{1,-3}}] = map:clone()
map_aug2[{{2,-2},{3,-1}}] = map:clone()
map_aug3[{{1,-3},{2,-2}}] = map:clone()
map_aug4[{{3,-1},{2,-2}}] = map:clone()
local peak_map1 = torch.gt(map_aug, map_aug1)
local peak_map2 = torch.gt(map_aug, map_aug2)
local peak_map3 = torch.gt(map_aug, map_aug3)
local peak_map4 = torch.gt(map_aug, map_aug4)
-- peak_map1:cmul(peak_map2):cmul(peak_map3):cmul(peak_map4)
-- local peak_map = peak_map1[{{2,-2},{2,-2}}]
local peak_map1_ = torch.ge(map_aug, map_aug1)
local peak_map2_ = torch.ge(map_aug, map_aug2)
local peak_map3_ = torch.ge(map_aug, map_aug3)
local peak_map4_ = torch.ge(map_aug, map_aug4)
peak_map1_:cmul(peak_map2):cmul(peak_map3):cmul(peak_map4)
peak_map2_:cmul(peak_map1):cmul(peak_map3):cmul(peak_map4)
peak_map3_:cmul(peak_map1):cmul(peak_map2):cmul(peak_map4)
peak_map4_:cmul(peak_map1):cmul(peak_map2):cmul(peak_map3)
peak_map1_:add(peak_map2_):add(peak_map3_):add(peak_map4_):clamp(0,1)
local peak_map = peak_map1_[{{2,-2},{2,-2}}]
local non_zero_indices = peak_map:nonzero()
if non_zero_indices:nElement() == 0 then return pick end
if non_zero_indices:size(1) > max_pt_num then return pick end
local peaks = torch.FloatTensor(non_zero_indices:size(1), 3)
peaks[{{},{1,2}}] = non_zero_indices
for i=1, peaks:size(1) do
peaks[i][3] = map[peaks[i][1]][peaks[i][2]]
end
local r = peaks[{{}, 1}]
local c = peaks[{{}, 2}]
local score = peaks[{{}, 3}]
local sorted_score, sorted_idx = torch.sort(score, false)
local sorted_idx_tab = torch.totable(sorted_idx)
while #sorted_idx_tab ~= 0 do
local last = #sorted_idx_tab
local max_idx = sorted_idx_tab[last]
table.insert(pick, torch.totable(peaks[max_idx]))
local removed_indice = {}
table.insert(removed_indice, last)
for idx=last-1, 1, -1 do
local sorted_idx = sorted_idx_tab[idx]
-- compute distance
local dist = math.sqrt(math.pow(r[max_idx]-r[sorted_idx],2)+math.pow(c[max_idx]-c[sorted_idx],2))
if dist < dist_thres then
table.insert(removed_indice, idx)
end
end
for ridx=1, #removed_indice do
-- print('removing ' .. removed_indice[r])
table.remove(sorted_idx_tab, removed_indice[ridx])
end
-- print('size = ' .. #sorted_idx_tab)
-- print(sorted_idx_tab)
end
return pick
-- local weighted_pts = torch.FloatTensor(map:nElement(),3)
-- local picked_num = 0
-- for i=1, map_height do
-- for j=1, map_width do
-- if map[i][j] >= max_score * score_factor_thres then
-- picked_num = picked_num + 1
-- weighted_pts[picked_num] = torch.FloatTensor({i, j, map[i][j]})
-- end
-- end
-- end
--
-- weighted_pts = weighted_pts[{{1, picked_num}}]
-- if weighted_pts == nil or weighted_pts:size(1) == 0 then
-- else
-- local x = weighted_pts[{{}, 1}]
-- local y = weighted_pts[{{}, 2}]
-- local score = weighted_pts[{{}, 3}]
--
-- local sorted_score, sorted_idx = torch.sort(score, false)
-- local sorted_idx_tab = torch.totable(sorted_idx)
-- while #sorted_idx_tab ~= 0 do
-- local last = #sorted_idx_tab
-- local max_idx = sorted_idx_tab[last]
-- table.insert(pick, torch.totable(weighted_pts[max_idx]))
-- local removed_indice = {}
-- table.insert(removed_indice, last)
-- for idx=last-1, 1, -1 do
-- local sorted_idx = sorted_idx_tab[idx]
-- -- compute distance
-- local dist = math.sqrt(math.pow(x[max_idx]-x[sorted_idx],2)+math.pow(y[max_idx]-y[sorted_idx],2))
-- if dist < dist_thres then
-- table.insert(removed_indice, idx)
-- end
-- end
-- for r=1, #removed_indice do
---- print('removing ' .. removed_indice[r])
-- table.remove(sorted_idx_tab, removed_indice[r])
-- end
---- print('size = ' .. #sorted_idx_tab)
---- print(sorted_idx_tab)
-- end
-- end
---- print(pick)
-- return pick
---- return torch.FloatTensor(pick)
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()
-- -- substract mean values
-- for i=1, 3 do
-- imgs_scaled:select(2, i):add(-1*meanRGBValue[i])
-- end
-- normailize
imgs_scaled = torch.div(imgs_scaled, 255)
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
function getJointPos(annotations, jointNames, joint_num)
local joint_num = #jointNames
local jointPos = torch.FloatTensor(joint_num*joint_num, 2):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
local joint_y = joint_anno[tool_idx].y
jointPos[joint_idx*2-1][1] = joint_x
jointPos[joint_idx*2][2] = joint_y
end
end
end
return jointPos
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
-- generate seperate joint map
-- todo: not consider same component from different tools overlapping with each other
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, joint_num 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
-- generate seperate PAF map
-- toolCompoNames = {LeftClasper ={LeftClasperPoint, HeadPoint},
-- RightClasper ={RightClasperPoint, HeadPoint},
-- Head ={HeadPoint, ShaftPoint},
-- Shaft ={ShaftPoint, EndPoint} }
function genPAFMap(annotations, toolCompoNames, side_thickness, frame, scale)
if scale == nil or scale == 0 then
scale = 1
end
side_thickness = side_thickness/scale
local frame_width = frame:size(3)
local frame_height = frame:size(2)
local pm_width = torch.floor(frame_width/scale)
local pm_height = torch.floor(frame_height/scale)
local compo_num=#toolCompoNames
local paf_map = torch.FloatTensor(2, pm_height, pm_width):fill(0)
for compo_idx = 1, compo_num do
local compo = toolCompoNames[compo_idx]
local joint1_anno = annotations[compo[1]]
local joint2_anno = annotations[compo[2]]
if joint1_anno == nil or joint2_anno == nil then
else
local paf_overlap_map = torch.FloatTensor(pm_height, pm_width):fill(1)
-- multi tools
for joint1_tool_idx=1, #joint1_anno do
for joint2_tool_idx=1, #joint2_anno do
if joint2_anno[joint2_tool_idx].id == joint1_anno[joint1_tool_idx].id then
local joint1_x = joint1_anno[joint1_tool_idx].x * frame_width/scale
local joint1_y = joint1_anno[joint1_tool_idx].y * frame_height/scale
local joint2_x = joint2_anno[joint2_tool_idx].x * frame_width/scale
local joint2_y = joint2_anno[joint2_tool_idx].y * frame_height/scale
-- generate paf points map
local tool_comp = toolCompo(joint1_x, joint1_y, joint2_x, joint2_y, side_thickness)
local min_vertex, max_vertex = tool_comp:getBoundingVertices()
local x_min = math.max(1, torch.round( min_vertex.x))
local x_max = math.min(pm_width, torch.round(max_vertex.x))
local y_min = math.max(1, torch.round(min_vertex.y))
local y_max = math.min(pm_height, torch.round(max_vertex.y))
for i=x_min, x_max do
for j=y_min, y_max do
if tool_comp:inside(i,j) then
paf_map[1][j][i] = paf_map[1][j][i] + 1 * tool_comp.rad
paf_map[2][j][i] = paf_map[2][j][i] + 1 * tool_comp.rad
paf_overlap_map[j][i] = paf_overlap_map[j][i] + 1
end
end
end
end
end
end
paf_map[1]:cdiv(paf_overlap_map)
paf_map[2]:cdiv(paf_overlap_map)
end
end
return paf_map
end
-- todo: not consider same component from different tools overlapping with each other
function genSepPAFMapDet(annotations, toolCompoNames, side_thickness, frame, scale)
if scale == nil or scale == 0 then
scale = 1
end
side_thickness = side_thickness/scale
local frame_width = frame:size(3)
local frame_height = frame:size(2)
local pm_width = torch.floor(frame_width/scale)
local pm_height = torch.floor(frame_height/scale)
local compo_num=0
local compo_num = #toolCompoNames
local paf_map = torch.ByteTensor(compo_num, pm_height, pm_width):fill(0)
local compo_idx = 1
for compo_idx=1, compo_num do
local compo = toolCompoNames[compo_idx]
local joint1_anno = annotations[compo[1]]
local joint2_anno = annotations[compo[2]]
if joint1_anno == nil or joint2_anno == nil then
else
-- multi tools
for joint1_tool_idx=1, #joint1_anno do
for joint2_tool_idx=1, #joint2_anno do
if joint2_anno[joint2_tool_idx].id == joint1_anno[joint1_tool_idx].id then
local joint1_x = joint1_anno[joint1_tool_idx].x * frame_width/scale
local joint1_y = joint1_anno[joint1_tool_idx].y * frame_height/scale
local joint2_x = joint2_anno[joint2_tool_idx].x * frame_width/scale
local joint2_y = joint2_anno[joint2_tool_idx].y * frame_height/scale
-- generate paf points map
local tool_comp = toolCompo(joint1_x, joint1_y, joint2_x, joint2_y, side_thickness)
local min_vertex, max_vertex = tool_comp:getBoundingVertices()
local x_min = math.max(1, torch.round( min_vertex.x))
local x_max = math.min(pm_width, torch.round(max_vertex.x))
local y_min = math.max(1, torch.round(min_vertex.y))
local y_max = math.min(pm_height, torch.round(max_vertex.y))
for i=x_min, x_max do
for j=y_min, y_max do
if tool_comp:inside(i, j) then
-- version 1
-- paf_map[compo_idx*2-1][j][i] = paf_map[compo_idx*2-1][j][i] + 1*torch.cos(tool_comp.rad)
-- paf_map[compo_idx*2][j][i] = paf_map[compo_idx*2][j][i] + 1*torch.sin(tool_comp.rad)
-- version 2
paf_map[compo_idx][j][i] = 1
end
end
end
end
end
end
end
compo_idx = compo_idx + 1
end
return paf_map
end
function genSepPAFMapReg(annotations, toolCompoNames, side_thickness, det_side_thickness, frame, scale, normalise_scale)
if scale == nil or scale == 0 then scale = 1 end
if normalise_scale== nil or normalise_scale == 0 then normalise_scale = 1 end
side_thickness = side_thickness/scale
det_side_thickness = det_side_thickness/scale
-- local min_side_thickness = math.min(side_thickness, det_side_thickness)
local min_side_thickness = det_side_thickness
local frame_width = frame:size(3)
local frame_height = frame:size(2)
local pm_width = torch.floor(frame_width/scale)
local pm_height = torch.floor(frame_height/scale)
local compo_num=0
local compo_num = #toolCompoNames
local paf_map = torch.FloatTensor(compo_num, pm_height, pm_width):fill(0)
local compo_idx = 1
for compo_idx=1, compo_num do
local compo = toolCompoNames[compo_idx]
local joint1_anno = annotations[compo[1]]
local joint2_anno = annotations[compo[2]]
if joint1_anno == nil or joint2_anno == nil then
else
-- multi tools
for joint1_tool_idx=1, #joint1_anno do
for joint2_tool_idx=1, #joint2_anno do
if joint2_anno[joint2_tool_idx].id == joint1_anno[joint1_tool_idx].id then
local joint1_x = joint1_anno[joint1_tool_idx].x * frame_width/scale
local joint1_y = joint1_anno[joint1_tool_idx].y * frame_height/scale
local joint2_x = joint2_anno[joint2_tool_idx].x * frame_width/scale
local joint2_y = joint2_anno[joint2_tool_idx].y * frame_height/scale
-- generate paf points map
local tool_comp = toolCompo(joint1_x, joint1_y, joint2_x, joint2_y, min_side_thickness)
local min_vertex, max_vertex = tool_comp:getBoundingVertices()
local x_min = math.max(1, torch.round( min_vertex.x))
local x_max = math.min(pm_width, torch.round(max_vertex.x))
local y_min = math.max(1, torch.round(min_vertex.y))
local y_max = math.min(pm_height, torch.round(max_vertex.y))
for i=x_min, x_max do
for j=y_min, y_max do
if tool_comp:inside(i, j) then
-- version 1
-- paf_map[compo_idx*2-1][j][i] = paf_map[compo_idx*2-1][j][i] + 1*torch.cos(tool_comp.rad)
-- paf_map[compo_idx*2][j][i] = paf_map[compo_idx*2][j][i] + 1*torch.sin(tool_comp.rad)
-- version 2
local tool_dist = tool_comp:getToolDist(i, j)
local v = distributions.norm.pdf(tool_dist, 0, side_thickness/2)
-- print(string.format('tool dist=%f, gauss_v=%f, side_thickness=%f', tool_dist, v, side_thickness))
paf_map[compo_idx][j][i] = v
end
end
end
end
end
end
end
compo_idx = compo_idx + 1
end
-- normalize?
paf_map = normalise_scale * torch.div(paf_map, paf_map:max())
return paf_map
end
-- [not used] generate seperate PAF map
-- toolCompoNames = {{LeftClasperPoint, HeadPoint},
-- {RightClasperPoint, HeadPoint},
-- {HeadPoint, ShaftPoint},
-- {ShaftPoint, EndPoint} }
function genSepPAFMap(annotations, toolCompoNames, side_thickness, frame, scale)
if scale == nil or scale == 0 then
scale = 1
end
side_thickness = side_thickness/scale
local frame_width = frame:size(3)
local frame_height = frame:size(2)
local pm_width = torch.floor(frame_width/scale)
local pm_height = torch.floor(frame_height/scale)
local compo_num=0
local compo_num = #toolCompoNames
local paf_map = torch.FloatTensor(compo_num*2, pm_height, pm_width):fill(0)
local compo_idx = 1
for compo_idx=1, compo_num do
local compo = toolCompoNames[compo_idx]
local joint1_anno = annotations[compo[1]]
local joint2_anno = annotations[compo[2]]
if joint1_anno == nil or joint2_anno == nil then
else
local paf_overlap_map = torch.FloatTensor(pm_height, pm_width):fill(0)
-- multi tools
for joint1_tool_idx=1, #joint1_anno do
for joint2_tool_idx=1, #joint2_anno do
if joint2_anno[joint2_tool_idx].id == joint1_anno[joint1_tool_idx].id then
local joint1_x = joint1_anno[joint1_tool_idx].x * frame_width/scale
local joint1_y = joint1_anno[joint1_tool_idx].y * frame_height/scale
local joint2_x = joint2_anno[joint2_tool_idx].x * frame_width/scale
local joint2_y = joint2_anno[joint2_tool_idx].y * frame_height/scale
-- generate paf points map
local tool_comp = toolCompo(joint1_x, joint1_y, joint2_x, joint2_y, side_thickness)
local min_vertex, max_vertex = tool_comp:getBoundingVertices()
local x_min = math.max(1, torch.round( min_vertex.x))
local x_max = math.min(pm_width, torch.round(max_vertex.x))
local y_min = math.max(1, torch.round(min_vertex.y))
local y_max = math.min(pm_height, torch.round(max_vertex.y))
for i=x_min, x_max do
for j=y_min, y_max do
if tool_comp:inside(i, j) then
-- version 1
-- paf_map[compo_idx*2-1][j][i] = paf_map[compo_idx*2-1][j][i] + 1*torch.cos(tool_comp.rad)
-- paf_map[compo_idx*2][j][i] = paf_map[compo_idx*2][j][i] + 1*torch.sin(tool_comp.rad)
-- version 2
paf_map[compo_idx*2-1][j][i] = paf_map[compo_idx*2-1][j][i] + 1
local rad = tool_comp.rad/(2*math.pi) + 0.5 -- from [-pi,pi] to [0,1]
paf_map[compo_idx*2][j][i] = paf_map[compo_idx*2][j][i] + rad
paf_overlap_map[j][i] = paf_overlap_map[j][i] + 1
end
end
end
end
end
end
paf_overlap_map[torch.eq(paf_overlap_map,0)] = 1
paf_map[compo_idx*2-1]:cdiv(paf_overlap_map)
paf_map[compo_idx*2]:cdiv(paf_overlap_map)
end
compo_idx = compo_idx + 1
end
return paf_map
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 genSepHeatMap(annotations, jointNames, sigma, det_radius, frame, scale, normalise_scale)
if scale == nil or scale == 0 then scale = 1 end
if normalise_scale== nil or normalise_scale == 0 then normalise_scale = 1 end
sigma = sigma / scale
det_radius = det_radius / scale
local frame_height = frame:size(2)
local frame_width = frame:size(3)
local hm_height = torch.floor(frame_height/scale)
local hm_width = torch.floor(frame_width/scale)
local joint_num = #jointNames
local heatmap = torch.FloatTensor(joint_num, hm_height, hm_width):fill(0)
local r = 2
local range = math.min(det_radius, r * sigma)
local sigma_matrix = torch.eye(2) * sigma
local x_min, x_max, y_min, y_max
for joint_idx=1, joint_num 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 mu = torch.FloatTensor({joint_x, joint_y})
x_min = math.max(1, torch.floor(joint_x - range))
x_max = math.min(hm_width, torch.ceil(joint_x + range))
y_min = math.max(1, torch.floor(joint_y - range))
y_max = math.min(hm_height, torch.ceil(joint_y + range))
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)) <= r * sigma then
local v = distributions.mvn.pdf(torch.Tensor({i,j}), mu, sigma_matrix)
heatmap[joint_idx][j][i] = math.max(heatmap[joint_idx][j][i], v)
end
end
end
end
end
end
-- normalize?
heatmap = normalise_scale * torch.div(heatmap, heatmap:max())
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.FloatTensor({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(hm_width, torch.ceil(ptx + r * sigma))
local y_min = math.max(1, torch.floor(pty - r * sigma))
local y_max = math.min(hm_height, torch.ceil(pty + r * sigma))
for i = x_min, x_max do
for j= y_min, y_max do
if math.sqrt(math.pow(i-ptx, 2) + math.pow(j-pty, 2)) <= r * sigma then
heatmap[1][j][i] = distributions.mvn.pdf(torch.Tensor({i,j}), mu, sigma_matrix)
end
end
end
return heatmap
end
-- flip (for normalized annotations)
function flipToolPosData(frame, hflip, vflip, annotations)
local flipped_frame, flipped_annos
flipped_annos = deepCopy(annotations)
local frame_width = frame:size(3)
local frame_height = frame:size(2)
if hflip == 0 and vflip == 0 then -- original
flipped_frame = frame
elseif hflip == 1 and vflip == 0 then -- horizonal flip
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
-- class -> flip left and right clasper
if joint_class == 'LeftClasperPoint' then
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
else
flipped_annos[joint_class][i].x = 1 - annotations[joint_class][i].x + 1/frame_width
end
end
end
elseif hflip == 0 and vflip == 1 then -- vertical flip
flipped_frame = image.vflip(frame)
for joint_class, __ in pairs(flipped_annos) do
for i=1, #flipped_annos[joint_class] do
-- x,y -> flip y, not x
-- class -> flip left and right clasper
if joint_class == 'LeftClasperPoint' then
flipped_annos['LeftClasperPoint'][i].x = annotations['RightClasperPoint'][i].x
flipped_annos['LeftClasperPoint'][i].y = 1 - annotations['RightClasperPoint'][i].y + 1/frame_height
elseif joint_class == 'RightClasperPoint' then
flipped_annos['RightClasperPoint'][i].x = annotations['LeftClasperPoint'][i].x
flipped_annos['RightClasperPoint'][i].y = 1 - annotations['LeftClasperPoint'][i].y + 1/frame_height
else
flipped_annos[joint_class][i].y = 1 - annotations[joint_class][i].y + 1/frame_height
end
end
end
else -- horizontal and vertical flip
local hflipped_frame = image.hflip(frame)
flipped_frame = image.vflip(hflipped_frame)
for joint_class, __ in pairs(flipped_annos) do
-- x,y -> flip x and y
for i=1, #flipped_annos[joint_class] do
flipped_annos[joint_class][i].x = 1 - annotations[joint_class][i].x + 1/frame_width
flipped_annos[joint_class][i].y = 1 - annotations[joint_class][i].y + 1/frame_height
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
function findJoints(joint_outputs_map, joint_names, dist_thres, score_factor_thres)
local batch_size = joint_outputs_map:size(1)
local output_height = joint_outputs_map:size(3)
local output_width = joint_outputs_map:size(4)
dist_thres = dist_thres or 10
score_factor_thres = score_factor_thres or 0.5
-- nms
local joints_batch = {}
for bidx = 1, batch_size do
local joints = {}
for i=1, #joint_names do
local joint_map = joint_outputs_map[bidx][i]
local output_peaks = nmsPt(joint_map, dist_thres, score_factor_thres)
-- for pidx=1, #output_peaks do
-- 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(joints, output_peaks)
end
table.insert(joints_batch, joints)
end
return joints_batch
end
function jointAssociation(outputs_map, joint_names, toolTreeStructure, dist_thres, score_factor_thres, min_joint_num)
min_joint_num = min_joint_num or 3
-- find the joints
local joints_batch = findJoints(outputs_map[{{},{1, #joint_names}}], joint_names, dist_thres, score_factor_thres)
local batch_size = #joints_batch
local subsets_batch = {}
local candidates_batch = {}
for bidx = 1, batch_size do
local candidates = {}
local maximum = {}
local count = 0
for i=1, #joint_names do table.insert(maximum, {}) end
for i=1, #joint_names do
local output_peaks = joints_batch[bidx][i]
for pidx=1, #output_peaks do
table.insert(maximum[i], {output_peaks[pidx][1], output_peaks[pidx][2], output_peaks[pidx][3], pidx+count})
table.insert(candidates, {output_peaks[pidx][1], output_peaks[pidx][2], output_peaks[pidx][3], i})
end
count = count + #output_peaks
end
-- the last number in each row is the total joint number of that tool
-- the second last number in each row is the score of the overall configuration
local subset = {}
local subset_col_num = #joint_names + 2
local connection = {}
for k=1, #toolTreeStructure do
local score_mid = outputs_map[bidx][#joint_names+k]
local tree = toolTreeStructure[k]
local jointtypeA = tree[1]
local jointtypeB = tree[2]
local candA = maximum[jointtypeA]
local candB = maximum[jointtypeB]
connection[k] = {}
local nA = #candA
local nB = #candB
-- add joint parts into the subset in special case
if(nA == 0 and nB == 0) then
-- do nothing
elseif nA == 0 then
for i = 1, nB do
local num = 0
for j = 1, #subset do
if subset[j][jointtypeB] == candB[i][4] then num = num + 1 end
end
if num == 0 then
local new_ = {}
new_[jointtypeB] = candB[i][4]
new_[subset_col_num-1] = candB[i][3]
new_[subset_col_num] = 1
table.insert(subset, new_)
end
end
elseif nB == 0 then
for i = 1, nA do
local num = 0
for j = 1, #subset do
if subset[j][jointtypeA] == candA[i][4] then num = num + 1 end
end
if num == 0 then
local new_ = {}
new_[jointtypeA] = candA[i][4]
new_[subset_col_num-1] = candA[i][3]
new_[subset_col_num] = 1
table.insert(subset, new_)
end
end
else
local temp = {}
local midPoint = torch.FloatTensor(2)
local vec = torch.FloatTensor(2)