-
Notifications
You must be signed in to change notification settings - Fork 86
/
util.lua
187 lines (159 loc) · 5.94 KB
/
util.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
require 'torch'
require 'xlua'
require 'nn'
require 'nnx'
require 'nngraph'
require 'image'
require 'hdf5'
require 'sys'
require 'cunn'
require 'cutorch'
require 'cudnn'
function loadAnnotations(set)
-- Load up a set of annotations for either: 'train', 'valid', or 'test'
-- There is no part information in 'test'
local a = hdf5.open('annot/' .. set .. '.h5')
annot = {}
-- Read in annotation information from hdf5 file
local tags = {'part','center','scale','normalize','torsoangle','visible'}
for _,tag in ipairs(tags) do annot[tag] = a:read(tag):all() end
annot.nsamples = annot.part:size()[1]
a:close()
-- Load in image file names
-- (workaround for not being able to read the strings in the hdf5 file)
annot.images = {}
local toIdxs = {}
local namesFile = io.open('annot/' .. set .. '_images.txt')
local idx = 1
for line in namesFile:lines() do
annot.images[idx] = line
if not toIdxs[line] then toIdxs[line] = {} end
table.insert(toIdxs[line], idx)
idx = idx + 1
end
namesFile:close()
-- This allows us to reference all people who are in the same image
annot.imageToIdxs = toIdxs
return annot
end
function getPreds(hms, center, scale)
if hms:size():size() == 3 then hms = hms:view(1, hms:size(1), hms:size(2), hms:size(3)) end
-- Get locations of maximum activations
local max, idx = torch.max(hms:view(hms:size(1), hms:size(2), hms:size(3) * hms:size(4)), 3)
local preds = torch.repeatTensor(idx, 1, 1, 2):float()
preds[{{}, {}, 1}]:apply(function(x) return (x - 1) % hms:size(4) + 1 end)
preds[{{}, {}, 2}]:add(-1):div(hms:size(3)):floor():add(1)
local predMask = max:gt(0):repeatTensor(1, 1, 2):float()
preds:add(-1):cmul(predMask):add(1)
-- Get transformed coordinates
local preds_tf = torch.zeros(preds:size())
for i = 1,hms:size(1) do -- Number of samples
for j = 1,hms:size(2) do -- Number of output heatmaps for one sample
preds_tf[i][j] = transform(preds[i][j],center,scale,0,hms:size(3),true)
end
end
return preds, preds_tf
end
-------------------------------------------------------------------------------
-- Functions for setting up the demo display
-------------------------------------------------------------------------------
function drawSkeleton(input, hms, coords)
local im = input:clone()
local pairRef = {
{1,2}, {2,3}, {3,7},
{4,5}, {4,7}, {5,6},
{7,9}, {9,10},
{14,9}, {11,12}, {12,13},
{13,9}, {14,15}, {15,16}
}
local partNames = {'RAnk','RKne','RHip','LHip','LKne','LAnk',
'Pelv','Thrx','Neck','Head',
'RWri','RElb','RSho','LSho','LElb','LWri'}
local partColor = {1,1,1,2,2,2,0,0,0,0,3,3,3,4,4,4}
local actThresh = 0.002
-- Loop through adjacent joint pairings
for i = 1,#pairRef do
if hms[pairRef[i][1]]:mean() > actThresh and hms[pairRef[i][2]]:mean() > actThresh then
-- Set appropriate line color
local color
if partColor[pairRef[i][1]] == 1 then color = {0,.3,1}
elseif partColor[pairRef[i][1]] == 2 then color = {1,.3,0}
elseif partColor[pairRef[i][1]] == 3 then color = {0,0,1}
elseif partColor[pairRef[i][1]] == 4 then color = {1,0,0}
else color = {.7,0,.7} end
-- Draw line
im = drawLine(im, coords[pairRef[i][1]], coords[pairRef[i][2]], 4, color, 0)
end
end
return im
end
function drawOutput(input, hms, coords)
local im = drawSkeleton(input, hms, coords)
local colorHms = {}
local inp64 = image.scale(input,64):mul(.3)
for i = 1,16 do
colorHms[i] = colorHM(hms[i])
colorHms[i]:mul(.7):add(inp64)
end
local totalHm = compileImages(colorHms, 4, 4, 64)
im = compileImages({im,totalHm}, 1, 2, 256)
im = image.scale(im,756)
return im
end
-------------------------------------------------------------------------------
-- Functions for evaluation
-------------------------------------------------------------------------------
function calcDists(preds, label, normalize)
local dists = torch.Tensor(preds:size(2), preds:size(1))
local diff = torch.Tensor(2)
for i = 1,preds:size(1) do
for j = 1,preds:size(2) do
if label[i][j][1] > 1 and label[i][j][2] > 1 then
dists[j][i] = torch.dist(label[i][j],preds[i][j])/normalize[i]
else
dists[j][i] = -1
end
end
end
return dists
end
function distAccuracy(dists, thr)
-- Return percentage below threshold while ignoring values with a -1
if not thr then thr = .5 end
if torch.ne(dists,-1):sum() > 0 then
return dists:le(thr):eq(dists:ne(-1)):sum() / dists:ne(-1):sum()
else
return -1
end
end
function displayPCK(dists, part_idx, label, title, show_key)
-- Generate standard PCK plot
if not (type(part_idx) == 'table') then
part_idx = {part_idx}
end
curve_res = 11
num_curves = #dists
local t = torch.linspace(0,.5,curve_res)
local pdj_scores = torch.zeros(num_curves, curve_res)
local plot_args = {}
print(title)
for curve = 1,num_curves do
for i = 1,curve_res do
t[i] = (i-1)*.05
local acc = 0.0
for j = 1,#part_idx do
acc = acc + distAccuracy(dists[curve][part_idx[j]], t[i])
end
pdj_scores[curve][i] = acc / #part_idx
end
plot_args[curve] = {label[curve],t,pdj_scores[curve],'-'}
print(label[curve],pdj_scores[curve][curve_res])
end
require 'gnuplot'
gnuplot.raw('set title "' .. title .. '"')
if not show_key then gnuplot.raw('unset key')
else gnuplot.raw('set key font ",6" right bottom') end
gnuplot.raw('set xrange [0:.5]')
gnuplot.raw('set yrange [0:1]')
gnuplot.plot(unpack(plot_args))
end