forked from e-lab/clustering-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.lua
executable file
·209 lines (180 loc) · 5.5 KB
/
demo.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
------------------------------------------------------------
-- e-Lab multinet demo
--
-- E. Culurciello, May 2013
--
require 'xlua'
require 'torch'
require 'qt'
require 'qtwidget'
require 'qtuiloader'
require 'inline'
require 'camera'
require 'nnx'
-- parse args
op = xlua.OptionParser('%prog [options]')
op:option{'-c', '--camera', action='store', dest='camidx',
help='camera index: /dev/videoIDX', default=0}
op:option{'-n', '--network', action='store', dest='network',
help='path to existing [trained] network',
default='demo.net'}
opt,args = op:parse()
torch.setdefaulttensortype('torch.FloatTensor')
torch.setnumthreads(4)
-- blob parser
parse = inline.load [[
// get args
const void* id = luaT_checktypename2id(L, "torch.FloatTensor");
THFloatTensor *tensor = luaT_checkudata(L, 1, id);
float threshold = lua_tonumber(L, 2);
int table_blobs = 3;
int idx = lua_objlen(L, 3) + 1;
float scale = lua_tonumber(L, 4);
// loop over pixels
int x,y;
for (y=0; y<tensor->size[0]; y++) {
for (x=0; x<tensor->size[1]; x++) {
float val = THFloatTensor_get2d(tensor, y, x);
if (val > threshold) {
// entry = {}
lua_newtable(L);
int entry = lua_gettop(L);
// entry[1] = x
lua_pushnumber(L, x);
lua_rawseti(L, entry, 1);
// entry[2] = y
lua_pushnumber(L, y);
lua_rawseti(L, entry, 2);
// entry[3] = scale
lua_pushnumber(L, scale);
lua_rawseti(L, entry, 3);
// blobs[idx] = entry; idx = idx + 1
lua_rawseti(L, table_blobs, idx++);
}
}
}
return 0;
]]
-- load pre-trained network from disk
network = nn.Sequential()
network = torch.load(opt.network):float()
network_fov = 46
network_sub = 4
is0 = 15-- gauussian kernel is 15 in training script
-- Preprocessor (normalizer)
normthres = 1e-1
preproc = nn.Sequential()
preproc:add(nn.SpatialColorTransform('rgb2yuv'))
do
ynormer = nn.Sequential()
ynormer:add(nn.Narrow(1,1,1))
ynormer:add(nn.SpatialContrastiveNormalization(1, image.gaussian1D(is0), normthres))
normer = nn.ConcatTable()
normer:add(ynormer)
normer:add(nn.Narrow(1,2,2))
end
preproc:add(normer)
preproc:add(nn.JoinTable(1))
-- setup camera
camera = image.Camera(opt.camidx)
-- process input at multiple scales
scales = {0.3, 0.24, 0.192, 0.15, 0.12}
-- use a pyramid packer/unpacker
require 'PyramidPacker'
require 'PyramidUnPacker'
packer = nn.PyramidPacker(network, scales)
unpacker = nn.PyramidUnPacker(network)
-- setup GUI (external UI file)
if not win or not widget then
widget = qtuiloader.load('g.ui')
win = qt.QtLuaPainter(widget.frame)
end
-- a gaussian for smoothing the distributions
gaussian = image.gaussian(3,0.15)
-- profiler
p = xlua.Profiler()
-- process function
function process()
-- (1) grab frame
frame = camera:forward()
-- (2) transform it into Y space
frameN = preproc(frame)
-- (3) create multiscale pyramid
pyramid, coordinates = packer:forward(frameN)
-- (4) run pre-trained network on it
multiscale = network:forward(pyramid)
-- (5) unpack pyramid
distributions = unpacker:forward(multiscale, coordinates)
-- (6) parse distributions to extract blob centroids
threshold = widget.verticalSlider.value/100
rawresults = {}
for i,distribution in ipairs(distributions) do
local smoothed = image.convolve(distribution[1]:add(1):mul(0.5), gaussian)
parse(smoothed, threshold, rawresults, scales[i])
end
-- (7) clean up results
a=0
k=0
detections = {}
for i,res in ipairs(rawresults) do
local scale = res[3]
local x = res[1]*network_sub/scale
local y = res[2]*network_sub/scale
local w = network_fov/scale
local h = network_fov/scale
--detections[i] = {x=x, y=y, w=w, h=h} -- below is rectangles cleanup from Ayse:s
for m=1, k do
if (detections[m].x<=x) and x<=(detections[m].x+detections[m].w) and (detections[m].y<=y) and (y<=(detections[m].y+detections[m].h)) then
a=1
end
end
for m=1, k do
if (detections[m].x>=x)and (x+w)>=detections[m].x and (detections[m].y>=y) and (y + h)>=detections[m].y then
a=1
end
end
if (a==0) then
k=k+1
detections[k] = {x=x, y=y, w=w, h=h}
end
a=0
end
end
-- display function
function display()
win:gbegin()
win:showpage()
-- (1) display input image + pyramid
image.display{image=frame, win=win, saturation=false, min=0, max=1}
-- (2) overlay bounding boxes for each detection
for i,detect in ipairs(detections) do
win:setcolor(1,0,0)
win:rectangle(detect.x, detect.y, detect.w, detect.h)
win:stroke()
win:setfont(qt.QFont{serif=false,italic=false,size=16})
win:moveto(detect.x, detect.y-1)
win:show('person')
end
win:gend()
end
-- setup gui
timer = qt.QTimer()
timer.interval = 1
timer.singleShot = true
qt.connect(timer,
'timeout()',
function()
p:start('full loop','fps')
p:start('prediction','fps')
process()
p:lap('prediction')
p:start('display','fps')
display()
p:lap('display')
timer:start()
p:lap('full loop')
p:printAll()
end)
widget.windowTitle = 'Multinet person Detector'
widget:show()
timer:start()