61 : inferenceType_(inferenceType), energyType_(energyType), numberOfIterations_(numberOfIterations), trwsTolerance_(0.0) {
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_;
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."));
281 return this->
infer(visitor);
285 template<
class VISITOR>
287 visitor.begin(*
this);
295 mrf_->optimize(1, t);
306 mrf_->optimize(1, t);
317 mrf_->optimize(1, t);
336 arg.resize(
gm_.numberOfVariables());
337 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
347 return mrf_->lowerBound();
355 return mrf_->totalEnergy();
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;
400 std::cout <<
"lambda: " <<
vCue_[0] << std::endl;
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;
433 std::cout <<
"lambda: " <<
vCue_[0] << std::endl;
451 throw(
RuntimeError(
"Energy table has to be symmetric."));
456 throw(
RuntimeError(
"All energy tables have to be equal with respect to a scaling factor."));
459 data_ =
new mrfLib::DataCost(
D_);
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) {
478 D_[i * numLabels_ + k] +=
gm_[gmFactorIndex](&k);
490 for(
IndexType i = 0; i <
gm_.numberOfFactors(gmVariableIndex); i++) {
491 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, i);
492 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
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)) {
521 if((
V_[(l * numLabels_) + m] != 0) && (
gm_[gmFactorIndex](index) != 0)) {
526 if(m != numLabels_) {
530 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
537 if((
V_[(l * numLabels_) + m] != 0) && (
gm_[gmFactorIndex](index) != 0)) {
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++) {
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)) {
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)) {
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)) {
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;
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) {
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)) {
663 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), 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++) {
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++) {
705 for(iter =
mySelfView_->verticalSecondOrderFactorLookupTable_[pix1].begin(); iter !=
mySelfView_->verticalSecondOrderFactorLookupTable_[pix1].end(); iter++) {
710 if(pix1 == pix2 + 1) {
712 for(iter =
mySelfView_->horizontalSecondOrderFactorLookupTable_[pix2].begin(); iter !=
mySelfView_->horizontalSecondOrderFactorLookupTable_[pix2].end(); iter++) {
717 for(iter =
mySelfView_->verticalSecondOrderFactorLookupTable_[pix2].begin(); iter !=
mySelfView_->verticalSecondOrderFactorLookupTable_[pix2].end(); iter++) {
728 if(
gm_.numberOfVariables(i) == 2) {
729 if(
gm_[i].isTruncatedAbsoluteDifference() ==
false) {
740 if(
gm_.numberOfVariables(i) == 2) {
741 if(
gm_[i].isTruncatedSquaredDifference() ==
false) {
756 return gm_[factor](index0)/
gm_[factor](index1);
775 if(
gm_.numberOfVariables(i) == 2) {
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)) {
801 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
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;
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) {
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)) {
874 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
883 }
else if((i != 0) &&
gm_.variableFactorConnection(
grid_(i - 1, j), gmFactorIndex)) {
886 }
else if((j != 0) &&
gm_.variableFactorConnection(
grid_(i, j - 1), gmFactorIndex)) {
911 if(pix2 == pix1 + 1) {
919 if(pix1 == pix2 + 1) {
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.
EnergyType
possible energy types for MRFLIB
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
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
void generateEnergyWeightedTable()
static mrfLib::MRF::CostVal secondOrderFactorTablesAccess(int pix1, int pix2, int i, int j)
InferenceType
possible optimization algorithms for MRFLIB
void generateEnergyView()
mrfLib::MRF::CostVal * hCue_
static mrfLib::MRF::CostVal firstOrderFactorViewAccess(int pix, int i)