OpenGM  2.3.x
Discrete Graphical Model Library
permutable_label_fusion_mover.hxx
Go to the documentation of this file.
1 #ifndef OPENGM_PERMUTABLE_LABEL_FUSION_MOVER_HXX
2 #define OPENGM_PERMUTABLE_LABEL_FUSION_MOVER_HXX
3 
4 
6 #ifdef WITH_CPLEX
9 #endif
11 
12 #if defined(WITH_QPBO) || (defined(WITH_PLANARITY) && defined(WITH_BLOSSOM5))
13 #include <opengm/inference/cgc.hxx>
14 #endif
15 
19 
20 
21 #ifndef NOVIGRA
22 
23 #ifdef WITH_BOOST
24  #ifndef WITH_BOOST_GRAPH
25  #define WITH_BOOST_GRAPH
26  #endif
27 #endif
28 
29 #include <vigra/adjacency_list_graph.hxx>
30 #include <vigra/merge_graph_adaptor.hxx>
31 #include <vigra/hierarchical_clustering.hxx>
32 #include <vigra/priority_queue.hxx>
33 #include <vigra/random.hxx>
34 #include <vigra/graph_algorithms.hxx>
35 
36 #endif
37 
38 namespace opengm{
39 
40 
41 
42 
43 
44 
45 
46  #ifndef NOVIGRA
47  template<class GM, class ACC >
48  class McClusterOp{
49  public:
50  typedef ACC AccumulationType;
51  typedef GM GraphicalModelType;
53 
54  typedef vigra::AdjacencyListGraph Graph;
55  typedef vigra::MergeGraphAdaptor< Graph > MergeGraph;
56 
57 
58  typedef typename MergeGraph::Edge Edge;
59  typedef ValueType WeightType;
60  typedef IndexType index_type;
61  struct Parameter
62  {
63 
64 
65 
67  const float stopWeight = 0.0
68  )
69  :
70  stopWeight_(stopWeight){
71  }
72  float stopWeight_;
73  };
74 
75 
76  McClusterOp(const Graph & graph ,
77  MergeGraph & mergegraph,
78  const Parameter & param,
79  std::vector<ValueType> & weights
80  )
81  :
82  graph_(graph),
83  mergeGraph_(mergegraph),
84  pq_(graph.edgeNum()),
85  param_(param),
86  weights_(weights){
87 
88  for(size_t i=0; i<graph_.edgeNum(); ++i){
89  size_t u = graph_.id(graph_.u(graph_.edgeFromId(i)));
90  size_t v = graph_.id(graph_.v(graph_.edgeFromId(i)));
91  pq_.push(i, weights_[i]);
92  }
93  }
94 
95 
96 
97 
98  void reset(){
99  pq_.reset();
100  }
101 
102 
103 
105  index_type minLabel = pq_.top();
106  while(mergeGraph_.hasEdgeId(minLabel)==false){
107  pq_.deleteItem(minLabel);
108  minLabel = pq_.top();
109  }
110  return Edge(minLabel);
111  }
112 
114  WeightType contractionWeight(){
115  index_type minLabel = pq_.top();
116  while(mergeGraph_.hasEdgeId(minLabel)==false){
117  pq_.deleteItem(minLabel);
118  minLabel = pq_.top();
119  }
120  return pq_.topPriority();
121 
122  }
123 
125  MergeGraph & mergeGraph(){
126  return mergeGraph_;
127  }
128 
129  bool done()const{
130  return pq_.topPriority()<=ValueType(param_.stopWeight_);
131  }
132 
133  void mergeEdges(const Edge & a,const Edge & b){
134  weights_[a.id()]+=weights_[b.id()];
135  pq_.push(a.id(), weights_[a.id()]);
136  pq_.deleteItem(b.id());
137  }
138 
139  void eraseEdge(const Edge & edge){
140  pq_.deleteItem(edge.id());
141  }
142 
143  const Graph & graph_;
144  MergeGraph & mergeGraph_;
145  vigra::ChangeablePriorityQueue< ValueType ,std::greater<ValueType> > pq_;
147  std::vector<ValueType> & weights_;
148  };
149 
150 
151  #endif
152 
153 
154 
155 
156 
157 template<class GM, class ACC>
159 
160 public:
161 
162  typedef GM GraphicalModelType;
163  typedef ACC AccumulationType;
165  typedef std::map<UInt64Type, ValueType> MapType;
166  typedef typename MapType::iterator MapIter;
167  typedef typename MapType::const_iterator MapCIter;
168 
169 
171 
179  ClassicFusion
180  };
181 
182  struct Parameter{
184  const FusionSolver fusionSolver = SelfType::DefaultSolver,
185  const bool planar = false,
186  const std::string workflow = std::string(),
187  const int nThreads = 1,
188  const bool decompose = false,
189  const std::vector<bool> & allowCutsWithin = std::vector<bool>()
190  )
191  :
192  fusionSolver_(fusionSolver),
193  planar_(planar),
194  workflow_(workflow),
195  nThreads_(nThreads),
196  decompose_(decompose),
197  allowCutsWithin_(allowCutsWithin)
198  {
199 
200  }
202  bool planar_;
203  std::string workflow_;
206  std::vector<bool> allowCutsWithin_;
207 
208  };
209 
214 
215 
216  PermutableLabelFusionMove(const GraphicalModelType & gm, const Parameter & param = Parameter())
217  :
218  gm_(gm),
219  param_(param)
220  {
221  if(param_.fusionSolver_ == DefaultSolver){
222 
223  #ifdef WITH_CPLEX
224  param_.fusionSolver_ = MulticutSolver;
225  #endif
226 
227  if(param_.fusionSolver_ == DefaultSolver){
228  #ifdef WITH_QPBO
229  param_.fusionSolver_ = CgcSolver;
230  #endif
231  }
232  if(param_.fusionSolver_ == DefaultSolver){
233  #ifdef WITH_ISINF
234  if(param_.planar_){
235  param_.fusionSolver_ = CgcSolver;
236  }
237  #endif
238  }
239  if(param_.fusionSolver_ == DefaultSolver){
240  #ifndef NOVIGRA
241  if(param_.planar_){
242  param_.fusionSolver_ = HierachicalClusteringSolver;
243  }
244  #endif
245  }
246  if(param_.fusionSolver_ == DefaultSolver){
247  throw RuntimeError("WITH_CPLEX or WITH_QPBO or WITH_ISINF must be enabled");
248  }
249  }
250  }
251 
252 
253 
254  void printArg(const std::vector<LabelType> arg) {
255  const size_t nx = 3; // width of the grid
256  const size_t ny = 3; // height of the grid
257  const size_t numberOfLabels = nx*ny;
258 
259  size_t i=0;
260  for(size_t y = 0; y < ny; ++y){
261 
262  for(size_t x = 0; x < nx; ++x) {
263  std::cout<<arg[i]<<" ";
264  }
265  std::cout<<"\n";
266  ++i;
267  }
268 
269  }
270 
271 
272  size_t intersect(
273  const std::vector<LabelType> & a,
274  const std::vector<LabelType> & b,
275  std::vector<LabelType> & res
276  ){
277  Partition<LabelType> ufd(gm_.numberOfVariables());
278  for(size_t fi=0; fi< gm_.numberOfFactors(); ++fi){
279  if(gm_[fi].numberOfVariables()==2){
280 
281  const size_t vi0 = gm_[fi].variableIndex(0);
282  const size_t vi1 = gm_[fi].variableIndex(1);
283 
284 
285 
286  if(a[vi0] == a[vi1] && b[vi0] == b[vi1]){
287  ufd.merge(vi0, vi1);
288  }
289  }
290  else if(gm_[fi].numberOfVariables()==1){
291 
292  }
293  else{
294  throw RuntimeError("only implemented for second order");
295  }
296  }
297  std::map<LabelType, LabelType> repr;
298  ufd.representativeLabeling(repr);
299  for(size_t vi=0; vi<gm_.numberOfVariables(); ++vi){
300  res[vi]=repr[ufd.find(vi)];
301  }
302  //std::cout<<" A\n";
303  //printArg(a);
304  //std::cout<<" B\n";
305  //printArg(b);
306  //std::cout<<" RES\n";
307  //printArg(res);
308 
309  return ufd.numberOfSets();
310  }
311 
312  bool fuse(
313  const std::vector<LabelType> & a,
314  const std::vector<LabelType> & b,
315  std::vector<LabelType> & res,
316  const ValueType valA,
317  const ValueType valB,
318  ValueType & valRes
319  ){
320 
321  if(param_.fusionSolver_ == ClassicFusion)
322  return fuseClassic(a,b,res,valA,valB,valRes);
323 
324  std::vector<LabelType> ab(gm_.numberOfVariables());
325  IndexType numNewVar = this->intersect(a, b, ab);
326  //std::cout<<"numNewVar "<<numNewVar<<"\n";
327 
328  if(numNewVar==1){
329  return false;
330  }
331 
332  const ValueType intersectedVal = gm_.evaluate(ab);
333 
334 
335 
336  // get the new smaller graph
337 
338 
339  MapType accWeights;
340  size_t erasedEdges = 0;
341  size_t notErasedEdges = 0;
342 
343 
344  LabelType lAA[2]={0, 0};
345  LabelType lAB[2]={0, 1};
346 
347 
348 
349 
350  for(size_t fi=0; fi< gm_.numberOfFactors(); ++fi){
351  if(gm_[fi].numberOfVariables()==2){
352  const size_t vi0 = gm_[fi].variableIndex(0);
353  const size_t vi1 = gm_[fi].variableIndex(1);
354 
355  const size_t cVi0 = ab[vi0] < ab[vi1] ? ab[vi0] : ab[vi1];
356  const size_t cVi1 = ab[vi0] < ab[vi1] ? ab[vi1] : ab[vi0];
357 
358  OPENGM_CHECK_OP(cVi0,<,gm_.numberOfVariables(),"");
359  OPENGM_CHECK_OP(cVi1,<,gm_.numberOfVariables(),"");
360 
361 
362  if(cVi0 == cVi1){
363  ++erasedEdges;
364  }
365  else{
366  ++notErasedEdges;
367 
368  // get the weight
369  ValueType val00 = gm_[fi](lAA);
370  ValueType val01 = gm_[fi](lAB);
371  ValueType weight = val01 - val00;
372 
373  //std::cout<<"vAA"<<val00<<" vAB "<<val01<<" w "<<weight<<"\n";
374 
375  // compute key
376  const UInt64Type key = cVi0 + numNewVar*cVi1;
377 
378  // check if key is in map
379  MapIter iter = accWeights.find(key);
380 
381  // key not yet in map
382  if(iter == accWeights.end()){
383  accWeights[key] = weight;
384  }
385  // key is in map
386  else{
387  iter->second += weight;
388  }
389 
390  }
391 
392  }
393  }
394  OPENGM_CHECK_OP(erasedEdges+notErasedEdges, == , gm_.numberOfFactors(),"something wrong");
395  //std::cout<<"erased edges "<<erasedEdges<<"\n";
396  //std::cout<<"not erased edges "<<notErasedEdges<<"\n";
397  //std::cout<<"LEFT OVER FACTORS "<<accWeights.size()<<"\n";
398 
399 
400 
401  //std::cout<<"INTERSECTED SIZE "<<numNewVar<<"\n";
402 
403  if(param_.fusionSolver_ == CgcSolver){
404  return doMoveCgc(accWeights,ab,numNewVar, a, b, res, valA, valB, valRes);
405  }
406  else if(param_.fusionSolver_ == MulticutSolver){
407  return doMoveMulticut(accWeights,ab,numNewVar, a, b, res, valA, valB, valRes);
408  }
409  else if(param_.fusionSolver_ == MulticutNativeSolver){
410  return doMoveMulticutNative(accWeights,ab,numNewVar, a, b, res, valA, valB, valRes);
411  }
412  else if(param_.fusionSolver_ == HierachicalClusteringSolver){
413  return doMoveHierachicalClustering(accWeights,ab,numNewVar, a, b, res, valA, valB, valRes);
414  }
415  else if(param_.fusionSolver_ == BaseSolver){
416  return doMoveBase(accWeights,ab,numNewVar, a, b, res, valA, valB, valRes);
417  }
418  else{
419  throw RuntimeError("unknown fusionSolver");
420  return false;
421  }
422 
423  }
424 
425 
427  const std::vector<LabelType> & a,
428  const std::vector<LabelType> & b,
429  std::vector<LabelType> & res,
430  const ValueType valA,
431  const ValueType valB,
432  ValueType & valRes
433  ){
434  LabelType maxL = *std::max_element(a.begin(), a.end());
435  std::vector<LabelType> bb = b;
436  for(size_t i=0; i<bb.size(); ++i){
437  bb[i] += maxL;
438  }
440  HlFusionMover<GM, ACC> hlf(gm_,p);
441  hlf.fuse(a,b,res, valA, valB, valRes);
442  // make dense
443  std::map<LabelType, LabelType> mdense;
444 
445  LabelType dl=0;
446  for(size_t i=0;i<res.size(); ++i){
447  const LabelType resL = res[i];
448  if(mdense.find(resL) == mdense.end()){
449  res[i] = dl;
450  ++dl;
451  }
452  else{
453  res[i] = mdense[res[i]];
454  }
455  }
456  valRes = gm_.evaluate(res);
457  if(valRes< std::min(valA,valB)){
458  // make dense
459  std::map<LabelType, LabelType> mdense;
460 
461  LabelType dl=0;
462  for(size_t i=0;i<res.size(); ++i){
463  const LabelType resL = res[i];
464  if(mdense.find(resL) == mdense.end()){
465  res[i] = dl;
466  ++dl;
467  }
468  else{
469  res[i] = mdense[res[i]];
470  }
471  }
472  }
473  else if(valA<valRes){
474  valRes=valA;
475  res = a;
476  }
477  else{
478  valRes=valB;
479  res = b;
480  }
481  }
482 
483  bool fuseMmwc(
484  const std::vector<LabelType> & a,
485  const std::vector<LabelType> & b,
486  std::vector<LabelType> & res,
487  const ValueType valA,
488  const ValueType valB,
489  ValueType & valRes
490  ){
491  std::vector<LabelType> ab(gm_.numberOfVariables());
492  IndexType numNewVar = this->intersect(a, b, ab);
493 
494  if(numNewVar==1){
495  return false;
496  }
497 
498  const ValueType intersectedVal = gm_.evaluate(ab);
499 
500 
501 
502  // get the new smaller graph
503 
504 
505  MapType accWeights;
506  size_t erasedEdges = 0;
507  size_t notErasedEdges = 0;
508 
509 
510  LabelType lAA[2]={0, 0};
511  LabelType lAB[2]={0, 1};
512 
513  size_t ushape[] = { size_t(numNewVar), size_t(gm_.maxNumberOfLabels()) };
514 
515  marray::Marray<ValueType> accUnaries(ushape, ushape+2,0.0);
516 
517  for(size_t fi=0; fi< gm_.numberOfFactors(); ++fi){
518  if(gm_[fi].numberOfVariables()==2){
519  const size_t vi0 = gm_[fi].variableIndex(0);
520  const size_t vi1 = gm_[fi].variableIndex(1);
521 
522  const size_t cVi0 = ab[vi0] < ab[vi1] ? ab[vi0] : ab[vi1];
523  const size_t cVi1 = ab[vi0] < ab[vi1] ? ab[vi1] : ab[vi0];
524 
525  OPENGM_CHECK_OP(cVi0,<,gm_.numberOfVariables(),"");
526  OPENGM_CHECK_OP(cVi1,<,gm_.numberOfVariables(),"");
527 
528 
529  if(cVi0 == cVi1){
530  ++erasedEdges;
531  }
532  else{
533  ++notErasedEdges;
534 
535  // get the weight
536  ValueType val00 = gm_[fi](lAA);
537  ValueType val01 = gm_[fi](lAB);
538  ValueType weight = val01 - val00;
539 
540  //std::cout<<"vAA"<<val00<<" vAB "<<val01<<" w "<<weight<<"\n";
541 
542  // compute key
543  const UInt64Type key = cVi0 + numNewVar*cVi1;
544 
545  // check if key is in map
546  MapIter iter = accWeights.find(key);
547 
548  // key not yet in map
549  if(iter == accWeights.end()){
550  accWeights[key] = weight;
551  }
552  // key is in map
553  else{
554  iter->second += weight;
555  }
556 
557  }
558 
559  }
560  if(gm_[fi].numberOfVariables()==1){
561  const IndexType cVi = ab[gm_[fi].numberOfVariables()];
562  for(LabelType l=0 ; l<ushape[1]; ++l){
563  accUnaries(cVi,l)+=gm_[fi](&l);
564  }
565  }
566  }
567 
568 
569 
570  OPENGM_CHECK_OP(erasedEdges+notErasedEdges, == , gm_.numberOfFactors(),"something wrong");
571 
572 
573 
574 
575  return doMoveMmcw(accWeights,accUnaries,ab,numNewVar, a, b, res, valA, valB, valRes);
576 
577 
578  }
580  const MapType & accWeights,
581  const marray::Marray<ValueType> & accUnaries,
582  const std::vector<LabelType> & ab,
583  const IndexType numNewVar,
584  const std::vector<LabelType> & a,
585  const std::vector<LabelType> & b,
586  std::vector<LabelType> & res,
587  const ValueType valA,
588  const ValueType valB,
589  ValueType & valRes
590  ){
591  // make the actual sub graphical model
592  SubSpace subSpace(numNewVar, 2);
593  SubModel subGm(subSpace);
594 
595  // reserve space
596  subGm. template reserveFunctions<PFunction>(accWeights.size());
597  subGm. template reserveFunctions<EFunction>(numNewVar);
598 
599  subGm.reserveFactors(accWeights.size()+numNewVar);
600  subGm.reserveFactorsVarialbeIndices(accWeights.size()*2+numNewVar);
601  size_t efshape[] = {accUnaries.shape(1)};
602  EFunction ef(efshape,efshape+1);
603 
604  // unaries
605  for(IndexType vi=0; vi<numNewVar; ++vi){
606  for(LabelType l=0; l<accUnaries.shape(1); ++l){
607  ef(&l) = accUnaries(vi, l);
608  }
609  subGm.addFactor(subGm.addFunction(ef), &vi, &vi+1);
610  }
611 
612  // higher order
613  for(MapCIter i = accWeights.begin(); i!=accWeights.end(); ++i){
614  const UInt64Type key = i->first;
615  const ValueType weight = i->second;
616 
617  const UInt64Type cVi1 = key/numNewVar;
618  const UInt64Type cVi0 = key - cVi1*numNewVar;
619  const UInt64Type vis[2] = {cVi0, cVi1};
620 
621  PFunction pf(numNewVar, numNewVar, 0.0, weight);
622  subGm.addFactor(subGm.addFunction(pf), vis, vis+2);
623  }
624 
625  std::vector<LabelType> subArg;
626 
627 
628  //::cout<<"WITH MC\n";
629  typedef Multicut<SubModel, Minimizer> Inf;
630  typedef typename Inf::Parameter Param;
631  Param p(0,0.0);
632 
633  if(param_.nThreads_ <= 0){
634  p.numThreads_ = 0;
635  }
636  else{
637  p.numThreads_ = param_.nThreads_;
638  }
639  p.workFlow_ = param_.workflow_;
640  p.allowCutsWithin_ = param_.allowCutsWithin_;
641 
642 
643  Inf inf(subGm,p);
644  inf.infer();
645 
646  // special arg
647  std::vector<size_t> oarg = inf.getSegmentation();
648  // usual arg
649  inf.arg(subArg);
650 
651  for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
652  res[vi] = oarg[ab[vi]];
653  }
654 
655  ValueType resultVal = subGm.evaluate(subArg);
656  //std::cout<<"gm val inf "<<resultVal<<"\n";
657  // add the weight from the cuts within
658  const LabelType lAB[] = {0,1};
659  for(size_t f=0; f<subGm.numberOfFactors(); ++f){
660 
661  if(gm_.numberOfFactors()==1){
662  IndexType vi0 = subGm[f].variableIndex(0);
663  IndexType vi1 = subGm[f].variableIndex(1);
664 
665  if(subArg[vi0] == subArg[vi1] && oarg[vi0] != oarg[vi1]){
666  resultVal+=gm_[f](lAB);
667  }
668  }
669  }
670  //std::cout<<"mmcw val inf "<<resultVal<<"\n";
671  valRes = resultVal;
672  return true;
673  }
674  bool doMoveCgc(
675  const MapType & accWeights,
676  const std::vector<LabelType> & ab,
677  const IndexType numNewVar,
678  const std::vector<LabelType> & a,
679  const std::vector<LabelType> & b,
680  std::vector<LabelType> & res,
681  const ValueType valA,
682  const ValueType valB,
683  ValueType & valRes
684  ){
685 
686 
687  // make the actual sub graphical model
688  SubSpace subSpace(numNewVar, numNewVar);
689  SubModel subGm(subSpace);
690 
691  // reserve space
692  subGm. template reserveFunctions<PFunction>(accWeights.size());
693  subGm.reserveFactors(accWeights.size());
694  subGm.reserveFactorsVarialbeIndices(accWeights.size()*2);
695 
696  for(MapCIter i = accWeights.begin(); i!=accWeights.end(); ++i){
697  const UInt64Type key = i->first;
698  const ValueType weight = i->second;
699 
700  const UInt64Type cVi1 = key/numNewVar;
701  const UInt64Type cVi0 = key - cVi1*numNewVar;
702  const UInt64Type vis[2] = {cVi0, cVi1};
703 
704  PFunction pf(numNewVar, numNewVar, 0.0, weight);
705  subGm.addFactor(subGm.addFunction(pf), vis, vis+2);
706  }
707 
708  std::vector<LabelType> subArg;
709 
710  //::cout<<"WITH MC\n";
711  typedef CGC<SubModel, Minimizer> Inf;
712  typedef typename Inf::Parameter Param;
713 
714  Param p;
715  p.planar_ = param_.planar_;
716 
717  Inf inf(subGm,p);
718  inf.infer();
719  inf.arg(subArg);
720 
721  for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
722  res[vi] = subArg[ab[vi]];
723  }
724  const ValueType resultVal = subGm.evaluate(subArg);
725  const ValueType projectedResultVal = gm_.evaluate(res);
726  const std::vector<LabelType> & bestArg = valA < valB ? a : b;
727  const ValueType bestProposalVal = valA < valB ? valA : valB;
728 
729  valRes = bestProposalVal < projectedResultVal ? bestProposalVal : projectedResultVal;
730  if(projectedResultVal < bestProposalVal){
731  //for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
732  // res[vi] = subArg[ab[vi]];
733  //}
734  }
735  else{
736  for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
737  res[vi] = bestArg[vi];
738  }
739  }
740  return true;
741 
742  }
743 
745  const MapType & accWeights,
746  const std::vector<LabelType> & ab,
747  const IndexType numNewVar,
748  const std::vector<LabelType> & a,
749  const std::vector<LabelType> & b,
750  std::vector<LabelType> & res,
751  const ValueType valA,
752  const ValueType valB,
753  ValueType & valRes
754  ){
755  const std::vector<LabelType> & bestArg = valA < valB ? a : b;
756  valRes = valA < valB ? valA : valB;
757  for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
758  res[vi] = bestArg[vi];
759  }
760  return true;
761  }
762 
764  const MapType & accWeights,
765  const std::vector<LabelType> & ab,
766  const IndexType numNewVar,
767  const std::vector<LabelType> & a,
768  const std::vector<LabelType> & b,
769  std::vector<LabelType> & res,
770  const ValueType valA,
771  const ValueType valB,
772  ValueType & valRes
773  ){
774  // make the actual sub graphical model
775  SubSpace subSpace(numNewVar, numNewVar);
776  SubModel subGm(subSpace);
777 
778  // reserve space
779  subGm. template reserveFunctions<PFunction>(accWeights.size());
780  subGm.reserveFactors(accWeights.size());
781  subGm.reserveFactorsVarialbeIndices(accWeights.size()*2);
782 
783  for(MapCIter i = accWeights.begin(); i!=accWeights.end(); ++i){
784  const UInt64Type key = i->first;
785  const ValueType weight = i->second;
786 
787  const UInt64Type cVi1 = key/numNewVar;
788  const UInt64Type cVi0 = key - cVi1*numNewVar;
789  const UInt64Type vis[2] = {cVi0, cVi1};
790 
791  PFunction pf(numNewVar, numNewVar, 0.0, weight);
792  subGm.addFactor(subGm.addFunction(pf), vis, vis+2);
793  }
794 
795  std::vector<LabelType> subArg;
796 
797  //try{
798  //::cout<<"WITH MC\n";
799  typedef Multicut<SubModel, Minimizer> McInf;
800  typedef typename McInf::Parameter McParam;
801  McParam pmc(0,0.0);
802 
803  if(param_.nThreads_ <= 0){
804  pmc.numThreads_ = 0;
805  }
806  else{
807  pmc.numThreads_ = param_.nThreads_;
808  }
809  pmc.workFlow_ = param_.workflow_;
810 
811 
812  if(param_.decompose_ == false){
813  McInf inf(subGm,pmc);
814  inf.infer();
815  inf.arg(subArg);
816  }
817  else{
818  typedef DMC<SubModel, McInf> DmcInf;
819  typedef typename DmcInf::Parameter DmcParam;
820  typedef typename DmcInf::InfParam DmcInfParam;
821  DmcParam dmcParam;
822  DmcInfParam dmcInfParam(pmc);
823 
824  dmcParam.infParam_ = dmcInfParam;
825 
826  DmcInf inf(subGm, dmcParam);
827  inf.infer();
828  inf.arg(subArg);
829  }
830  //}
831  //catch(...){
832  // std::cout<<"error from cplex\n....\n";
833  //}
834 
835  for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
836  res[vi] = subArg[ab[vi]];
837  }
838  const ValueType resultVal = subGm.evaluate(subArg);
839  const ValueType projectedResultVal = gm_.evaluate(res);
840  const std::vector<LabelType> & bestArg = valA < valB ? a : b;
841  const ValueType bestProposalVal = valA < valB ? valA : valB;
842 
843  valRes = bestProposalVal < projectedResultVal ? bestProposalVal : projectedResultVal;
844  if(projectedResultVal < bestProposalVal){
845  //for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
846  // res[vi] = subArg[ab[vi]];
847  //}
848  }
849  else{
850  for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
851  res[vi] = bestArg[vi];
852  }
853  }
854  return true;
855  }
856 
857 
859  const MapType & accWeights,
860  const std::vector<LabelType> & ab,
861  const IndexType numNewVar,
862  const std::vector<LabelType> & a,
863  const std::vector<LabelType> & b,
864  std::vector<LabelType> & res,
865  const ValueType valA,
866  const ValueType valB,
867  ValueType & valRes
868  ){
869 
870  std::vector<LabelType> subArg;
871 
872  //::cout<<"WITH MC\n";
873  typedef Multicut<SubModel, Minimizer> Inf;
874  typedef typename Inf::Parameter Param;
875  Param p(0,0.0);
876 
877  if(param_.nThreads_ <= 0){
878  p.numThreads_ = 0;
879  }
880  else{
881  p.numThreads_ = param_.nThreads_;
882  }
883  p.workFlow_ = param_.workflow_;
884 
885  Inf inf(numNewVar, accWeights, p);
886  inf.infer();
887  inf.arg(subArg);
888 
889 
890 
891  for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
892  res[vi] = subArg[ab[vi]];
893  }
894 
895  const ValueType projectedResultVal = gm_.evaluate(res);
896  const std::vector<LabelType> & bestArg = valA < valB ? a : b;
897  const ValueType bestProposalVal = valA < valB ? valA : valB;
898 
899  valRes = bestProposalVal < projectedResultVal ? bestProposalVal : projectedResultVal;
900  if(projectedResultVal < bestProposalVal){
901  //for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
902  // res[vi] = subArg[ab[vi]];
903  //}
904  }
905  else{
906  for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
907  res[vi] = bestArg[vi];
908  }
909  }
910  return true;
911  }
912 
914  const MapType & accWeights,
915  const std::vector<LabelType> & ab,
916  const IndexType numNewVar,
917  const std::vector<LabelType> & a,
918  const std::vector<LabelType> & b,
919  std::vector<LabelType> & res,
920  const ValueType valA,
921  const ValueType valB,
922  ValueType & valRes
923  ){
924  #ifndef NOVIGRA
925  typedef vigra::AdjacencyListGraph Graph;
926  typedef typename Graph::Edge Edge;
927  typedef vigra::MergeGraphAdaptor< Graph > MergeGraph;
928  typedef McClusterOp<GM,ACC> ClusterOp;
929  typedef typename ClusterOp::Parameter ClusterOpParam;
930  typedef vigra::HierarchicalClustering< ClusterOp > HC;
931  typedef typename HC::Parameter HcParam;
932 
933  std::vector<ValueType> weights(accWeights.size(),0.0);
934 
935  Graph graph(numNewVar, accWeights.size());
936  for(MapCIter i = accWeights.begin(); i!=accWeights.end(); ++i){
937  const UInt64Type key = i->first;
938  const ValueType weight = i->second;
939 
940  const UInt64Type cVi1 = key/numNewVar;
941  const UInt64Type cVi0 = key - cVi1*numNewVar;
942 
943  const Edge e = graph.addEdge(cVi0, cVi1);
944  weights[graph.id(e)] = weight;
945  }
946 
947  MergeGraph mg(graph);
948 
949 
950 
951 
952  const ClusterOpParam clusterOpParam;
953  ClusterOp clusterOp(graph, mg, clusterOpParam, weights);
954 
955 
956 
957 
958  HcParam p;
959  HC hc(clusterOp,p);
960 
961  //std::cout<<"start\n";
962  hc.cluster();
963 
964 
965 
966  for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
967  res[vi] = hc.reprNodeId(ab[vi]);
968  }
969 
970  const ValueType projectedResultVal = gm_.evaluate(res);
971  const std::vector<LabelType> & bestArg = valA < valB ? a : b;
972  const ValueType bestProposalVal = valA < valB ? valA : valB;
973 
974  valRes = bestProposalVal < projectedResultVal ? bestProposalVal : projectedResultVal;
975  if(projectedResultVal < bestProposalVal){
976  //for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
977  // res[vi] = subArg[ab[vi]];
978  //}
979  }
980  else{
981  for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
982  res[vi] = bestArg[vi];
983  }
984  }
985  return true;
986  #else
987  throw RuntimeError("needs VIGRA");
988  return false;
989  #endif
990  }
991 
992 private:
993  const GM & gm_;
995 };
996 
997 
998 
999 
1000 
1001 }
1002 
1003 
1004 #endif /* OPENGM_PERMUTABLE_LABEL_FUSION_MOVER_HXX */
IndexType addFactor(const FunctionIdentifier &, ITERATOR, ITERATOR)
add a factor to the graphical model
The OpenGM namespace.
Definition: config.hxx:43
MergeGraph & mergeGraph()
get a reference to the merge
McClusterOp(const Graph &graph, MergeGraph &mergegraph, const Parameter &param, std::vector< ValueType > &weights)
PermutableLabelFusionMove< GM, ACC > SelfType
vigra::AdjacencyListGraph Graph
SimpleDiscreteSpace< IndexType, LabelType > SubSpace
bool doMoveMmcw(const MapType &accWeights, const marray::Marray< ValueType > &accUnaries, const std::vector< LabelType > &ab, const IndexType numNewVar, const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res, const ValueType valA, const ValueType valB, ValueType &valRes)
size_t intersect(const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res)
FunctionIdentifier addFunction(const FUNCTION_TYPE &)
add a function to the graphical model
void merge(value_type, value_type)
Merge two sets.
Definition: partition.hxx:147
const size_t shape(const size_t) const
Get the shape in one dimension.
Definition: marray.hxx:1725
Discrete space in which all variables have the same number of labels.
ValueType evaluate(ITERATOR) const
evaluate the modeled function for a given labeling
void printArg(const std::vector< LabelType > arg)
void reserveFactors(const size_t numF)
bool doMoveHierachicalClustering(const MapType &accWeights, const std::vector< LabelType > &ab, const IndexType numNewVar, const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res, const ValueType valA, const ValueType valB, ValueType &valRes)
detail_types::UInt64Type UInt64Type
uint64
Definition: config.hxx:300
Experimental Multicut.
Definition: cgc.hxx:121
PermutableLabelFusionMove(const GraphicalModelType &gm, const Parameter &param=Parameter())
void mergeEdges(const Edge &a, const Edge &b)
PottsFunction< ValueType, IndexType, LabelType > PFunction
Parameter(const FusionSolver fusionSolver=SelfType::DefaultSolver, const bool planar=false, const std::string workflow=std::string(), const int nThreads=1, const bool decompose=false, const std::vector< bool > &allowCutsWithin=std::vector< bool >())
bool doMoveBase(const MapType &accWeights, const std::vector< LabelType > &ab, const IndexType numNewVar, const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res, const ValueType valA, const ValueType valB, ValueType &valRes)
WeightType contractionWeight()
get the edge weight of the edge which should be contracted next
bool doMoveMulticut(const MapType &accWeights, const std::vector< LabelType > &ab, const IndexType numNewVar, const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res, const ValueType valA, const ValueType valB, ValueType &valRes)
void reserveFactorsVarialbeIndices(const size_t size)
vigra::MergeGraphAdaptor< Graph > MergeGraph
bool doMoveMulticutNative(const MapType &accWeights, const std::vector< LabelType > &ab, const IndexType numNewVar, const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res, const ValueType valA, const ValueType valB, ValueType &valRes)
bool doMoveCgc(const MapType &accWeights, const std::vector< LabelType > &ab, const IndexType numNewVar, const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res, const ValueType valA, const ValueType valB, ValueType &valRes)
bool fuse(const LabelVector &argA, const LabelVector argB, LabelVector &argRes, const ValueType valA, const ValueType valB, ValueType &valRes)
ExplicitFunction< ValueType, IndexType, LabelType > EFunction
std::map< UInt64Type, ValueType > MapType
#define OPENGM_CHECK_OP(A, OP, B, TXT)
Definition: submodel2.hxx:24
Potts function for two variables.
Definition: potts.hxx:19
std::vector< ValueType > & weights_
Disjoint set data structure with path compression.
Definition: partition.hxx:13
bool fuse(const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res, const ValueType valA, const ValueType valB, ValueType &valRes)
bool fuseMmwc(const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res, const ValueType valA, const ValueType valB, ValueType &valRes)
IndexType numberOfFactors() const
GraphicalModel< ValueType, Adder, OPENGM_TYPELIST_2(EFunction, PFunction), SubSpace > SubModel
OpenGM runtime error.
Definition: opengm.hxx:100
bool fuseClassic(const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res, const ValueType valA, const ValueType valB, ValueType &valRes)
vigra::ChangeablePriorityQueue< ValueType,std::greater< ValueType > > pq_