52 template<
class _GM,
class _ACC>
72 : inferenceType_(inferenceType), energyType_(energyType), numberOfIterations_(numberOfIterations), trwsTolerance_(0.0) {
76 : inferenceType_(p.inferenceType_), energyType_(p.energyType_), numberOfIterations_(p.numberOfIterations_), trwsTolerance_(p.trwsTolerance_) {
84 std::string
name()
const;
87 template<
class VISITOR>
91 typename GM::ValueType
bound()
const;
92 typename GM::ValueType
value()
const;
95 const GraphicalModelType&
gm_;
102 mrfLib::MRF::CostVal*
D_;
103 mrfLib::MRF::CostVal*
V_;
169 throw(
RuntimeError(
"MRFLIB only supports graphical models which have a grid structure."));
177 throw(
RuntimeError(
"MRFLIB only supports graphical models where each variable has the same number of states."));
184 throw(
RuntimeError(
"Singleton policy: MRFLIB only supports one instance with energy type \"VIEW\" at a time."));
192 throw(
RuntimeError(
"Singleton policy: MRFLIB only supports one instance with energy type \"TABLES\" at a time."));
296 return this->
infer(visitor);
300 template<
class VISITOR>
302 visitor.begin(*
this);
310 mrf_->optimize(1, t);
321 mrf_->optimize(1, t);
332 mrf_->optimize(1, t);
351 arg.resize(
gm_.numberOfVariables());
352 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
362 return mrf_->lowerBound();
370 return mrf_->totalEnergy();
398 if(
gm_.numberOfVariables(i) == 2) {
402 std::cout <<
"T: " << t << std::endl;
406 data_ =
new mrfLib::DataCost(
D_);
412 std::cout <<
"lambda: " <<
hCue_[0] << std::endl;
415 std::cout <<
"lambda: " <<
vCue_[0] << std::endl;
431 if(
gm_.numberOfVariables(i) == 2) {
435 std::cout <<
"T: " << t << std::endl;
439 data_ =
new mrfLib::DataCost(
D_);
445 std::cout <<
"lambda: " <<
hCue_[0] << std::endl;
448 std::cout <<
"lambda: " <<
vCue_[0] << std::endl;
466 throw(
RuntimeError(
"Energy table has to be symmetric."));
471 throw(
RuntimeError(
"All energy tables have to be equal with respect to a scaling factor."));
474 data_ =
new mrfLib::DataCost(
D_);
487 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
489 for(
IndexType j = 0; j <
gm_.numberOfFactors(gmVariableIndex); j++) {
490 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, j);
491 if(
gm_.numberOfVariables(gmFactorIndex) == 1) {
493 D_[i * numLabels_ + k] +=
gm_[gmFactorIndex](&k);
505 for(
IndexType i = 0; i <
gm_.numberOfFactors(gmVariableIndex); i++) {
506 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, i);
507 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
526 for(
IndexType k = 0; k <
gm_.numberOfFactors(gmVariableIndex); k++) {
527 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, k);
528 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
529 if((i < sizeX_ - 1) &&
gm_.variableFactorConnection(
grid_(i + 1, j), gmFactorIndex)) {
536 if((
V_[(l * numLabels_) + m] != 0) && (
gm_[gmFactorIndex](index) != 0)) {
541 if(m != numLabels_) {
545 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
552 if((
V_[(l * numLabels_) + m] != 0) && (
gm_[gmFactorIndex](index) != 0)) {
557 if(m != numLabels_) {
561 }
else if((i != 0) &&
gm_.variableFactorConnection(
grid_(i - 1, j), gmFactorIndex)) {
563 }
else if((j != 0) &&
gm_.variableFactorConnection(
grid_(i, j - 1), gmFactorIndex)) {
577 for(
IndexType i = 1; i <
gm_.numberOfVariables(); i++) {
591 for(
IndexType k = 0; k <
gm_.numberOfFactors(gmVariableIndex); k++) {
592 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, k);
593 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
594 if(
gm_.variableFactorConnection(
grid_(i + 1, j), gmFactorIndex)) {
598 if((fabs(
V_[(l * numLabels_) + m] *
hCue_[i + (j * sizeX_)]) -
gm_[gmFactorIndex](index)) > eps) {
603 }
else if(
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
607 if(fabs((
V_[(l * numLabels_) + m] *
vCue_[i + (j * sizeX_)]) -
gm_[gmFactorIndex](index)) > eps) {
612 }
else if((i != 0) &&
gm_.variableFactorConnection(
grid_(i - 1, j), gmFactorIndex)) {
614 }
else if((j != 0) &&
gm_.variableFactorConnection(
grid_(i, j - 1), gmFactorIndex)) {
631 if (
V_[(i * numLabels_) + j] !=
V_[(j * numLabels_) + i]) {
641 std::vector<LabelType> state;
643 if(fabs(
value() -
gm_.evaluate(state)) < 0.0001){
646 std::cout <<
value() <<
" "<<
gm_.evaluate(state) <<std::endl;
654 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
656 for(
IndexType j = 0; j <
gm_.numberOfFactors(gmVariableIndex); j++) {
657 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, j);
658 if(
gm_.numberOfVariables(gmFactorIndex) == 1) {
673 for(
IndexType k = 0; k <
gm_.numberOfFactors(gmVariableIndex); k++) {
674 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, k);
675 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
676 if((i < sizeX_ - 1) &&
gm_.variableFactorConnection(
grid_(i + 1, j), gmFactorIndex)) {
678 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
680 }
else if((i != 0) &&
gm_.variableFactorConnection(
grid_(i - 1, j), gmFactorIndex)) {
682 }
else if((j != 0) &&
gm_.variableFactorConnection(
grid_(i, j - 1), gmFactorIndex)) {
696 mrfLib::MRF::CostVal result = 0.0;
698 typename std::vector<IndexType>::const_iterator iter;
699 for(iter =
mySelfView_->firstOrderFactorLookupTable_[pix].begin(); iter !=
mySelfView_->firstOrderFactorLookupTable_[pix].end(); iter++) {
710 mrfLib::MRF::CostVal result = 0.0;
711 typename std::vector<IndexType>::const_iterator iter;
713 if(pix2 == pix1 + 1) {
715 for(iter =
mySelfView_->horizontalSecondOrderFactorLookupTable_[pix1].begin(); iter !=
mySelfView_->horizontalSecondOrderFactorLookupTable_[pix1].end(); iter++) {
720 for(iter =
mySelfView_->verticalSecondOrderFactorLookupTable_[pix1].begin(); iter !=
mySelfView_->verticalSecondOrderFactorLookupTable_[pix1].end(); iter++) {
725 if(pix1 == pix2 + 1) {
727 for(iter =
mySelfView_->horizontalSecondOrderFactorLookupTable_[pix2].begin(); iter !=
mySelfView_->horizontalSecondOrderFactorLookupTable_[pix2].end(); iter++) {
732 for(iter =
mySelfView_->verticalSecondOrderFactorLookupTable_[pix2].begin(); iter !=
mySelfView_->verticalSecondOrderFactorLookupTable_[pix2].end(); iter++) {
743 if(
gm_.numberOfVariables(i) == 2) {
744 if(
gm_[i].isTruncatedAbsoluteDifference() ==
false) {
755 if(
gm_.numberOfVariables(i) == 2) {
756 if(
gm_[i].isTruncatedSquaredDifference() ==
false) {
771 return gm_[factor](index0)/
gm_[factor](index1);
790 if(
gm_.numberOfVariables(i) == 2) {
809 for(
IndexType k = 0; k <
gm_.numberOfFactors(gmVariableIndex); k++) {
810 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, k);
811 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
812 if((i < sizeX_ - 1) &&
gm_.variableFactorConnection(
grid_(i + 1, j), gmFactorIndex)) {
816 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
820 }
else if((i != 0) &&
gm_.variableFactorConnection(
grid_(i - 1, j), gmFactorIndex)) {
822 }
else if((j != 0) &&
gm_.variableFactorConnection(
grid_(i, j - 1), gmFactorIndex)) {
836 mrfLib::MRF::CostVal lambda;
858 for(
IndexType i = 0; i <
gm_.numberOfVariables(); i++) {
860 for(
IndexType j = 0; j <
gm_.numberOfFactors(gmVariableIndex); j++) {
861 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, j);
862 if(
gm_.numberOfVariables(gmFactorIndex) == 1) {
877 for(
IndexType k = 0; k <
gm_.numberOfFactors(gmVariableIndex); k++) {
878 IndexType gmFactorIndex =
gm_.factorOfVariable(gmVariableIndex, k);
879 if(
gm_.numberOfVariables(gmFactorIndex) == 2) {
880 if((i < sizeX_ - 1) &&
gm_.variableFactorConnection(
grid_(i + 1, j), gmFactorIndex)) {
889 }
else if((j < sizeY_ -1 ) &&
gm_.variableFactorConnection(
grid_(i, j + 1), gmFactorIndex)) {
898 }
else if((i != 0) &&
gm_.variableFactorConnection(
grid_(i - 1, j), gmFactorIndex)) {
901 }
else if((j != 0) &&
gm_.variableFactorConnection(
grid_(i, j - 1), gmFactorIndex)) {
926 if(pix2 == pix1 + 1) {
934 if(pix1 == pix2 + 1) {
marray::Matrix< size_t > grid_
std::vector< mrfLib::MRF::CostVal > firstOrderFactorValues
InferenceTermination arg(std::vector< LabelType > &, const size_t &=1) const
bool equalWeights() const
visitors::VerboseVisitor< MRFLIB< GM > > VerboseVisitorType
bool hasSameLabelNumber() const
double trwsTolerance_
TRWS termintas if fabs(value - bound) / max(fabs(value), 1) < trwsTolerance_.
static MRFLIB< GM > * mySelfTables_
ValueType getT(IndexType factor, ValueType e) const
EnergyType
possible energy types for MRFLIB
bool symmetricEnergyTable() const
MRFLIB MRFLIB inference algorithm class.
bool sameT(ValueType T, ValueType e) const
std::vector< std::vector< IndexType > > firstOrderFactorLookupTable_
virtual ValueType bound() const
return a bound on the solution
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 truncatedAbsoluteDifferenceFactors() const
GM::ValueType bound() const
return a bound on the solution
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.
MRFLIB(const GraphicalModelType &gm, const Parameter ¶=Parameter())
GraphicalModelType::ValueType ValueType
const GraphicalModelType & graphicalModel() const
Inference algorithm interface.
bool sameEnergyTable() const
Iterated Conditional Modes Algorithm J. E. Besag, "On the Statistical Analysis of Dirty Pictures"...
static const IndexType down_
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)
bool truncatedSquaredDifferenceFactors() const
GM::ValueType value() const
return the solution (value)
InferenceType inferenceType_
selected optimization algorithm
mrfLib::MRF::CostVal * V_
InferenceTermination infer()
const size_t shape(const size_t) const
Get the shape in one dimension.
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)