61 : inferenceType_(inferenceType), energyType_(energyType), numberOfIterations_(numberOfIterations), trwsTolerance_(0.0) {
65 MRFLIB(
const GraphicalModelType& gm,
const Parameter& para = Parameter());
69 std::string
name()
const;
72 template<
class VISITOR>
76 typename GM::ValueType
bound()
const;
77 typename GM::ValueType
value()
const;
80 const GraphicalModelType&
gm_;
87 mrfLib::MRF::CostVal*
D_;
88 mrfLib::MRF::CostVal*
V_;
149 : gm_(gm), parameter_(para), mrf_(NULL), D_(NULL), V_(NULL), hCue_(NULL), vCue_(NULL),
150 data_(NULL), smooth_(NULL), energy_(NULL) {
154 throw(
RuntimeError(
"MRFLIB only supports graphical models which have a grid structure."));
162 throw(
RuntimeError(
"MRFLIB only supports graphical models where each variable has the same number of states."));
169 throw(
RuntimeError(
"Singleton policy: MRFLIB only supports one instance with energy type \"VIEW\" at a time."));
177 throw(
RuntimeError(
"Singleton policy: MRFLIB only supports one instance with energy type \"TABLES\" at a time."));
240 mySelfTables_ = NULL;
280 EmptyVisitorType visitor;
281 return this->infer(visitor);
285 template<
class VISITOR>
287 visitor.begin(*
this);
292 if((parameter_.inferenceType_ == Parameter::ICM) || (parameter_.inferenceType_ == Parameter::EXPANSION) || (parameter_.inferenceType_ == Parameter::SWAP)) {
293 for (
size_t i = 0; i <parameter_.numberOfIterations_; i++) {
294 ValueType totalEnergyOld = mrf_->totalEnergy();
295 mrf_->optimize(1, t);
304 }
else if((parameter_.inferenceType_ == Parameter::TRWS) && (parameter_.trwsTolerance_ > 0.0)) {
305 for (
size_t i = 0; i <parameter_.numberOfIterations_; i++) {
306 mrf_->optimize(1, t);
310 if(fabs(mrf_->totalEnergy() - mrf_->lowerBound()) / std::max(fabs(mrf_->totalEnergy()), 1.0) < parameter_.trwsTolerance_) {
316 for (
size_t i = 0; i <parameter_.numberOfIterations_; i++) {
317 mrf_->optimize(1, t);
336 arg.resize( gm_.numberOfVariables());
337 for(
IndexType i = 0; i < gm_.numberOfVariables(); i++) {
338 arg[grid_(i)] = mrf_->getLabel(i);
346 if(parameter_.inferenceType_ == Parameter::TRWS) {
347 return mrf_->lowerBound();
355 return mrf_->totalEnergy();
360 generateFirstOrderFactorLookupTable_();
361 generateSecondOrderFactorLookupTables_();
363 data_ =
new mrfLib::DataCost(firstOrderFactorViewAccess);
364 smooth_ =
new mrfLib::SmoothnessCost(secondOrderFactorViewAccess);
365 energy_ =
new mrfLib::EnergyFunction(data_,smooth_);
372 data_ =
new mrfLib::DataCost(firstOrderFactorTablesAccess);
373 smooth_ =
new mrfLib::SmoothnessCost(secondOrderFactorTablesAccess);
374 energy_ =
new mrfLib::EnergyFunction(data_,smooth_);
382 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
383 if(gm_.numberOfVariables(i) == 2) {
387 std::cout <<
"T: " << t << std::endl;
391 data_ =
new mrfLib::DataCost(D_);
397 std::cout <<
"lambda: " << hCue_[0] << std::endl;
398 smooth_ =
new mrfLib::SmoothnessCost(1, t, hCue_[0]);
400 std::cout <<
"lambda: " << vCue_[0] << std::endl;
401 smooth_ =
new mrfLib::SmoothnessCost(1, t, vCue_[0]);
404 smooth_ =
new mrfLib::SmoothnessCost(1, t, 1.0, hCue_, vCue_);
407 energy_ =
new mrfLib::EnergyFunction(data_,smooth_);
415 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
416 if(gm_.numberOfVariables(i) == 2) {
420 std::cout <<
"T: " << t << std::endl;
424 data_ =
new mrfLib::DataCost(D_);
430 std::cout <<
"lambda: " << hCue_[0] << std::endl;
431 smooth_ =
new mrfLib::SmoothnessCost(2, t, hCue_[0]);
433 std::cout <<
"lambda: " << vCue_[0] << std::endl;
434 smooth_ =
new mrfLib::SmoothnessCost(2, t, vCue_[0]);
437 smooth_ =
new mrfLib::SmoothnessCost(2, t, 1.0, hCue_, vCue_);
440 energy_ =
new mrfLib::EnergyFunction(data_,smooth_);
447 setWeightedTableWeights();
450 if(!symmetricEnergyTable()) {
451 throw(
RuntimeError(
"Energy table has to be symmetric."));
455 if(!sameEnergyTable()) {
456 throw(
RuntimeError(
"All energy tables have to be equal with respect to a scaling factor."));
459 data_ =
new mrfLib::DataCost(D_);
460 smooth_ =
new mrfLib::SmoothnessCost(V_, hCue_, vCue_);
461 energy_ =
new mrfLib::EnergyFunction(data_,smooth_);
467 D_ =
new mrfLib::MRF::CostVal[gm_.numberOfVariables() * numLabels_];
468 for(
IndexType i = 0; i < gm_.numberOfVariables() * numLabels_; i++) {
472 for(
IndexType i = 0; i < gm_.numberOfVariables(); i++) {
474 for(
IndexType j = 0; j < gm_.numberOfFactors(gmVariableIndex); j++) {
475 IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, j);
476 if(gm_.numberOfVariables(gmFactorIndex) == 1) {
477 for(
IndexType k = 0; k < numLabels_; k++) {
478 D_[i * numLabels_ + k] += gm_[gmFactorIndex](&k);
487 V_ =
new mrfLib::MRF::CostVal[numLabels_ * numLabels_];
490 for(
IndexType i = 0; i < gm_.numberOfFactors(gmVariableIndex); i++) {
491 IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, i);
492 if(gm_.numberOfVariables(gmFactorIndex) == 2) {
493 for(
IndexType j = 0; j < numLabels_; j++) {
494 for(
IndexType k = 0; k < numLabels_; k++) {
496 V_[(j * numLabels_) + k] = gm_[gmFactorIndex](index);
505 hCue_ =
new mrfLib::MRF::CostVal[sizeX_ * sizeY_];
506 vCue_ =
new mrfLib::MRF::CostVal[sizeX_ * sizeY_];
511 for(
IndexType k = 0; k < gm_.numberOfFactors(gmVariableIndex); k++) {
512 IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, k);
513 if(gm_.numberOfVariables(gmFactorIndex) == 2) {
514 if((i < sizeX_ - 1) && gm_.variableFactorConnection(grid_(i + 1, j), gmFactorIndex)) {
516 hCue_[i + (j * sizeX_)] = 0;
517 for(
IndexType l = 0; l < numLabels_; l++) {
519 for(m = 0; m < numLabels_; m++) {
521 if((V_[(l * numLabels_) + m] != 0) && (gm_[gmFactorIndex](index) != 0)) {
522 hCue_[i + (j * sizeX_)] = gm_[gmFactorIndex](index) / V_[(l * numLabels_) + m];
526 if(m != numLabels_) {
530 }
else if((j < sizeY_ -1 ) && gm_.variableFactorConnection(grid_(i, j + 1), gmFactorIndex)) {
532 vCue_[i + (j * sizeX_)] = 0;
533 for(
IndexType l = 0; l < numLabels_; l++) {
535 for(m = 0; m < numLabels_; m++) {
537 if((V_[(l * numLabels_) + m] != 0) && (gm_[gmFactorIndex](index) != 0)) {
538 vCue_[i + (j * sizeX_)] = gm_[gmFactorIndex](index) / V_[(l * numLabels_) + m];
542 if(m != numLabels_) {
546 }
else if((i != 0) && gm_.variableFactorConnection(grid_(i - 1, j), gmFactorIndex)) {
548 }
else if((j != 0) && gm_.variableFactorConnection(grid_(i, j - 1), gmFactorIndex)) {
562 for(
IndexType i = 1; i < gm_.numberOfVariables(); i++) {
563 if(gm_.numberOfLabels(i) != numLabels_) {
573 for(
IndexType i = 0; i < sizeX_ - 1; i++) {
574 for(
IndexType j = 0; j < sizeY_ - 1; j++) {
576 for(
IndexType k = 0; k < gm_.numberOfFactors(gmVariableIndex); k++) {
577 IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, k);
578 if(gm_.numberOfVariables(gmFactorIndex) == 2) {
579 if(gm_.variableFactorConnection(grid_(i + 1, j), gmFactorIndex)) {
580 for(
IndexType l = 0; l < numLabels_; l++) {
581 for(
IndexType m = 0; m < numLabels_; m++) {
583 if((fabs(V_[(l * numLabels_) + m] * hCue_[i + (j * sizeX_)]) - gm_[gmFactorIndex](index)) > eps) {
588 }
else if(gm_.variableFactorConnection(grid_(i, j + 1), gmFactorIndex)) {
589 for(
IndexType l = 0; l < numLabels_; l++) {
590 for(
IndexType m = 0; m < numLabels_; m++) {
592 if(fabs((V_[(l * numLabels_) + m] * vCue_[i + (j * sizeX_)]) - gm_[gmFactorIndex](index)) > eps) {
597 }
else if((i != 0) && gm_.variableFactorConnection(grid_(i - 1, j), gmFactorIndex)) {
599 }
else if((j != 0) && gm_.variableFactorConnection(grid_(i, j - 1), gmFactorIndex)) {
614 for (
IndexType i = 0; i < numLabels_; i++) {
615 for (
IndexType j = i; j < numLabels_; j++) {
616 if (V_[(i * numLabels_) + j] != V_[(j * numLabels_) + i]) {
626 std::vector<LabelType> state;
628 if(fabs(value() - gm_.evaluate(state)) < 0.0001){
631 std::cout << value() <<
" "<< gm_.evaluate(state) <<std::endl;
638 firstOrderFactorLookupTable_.resize(gm_.numberOfVariables());
639 for(
IndexType i = 0; i < gm_.numberOfVariables(); i++) {
641 for(
IndexType j = 0; j < gm_.numberOfFactors(gmVariableIndex); j++) {
642 IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, j);
643 if(gm_.numberOfVariables(gmFactorIndex) == 1) {
644 firstOrderFactorLookupTable_[i].push_back(gmFactorIndex);
652 horizontalSecondOrderFactorLookupTable_.resize(gm_.numberOfVariables());
653 verticalSecondOrderFactorLookupTable_.resize(gm_.numberOfVariables());
658 for(
IndexType k = 0; k < gm_.numberOfFactors(gmVariableIndex); k++) {
659 IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, k);
660 if(gm_.numberOfVariables(gmFactorIndex) == 2) {
661 if((i < sizeX_ - 1) && gm_.variableFactorConnection(grid_(i + 1, j), gmFactorIndex)) {
662 horizontalSecondOrderFactorLookupTable_[i + (j * sizeX_)].push_back(gmFactorIndex);
663 }
else if((j < sizeY_ -1 ) && gm_.variableFactorConnection(grid_(i, j + 1), gmFactorIndex)) {
664 verticalSecondOrderFactorLookupTable_[i + (j * sizeX_)].push_back(gmFactorIndex);
665 }
else if((i != 0) && gm_.variableFactorConnection(grid_(i - 1, j), gmFactorIndex)) {
667 }
else if((j != 0) && gm_.variableFactorConnection(grid_(i, j - 1), gmFactorIndex)) {
681 mrfLib::MRF::CostVal result = 0.0;
683 typename std::vector<IndexType>::const_iterator iter;
684 for(iter = mySelfView_->firstOrderFactorLookupTable_[pix].begin(); iter != mySelfView_->firstOrderFactorLookupTable_[pix].end(); iter++) {
685 result += mySelfView_->gm_[*iter](&i);
695 mrfLib::MRF::CostVal result = 0.0;
696 typename std::vector<IndexType>::const_iterator iter;
698 if(pix2 == pix1 + 1) {
700 for(iter = mySelfView_->horizontalSecondOrderFactorLookupTable_[pix1].begin(); iter != mySelfView_->horizontalSecondOrderFactorLookupTable_[pix1].end(); iter++) {
701 result += mySelfView_->gm_[*iter](index);
705 for(iter = mySelfView_->verticalSecondOrderFactorLookupTable_[pix1].begin(); iter != mySelfView_->verticalSecondOrderFactorLookupTable_[pix1].end(); iter++) {
706 result += mySelfView_->gm_[*iter](index);
710 if(pix1 == pix2 + 1) {
712 for(iter = mySelfView_->horizontalSecondOrderFactorLookupTable_[pix2].begin(); iter != mySelfView_->horizontalSecondOrderFactorLookupTable_[pix2].end(); iter++) {
713 result += mySelfView_->gm_[*iter](index);
717 for(iter = mySelfView_->verticalSecondOrderFactorLookupTable_[pix2].begin(); iter != mySelfView_->verticalSecondOrderFactorLookupTable_[pix2].end(); iter++) {
718 result += mySelfView_->gm_[*iter](index);
727 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
728 if(gm_.numberOfVariables(i) == 2) {
729 if(gm_[i].isTruncatedAbsoluteDifference() ==
false) {
739 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
740 if(gm_.numberOfVariables(i) == 2) {
741 if(gm_[i].isTruncatedSquaredDifference() ==
false) {
756 return gm_[factor](index0)/gm_[factor](index1);
774 for(
IndexType i = 0; i < gm_.numberOfFactors(); i++) {
775 if(gm_.numberOfVariables(i) == 2) {
788 hCue_ =
new mrfLib::MRF::CostVal[sizeX_ * sizeY_];
789 vCue_ =
new mrfLib::MRF::CostVal[sizeX_ * sizeY_];
794 for(
IndexType k = 0; k < gm_.numberOfFactors(gmVariableIndex); k++) {
795 IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, k);
796 if(gm_.numberOfVariables(gmFactorIndex) == 2) {
797 if((i < sizeX_ - 1) && gm_.variableFactorConnection(grid_(i + 1, j), gmFactorIndex)) {
800 hCue_[i + (j * sizeX_)] = gm_[gmFactorIndex](index);
801 }
else if((j < sizeY_ -1 ) && gm_.variableFactorConnection(grid_(i, j + 1), gmFactorIndex)) {
804 vCue_[i + (j * sizeX_)] = gm_[gmFactorIndex](index);
805 }
else if((i != 0) && gm_.variableFactorConnection(grid_(i - 1, j), gmFactorIndex)) {
807 }
else if((j != 0) && gm_.variableFactorConnection(grid_(i, j - 1), gmFactorIndex)) {
821 mrfLib::MRF::CostVal lambda;
829 if((i < sizeX_ - 1) && (fabs(hCue_[i + (j * sizeX_)] - lambda) >
OPENGM_FLOAT_TOL)) {
831 }
else if((j < sizeY_ -1 ) && (fabs(vCue_[i + (j * sizeX_)] - lambda) >
OPENGM_FLOAT_TOL)) {
842 firstOrderFactorValues.resize(gm_.numberOfVariables() * numLabels_, 0.0);
843 for(
IndexType i = 0; i < gm_.numberOfVariables(); i++) {
845 for(
IndexType j = 0; j < gm_.numberOfFactors(gmVariableIndex); j++) {
846 IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, j);
847 if(gm_.numberOfVariables(gmFactorIndex) == 1) {
848 for(
IndexType k = 0; k < numLabels_; k++) {
849 firstOrderFactorValues[(i * numLabels_) + k] += gm_[gmFactorIndex](&k);
856 const size_t size = 2 * gm_.numberOfVariables() * numLabels_ * numLabels_;
857 secondOrderFactorValues.resize(size, 0.0);
862 for(
IndexType k = 0; k < gm_.numberOfFactors(gmVariableIndex); k++) {
863 IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, k);
864 if(gm_.numberOfVariables(gmFactorIndex) == 2) {
865 if((i < sizeX_ - 1) && gm_.variableFactorConnection(grid_(i + 1, j), gmFactorIndex)) {
867 for(
IndexType l = 0; l < numLabels_; l++) {
868 for(
IndexType m = 0; m < numLabels_; m++) {
870 IndexType linearIndex = (l * numLabels_) + m;
871 secondOrderFactorValues[((2 * (i + (j * sizeX_))) + down_) * numLabels_ * numLabels_ + linearIndex] += gm_[gmFactorIndex](index);
874 }
else if((j < sizeY_ -1 ) && gm_.variableFactorConnection(grid_(i, j + 1), gmFactorIndex)) {
876 for(
IndexType l = 0; l < numLabels_; l++) {
877 for(
IndexType m = 0; m < numLabels_; m++) {
879 IndexType linearIndex = (l * numLabels_) + m;
880 secondOrderFactorValues[((2 * (i + (j * sizeX_))) + right_) * numLabels_ * numLabels_ + linearIndex] += gm_[gmFactorIndex](index);
883 }
else if((i != 0) && gm_.variableFactorConnection(grid_(i - 1, j), gmFactorIndex)) {
886 }
else if((j != 0) && gm_.variableFactorConnection(grid_(i, j - 1), gmFactorIndex)) {
901 return mySelfTables_->firstOrderFactorValues[(pix * mySelfTables_->numLabels_) + i];
908 IndexType linearIndex = (i * mySelfTables_->numLabels_) + j;
911 if(pix2 == pix1 + 1) {
913 return mySelfTables_->secondOrderFactorValues[((2 * pix1) + mySelfTables_->down_) * mySelfTables_->numLabels_ * mySelfTables_->numLabels_ + linearIndex];
916 return mySelfTables_->secondOrderFactorValues[((2 * pix1) + mySelfTables_->right_) * mySelfTables_->numLabels_ * mySelfTables_->numLabels_ + linearIndex];
919 if(pix1 == pix2 + 1) {
921 return mySelfTables_->secondOrderFactorValues[((2 * pix2) + mySelfTables_->down_) * mySelfTables_->numLabels_ * mySelfTables_->numLabels_ + linearIndex];
924 return mySelfTables_->secondOrderFactorValues[((2 * pix2) + mySelfTables_->right_) * mySelfTables_->numLabels_ * mySelfTables_->numLabels_ + linearIndex];
marray::Matrix< size_t > grid_
bool hasSameLabelNumber() const
std::vector< mrfLib::MRF::CostVal > firstOrderFactorValues
visitors::VerboseVisitor< MRFLIB< GM > > VerboseVisitorType
GM::ValueType value() const
return the solution (value)
double trwsTolerance_
TRWS termintas if fabs(value - bound) / max(fabs(value), 1) < trwsTolerance_.
static MRFLIB< GM > * mySelfTables_
const size_t shape(const size_t) const
Get the shape in one dimension.
MRFLIB MRFLIB inference algorithm class.
ValueType getT(IndexType factor, ValueType e) const
std::vector< std::vector< IndexType > > firstOrderFactorLookupTable_
const GraphicalModelType & gm_
static mrfLib::MRF::CostVal secondOrderFactorViewAccess(int pix1, int pix2, int i, int j)
visitors::EmptyVisitor< MRFLIB< GM > > EmptyVisitorType
#define OPENGM_ASSERT(expression)
std::vector< std::vector< IndexType > > horizontalSecondOrderFactorLookupTable_
mrfLib::SmoothnessCost * smooth_
static MRFLIB< GM > * mySelfView_
void setWeightedTableWeights()
void generateEnergyTables()
bool sameT(ValueType T, ValueType e) const
opengm::Minimizer AccumulationType
GraphicalModelType::IndexType IndexType
std::vector< std::vector< IndexType > > verticalSecondOrderFactorLookupTable_
void generateSecondOrderFactorLookupTables_()
Parameter(const InferenceType inferenceType=ICM, const EnergyType energyType=VIEW, const size_t numberOfIterations=1000)
Constructor.
bool symmetricEnergyTable() const
MRFLIB(const GraphicalModelType &gm, const Parameter ¶=Parameter())
virtual ValueType bound() const
return a bound on the solution
bool truncatedSquaredDifferenceFactors() const
GraphicalModelType::ValueType ValueType
InferenceTermination arg(std::vector< LabelType > &, const size_t &=1) const
Inference algorithm interface.
GM::ValueType bound() const
return a bound on the solution
bool equalWeights() const
bool sameEnergyTable() const
EnergyType
possible energy types for MRFLIB
Iterated Conditional Modes Algorithm J. E. Besag, "On the Statistical Analysis of Dirty Pictures"...
const GraphicalModelType & graphicalModel() const
static const IndexType down_
bool truncatedAbsoluteDifferenceFactors() const
mrfLib::MRF::CostVal * vCue_
EnergyType energyType_
selected energy type
mrfLib::EnergyFunction * energy_
visitors::TimingVisitor< MRFLIB< GM > > TimingVisitorType
static mrfLib::MRF::CostVal firstOrderFactorTablesAccess(int pix, int i)
InferenceType inferenceType_
selected optimization algorithm
mrfLib::MRF::CostVal * V_
InferenceTermination infer()
Minimization as a unary accumulation.
static const IndexType right_
size_t numberOfIterations_
number of iterations
void generateFirstOrderFactorLookupTable_()
static const size_t ContinueInf
mrfLib::MRF::CostVal * D_
std::vector< mrfLib::MRF::CostVal > secondOrderFactorValues
InferenceType
possible optimization algorithms for MRFLIB
void generateEnergyWeightedTable()
static mrfLib::MRF::CostVal secondOrderFactorTablesAccess(int pix1, int pix2, int i, int j)
void generateEnergyView()
mrfLib::MRF::CostVal * hCue_
static mrfLib::MRF::CostVal firstOrderFactorViewAccess(int pix, int i)