-
Notifications
You must be signed in to change notification settings - Fork 9
/
optimizecorresp.cc
595 lines (569 loc) · 27.3 KB
/
optimizecorresp.cc
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
#include <Eigen/Dense>
#include "mex.h"
#include "math.h"
#include <iostream>
#include <omp.h>
#include <vector>
#include "MinIndexedPQ.h"
using namespace std;
using Eigen::Matrix2d;
using Eigen::Vector2d;
using Eigen::VectorXd;
using Eigen::SelfAdjointEigenSolver;
using Eigen::Map;
// To cache the dot products that we need to compute between the
// query image features and the predictor image features. Each
// cachecell is associated with one locaton in the query image
struct CacheCell{
// stores the locations in the predictor image where the
// cache is valid
int xmin;
int ymin;
int xmax;
int ymax;
// The actual dot products.
double* data;
CacheCell():xmin(0),ymin(0),xmax(0),ymax(0),data(NULL){}
};
vector<vector<CacheCell> > cache;
//extern bool mxUnshareArray(const mxArray *pr, const bool noDeepCopy);
mxArray* getfield(mxArray* str, const string& fnam){
int field_num = mxGetFieldNumber(str, fnam.c_str());
return mxGetFieldByNumber(str, 0, field_num);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
// Note that you should disable the parallel loop
// before you enable debugging, or you will get segfaults!
const bool DEBUG=false;
// TODO: The first thing this mex function does is make a copy of its
// input, which is really stupid because the structure of corresp does
// not change, and the old values don't need to be kept. Supposedly
// these lines will let you avoid the copy, but I haven't tested it.
//mxUnshareArray(const_cast<mxarray*> prhs[1],false);
//mxArray* corresp=const_cast<mxarray*>(prhs[1]);
mxArray* corresp=mxDuplicateArray(prhs[1]);
int maxupdates = (int) mxGetScalar(prhs[3]);
const mxArray* hogim = prhs[0];
const int* hogimdims=mxGetDimensions(hogim);
double* hogdata = mxGetPr(hogim);
int ndims=hogimdims[0];
int hogrows=hogimdims[1];
int hogcols=hogimdims[2];
int npyrs=* mxGetDimensions(prhs[2]);
int* correspidx=(int *) mxGetData(prhs[4]);
bool* inferred=(bool*)mxGetData(prhs[5]);
int* inferinds=(int *)mxGetData(prhs[6]);
int ntoinfer=*mxGetDimensions(prhs[6]);
double* confidences = mxGetPr(prhs[7]);
double lambda = mxGetScalar(prhs[8]);
bool deleteCache = mxGetScalar(prhs[9])!=0;
double* numneighbors = mxGetPr(prhs[10]);
double lambdaprime = mxGetScalar(prhs[11]);
if(DEBUG){mexPrintf("lam2 %f\n",lambda);mexEvalString("drawnow;");}
if(DEBUG){mexPrintf("%d %d\n",ntoinfer,npyrs);}
double** transf_out = new double*[npyrs];
int ntransfs=0;
// if deleteCache is 1, we're starting a new image, so clear out all
// the cached dot products.
if(deleteCache){
for(int i = 0; i<cache.size(); ++i){
for(int j=0; j<cache[i].size(); ++j){
delete cache[i][j].data;
}
}
cache.clear();
}
if(cache.size()==0){
for(int i = 0; i<npyrs; ++i){
cache.push_back(vector<CacheCell>(hogrows*hogcols));
}
}
int cachehit=0;
int cachemiss=0;
string errmsg="";
bool error=false;
if(DEBUG){mexPrintf("start main loop over pyramids\n");}
// Each pyramid's correspondence can be inferred in parallel.
#pragma omp parallel for reduction(+:cachehit,cachemiss) shared(error,errmsg)
for(int pyridx=0; pyridx<npyrs; ++pyridx){
// These help us estimate when we need to return,
// first by counting the number of times we've updated a
// mu and sigma pair, and second by seeing how long it's
// been since something moved a lot.
int nupdates=0;
int nsincebigmove=0;
// transf==alpha in the paper. Figure out which cells need to have an
// alpha value inferred before we can start updating mu's and sigmas.
// alpha's aren't kept around between calls to optimizecorresp, so
// when we begin we need to infer an alpha for every cell in the
// condition region.
//
// Likewise, cellheap associates with each cell an estimate of how
// much that cell will change if it's updated. We attempt to update
// first any cell that seems like it will move a lot; this lets us
// focus our computation on the parts of f that make the biggest
// difference.
vector<int> mustcomptransf;
MinIndexedPQ cellheap(hogrows*hogcols);
for(int ti = 0; ti<ntoinfer; ++ti){
mustcomptransf.push_back(inferinds[ti]);
cellheap.insert(inferinds[ti],-1000000);
}
vector<Matrix2d> transfs(hogrows*hogcols);
if(DEBUG){cout<<"start main optimization loop\n";}
// On each iteration of this loop, we update one mu and one sigma.
while(!error){
++nupdates;
++nsincebigmove;
// However, we may need to compute updates for many alphas, especially
// at the beginning.
for(int ti=0; ti<mustcomptransf.size(); ++ti){
int infidx=mustcomptransf[ti];
int xpos=infidx/hogrows;
int ypos=infidx-xpos*hogrows;
Matrix2d xsigma=Matrix2d::Zero();
Matrix2d ysigma=Matrix2d::Zero();
Vector2d xb=Vector2d::Zero();
Vector2d yb=Vector2d::Zero();
// Iterate over the edges of the lattice that this \alpha participates
// in. We aggregate statistics for both vertical and horizontal edges
// simultaneously. xsigma/ysigma aggregate the quadratic term
// of equation 15, and xb/yb aggregate the linear term.
for(int edgex=-2; edgex<=2; ++edgex){
for(int edgey=-2; edgey<=2; ++edgey){
if(edgex>-2&&edgex+xpos-1>=0&&edgex+xpos<hogcols&&edgey+ypos>=0&&edgey+ypos<hogrows &&
inferred[edgey+ypos+hogrows*(edgex+xpos-1)]&&inferred[edgey+ypos+hogrows*(edgex+xpos)]){
mxArray* othcorresp=mxGetCell(corresp,edgey+ypos+(edgex+xpos)*hogrows);
mxArray* othcorresp2=mxGetCell(corresp,edgey+ypos+(edgex+xpos-1)*hogrows);
Map<Vector2d> othmu(mxGetPr(mxGetFieldByNumber(othcorresp, 0, 0))+2*pyridx);
Map<Matrix2d> othcovar(mxGetPr(mxGetFieldByNumber(othcorresp, 0, 1))+4*pyridx);
Map<Vector2d> othmu2(mxGetPr(mxGetFieldByNumber(othcorresp2, 0, 0))+2*pyridx);
Map<Matrix2d> othcovar2(mxGetPr(mxGetFieldByNumber(othcorresp2, 0, 1))+4*pyridx);
xsigma=xsigma+(othcovar.inverse()+othcovar2.inverse());
xb=xb+(othcovar.inverse()+othcovar2.inverse())*(othmu-othmu2);
}
if(edgey>-2&&edgex+xpos>=0&&edgex+xpos<hogcols&&edgey+ypos-1>=0&&edgey+ypos<hogrows &&
inferred[edgey+ypos-1+hogrows*(edgex+xpos)]&&inferred[edgey+ypos+hogrows*(edgex+xpos)]){
mxArray* othcorresp=mxGetCell(corresp,edgey+ypos+(edgex+xpos)*hogrows);
mxArray* othcorresp2=mxGetCell(corresp,edgey+ypos-1+(edgex+xpos)*hogrows);
Map<Vector2d> othmu(mxGetPr(mxGetFieldByNumber(othcorresp, 0, 0))+2*pyridx);
Map<Matrix2d> othcovar(mxGetPr(mxGetFieldByNumber(othcorresp, 0, 1))+4*pyridx);
Map<Vector2d> othmu2(mxGetPr(mxGetFieldByNumber(othcorresp2, 0, 0))+2*pyridx);
Map<Matrix2d> othcovar2(mxGetPr(mxGetFieldByNumber(othcorresp2, 0, 1))+4*pyridx);
ysigma=ysigma+(othcovar.inverse()+othcovar2.inverse());
yb=yb+(othcovar.inverse()+othcovar2.inverse())*(othmu-othmu2);
}
}
}
transfs[ypos+xpos*hogrows].leftCols(1)=(xsigma).colPivHouseholderQr().solve(xb);
transfs[ypos+xpos*hogrows].rightCols(1)=(ysigma).colPivHouseholderQr().solve(yb);
if(DEBUG){cout<<"xsig\n"<<xsigma<<"\nxb\n"<<xb<<"\ntransfs\n"<<transfs[ypos+xpos*hogrows]<<"\n";}
}
mustcomptransf.clear();
if(DEBUG){cout<<"start computing sigma's\n";}
mxArray *mypyr=getfield(mxGetCell(prhs[2],pyridx),"features");
// Get the linear index of the next mu/sigma pair to optimize, and store the estimated
// change.
double heapdist=cellheap.minKey();
int infidx=cellheap.deleteMin();
mxArray* mycorresp=mxGetCell(corresp,infidx);
int edgex=1;
int edgey=0;
int xpos=infidx/hogrows;
int ypos=infidx-xpos*hogrows;
if(DEBUG){mexPrintf("get predictor feats\n");mexEvalString("drawnow;");}
if(DEBUG){mexPrintf("%f\n",mxGetPr(mxGetFieldByNumber(mycorresp,0,2))[pyridx]);}
mxArray* othhogarr=mxGetCell(mypyr,mxGetPr(mxGetFieldByNumber(mycorresp,0,2))[pyridx]-1);
double* othhog=mxGetPr(othhogarr);
const int* othhogdims=mxGetDimensions(othhogarr);
int othrows=othhogdims[1];
int othcols=othhogdims[2];
// relsigma aggregates both Sigma in equation 21 and the outer product in equation 23.
Matrix2d relsigma = Matrix2d::Zero();
// relsigmainv aggregates inv(Sigma) in equation 21.
Matrix2d relsigmainv = Matrix2d::Zero();
if(DEBUG){mexPrintf("iterate over neighbors\n");mexEvalString("drawnow;");}
Map<Vector2d> mymu(mxGetPr(mxGetFieldByNumber(mycorresp, 0, 0))+2*pyridx);
Map<Matrix2d> mycovar(mxGetPr(mxGetFieldByNumber(mycorresp, 0, 1))+4*pyridx);
// We first iterate over edges
for(int edgeidx=0; edgeidx<4; ++edgeidx){
if(DEBUG){mexPrintf("%d %d %d %d %d %d\n",edgex, edgey, xpos, ypos, hogrows, hogcols);}
if(edgex+xpos>=0&&edgex+xpos<hogcols&&edgey+ypos>=0&&edgey+ypos<hogrows &&
inferred[edgey+ypos+hogrows*(edgex+xpos)]){
mxArray* othcorresp=mxGetCell(corresp,edgey+ypos+(edgex+xpos)*hogrows);
Map<Vector2d> othmu(mxGetPr(mxGetFieldByNumber(othcorresp, 0, 0))+2*pyridx);
Map<Matrix2d> othcovar(mxGetPr(mxGetFieldByNumber(othcorresp, 0, 1))+4*pyridx);
Vector2d edge;
edge << edgex, edgey;
Matrix2d relsigmainv2=Matrix2d::Zero();
Matrix2d relsigma2=Matrix2d::Zero();
double n=0.0;
// Then we iterate over the \alpha's that give us constraints over these edges
for(int transfx=max(0,max(xpos,xpos+edgex)-2);transfx<=min(hogcols-1,min(xpos,xpos+edgex)+2); ++transfx){
for(int transfy=max(0,max(ypos,ypos+edgey)-2);transfy<=min(hogrows-1,min(ypos,ypos+edgey)+2); ++transfy){
if(DEBUG){cout << "tx " << transfx << " ty " <<transfy << "\n";}
if(inferred[transfy+hogrows*(transfx)]){
Vector2d mudiff=othmu-mymu-transfs[transfy+(transfx)*hogrows]*edge;
if(DEBUG){cout << "othmu\n" << othmu << "\n" << mymu << "\n" << edge << "\ntransf\n"<<transfs[transfy+(transfx)*hogrows]<<"\n";}
relsigma2=relsigma2+(othcovar+mudiff*mudiff.transpose())*lambdaprime;
relsigmainv2=relsigmainv2+othcovar.inverse()*lambdaprime;
n+=1.0;
}
}
}
// n is very nearly a constant and so in the paper I just rolled into lambdaprime. However,
// near the edge n will get smaller, and so dividing by n will give the warping
// a little boost near the edge where there are fewer alpha's affecting each
// edge.
relsigma=relsigma+relsigma2/n;
relsigmainv=relsigmainv+relsigmainv2/n;
Vector2d mudiff=othmu-mymu-edge;
relsigma=relsigma+(othcovar+mudiff*mudiff.transpose())*lambda;
relsigmainv=relsigmainv+othcovar.inverse()*lambda;
}
// We rotate the edge to get the next edge in the sequence.
int tmp=edgey;
edgey=edgex;
edgex=-tmp;
}
// Next we need to aggregate the unary terms. We actually do an e-step (equation 13) as we
// go along; the values computed on the e-step are not stored.
//
// First we need to compute a small region over which we should aggregate our statistics.
// we use the same major/minor axis trick that we used in contextpredict to get
// the window.
SelfAdjointEigenSolver<Matrix2d> eigensolver(mycovar);
Matrix2d V=eigensolver.eigenvectors().cwiseAbs();
Vector2d D=eigensolver.eigenvalues();
double mycovardet=mycovar.determinant();
if(DEBUG){mexPrintf("computing eigenvalues 1\n");mexEvalString("drawnow;");}
Matrix2d mycovarinv=mycovar.inverse();
double dist=sqrt(fabs(1/(((1/(log(.0001)+log(mycovardet)/2))*(V.block(0,0,2,1).transpose()*mycovarinv*V.block(0,0,2,1)/2))(0))));
double dist2=sqrt(fabs(1/(((1/(log(.0001)+log(mycovardet)/2))*(V.block(0,1,2,1).transpose()*mycovarinv*V.block(0,1,2,1)/2))(0))));
if(DEBUG){mexPrintf("%f %f\n",dist,dist2);}
Vector2d distvec = dist*V.block(0,0,2,1).cwiseMax(dist2*V.block(0,1,2,1));
Map<VectorXd> mydata(hogdata+ndims*ypos+ndims*hogrows*xpos,ndims-1);
double myconst = *(hogdata+ndims*ypos+ndims*hogrows*xpos+ndims-1);
VectorXd mydatatransf=mydata;
double sumprob=0;
Matrix2d sumwt = Matrix2d::Zero();
if(DEBUG){mexPrintf("5\n");mexEvalString("drawnow;");}
//NOTE: window is in 1-indexed coordinates
if(DEBUG){cout << distvec << "\n";}
int xmin=floor(mymu(0)-min(distvec(0),30.0));
int xmax=ceil(mymu(0)+min(distvec(0),30.0));
int ymin=floor(mymu(1)-min(distvec(1),30.0));
int ymax=ceil(mymu(1)+min(distvec(1),30.0));
if(DEBUG){mexPrintf("5.1\n");mexEvalString("drawnow;");}
// Now that we have the window, check to see if we have all the dot
// products in that window cached. We make sure that no more than 1/5
// of the region that we need to compute is hanging outside of the
// region stored in the cache on each side.
CacheCell& mycache=cache[pyridx][hogrows*xpos+ypos];
bool createcache=false;
double* cachedata;
if(DEBUG){mexPrintf("5.2\n");mexEvalString("drawnow;");}
if(mycache.data!=NULL&&mycache.xmin<xmin+(xmax-xmin)/5&&mycache.ymin<ymin+(ymax-ymin)/5&&
mycache.xmax>xmin+((xmax-xmin)*4+4)/5&&mycache.ymax>ymin+((ymax-ymin)*4+4)/5){
++cachehit;
}else{
++cachemiss;
createcache=true;
mycache.xmin=xmin;
mycache.ymin=ymin;
mycache.xmax=xmax;
mycache.ymax=ymax;
if(mycache.data!=NULL){
delete mycache.data;
}
mycache.data=new double[(ymax-ymin+1)*(xmax-xmin+1)];
}
if(DEBUG){mexPrintf("5.3\n");mexEvalString("drawnow;");}
cachedata=mycache.data;
if(DEBUG){mexPrintf("5.4\n");mexEvalString("drawnow;");}
// Loop over the cache or the window, computing the inner products
// and summing the total probability as required in equation 19.
for(int windowx = max(xmin,mycache.xmin); windowx<=min(xmax,mycache.xmax); ++windowx){
for(int windowy = max(ymin,mycache.ymin); windowy<=min(ymax,mycache.ymax); ++windowy){
Vector2d window;
window << windowx,windowy;
Vector2d pt=window-mymu;
double prob=1/(2*M_PI*sqrt(mycovardet))*exp((-pt.transpose()*mycovarinv*pt)(0)/2);
if(prob!=prob){
if(DEBUG){mexPrintf("%d\n",mycovardet);}
errmsg="nan prob";
if(DEBUG){mexErrMsgTxt(errmsg.c_str());}
error=1;
continue;
}
double tmpprob;
if(createcache){
int idx=ndims*(min(othrows-1,max(0,windowy-1))+min(othcols-1,max(0,windowx-1))*othrows);
Map<VectorXd> othdata(othhog+idx,ndims-1);
tmpprob = exp(-(mydatatransf.dot(othdata)+myconst)/2);
tmpprob=tmpprob/(tmpprob+.01*(*(othhog+idx+ndims-1)));
cachedata[(windowx-xmin)*(ymax-ymin+1)+windowy-ymin]=tmpprob;
if(prob!=prob){
if(DEBUG){mexPrintf("%f %f %f %f\n",tmpprob,myconst,mydatatransf.dot(othdata),*(othhog+idx+ndims-1));}
if(DEBUG){cout<<"othdata\n"<<othdata<<"\nmydata\n"<<mydata<<"\n";}
errmsg="nan prob v2";
if(DEBUG){mexErrMsgTxt(errmsg.c_str());}
error=1;
continue;
}
}else{
tmpprob=cachedata[(windowx-mycache.xmin)*(mycache.ymax-mycache.ymin+1)+windowy-mycache.ymin];
}
prob=prob*tmpprob;
sumprob+=prob;
sumwt=sumwt+prob*pt*(pt.transpose());
}
}
if(DEBUG){mexPrintf("6\n");mexEvalString("drawnow;");}
if(DEBUG){mexPrintf("sumwt\n");mexEvalString("drawnow;");}
if(DEBUG){cout << sumwt << "\n" << sumprob << "\n";}
if(DEBUG){mexPrintf("relsigma\n");mexEvalString("drawnow;");}
if(DEBUG){cout << relsigma << "\n";}
if(DEBUG){mexPrintf("sumwt\n");mexEvalString("drawnow;");}
if(DEBUG){cout << sumwt << "\n";}
if(DEBUG){mexPrintf("relsigmainv\n");mexEvalString("drawnow;");}
if(DEBUG){cout << relsigmainv << "\n";}
if(DEBUG){mexPrintf("mycovarinv\n");mexEvalString("drawnow;");}
if(DEBUG){cout << mycovarinv << "\n";}
double myconfidence = confidences[ypos+hogrows*(xpos)];
if(DEBUG){mexPrintf("myconfidence\n");mexEvalString("drawnow;");}
if(DEBUG){cout << myconfidence << "\n";}
// Finally, put it all together
Matrix2d nablasigma=(mycovarinv*(relsigma+sumwt*myconfidence)*mycovarinv-relsigmainv-mycovarinv*sumprob*myconfidence)/2;
double covarstepsz=.1;
if(nablasigma(0)!=nablasigma(0)){
errmsg="nans in nablasigma";
if(DEBUG){mexErrMsgTxt(errmsg.c_str());}
error=1;
continue;
}
// As is often the case with EM algorithms, we can end up with a covariance matrix shrinking to zero and
// causing problems. The right thing to do is to put a constraint on the algorithm that the determinant
// of the inverse covariance must be less than 1, and correctly project onto the constraint set. The wrong
// thing to do is to not take gradient steps when the determinant of the covariance matrix ends up less
// than 1. However, out of sheer laziness I've done the latter. In practice this happens very rarely.
while(-nablasigma(0)*covarstepsz>mycovar(0)||-nablasigma(3)*covarstepsz>mycovar(3)||(mycovar+nablasigma*covarstepsz).determinant()<1){
covarstepsz=covarstepsz*.5;
}
if(DEBUG){mexPrintf("nablasigma\n");mexEvalString("drawnow;");}
if(DEBUG){cout << nablasigma << "\n";}
Matrix2d newcovar=mycovar+nablasigma*covarstepsz;
if(newcovar(0)>1e4||newcovar(4)>1e4){
errmsg="exploding sigma";
if(DEBUG){mexErrMsgTxt(errmsg.c_str());}
error=1;
continue;
}
Matrix2d newcovarinv=newcovar.inverse();
Vector2d constr=Vector2d::Zero();
Matrix2d syst=Matrix2d::Zero();
if(DEBUG){mexPrintf("starting mu update\n");mexEvalString("drawnow;");}
// Just like before, we start off by iterating over edges, this time
// aggregating system of linear equations that's defined by taking the
// derivative of equation 17 (note that this system also needs to have
// some terms from equation 16 added in, but
// we'll add those terms to the system later) and setting it to 0.
//
// We ultimately end up with an equation of the form syst*mu=constr,
// where each term in the sums of equation 17 adds to both syst and constr.
edgex=1;
edgey=0;
for(int edgeidx=0; edgeidx<4; ++edgeidx){
if(DEBUG){mexPrintf("%d %d %d %d %d %d\n",edgex, edgey, xpos, ypos, hogrows, hogcols);mexEvalString("drawnow;");}
if(edgex+xpos>=0&&edgex+xpos<hogcols&&edgey+ypos>=0&&edgey+ypos<hogrows &&
inferred[edgey+ypos+hogrows*(edgex+xpos)]){
mxArray* othcorresp=mxGetCell(corresp,edgey+ypos+(edgex+xpos)*hogrows);
Map<Vector2d> othmu(mxGetPr(mxGetFieldByNumber(othcorresp, 0, 0))+2*pyridx);
Map<Matrix2d> othcovar(mxGetPr(mxGetFieldByNumber(othcorresp, 0, 1))+4*pyridx);
Vector2d edge;
edge << edgex, edgey;
if(DEBUG){mexPrintf("17\n");mexEvalString("drawnow;");}
if(DEBUG){cout << othcovar << "\n";}
if(DEBUG){cout << othmu << "\n";}
Matrix2d othinvcovar=othcovar.inverse();
Matrix2d syst2=Matrix2d::Zero();
Vector2d constr2=Vector2d::Zero();
double n=0.0;
for(int transfx=max(0,max(xpos,xpos+edgex)-2);transfx<=min(hogcols-1,min(xpos,xpos+edgex)+2); ++transfx){
for(int transfy=max(0,max(ypos,ypos+edgey)-2);transfy<=min(hogrows-1,min(ypos,ypos+edgey)+2); ++transfy){
if(inferred[transfy+hogrows*(transfx)]){
Vector2d mudiff=othmu-transfs[transfy+(transfx)*hogrows]*edge;
constr2=constr2+(othinvcovar+newcovarinv)*(mudiff)*lambdaprime;
syst2=syst2+(othinvcovar+newcovarinv)*lambdaprime;
n+=1.0;
}
}
}
// n is very nearly a constant and so in the paper I just rolled it into lambdaprime. However,
// near the edge n will get smaller, and so dividing by n will give the warping
// a little boost near the edge where there are fewer alpha's affecting each
// edge.
constr=constr+constr2/n;
syst=syst+syst2/n;
Vector2d mudiff=othmu-edge;
constr=constr+(othinvcovar+newcovarinv)*(mudiff)*lambda;
syst=syst+(othinvcovar+newcovarinv)*lambda;
}
int tmp=edgey;
edgey=edgex;
edgex=-tmp;
}
if(DEBUG){mexPrintf("11\n");mexEvalString("drawnow;");}
if(DEBUG){cout << constr << "\n";}
if(DEBUG){cout << syst << "\n";}
double newcovardet=newcovar.determinant();
Vector2d mn = Vector2d::Zero();
sumprob=0;
// Now aggregate the contribution from the system from equation 16. Computing
// all the stuff for the e-step (including the window, the cache, etc.) is the
// same as above.
xmin=floor(mymu(0)-min(distvec(0),30.0));
xmax=ceil(mymu(0)+min(distvec(0),30.0));
ymin=floor(mymu(1)-min(distvec(1),30.0));
ymax=ceil(mymu(1)+min(distvec(1),30.0));
mycache=cache[pyridx][hogrows*xpos+ypos];
createcache=false;
if(mycache.xmin<xmin+(xmax-xmin)/5&&mycache.ymin<ymin+(ymax-ymin)/5&&
mycache.xmax>xmin+((xmax-xmin)*4+4)/5&&mycache.ymax>ymin+((ymax-ymin)*4+4)/5){
++cachehit;
}else{
++cachemiss;
createcache=true;
mycache.xmin=xmin;
mycache.ymin=ymin;
mycache.xmax=xmax;
mycache.ymax=ymax;
if(mycache.data!=NULL){
delete mycache.data;
}
mycache.data=new double[(ymax-ymin+1)*(xmax-xmin+1)];
}
cachedata=mycache.data;
for(int windowx = max(xmin,mycache.xmin); windowx<=min(xmax,mycache.xmax); ++windowx){
for(int windowy = max(ymin,mycache.ymin); windowy<=min(ymax,mycache.ymax); ++windowy){
Vector2d window;
window << windowx,windowy;
Vector2d pt=window-mymu;
double prob=1/(2*M_PI*sqrt(newcovardet))*exp((-pt.transpose()*newcovarinv*pt)(0)/2);
double tmpprob;
if(createcache){
int idx=ndims*(min(othrows-1,max(0,windowy-1))+min(othcols-1,max(0,windowx-1))*othrows);
Map<VectorXd> othdata(othhog+idx,ndims-1);
tmpprob = exp(-(mydatatransf.dot(othdata)+myconst)/2);
tmpprob=tmpprob/(tmpprob+.01*(*(othhog+idx+ndims-1)));
cachedata[(windowx-xmin)*(ymax-ymin+1)+windowy-ymin]=tmpprob;
if(prob!=prob){
if(DEBUG){mexPrintf("%f %f %f %f\n",tmpprob,myconst,mydatatransf.dot(othdata),*(othhog+idx+ndims-1));}
if(DEBUG){cout<<"othdata\n"<<othdata<<"\nmydata\n"<<mydata<<"\n";}
errmsg="nan prob v3";
if(DEBUG){mexErrMsgTxt(errmsg.c_str());}
error=1;
continue;
}
}else{
tmpprob=cachedata[(windowx-mycache.xmin)*(mycache.ymax-mycache.ymin+1)+windowy-mycache.ymin];
}
prob=prob*tmpprob;
sumprob+=prob;
mn=mn+prob*pt;
}
}
if(DEBUG){cout << mn << "\n" << mymu << "\n";}
if(DEBUG){cout << sumprob << "\n";}
constr=constr+newcovarinv*(mn+mymu*sumprob)*myconfidence;
syst=syst+newcovarinv*sumprob*myconfidence;
// Now put it all together.
Vector2d newmu=syst.inverse()*constr;
if(DEBUG){mexPrintf("Computed newmu, saving\n");mexEvalString("drawnow;");}
if(DEBUG){mexPrintf("%f\n",infidx);mexEvalString("drawnow;");}
mxArray* newcorresp=mxGetCell(corresp,infidx);
Map<Vector2d> infmu(mxGetPr(mxGetFieldByNumber(newcorresp, 0, 0))+2*pyridx);
Map<Matrix2d> infcovar(mxGetPr(mxGetFieldByNumber(newcorresp, 0, 1))+4*pyridx);
if(DEBUG){cout << newmu << "\n";}
if(DEBUG){cout << newcovar << "\n";}
if(DEBUG){mexPrintf("%d\n",ntransfs);mexEvalString("drawnow;");}
if(newmu(0)!=newmu(0)){
errmsg="nans in newmu";
if(DEBUG){mexErrMsgTxt(errmsg.c_str());}
error=1;
continue;
}
// At this point, actually updating the mu and sigma is finished. All that's left is to
// put this cell back in the queue, and update all the other nodes that it's connected
// to by an edge (since when this node moves, it will probably make its neighbors move).
// In general, we aim to overestimate. so we don't have to be afraid of thinking we're
// converged when we're not.
double diff=(infmu-newmu).norm()*myconfidence;
infmu=newmu;
infcovar=newcovar;
// We guess that, next time, the current node will move as much as it did this time.
cellheap.insert(infidx,-diff);
// Now, for each neighbor, we increment our estimate of how much it will move by the
// amount that this node moved, divided by the number of neighbors that node has.
// This is in general a pretty bad estimate, but it's far better than just sweeping
// over everything.
for(int edge=0; edge<4; ++edge){
if(DEBUG){mexPrintf("%d %d %d %d %d %d\n",edgex, edgey, xpos, ypos, hogrows, hogcols);mexEvalString("drawnow;");}
if(edgex+xpos>=0&&edgex+xpos<hogcols&&edgey+ypos>=0&&edgey+ypos<hogrows &&
inferred[edgey+ypos+hogrows*(edgex+xpos)]){
cellheap.decreaseKey(edgey+ypos+hogrows*(edgex+xpos),cellheap.keyOf(edgey+ypos+hogrows*(edgex+xpos))-diff/numneighbors[edgey+ypos+hogrows*(edgex+xpos)]);
}
int tmp=edgey;
edgey=edgex;
edgex=-tmp;
}
// On the next round, re-infer some alphas. Specifically, only re-infer the alpha
// associated with the current node. There's probably a better way to do this, but
// in practice this seems to work well enough.
mustcomptransf.push_back(infidx);
// If we've run out of computation budget or we don't seem to be
// moving very much anymore, return. Copy the set of computed alpha's
// from the stack to the heap so we can return them.
if(diff>=.1){
nsincebigmove=0;
}
if((nupdates>=maxupdates||nsincebigmove==100)&&heapdist>-100000){
if(nlhs>=1){
transf_out[pyridx]=new double[hogrows*hogcols*4];
++ntransfs;
for(int i=0;i<hogrows*hogcols; ++i){
Map<Matrix2d> transf_out_i(transf_out[pyridx]+4*i);
transf_out_i=transfs[i];
}
}
break;
}
}
}
if(error){
mexErrMsgTxt(errmsg.c_str());
}
// Copy the alpha's from the heap to Matlab.
plhs[0]=corresp;
if(nlhs>=1){
mxArray* tout=mxCreateCellMatrix(npyrs,1);
for(int i=0; i<npyrs; ++i){
int dims[4];
dims[0]=2;
dims[1]=2;
dims[2]=hogrows;
dims[3]=hogcols;
mxArray* data=mxCreateNumericArray(4, dims, mxDOUBLE_CLASS, mxREAL);
double* transf_out2=mxGetPr(data);
for(int j=0; j<dims[2]*dims[3]*4; ++j){
transf_out2[j]=transf_out[i][j];
}
mxSetCell(tout,i,data);
delete transf_out[i];
}
plhs[1]=tout;
}
// Useful for debugging/performance analysis.
mexPrintf("cache hit %d miss %d\n",cachehit,cachemiss);
delete transf_out;
}