/usr/include/opengm/inference/external/gco.hxx is in libopengm-dev 2.3.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 | #ifndef GCO_HXX_
#define GCO_HXX_
#include <map>
#include "opengm/inference/inference.hxx"
#include "opengm/graphicalmodel/graphicalmodel.hxx"
#include "opengm/operations/minimizer.hxx"
#include "opengm/inference/inference.hxx"
#include "opengm/inference/visitors/visitors.hxx"
#include "GCoptimization.h"
namespace opengm {
namespace external {
/// GCOLIB
/// GCOLIB inference algorithm class
/// \ingroup inference
/// \ingroup external_inference
///
// GCOLIB
/// - cite :[?]
/// - Maximum factor order :2
/// - Maximum number of labels : \f$\infty\f$
/// - Restrictions : ?
/// - Convergent : ?
template<class GM>
class GCOLIB : public Inference<GM, opengm::Minimizer> {
public:
typedef GM GraphicalModelType;
typedef opengm::Minimizer AccumulationType;
OPENGM_GM_TYPE_TYPEDEFS;
typedef visitors::VerboseVisitor<GCOLIB<GM> > VerboseVisitorType;
typedef visitors::EmptyVisitor<GCOLIB<GM> > EmptyVisitorType;
typedef visitors::TimingVisitor<GCOLIB<GM> > TimingVisitorType;
///Parameter
struct Parameter {
/// possible optimization algorithms for GCOLIB
enum InferenceType {EXPANSION, SWAP};
/// possible energy types for GCOLIB
enum EnergyType {VIEW, TABLES, WEIGHTEDTABLE};
/// selected optimization algorithm
InferenceType inferenceType_;
/// selected energy type
EnergyType energyType_;
/// number of iterations
size_t numberOfIterations_;
/// Enable random label order. By default, the labels for the swap and expansion algorithms are visited in not random order, but random label visitation might give better results.
bool randomLabelOrder_;
/// Use adaptive cycles for alpha-expansion
bool useAdaptiveCycles_;
/// Do not use grid structure
bool doNotUseGrid_;
Parameter(const InferenceType inferenceType = EXPANSION, const EnergyType energyType = VIEW, const size_t numberOfIterations = 1000)
: inferenceType_(inferenceType), energyType_(energyType), numberOfIterations_(numberOfIterations), randomLabelOrder_(false), useAdaptiveCycles_(false), doNotUseGrid_(false) {
}
};
// construction
GCOLIB(const GraphicalModelType& gm, const Parameter& para);
// destruction
~GCOLIB();
// query
std::string name() const;
const GraphicalModelType& graphicalModel() const;
// inference
template<class VISITOR>
InferenceTermination infer(VISITOR & visitor);
InferenceTermination infer();
InferenceTermination arg(std::vector<LabelType>&, const size_t& = 1) const;
typename GM::ValueType bound() const;
typename GM::ValueType value() const;
protected:
typedef gcoLib::GCoptimization::EnergyTermType EnergyTermType;
const GraphicalModelType& gm_;
Parameter parameter_;
bool isGrid_;
IndexType sizeX_;
IndexType sizeY_;
const IndexType numNodes_;
const LabelType numLabels_;
marray::Matrix<size_t> grid_;
gcoLib::GCoptimizationGeneralGraph* GCOGeneralGraph_;
gcoLib::GCoptimizationGridGraph* GCOGridGraph_;
// required for energy type weighted table
void generateEnergyWeightedTable();
EnergyTermType* D_;
EnergyTermType* V_;
EnergyTermType* hCue_;
EnergyTermType* vCue_;
void setD();
void setV();
void setWeightedTableWeights();
bool hasSameLabelNumber() const;
bool sameEnergyTable() const;
bool symmetricEnergyTable() const;
// required for energy type view
void generateEnergyView();
static GCOLIB<GM>* mySelfView_;
std::vector<std::vector<IndexType> > firstOrderFactorLookupTable_;
std::vector<std::vector<IndexType> > horizontalSecondOrderFactorLookupTable_;
std::vector<std::vector<IndexType> > verticalSecondOrderFactorLookupTable_;
std::map<std::pair<IndexType, IndexType>, std::vector<IndexType> > generalSecondOrderFactorLookupTable_;
void generateFirstOrderFactorLookupTable();
void generateSecondOrderFactorLookupTables();
static EnergyTermType firstOrderFactorViewAccess(int pix, int i);
static EnergyTermType secondOrderFactorViewGridAccess(int pix1, int pix2, int i, int j);
// uses a std::map as lookup table ==> no constant access time.
static EnergyTermType secondOrderFactorViewGeneralAccess(int pix1, int pix2, int i, int j);
// required for energy type tables
// only supported if graphical model is a grid.
// A general graphical model would require to much memory to allocate all tables.
void generateEnergyTables();
static GCOLIB<GM>* mySelfTables_;
std::vector<EnergyTermType> firstOrderFactorValues;
std::vector<EnergyTermType> secondOrderFactorGridValues;
static const IndexType right_ = 0;
static const IndexType down_ = 1;
void copyFactorValues();
static EnergyTermType firstOrderFactorTablesAccess(int pix, int i);
static EnergyTermType secondOrderFactorTablesGridAccess(int pix1, int pix2, int i, int j);
bool valueCheck() const;
};
template<class GM>
GCOLIB<GM>* GCOLIB<GM>::mySelfView_ = NULL;
template<class GM>
GCOLIB<GM>* GCOLIB<GM>::mySelfTables_ = NULL;
template<class GM>
GCOLIB<GM>::GCOLIB(const typename GCOLIB::GraphicalModelType& gm, const Parameter& para)
: gm_(gm), parameter_(para), numNodes_(gm_.numberOfVariables()), numLabels_(gm_.numberOfLabels(0)), GCOGeneralGraph_(NULL),
GCOGridGraph_(NULL), D_(NULL), V_(NULL), hCue_(NULL), vCue_(NULL) {
// check label number
if(!hasSameLabelNumber()) {
throw(RuntimeError("GCOLIB only supports graphical models where each variable has the same number of states."));
}
// check for grid structure
if(para.doNotUseGrid_){
isGrid_ = false;
}
else{
isGrid_ = gm_.isGrid(grid_);
}
// create graph
if(isGrid_) {
std::cout <<"GRID"<<std::endl;
sizeX_ = grid_.shape(0);
sizeY_ = grid_.shape(1);
GCOGridGraph_ = new gcoLib::GCoptimizationGridGraph(sizeX_, sizeY_, numLabels_);
GCOGridGraph_->setLabelOrder(parameter_.randomLabelOrder_);
} else {
std::cout <<"NO GRID"<<std::endl;
GCOGeneralGraph_ = new gcoLib::GCoptimizationGeneralGraph(numNodes_, numLabels_);
GCOGeneralGraph_->setLabelOrder(parameter_.randomLabelOrder_);
}
// generate energy function
switch(parameter_.energyType_) {
case Parameter::VIEW: {
if(mySelfView_ != NULL) {
throw(RuntimeError("Singleton policy: GCOLIB only supports one instance with energy type \"VIEW\" at a time."));
}
mySelfView_ = this;
generateEnergyView();
break;
}
case Parameter::TABLES: {
if(!isGrid_) {
throw(RuntimeError("GCOLIB only supports energy type \"TABLES\" if model is a grid."));
}
if(mySelfTables_ != NULL) {
throw(RuntimeError("Singleton policy: GCOLIB only supports one instance with energy type \"TABLES\" at a time."));
}
mySelfTables_ = this;
generateEnergyTables();
break;
}
case Parameter::WEIGHTEDTABLE: {
if(!isGrid_) {
throw(RuntimeError("GCOLIB only supports energy type \"WEIGHTEDTABLE\" if model is a grid."));
}
generateEnergyWeightedTable();
break;
}
default: {
throw(RuntimeError("Unknown energy type."));
}
}
}
template<class GM>
GCOLIB<GM>::~GCOLIB() {
std::cout <<"~~"<<std::endl;
if(parameter_.energyType_ == Parameter::VIEW) {
mySelfView_ = NULL;
} else if(parameter_.energyType_ == Parameter::TABLES) {
mySelfTables_ = NULL;
}
if(GCOGeneralGraph_) {
delete GCOGeneralGraph_;
}
if(GCOGridGraph_) {
delete GCOGridGraph_;
}
if(D_) {
delete[] D_;
}
if(V_) {
delete[] V_;
}
if(hCue_) {
delete[] hCue_;
}
if(vCue_) {
delete[] vCue_;
}
}
template<class GM>
inline std::string GCOLIB<GM>::name() const {
return "GCOLIB";
}
template<class GM>
inline const typename GCOLIB<GM>::GraphicalModelType& GCOLIB<GM>::graphicalModel() const {
return gm_;
}
template<class GM>
inline InferenceTermination GCOLIB<GM>::infer() {
EmptyVisitorType visitor;
return this->infer(visitor);
}
template<class GM>
template<class VISITOR>
inline InferenceTermination GCOLIB<GM>::infer(VISITOR & visitor) {
visitor.begin(*this);
if(GCOGeneralGraph_) {
// Expansion and Swap converge
if(parameter_.inferenceType_ == Parameter::EXPANSION) {
if(parameter_.useAdaptiveCycles_) {
for (size_t i = 0; i <parameter_.numberOfIterations_; i++) {
ValueType totalEnergyOld = GCOGeneralGraph_->compute_energy();
ValueType totalEnergyNew = GCOGeneralGraph_->expansion(-1);
if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
break;
}
if(fabs(totalEnergyOld - totalEnergyNew) < OPENGM_FLOAT_TOL) {
break;
}
}
} else {
for (size_t i = 0; i <parameter_.numberOfIterations_; i++) {
ValueType totalEnergyOld = GCOGeneralGraph_->compute_energy();
ValueType totalEnergyNew = GCOGeneralGraph_->expansion(1);
if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
break;
}
if(fabs(totalEnergyOld - totalEnergyNew) < OPENGM_FLOAT_TOL) {
break;
}
}
}
} else {
for (size_t i = 0; i <parameter_.numberOfIterations_; i++) {
ValueType totalEnergyOld = GCOGeneralGraph_->compute_energy();
ValueType totalEnergyNew = GCOGeneralGraph_->swap(1);
if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
break;
}
if(fabs(totalEnergyOld - totalEnergyNew) < OPENGM_FLOAT_TOL) {
break;
}
}
}
} else {
// Expansion and Swap converge
if(parameter_.inferenceType_ == Parameter::EXPANSION) {
if(parameter_.useAdaptiveCycles_) {
for (size_t i = 0; i <parameter_.numberOfIterations_; i++) {
ValueType totalEnergyOld = GCOGridGraph_->compute_energy();
ValueType totalEnergyNew = GCOGridGraph_->expansion(-1);
if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
break;
}
if(fabs(totalEnergyOld - totalEnergyNew) < OPENGM_FLOAT_TOL) {
break;
}
}
} else {
for (size_t i = 0; i <parameter_.numberOfIterations_; i++) {
ValueType totalEnergyOld = GCOGridGraph_->compute_energy();
ValueType totalEnergyNew = GCOGridGraph_->expansion(1);
if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
break;
}
if(fabs(totalEnergyOld - totalEnergyNew) < OPENGM_FLOAT_TOL) {
break;
}
}
}
} else {
for (size_t i = 0; i <parameter_.numberOfIterations_; i++) {
ValueType totalEnergyOld = GCOGridGraph_->compute_energy();
ValueType totalEnergyNew = GCOGridGraph_->swap(1);
if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
break;
}
if(fabs(totalEnergyOld - totalEnergyNew) < OPENGM_FLOAT_TOL) {
break;
}
}
}
}
visitor.end(*this);
OPENGM_ASSERT(valueCheck());
return NORMAL;
}
template<class GM>
inline InferenceTermination GCOLIB<GM>::arg(std::vector<LabelType>& arg, const size_t& n) const {
if(n > 1) {
return UNKNOWN;
}
else {
arg.resize( gm_.numberOfVariables());
if(GCOGridGraph_) {
for(IndexType i = 0; i < gm_.numberOfVariables(); i++) {
arg[grid_(i)] = GCOGridGraph_->whatLabel(i);
}
} else {
for(IndexType i = 0; i < gm_.numberOfVariables(); i++) {
arg[i] = GCOGeneralGraph_->whatLabel(i);
}
}
return NORMAL;
}
}
template<class GM>
inline typename GM::ValueType GCOLIB<GM>::bound() const {
return Inference<GM, opengm::Minimizer>::bound();
}
template<class GM>
inline typename GM::ValueType GCOLIB<GM>::value() const {
if(GCOGeneralGraph_) {
return GCOGeneralGraph_->compute_energy();
} else {
return GCOGridGraph_->compute_energy();
}
}
template<class GM>
inline void GCOLIB<GM>::generateEnergyView() {
generateFirstOrderFactorLookupTable();
generateSecondOrderFactorLookupTables();
if(isGrid_) {
GCOGridGraph_->setDataCost(firstOrderFactorViewAccess);
GCOGridGraph_->setSmoothCost(secondOrderFactorViewGridAccess);
} else {
// add edges
for(IndexType i = 0; i < gm_.numberOfFactors(); i++) {
if(gm_[i].numberOfVariables() == 2) {
IndexType a = gm_[i].variableIndex(0);
IndexType b = gm_[i].variableIndex(1);
GCOGeneralGraph_->setNeighbors(a, b);
}
}
GCOGeneralGraph_->setDataCost(firstOrderFactorViewAccess);
GCOGeneralGraph_->setSmoothCost(secondOrderFactorViewGeneralAccess);
}
}
template<class GM>
inline void GCOLIB<GM>::generateEnergyTables() {
copyFactorValues();
GCOGridGraph_->setDataCost(firstOrderFactorTablesAccess);
GCOGridGraph_->setSmoothCost(secondOrderFactorTablesGridAccess);
}
template<class GM>
inline void GCOLIB<GM>::generateEnergyWeightedTable() {
setD();
setV();
// TODO check if this is a requirement only for mrf or also for gco.
/*// check if energy table is symmetric. This is required by mrf.
if(!symmetricEnergyTable()) {
throw(RuntimeError("Energy table has to be symmetric."));
}*/
if(isGrid_) {
setWeightedTableWeights();
// check if all energy tables are Equal with respect to a scaling factor
if(!sameEnergyTable()) {
throw(RuntimeError("All energy tables have to be equal with respect to a scaling factor."));
}
GCOGridGraph_->setDataCost(D_);
GCOGridGraph_->setSmoothCostVH(V_, vCue_, hCue_);
} else {
// add edges
for(IndexType i = 0; i < gm_.numberOfFactors(); i++) {
if(gm_[i].numberOfVariables() == 2) {
IndexType a = gm_[i].variableIndex(0);
IndexType b = gm_[i].variableIndex(1);
// compute weight
EnergyTermType weight;
for(IndexType l = 0; l < numLabels_; l++) {
IndexType m;
for(m = 0; m < numLabels_; m++) {
IndexType index[] = {l, m};
if((V_[(l * numLabels_) + m] != 0) && (gm_[i](index) != 0)) {
weight = gm_[i](index) / V_[(l * numLabels_) + m];
break;
}
}
if(m != numLabels_) {
break;
}
}
// check values
for(IndexType l = 0; l < numLabels_; l++) {
for(IndexType m = 0; m < numLabels_; m++) {
IndexType index[] = {l, m};
if(fabs((V_[(l * numLabels_) + m] * weight) - gm_[i](index)) > OPENGM_FLOAT_TOL) {
throw(RuntimeError("All energy tables have to be equal with respect to a scaling factor."));
}
}
}
// add edge
GCOGeneralGraph_->setNeighbors(a, b, weight);
}
}
GCOGeneralGraph_->setDataCost(D_);
GCOGeneralGraph_->setSmoothCost(V_);
}
}
template<class GM>
inline void GCOLIB<GM>::setD() {
D_ = new EnergyTermType[gm_.numberOfVariables() * numLabels_];
for(IndexType i = 0; i < gm_.numberOfVariables() * numLabels_; i++) {
D_[i] = 0;
}
for(IndexType i = 0; i < gm_.numberOfVariables(); i++) {
IndexType gmVariableIndex = grid_(i);
for(IndexType j = 0; j < gm_.numberOfFactors(gmVariableIndex); j++) {
IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, j);
if(gm_.numberOfVariables(gmFactorIndex) == 1) {
for(IndexType k = 0; k < numLabels_; k++) {
D_[i * numLabels_ + k] += gm_[gmFactorIndex](&k);
}
}
}
}
}
template<class GM>
inline void GCOLIB<GM>::setV() {
V_ = new EnergyTermType[numLabels_ * numLabels_];
IndexType gmVariableIndex = grid_(0);
for(IndexType i = 0; i < gm_.numberOfFactors(gmVariableIndex); i++) {
IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, i);
if(gm_.numberOfVariables(gmFactorIndex) == 2) {
for(IndexType j = 0; j < numLabels_; j++) {
for(IndexType k = 0; k < numLabels_; k++) {
IndexType index[] = {j, k};
V_[(j * numLabels_) + k] = gm_[gmFactorIndex](index);
}
}
}
}
}
template<class GM>
inline void GCOLIB<GM>::setWeightedTableWeights() {
hCue_ = new EnergyTermType[sizeX_ * sizeY_];
vCue_ = new EnergyTermType[sizeX_ * sizeY_];
for(IndexType i = 0; i < sizeX_; i++) {
for(IndexType j = 0; j < sizeY_; j++) {
IndexType gmVariableIndex = grid_(i, j);
for(IndexType k = 0; k < gm_.numberOfFactors(gmVariableIndex); k++) {
IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, k);
if(gm_.numberOfVariables(gmFactorIndex) == 2) {
if((i < sizeX_ - 1) && gm_.variableFactorConnection(grid_(i + 1, j), gmFactorIndex)) {
// set hCue
hCue_[i + (j * sizeX_)] = 0;
for(IndexType l = 0; l < numLabels_; l++) {
IndexType m;
for(m = 0; m < numLabels_; m++) {
IndexType index[] = {l, m};
if((V_[(l * numLabels_) + m] != 0) && (gm_[gmFactorIndex](index) != 0)) {
hCue_[i + (j * sizeX_)] = gm_[gmFactorIndex](index) / V_[(l * numLabels_) + m];
break;
}
}
if(m != numLabels_) {
break;
}
}
} else if((j < sizeY_ -1 ) && gm_.variableFactorConnection(grid_(i, j + 1), gmFactorIndex)) {
// set vCue
vCue_[i + (j * sizeX_)] = 0;
for(IndexType l = 0; l < numLabels_; l++) {
IndexType m;
for(m = 0; m < numLabels_; m++) {
IndexType index[] = {l, m};
if((V_[(l * numLabels_) + m] != 0) && (gm_[gmFactorIndex](index) != 0)) {
vCue_[i + (j * sizeX_)] = gm_[gmFactorIndex](index) / V_[(l * numLabels_) + m];
break;
}
}
if(m != numLabels_) {
break;
}
}
} else if((i != 0) && gm_.variableFactorConnection(grid_(i - 1, j), gmFactorIndex)) {
continue;
} else if((j != 0) && gm_.variableFactorConnection(grid_(i, j - 1), gmFactorIndex)) {
continue;
} else {
// should never be reached as this can only happen if gm_ is not a grid which is checked during construction
OPENGM_ASSERT(false);
}
}
}
}
}
}
template<class GM>
inline bool GCOLIB<GM>::hasSameLabelNumber() const {
for(IndexType i = 1; i < gm_.numberOfVariables(); i++) {
if(gm_.numberOfLabels(i) != numLabels_) {
return false;
}
}
return true;
}
template<class GM>
inline bool GCOLIB<GM>::sameEnergyTable() const {
const double eps = OPENGM_FLOAT_TOL;
for(IndexType i = 0; i < sizeX_ - 1; i++) {
for(IndexType j = 0; j < sizeY_ - 1; j++) {
IndexType gmVariableIndex = grid_(i, j);
for(IndexType k = 0; k < gm_.numberOfFactors(gmVariableIndex); k++) {
IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, k);
if(gm_.numberOfVariables(gmFactorIndex) == 2) {
if(gm_.variableFactorConnection(grid_(i + 1, j), gmFactorIndex)) {
for(IndexType l = 0; l < numLabels_; l++) {
for(IndexType m = 0; m < numLabels_; m++) {
IndexType index[] = {l, m};
if(fabs((V_[(l * numLabels_) + m] * hCue_[i + (j * sizeX_)]) - gm_[gmFactorIndex](index)) > eps) {
return false;
}
}
}
} else if(gm_.variableFactorConnection(grid_(i, j + 1), gmFactorIndex)) {
for(IndexType l = 0; l < numLabels_; l++) {
for(IndexType m = 0; m < numLabels_; m++) {
IndexType index[] = {l, m};
if(fabs((V_[(l * numLabels_) + m] * vCue_[i + (j * sizeX_)]) - gm_[gmFactorIndex](index)) > eps) {
return false;
}
}
}
} else if((i != 0) && gm_.variableFactorConnection(grid_(i - 1, j), gmFactorIndex)) {
continue;
} else if((j != 0) && gm_.variableFactorConnection(grid_(i, j - 1), gmFactorIndex)) {
continue;
} else {
// should never be reached as this can only happen if gm_ is not a grid which is checked during construction
OPENGM_ASSERT(false);
}
}
}
}
}
return true;
}
template<class GM>
inline bool GCOLIB<GM>::symmetricEnergyTable() const {
for (IndexType i = 0; i < numLabels_; i++) {
for (IndexType j = i; j < numLabels_; j++) {
if (V_[(i * numLabels_) + j] != V_[(j * numLabels_) + i]) {
return false;
}
}
}
return true;
}
template<class GM>
inline bool GCOLIB<GM>::valueCheck() const {
std::vector<LabelType> state;
arg(state);
if(fabs(value() - gm_.evaluate(state)) < OPENGM_FLOAT_TOL) {
return true;
} else {
return false;
}
}
template<class GM>
inline void GCOLIB<GM>::generateFirstOrderFactorLookupTable() {
firstOrderFactorLookupTable_.resize(gm_.numberOfVariables());
if(isGrid_) {
for(IndexType i = 0; i < gm_.numberOfVariables(); i++) {
IndexType gmVariableIndex = grid_(i);
for(IndexType j = 0; j < gm_.numberOfFactors(gmVariableIndex); j++) {
IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, j);
if(gm_.numberOfVariables(gmFactorIndex) == 1) {
firstOrderFactorLookupTable_[i].push_back(gmFactorIndex);
}
}
}
} else {
for(IndexType i = 0; i < gm_.numberOfFactors(); i++) {
if(gm_[i].numberOfVariables() == 1) {
IndexType a = gm_[i].variableIndex(0);
firstOrderFactorLookupTable_[a].push_back(i);
}
}
}
}
template<class GM>
inline void GCOLIB<GM>::generateSecondOrderFactorLookupTables() {
if(isGrid_) {
horizontalSecondOrderFactorLookupTable_.resize(gm_.numberOfVariables());
verticalSecondOrderFactorLookupTable_.resize(gm_.numberOfVariables());
for(IndexType i = 0; i < sizeX_; i++) {
for(IndexType j = 0; j < sizeY_; j++) {
IndexType gmVariableIndex = grid_(i, j);
for(IndexType k = 0; k < gm_.numberOfFactors(gmVariableIndex); k++) {
IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, k);
if(gm_.numberOfVariables(gmFactorIndex) == 2) {
if((i < sizeX_ - 1) && gm_.variableFactorConnection(grid_(i + 1, j), gmFactorIndex)) {
horizontalSecondOrderFactorLookupTable_[i + (j * sizeX_)].push_back(gmFactorIndex);
} else if((j < sizeY_ -1 ) && gm_.variableFactorConnection(grid_(i, j + 1), gmFactorIndex)) {
verticalSecondOrderFactorLookupTable_[i + (j * sizeX_)].push_back(gmFactorIndex);
} else if((i != 0) && gm_.variableFactorConnection(grid_(i - 1, j), gmFactorIndex)) {
continue;
} else if((j != 0) && gm_.variableFactorConnection(grid_(i, j - 1), gmFactorIndex)) {
continue;
} else {
// should never be reached as this can only happen if gm_ is not a grid which is checked during construction
OPENGM_ASSERT(false);
}
}
}
}
}
} else {
for(IndexType i = 0; i < gm_.numberOfFactors(); i++) {
if(gm_[i].numberOfVariables() == 2) {
IndexType a = gm_[i].variableIndex(0);
IndexType b = gm_[i].variableIndex(1);
if(a <= b) {
const std::pair<IndexType, IndexType> variables(a, b);
generalSecondOrderFactorLookupTable_[variables].push_back(i);
} else {
const std::pair<IndexType, IndexType> variables(b, a);
generalSecondOrderFactorLookupTable_[variables].push_back(i);
}
}
}
}
}
template<class GM>
inline typename GCOLIB<GM>::EnergyTermType GCOLIB<GM>::firstOrderFactorViewAccess(int pix, int i) {
EnergyTermType result = 0.0;
typename std::vector<IndexType>::const_iterator iter;
for(iter = mySelfView_->firstOrderFactorLookupTable_[pix].begin(); iter != mySelfView_->firstOrderFactorLookupTable_[pix].end(); iter++) {
result += mySelfView_->gm_[*iter](&i);
}
return result;
}
template<class GM>
inline typename GCOLIB<GM>::EnergyTermType GCOLIB<GM>::secondOrderFactorViewGridAccess(int pix1, int pix2, int i, int j) {
OPENGM_ASSERT(pix1 != pix2);
IndexType index[] = {i, j};
EnergyTermType result = 0.0;
typedef typename std::vector<IndexType>::const_iterator vecIter;
if(pix1 < pix2) {
if(pix2 == pix1 + 1) {
// horizontal connection
for(vecIter iter = mySelfView_->horizontalSecondOrderFactorLookupTable_[pix1].begin(); iter != mySelfView_->horizontalSecondOrderFactorLookupTable_[pix1].end(); iter++) {
result += mySelfView_->gm_[*iter](index);
}
} else {
// vertical connection
for(vecIter iter = mySelfView_->verticalSecondOrderFactorLookupTable_[pix1].begin(); iter != mySelfView_->verticalSecondOrderFactorLookupTable_[pix1].end(); iter++) {
result += mySelfView_->gm_[*iter](index);
}
}
} else {
if(pix1 == pix2 + 1) {
// horizontal connection
for(vecIter iter = mySelfView_->horizontalSecondOrderFactorLookupTable_[pix2].begin(); iter != mySelfView_->horizontalSecondOrderFactorLookupTable_[pix2].end(); iter++) {
result += mySelfView_->gm_[*iter](index);
}
} else {
// vertical connection
for(vecIter iter = mySelfView_->verticalSecondOrderFactorLookupTable_[pix2].begin(); iter != mySelfView_->verticalSecondOrderFactorLookupTable_[pix2].end(); iter++) {
result += mySelfView_->gm_[*iter](index);
}
}
}
return result;
}
template<class GM>
inline typename GCOLIB<GM>::EnergyTermType GCOLIB<GM>::secondOrderFactorViewGeneralAccess(int pix1, int pix2, int i, int j) {
OPENGM_ASSERT(pix1 != pix2);
IndexType index[] = {i, j};
EnergyTermType result = 0.0;
typedef typename std::vector<IndexType>::const_iterator vecIter;
typedef typename std::map<std::pair<IndexType, IndexType>, std::vector<IndexType> >::const_iterator mapIter;
if(pix1 <= pix2) {
const std::pair<IndexType, IndexType> variables(pix1, pix2);
mapIter generalSecondOrderFactors = mySelfView_->generalSecondOrderFactorLookupTable_.find(variables);
if(generalSecondOrderFactors != mySelfView_->generalSecondOrderFactorLookupTable_.end()) {
for(vecIter iter = generalSecondOrderFactors->second.begin(); iter != generalSecondOrderFactors->second.end(); iter++) {
result += mySelfView_->gm_[*iter](index);
}
}
} else {
const std::pair<IndexType, IndexType> variables(pix2, pix1);
mapIter generalSecondOrderFactors = mySelfView_->generalSecondOrderFactorLookupTable_.find(variables);
if(generalSecondOrderFactors != mySelfView_->generalSecondOrderFactorLookupTable_.end()) {
for(vecIter iter = generalSecondOrderFactors->second.begin(); iter != generalSecondOrderFactors->second.end(); iter++) {
result += mySelfView_->gm_[*iter](index);
}
}
}
return result;
}
template<class GM>
inline void GCOLIB<GM>::copyFactorValues() {
// first order
firstOrderFactorValues.resize(gm_.numberOfVariables() * numLabels_, 0.0);
for(IndexType i = 0; i < gm_.numberOfVariables(); i++) {
IndexType gmVariableIndex = grid_(i);
for(IndexType j = 0; j < gm_.numberOfFactors(gmVariableIndex); j++) {
IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, j);
if(gm_.numberOfVariables(gmFactorIndex) == 1) {
for(IndexType k = 0; k < numLabels_; k++) {
firstOrderFactorValues[(i * numLabels_) + k] += gm_[gmFactorIndex](&k);
}
}
}
}
// second order
const size_t size = 2 * gm_.numberOfVariables() * numLabels_ * numLabels_;
secondOrderFactorGridValues.resize(size, 0.0);
for(IndexType i = 0; i < sizeX_; i++) {
for(IndexType j = 0; j < sizeY_; j++) {
IndexType gmVariableIndex = grid_(i, j);
for(IndexType k = 0; k < gm_.numberOfFactors(gmVariableIndex); k++) {
IndexType gmFactorIndex = gm_.factorOfVariable(gmVariableIndex, k);
if(gm_.numberOfVariables(gmFactorIndex) == 2) {
if((i < sizeX_ - 1) && gm_.variableFactorConnection(grid_(i + 1, j), gmFactorIndex)) {
// down
for(IndexType l = 0; l < numLabels_; l++) {
for(IndexType m = 0; m < numLabels_; m++) {
IndexType index[] = {l, m};
IndexType linearIndex = (l * numLabels_) + m;
secondOrderFactorGridValues[((2 * (i + (j * sizeX_))) + down_) * numLabels_ * numLabels_ + linearIndex] += gm_[gmFactorIndex](index);
}
}
} else if((j < sizeY_ -1 ) && gm_.variableFactorConnection(grid_(i, j + 1), gmFactorIndex)) {
// right
for(IndexType l = 0; l < numLabels_; l++) {
for(IndexType m = 0; m < numLabels_; m++) {
IndexType index[] = {l, m};
IndexType linearIndex = (l * numLabels_) + m;
secondOrderFactorGridValues[((2 * (i + (j * sizeX_))) + right_) * numLabels_ * numLabels_ + linearIndex] += gm_[gmFactorIndex](index);
}
}
} else if((i != 0) && gm_.variableFactorConnection(grid_(i - 1, j), gmFactorIndex)) {
// up
continue;
} else if((j != 0) && gm_.variableFactorConnection(grid_(i, j - 1), gmFactorIndex)) {
// left
continue;
} else {
// should never be reached as this can only happen if gm_ is not a grid which is checked during construction
OPENGM_ASSERT(false);
}
}
}
}
}
}
template<class GM>
inline typename GCOLIB<GM>::EnergyTermType GCOLIB<GM>::firstOrderFactorTablesAccess(int pix, int i) {
return mySelfTables_->firstOrderFactorValues[(pix * mySelfTables_->numLabels_) + i];
}
template<class GM>
inline typename GCOLIB<GM>::EnergyTermType GCOLIB<GM>::secondOrderFactorTablesGridAccess(int pix1, int pix2, int i, int j) {
OPENGM_ASSERT(pix1 != pix2);
IndexType linearIndex = (i * mySelfTables_->numLabels_) + j;
if(pix1 < pix2) {
if(pix2 == pix1 + 1) {
// down
return mySelfTables_->secondOrderFactorGridValues[((2 * pix1) + mySelfTables_->down_) * mySelfTables_->numLabels_ * mySelfTables_->numLabels_ + linearIndex];
} else {
// right
return mySelfTables_->secondOrderFactorGridValues[((2 * pix1) + mySelfTables_->right_) * mySelfTables_->numLabels_ * mySelfTables_->numLabels_ + linearIndex];
}
} else {
if(pix1 == pix2 + 1) {
// up
return mySelfTables_->secondOrderFactorGridValues[((2 * pix2) + mySelfTables_->down_) * mySelfTables_->numLabels_ * mySelfTables_->numLabels_ + linearIndex];
} else {
// left
return mySelfTables_->secondOrderFactorGridValues[((2 * pix2) + mySelfTables_->right_) * mySelfTables_->numLabels_ * mySelfTables_->numLabels_ + linearIndex];
}
}
}
} // namespace external
} // namespace opengm
#endif /* GCO_HXX_ */
|