-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSADtest_v2.lua
More file actions
254 lines (211 loc) · 6.84 KB
/
Copy pathSADtest_v2.lua
File metadata and controls
254 lines (211 loc) · 6.84 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
-- May 10th 2012
-- EC test on sum of absolute differences (SAD) algorithm
-- test on TLD video dataset: search-region bounded algorithm (more efficient)
-- motocross lost on frame 27 with a 31x31 kernel at y_init,x_init=40,300
require 'nn'
require 'image'
require 'inline'
require 'qt'
require 'qtwidget'
require 'qtuiloader'
-- do everything in single precision
torch.setdefaulttensortype('torch.FloatTensor')
-- some coroutines
local c = {}
-- some defines
inline.preamble [[
#define max(a,b) ((a)>(b) ? (a) : (b))
#define abs(a) (a) < 0 ? -(a) : (a)
#define square(a) (a)*(a)
]]
SADbw = inline.load [[
// get args
const void* id = luaT_checktypename2id(L, "torch.FloatTensor");
THFloatTensor *input = luaT_checkudata(L, 1, id);
THFloatTensor *kernel = luaT_checkudata(L, 2, id);
THFloatTensor *output = luaT_checkudata(L, 3, id);
// get raw pointers
float *input_data = THFloatTensor_data(input);
float *kernel_data = THFloatTensor_data(kernel);
float *output_data = THFloatTensor_data(output);
// dims
int ir = input->size[0];
int ic = input->size[1];
int kr = kernel->size[0];
int kc = kernel->size[1];
int or = (ir - kr) + 1;
int oc = (ic - kc) + 1;
// sum of absolute differences (SAD)
int xx,yy,kx,ky;
for(yy = 0; yy < or; yy++) {
for(xx = 0; xx < oc; xx++) {
/* SAD (input image vs kernel) */
float *pi_ = input_data + yy*ic + xx;
float *pw_ = kernel_data;
float *pw1_ = kernel_data + kr*kc;
float *pw2_ = kernel_data + 2*kr*kc;
float sum = 0;
for(ky = 0; ky < kr; ky++) {
for(kx = 0; kx < kc; kx++) {
sum += abs(pi_[kx]-pw_[kx]);
}
pi_ += ic; /* next input line */
pw_ += kc; /* next mask line */
}
/* Update output */
*output_data++ += sum;
}
}
return 0;
]]
-- function max
function GetMax(a)
x,xi = torch.max(a,1)
y,yi = torch.max(x,2) -- y = value
x_out = yi[1][1] -- y coord
y_out = xi[1][x_out] -- x coord
return y,x_out,y_out
end
-- setup GUI (external UI file)
if not win or not widget then
widget = qtuiloader.load('g.ui')
win = qt.QtLuaPainter(widget.frame)
end
-- profiler
p = xlua.Profiler()
-- input image/test:
--filename = '/Users/eugenioculurciello/AdvancedResearch/SyntheticVision/datasets/TLD/07_motocross/'
--inputim=image.loadPNG(filename..'00001.png',3)
---- initial object/patch location and video frame
--px=300
--py=40
--src_rng=100
--i=1
filename = '/Users/eugenioculurciello/AdvancedResearch/SyntheticVision/datasets/TLD/05_pedestrian3/'
inputim=image.loadJPG(filename..'00001.jpg',3)
-- initial object/patch location and video frame
px=160
py=120
src_rng=50
i=1
-- global linear normalization of input frame
kNorm = torch.ones(5)
m=nn.SpatialSubtractiveNormalization(3,kNorm)
Ninputim = m:forward(inputim)
-- kernel/patch size
fil_r = 31 -- has to be odd sized
fil_c = 31
fil2r = (fil_r-1)/2 -- because of this...
fil2c = (fil_c-1)/2
-- get sizes:
ir = Ninputim:size(2)
ic = Ninputim:size(3)
kr = fil_r
kc = fil_c
our = (ir - kr) + 1
ouc = (ic - kc) + 1
-- search range for SAD maxima/min
if src_rng < fil2r then print("Error: search range src_rng < patch size/2") end
-- inits:
processed=torch.zeros(ir,ic)
outim=torch.zeros(inputim:size())
ker=torch.zeros(inputim:size(1), fil_r, fil_c)
ker=image.crop(Ninputim, px-fil2c, py-fil2r, px+fil2c+1, py+fil2r+1)
-- loop on video frames:
function process()
--inputim=image.loadPNG(string.format(filename..'%0005d', i) .. '.png',3)
inputim=image.loadJPG(string.format(filename..'%0005d', i) .. '.jpg',3)
print('Frame count:'..i)
-- global linear normalization of input frame
kNorm = torch.ones(5) --gaussian = image.gaussian(3,0.15)
m=nn.SpatialSubtractiveNormalization(3,kNorm)
Ninputim = m:forward(inputim)
--ker=image.crop(Ninputim, px-fil2c, py-fil2r, px+fil2c+1, py+fil2r+1) -- test ONLY!!!!
-- SAD: REMEMBER TO INIT OUTput to 0!!!
processed=processed:mul(0)
y1=math.max(py-src_rng,1) -- inputs box: bounded (src_rng) around (px,py)
y2=math.min(py+src_rng,ir)
x1=math.max(px-src_rng,1)
x2=math.min(px+src_rng,ic)
tim=image.crop(Ninputim, x1,y1,x2,y2) -- input patch if input image
nir = tim:size(2)
nic = tim:size(3)
nour = (nir - kr) + 1
nouc = (nic - kc) + 1
ym = y1+fil2r -- outputs box (shrinked from input box)
yM = y2-fil2r-1
xm = x1+fil2c
xM = x2-fil2c-1
total=torch.zeros(nour, nouc) -- outputs
temp1=torch.zeros(nour, nouc)
temp2=torch.zeros(nour, nouc)
temp3=torch.zeros(nour, nouc)
SADbw(tim[1],ker[1],temp1)
SADbw(tim[2],ker[2],temp2)
SADbw(tim[3],ker[3],temp3)
total=temp1+temp2+temp3
total=total/torch.max(total)
total = total:mul(-1):add(1)
processed[{{ym,yM},{xm,xM}}] = total
outim[1]:copy(processed)
outim[2]:copy(processed)
outim[3]:copy(processed)
-- update object/patch location
value, px_nxt, py_nxt = GetMax(total)
--print('SAD output:', value)
px = px_nxt + xm -1
py = py_nxt + ym -1
-- next patch extraction
ker=image.crop(Ninputim, px-fil2c, py-fil2r, px+fil2c+1, py+fil2r+1)
--if i==1 then image.display(total) end
-- continue loop, chose endframe
if i==100 then i=1 else i=i+1 end
-- close program window at a certain frame if needed:
--if i==10 then exit() 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}
image.display{image={inputim,Ninputim,outim},
win=win, --saturation=false, min=0, max=1,
legends={'Input','Normed', 'processed'}}
-- (2) overlay bounding boxes for each detection
win:setcolor(1,0,0)
win:rectangle(px-fil2c, py-fil2r, fil_c, fil_r)
win:rectangle(ic+px-fil2c, py-fil2r, fil_c, fil_r)
win:rectangle(2*ic+px-fil2c, py-fil2r, fil_c, fil_r)
win:stroke()
--win:setfont(qt.QFont{serif=false,italic=false,size=16})
--win:moveto(detect.x, detect.y-1)
--win:show('face')
win:gend()
-- wait for key pressed before moving to next frame:
-- while qt.connect(widget.listener,
-- 'sigMousePress(int,int,QByteArray,QByteArray,QByteArray)',
-- function(...) print("MousePress",...) end )==0 do break end
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')
require 'openmp'
timer:start()
p:lap('full loop')
p:printAll()
end)
widget.windowTitle = 'SAD tracker'
widget:show()
timer:start()