Exemplo n.º 1
0
void GI_libDAI::copyLabels_MSRC(vector<std::size_t>& labels,
                                labelType* nodeLabels,
                                BP& bp,
                                FactorGraph& fg)
{
  labelType voidLabel = classIdxToLabel[0];
  labelType moutainLabel = classIdxToLabel[4161600];
  labelType horseLabel = classIdxToLabel[8323328];

  INFERENCE_PRINT("[gi_libDAI] copyLabels_MSRC void=%d, moutain=%d, horse=%d\n",
                  (int)voidLabel, (int)moutainLabel, (int)horseLabel);

  int label;
  int n = slice->getNbSupernodes();
  for(int sid = 0; sid < n; sid++) {
    label = labels[sid];

    //if(lossPerLabel == 0 && label > 20) // void label
    if(label == voidLabel || label == moutainLabel || label == horseLabel) {
      //Factor f = bp.beliefV(sid);
      Factor f = bp.belief(fg.var(sid));
      double maxProb = -1;
      for(int i = 0; i < (int)f.states(); i++) {
        if(i != voidLabel && i != moutainLabel && i != horseLabel)
          if(maxProb < f[i]) {
            maxProb = f[i];
            label = i;
          }
      }
      //INFERENCE_PRINT("label=%d\n", label);
    }
    nodeLabels[sid] = label;
  }
}
void BruteForceOptMatching::getMaxProbAssignments(
    const BP& ia, const FactorGraph& fg, const ConnectedFactorGraph& graph,
    McDArray<McVec2i>& pairs) {

    for (int i = 0; i < graph.variables.size(); i++) {

        McDArray<int> possibleAssignments;
        getAssignmentsForVariable(graph.variables[i], possibleAssignments);
        Factor belief =
            ia.belief(Var(graph.variables[i], possibleAssignments.size() + 1));
        float maxVal = -1 * FLT_MAX;
        int maxIdx = -1;

        for (int j = 0; j < possibleAssignments.size() + 1; j++) {
            if (belief.get(j) > maxVal) {
                maxVal = belief.get(j);
                maxIdx = j;
            }
        }
        int indexOfAssignmentInVertexList =
            mapVariableAssignmentToIndexInVertexList(graph.variables[i],
                                                     maxIdx);

        McVec2i pair =
            McVec2i(graph.variables[i], indexOfAssignmentInVertexList);

        pairs.append(pair);
    }
    outputSingleFactorValues(graph);

    //    std::vector<std::size_t> maxes= ia.findMaximum();
    //    vector<std::size_t>::iterator it=maxes.begin();
}
Exemplo n.º 3
0
// use bp.findmaximum instead
void GI_libDAI::getLabels(BP& bp,
                          FactorGraph& fg,
                          vector<std::size_t>& labels)
{
  float maxProb;
  int label;
  ofstream ofs("beliefs2");
  ofs << "------------------------------- UNARY -------------------------------\n";
  for(long sid = 0; sid < slice->getNbSupernodes(); sid++) {
    Factor f = bp.belief(fg.var(sid));
    maxProb = f[0];
    label = 0;
    ofs << sid;
    for(int i=1; i < (int)param->nClasses; i++) {
      ofs << " " << i << ":" << f[i];
      if(f[i] > maxProb) {
        maxProb = f[i];
        label = i;
      }
    }
    ofs << endl;
    labels[sid] = label;
  }
  ofs.close();
}
void BruteForceOptMatching::checkAmbiguities(const BP& ia,
                                             const FactorGraph& fg,
                                             const ConnectedFactorGraph& graph,
                                             McDArray<int>& ambiguities) {
    for (int h = 0; h < graph.variables.size(); h++) {
        McDArray<int> possibleAssignments;
        getAssignmentsForVariable(graph.variables[h], possibleAssignments);
        Factor belief =
            ia.belief(Var(graph.variables[h], possibleAssignments.size() + 1));

        float maxProb = belief.max();
        int countSame = 0;

        for (int k = 0; k < possibleAssignments.size() + 1; k++) {
            float curProb = belief.get(k);
            if (fabs(curProb - maxProb) < 0.1)
                countSame++;
        }

        /////
        cout << "\n Belief for var " << graph.variables[h] << "\n";
        for (int k = 0; k < possibleAssignments.size() + 1; k++) {
            float curProb = belief.get(k);
            cout << curProb << " ";
        }
        cout << "\n";

        ////

        if (countSame > 1) {
            // oh no! We found an ambiguos assignment!

            ambiguities.append(graph.variables[h]);

            // print it out:
            cout << "Found an ambiguous assignemnt to variable "
                 << graph.variables[h] << "\n";
            for (int k = 0; k < possibleAssignments.size() + 1; k++) {
                float curProb = belief.get(k);
                cout << curProb << " ";
            }
            cout << "\n";
        }
    }
}