Ejemplo n.º 1
0
void  Classifier2::ClassifyAExample (FeatureVector&  example,
                                     MLClassPtr&     predClass1,
                                     MLClassPtr&     predClass2,
                                     kkint32&        predClass1Votes,
                                     kkint32&        predClass2Votes,
                                     double&         knownClassProb,
                                     double&         predClass1Prob,
                                     double&         predClass2Prob,
                                     kkint32&        numOfWinners,
                                     double&         breakTie
                                    )
{
  bool   knownClassOneOfTheWiners = false;

  predClass1     = NULL;
  predClass2     = NULL;
  knownClassProb = -1.0f;
  predClass1Prob = -1.0f;
  predClass2Prob = -1.0f;

  MLClassPtr origClass  = example.MLClass ();
  
  trainedModel->Predict (&example,
                         origClass,
                         predClass1,
                         predClass2,
                         predClass1Votes,
                         predClass2Votes,
                         knownClassProb,
                         predClass1Prob,
                         predClass2Prob,
                         numOfWinners,
                         knownClassOneOfTheWiners,
                         breakTie,
                         log
                        );

  if  (!predClass1)
    predClass1 = noiseMLClass;

  if  (subClassifiers)
  {
    Classifier2Ptr  subClassifer = LookUpSubClassifietByClass (predClass1);
    if  (subClassifer)
    {
      MLClassPtr       subPredClass1      = NULL;
      MLClassPtr       subPredClass2      = NULL;
      kkint32          subPredClass1Votes = 0;
      kkint32          subPredClass2Votes = 0;
      double           subKnownClassProb  = 0.0;
      double           subPredClass1Prob  = 0.0;
      double           subPredClass2Prob  = 0.0;
      kkint32          subNumOfWinners    = 0;
      double           subBreakTie        = 0.0;

      subClassifer->ClassifyAExample (example, subPredClass1, subPredClass2, 
                                    subPredClass1Votes, subPredClass2Votes, subKnownClassProb,
                                    subPredClass1Prob,  subPredClass2Prob,  subNumOfWinners,
                                    subBreakTie
                                   );
      predClass1 = subPredClass1;
      predClass1Votes += subPredClass1Votes;
      predClass1Prob  *= subPredClass1Prob;
      knownClassProb  *= subKnownClassProb;
      numOfWinners    += subNumOfWinners;
      breakTie        += subBreakTie * (1.0 - breakTie);
    }

    subClassifer = LookUpSubClassifietByClass (predClass2);
    if  (subClassifer)
    {
      MLClassPtr       subPredClass1      = NULL;
      MLClassPtr       subPredClass2      = NULL;
      kkint32          subPredClass1Votes = 0;
      kkint32          subPredClass2Votes = 0;
      double           subKnownClassProb  = 0.0;
      double           subPredClass1Prob  = 0.0;
      double           subPredClass2Prob  = 0.0;
      kkint32          subNumOfWinners    = 0;
      double           subBreakTie        = 0.0;

      subClassifer->ClassifyAExample (example, subPredClass1, subPredClass2, 
                                    subPredClass1Votes, subPredClass2Votes, subKnownClassProb,
                                    subPredClass1Prob,  subPredClass2Prob,  subNumOfWinners,
                                    subBreakTie        
                                   );
      predClass2 = subPredClass1;
      predClass2Votes += subPredClass1Votes;
      predClass2Prob  *= subPredClass1Prob;
    }
  }

  example.MLClass (predClass1);

  return;
}  /* ClassifyAExample */
Ejemplo n.º 2
0
MLClassPtr  Classifier2::ClassifyAImageOneLevel (FeatureVector&  example,
                                                 double&         probability,
                                                 kkint32&        numOfWinners, 
                                                 bool&           knownClassOneOfTheWinners,
                                                 double&         breakTie
                                                )

{
  probability       = 0.0;

  double  probOfKnownClass = 0.0f;
  probability = 0.0;

  MLClassPtr  origClass       = example.MLClass ();
  MLClassPtr  predictedClass  = NULL;
  MLClassPtr  predictedClass2 = NULL;

  kkint32        class1Votes = -1;
  kkint32        class2Votes = -1;

  double         predictedClass2Prob = 0.0f;

  trainedModel->Predict (&example, 
                   origClass,
                   predictedClass,
                   predictedClass2,
                   class1Votes,
                   class2Votes,
                   probOfKnownClass,
                   probability,
                   predictedClass2Prob,
                   numOfWinners,
                   knownClassOneOfTheWinners,
                   breakTie,
                   log
                   );

  if  (predictedClass == NULL)
  {
    log.Level (-1) << endl << endl 
                   << "Classifier2::ClassifyAImageOneLevel   The trainedModel returned back a NULL pointer for predicted class" << endl
                   << endl;
    predictedClass = unKnownMLClass;
  }

  if  (subClassifiers)
  {
    Classifier2Ptr  subClassifer = LookUpSubClassifietByClass (predictedClass);
    if (subClassifer)
    {
      double  subProbability = 0.0;
      kkint32 subNumOfWinners = 0;
      double  subBreakTie = 0.0;
      /**@todo  make sure that the following call does not normalize the features. */
      MLClassPtr       subPrediction
        = subClassifer->ClassifyAImageOneLevel (example, subProbability, subNumOfWinners, knownClassOneOfTheWinners, subBreakTie);
      if (subPrediction)
      {
        probability = probability * subProbability;
        numOfWinners = numOfWinners + subNumOfWinners;
        breakTie += subBreakTie * (1.0 - breakTie);
      }
    }
  }

  if  (predictedClass->UnDefined ())
    predictedClass = noiseMLClass;

  example.MLClass (predictedClass);

  return  predictedClass;
}  /* ClassifyAImageOneLevel */