1 #ifndef OPENGM_PERMUTABLE_LABEL_FUSION_MOVER_HXX 2 #define OPENGM_PERMUTABLE_LABEL_FUSION_MOVER_HXX 12 #if defined(WITH_QPBO) || (defined(WITH_PLANARITY) && defined(WITH_BLOSSOM5)) 24 #ifndef WITH_BOOST_GRAPH 25 #define WITH_BOOST_GRAPH 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> 47 template<
class GM,
class ACC >
54 typedef vigra::AdjacencyListGraph
Graph;
58 typedef typename MergeGraph::Edge
Edge;
67 const float stopWeight = 0.0
77 MergeGraph & mergegraph,
79 std::vector<ValueType> & weights
88 for(
size_t i=0; i<
graph_.edgeNum(); ++i){
105 index_type minLabel =
pq_.top();
107 pq_.deleteItem(minLabel);
108 minLabel =
pq_.top();
110 return Edge(minLabel);
115 index_type minLabel =
pq_.top();
117 pq_.deleteItem(minLabel);
118 minLabel =
pq_.top();
120 return pq_.topPriority();
136 pq_.deleteItem(b.id());
140 pq_.deleteItem(edge.id());
145 vigra::ChangeablePriorityQueue< ValueType ,std::greater<ValueType> >
pq_;
157 template<
class GM,
class ACC>
165 typedef std::map<UInt64Type, ValueType>
MapType;
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>()
192 fusionSolver_(fusionSolver),
196 decompose_(decompose),
197 allowCutsWithin_(allowCutsWithin)
221 if(
param_.fusionSolver_ == DefaultSolver){
224 param_.fusionSolver_ = MulticutSolver;
227 if(
param_.fusionSolver_ == DefaultSolver){
229 param_.fusionSolver_ = CgcSolver;
232 if(
param_.fusionSolver_ == DefaultSolver){
235 param_.fusionSolver_ = CgcSolver;
239 if(
param_.fusionSolver_ == DefaultSolver){
242 param_.fusionSolver_ = HierachicalClusteringSolver;
246 if(
param_.fusionSolver_ == DefaultSolver){
247 throw RuntimeError(
"WITH_CPLEX or WITH_QPBO or WITH_ISINF must be enabled");
257 const size_t numberOfLabels = nx*ny;
260 for(
size_t y = 0; y < ny; ++y){
262 for(
size_t x = 0; x < nx; ++x) {
263 std::cout<<arg[i]<<
" ";
273 const std::vector<LabelType> & a,
274 const std::vector<LabelType> & b,
275 std::vector<LabelType> & res
278 for(
size_t fi=0; fi< gm_.numberOfFactors(); ++fi){
279 if(gm_[fi].numberOfVariables()==2){
281 const size_t vi0 = gm_[fi].variableIndex(0);
282 const size_t vi1 = gm_[fi].variableIndex(1);
286 if(a[vi0] == a[vi1] && b[vi0] == b[vi1]){
290 else if(gm_[fi].numberOfVariables()==1){
294 throw RuntimeError(
"only implemented for second order");
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)];
309 return ufd.numberOfSets();
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,
321 if(
param_.fusionSolver_ == ClassicFusion)
322 return fuseClassic(a,b,res,valA,valB,valRes);
324 std::vector<LabelType> ab(gm_.numberOfVariables());
325 IndexType numNewVar = this->intersect(a, b, ab);
332 const ValueType intersectedVal = gm_.evaluate(ab);
340 size_t erasedEdges = 0;
341 size_t notErasedEdges = 0;
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);
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];
369 ValueType val00 = gm_[fi](lAA);
370 ValueType val01 = gm_[fi](lAB);
371 ValueType weight = val01 - val00;
379 MapIter iter = accWeights.find(key);
382 if(iter == accWeights.end()){
383 accWeights[key] = weight;
387 iter->second += weight;
394 OPENGM_CHECK_OP(erasedEdges+notErasedEdges, == , gm_.numberOfFactors(),
"something wrong");
403 if(
param_.fusionSolver_ == CgcSolver){
404 return doMoveCgc(accWeights,ab,numNewVar, a, b, res, valA, valB, valRes);
406 else if(
param_.fusionSolver_ == MulticutSolver){
407 return doMoveMulticut(accWeights,ab,numNewVar, a, b, res, valA, valB, valRes);
409 else if(
param_.fusionSolver_ == MulticutNativeSolver){
410 return doMoveMulticutNative(accWeights,ab,numNewVar, a, b, res, valA, valB, valRes);
412 else if(
param_.fusionSolver_ == HierachicalClusteringSolver){
413 return doMoveHierachicalClustering(accWeights,ab,numNewVar, a, b, res, valA, valB, valRes);
415 else if(
param_.fusionSolver_ == BaseSolver){
416 return doMoveBase(accWeights,ab,numNewVar, a, b, res, valA, valB, valRes);
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,
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){
441 hlf.
fuse(a,b,res, valA, valB, valRes);
443 std::map<LabelType, LabelType> mdense;
446 for(
size_t i=0;i<res.size(); ++i){
448 if(mdense.find(resL) == mdense.end()){
453 res[i] = mdense[res[i]];
456 valRes = gm_.evaluate(res);
457 if(valRes< std::min(valA,valB)){
459 std::map<LabelType, LabelType> mdense;
462 for(
size_t i=0;i<res.size(); ++i){
464 if(mdense.find(resL) == mdense.end()){
469 res[i] = mdense[res[i]];
473 else if(valA<valRes){
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,
491 std::vector<LabelType> ab(gm_.numberOfVariables());
492 IndexType numNewVar = this->intersect(a, b, ab);
498 const ValueType intersectedVal = gm_.evaluate(ab);
506 size_t erasedEdges = 0;
507 size_t notErasedEdges = 0;
513 size_t ushape[] = {
size_t(numNewVar),
size_t(gm_.maxNumberOfLabels()) };
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);
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];
536 ValueType val00 = gm_[fi](lAA);
537 ValueType val01 = gm_[fi](lAB);
538 ValueType weight = val01 - val00;
546 MapIter iter = accWeights.find(key);
549 if(iter == accWeights.end()){
550 accWeights[key] = weight;
554 iter->second += weight;
560 if(gm_[fi].numberOfVariables()==1){
561 const IndexType cVi = ab[gm_[fi].numberOfVariables()];
563 accUnaries(cVi,l)+=gm_[fi](&l);
570 OPENGM_CHECK_OP(erasedEdges+notErasedEdges, == , gm_.numberOfFactors(),
"something wrong");
575 return doMoveMmcw(accWeights,accUnaries,ab,numNewVar, a, b, res, valA, valB, valRes);
580 const MapType & accWeights,
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,
592 SubSpace subSpace(numNewVar, 2);
593 SubModel subGm(subSpace);
596 subGm.
template reserveFunctions<PFunction>(accWeights.size());
597 subGm.
template reserveFunctions<EFunction>(numNewVar);
601 size_t efshape[] = {accUnaries.
shape(1)};
602 EFunction ef(efshape,efshape+1);
605 for(IndexType vi=0; vi<numNewVar; ++vi){
607 ef(&l) = accUnaries(vi, l);
613 for(MapCIter i = accWeights.begin(); i!=accWeights.end(); ++i){
615 const ValueType weight = i->second;
621 PFunction pf(numNewVar, numNewVar, 0.0, weight);
625 std::vector<LabelType> subArg;
630 typedef typename Inf::Parameter Param;
633 if(
param_.nThreads_ <= 0){
637 p.numThreads_ =
param_.nThreads_;
639 p.workFlow_ =
param_.workflow_;
640 p.allowCutsWithin_ =
param_.allowCutsWithin_;
647 std::vector<size_t> oarg = inf.getSegmentation();
651 for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
652 res[vi] = oarg[ab[vi]];
655 ValueType resultVal = subGm.
evaluate(subArg);
661 if(gm_.numberOfFactors()==1){
662 IndexType vi0 = subGm[f].variableIndex(0);
663 IndexType vi1 = subGm[f].variableIndex(1);
665 if(subArg[vi0] == subArg[vi1] && oarg[vi0] != oarg[vi1]){
666 resultVal+=gm_[f](lAB);
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,
688 SubSpace subSpace(numNewVar, numNewVar);
689 SubModel subGm(subSpace);
692 subGm.
template reserveFunctions<PFunction>(accWeights.size());
696 for(MapCIter i = accWeights.begin(); i!=accWeights.end(); ++i){
698 const ValueType weight = i->second;
704 PFunction pf(numNewVar, numNewVar, 0.0, weight);
708 std::vector<LabelType> subArg;
712 typedef typename Inf::Parameter Param;
715 p.planar_ =
param_.planar_;
721 for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
722 res[vi] = subArg[ab[vi]];
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;
729 valRes = bestProposalVal < projectedResultVal ? bestProposalVal : projectedResultVal;
730 if(projectedResultVal < bestProposalVal){
736 for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
737 res[vi] = bestArg[vi];
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,
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];
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,
775 SubSpace subSpace(numNewVar, numNewVar);
776 SubModel subGm(subSpace);
779 subGm.
template reserveFunctions<PFunction>(accWeights.size());
783 for(MapCIter i = accWeights.begin(); i!=accWeights.end(); ++i){
785 const ValueType weight = i->second;
791 PFunction pf(numNewVar, numNewVar, 0.0, weight);
795 std::vector<LabelType> subArg;
800 typedef typename McInf::Parameter McParam;
803 if(
param_.nThreads_ <= 0){
807 pmc.numThreads_ =
param_.nThreads_;
809 pmc.workFlow_ =
param_.workflow_;
812 if(
param_.decompose_ ==
false){
813 McInf inf(subGm,pmc);
819 typedef typename DmcInf::Parameter DmcParam;
820 typedef typename DmcInf::InfParam DmcInfParam;
822 DmcInfParam dmcInfParam(pmc);
824 dmcParam.infParam_ = dmcInfParam;
826 DmcInf inf(subGm, dmcParam);
835 for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
836 res[vi] = subArg[ab[vi]];
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;
843 valRes = bestProposalVal < projectedResultVal ? bestProposalVal : projectedResultVal;
844 if(projectedResultVal < bestProposalVal){
850 for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
851 res[vi] = bestArg[vi];
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,
870 std::vector<LabelType> subArg;
874 typedef typename Inf::Parameter Param;
877 if(
param_.nThreads_ <= 0){
881 p.numThreads_ =
param_.nThreads_;
883 p.workFlow_ =
param_.workflow_;
885 Inf inf(numNewVar, accWeights, p);
891 for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
892 res[vi] = subArg[ab[vi]];
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;
899 valRes = bestProposalVal < projectedResultVal ? bestProposalVal : projectedResultVal;
900 if(projectedResultVal < bestProposalVal){
906 for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
907 res[vi] = bestArg[vi];
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,
925 typedef vigra::AdjacencyListGraph
Graph;
926 typedef typename Graph::Edge
Edge;
927 typedef vigra::MergeGraphAdaptor< Graph >
MergeGraph;
929 typedef typename ClusterOp::Parameter ClusterOpParam;
930 typedef vigra::HierarchicalClustering< ClusterOp > HC;
931 typedef typename HC::Parameter HcParam;
933 std::vector<ValueType> weights(accWeights.size(),0.0);
935 Graph graph(numNewVar, accWeights.size());
936 for(MapCIter i = accWeights.begin(); i!=accWeights.end(); ++i){
938 const ValueType weight = i->second;
943 const Edge e = graph.addEdge(cVi0, cVi1);
944 weights[graph.id(e)] = weight;
947 MergeGraph mg(graph);
952 const ClusterOpParam clusterOpParam;
953 ClusterOp clusterOp(graph, mg, clusterOpParam, weights);
966 for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
967 res[vi] = hc.reprNodeId(ab[vi]);
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;
974 valRes = bestProposalVal < projectedResultVal ? bestProposalVal : projectedResultVal;
975 if(projectedResultVal < bestProposalVal){
981 for(IndexType vi=0; vi<gm_.numberOfVariables(); ++vi){
982 res[vi] = bestArg[vi];
IndexType addFactor(const FunctionIdentifier &, ITERATOR, ITERATOR)
add a factor to the graphical model
MergeGraph & mergeGraph()
get a reference to the merge
McClusterOp(const Graph &graph, MergeGraph &mergegraph, const Parameter ¶m, 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.
FusionSolver fusionSolver_
const size_t shape(const size_t) const
Get the shape in one dimension.
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)
std::vector< bool > allowCutsWithin_
detail_types::UInt64Type UInt64Type
uint64
MapType::const_iterator MapCIter
PermutableLabelFusionMove(const GraphicalModelType &gm, const Parameter ¶m=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)
Potts function for two variables.
std::vector< ValueType > & weights_
MapType::iterator MapIter
Disjoint set data structure with path compression.
bool fuse(const std::vector< LabelType > &a, const std::vector< LabelType > &b, std::vector< LabelType > &res, const ValueType valA, const ValueType valB, ValueType &valRes)
void eraseEdge(const Edge &edge)
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
Parameter(const float stopWeight=0.0)
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_