Exemplo n.º 1
0
////////////////////////////////////////////////////////////////////////////////
// 'processFeatureVector' reads a feature vector and modifies the classifier's
// data members accordingly
////////////////////////////////////////////////////////////////////////////////
void
MaxEnt::processFeatureVector(const FeatureVector& features){  
//  Class=2; // what does this do? Need to be there?
  en.f.resize(C);
  en.fs.resize(C);
  en.Ny.resize(C);
  size_t c=0; // class
  en.Ny[c]=0.0;         // strength of class (first num after @)
  // for each feature
  for (FeatureVector::const_iterator feat = features.begin();
      feat!=features.end();feat++)
  {
    // these loops may have to be reversed !!
    // the classes may need to be looped over first, and features within
    // instead of classes within feature loop
    StringXML feature;
    double value = feat->getValue();
    // for each class
    for (unsigned int i=0;i<C;i++){
      stringstream ss;
      ss << "cat" << i << "_" << feat->getFeature();
      feature = ss.str();
      if (value!=0) {
      // set values
      if (f2s.count(feature)!=0) { // check whether feature exists
        (s2f)[f2s[feature]].second+=(en.Ny[i]!=0);
        en.f[i].push_back(make_pair(f2s[feature], value));
        en.fs[i]+=value;
        F=max(F, en.fs[i]);
      }
      }
    }
  }
}
Exemplo n.º 2
0
ErrorStruct StrongClassifier::errorForFeatures(const TrainingData &features, bool printStats) const {

  ErrorStruct e;

  for (int i=0; i<features.size(); i++) {
    FeatureVector feature = *(features.feature(i));
    if (decide(feature)) {
      feature.val() == POS ? e.true_pos++ : e.false_pos++;//it is really positive
    } else {
      feature.val() == NEG ? e.true_neg++ : e.false_neg++;//it is really negative
    }
  }

  // if all 10 samples, 3 is misclassified, error is 3/10
  e.error = (e.false_pos + e.false_neg) / ((float)features.size()); 

  if (printStats) {
    std::cout << e.true_pos << " true positives" << std::endl;
    std::cout << e.false_pos << " false positives" << std::endl;
    std::cout << e.true_neg << " true negatives" << std::endl;
    std::cout << e.false_neg << " false negatives" << std::endl;
    std::cout << e.error * 100 << "% error" << std::endl;
    std::cout << std::endl;
  }

  return e;
}
Exemplo n.º 3
0
bool StrongClassifier::decide(const FeatureVector &featureVector) const {
  std::vector<float> features(featureVector.size());
  for (int i=0; i<featureVector.size(); i++) {
    features[i] = featureVector.at(i);
  }

  return decide(features);
}
Exemplo n.º 4
0
realv Layer::signalWeighting(FeatureVector _signal, Mat _weights) {
	if (_signal.getLength() != ((uint) _weights.cols)) {
		throw length_error("Layer : Uncorrect length between signal and weights");
	}
	realv sum = 0;
	for (uint i = 0; i < _signal.getLength(); i++) {
		sum += _signal[i] * _weights.at<realv>(0, i);
	}
	return sum;
}
Exemplo n.º 5
0
double Slic::_ComputeDistance(const FeatureVector &vec1, const FeatureVector &vec2)
{
    assert(vec1.size() == vec2.size());
    double dis = 0;
    for (unsigned int i=0;i<vec1.size();++i)
    {
        dis += (vec1[i]-vec2[i])*(vec1[i]-vec2[i]);
    }
    return dis;
}
Exemplo n.º 6
0
realv SEMeasurer::totalError(FeatureVector _output, FeatureVector _target) {
	if (_output.getLength() != _target.getLength()) {
		throw length_error("SE : Output and target do not have the same size");
	}
	realv result = 0;
	for (uint i = 0; i < _output.getLength(); i++) {
		result += (_output[i] - _target[i]) * (_output[i] - _target[i]);
	}
	err = result;
	return err;
}
Exemplo n.º 7
0
ErrorVector SEMeasurer::errorPerUnit(FeatureVector _output, FeatureVector _target) {
	if (_output.getLength() != _target.getLength()) {
		throw length_error("SEMeasurer : Output and target do not have the same size");
	}
	ErrorVector result(_output.getLength());
	for (uint i = 0; i < _output.getLength(); i++) {
		result[i] = (_output[i] - _target[i]) * (_output[i] - _target[i]);
	}
	errPerUnit = result;
	return result;
}
Exemplo n.º 8
0
/**
 * Determines the Random Approximation of GVC
 * (Determines the optimal Regularization Factor)
 */
double KerDenSOM::randApproxGVC(const TS* _examples, const FuzzyMap* _som, double _dataSD, double _reg)
{
    unsigned j, vv, cc;
    double num, den, r;
    FeatureVector VV;
    VV.resize(dim, 0.0);
    FuzzyMap tmpSOM(*_som);
    TS tmpTS(*_examples);
    num = 0;

    for (vv = 0; vv < numVectors; vv++)
    {
        for (j = 0; j < dim; j++)
            VV[j] = 0.0;
        for (cc = 0; cc < numNeurons; cc++)
        {
            for (j = 0; j < dim; j++)
                VV[j] += (_examples->theItems[vv][j] - _som->theItems[cc][j]) * _som->memb[vv][cc];
        }
        for (j = 0; j < dim; j++)
            num += VV[j] * VV[j];
    }

    init_random_generator();
    for (vv = 0; vv < numVectors; vv++)
    {
        for (j = 0; j < dim; j++)
            tmpTS.theItems[vv][j] += rnd_gaus() * _dataSD;
    }
    updateV(&tmpSOM, &tmpTS, _reg);
    den = 0.0;

    init_random_generator();
    for (vv = 0; vv < numVectors; vv++)
    {
        for (j = 0; j < dim; j++)
            VV[j] = 0.0;
        for (cc = 0; cc < numNeurons; cc++)
        {
            for (j = 0; j < dim; j++)
                VV[j] += (tmpTS.theItems[vv][j] - tmpSOM.theItems[cc][j] - _examples->theItems[vv][j] + _som->theItems[cc][j]) * _som->memb[vv][cc];
        }
        r = 0.;
        for (j = 0; j < dim; j++)
            r += VV[j] * rnd_gaus() * _dataSD;
        den += r * r;
    }
    if (den != 0)
        return (double) num / den;
    else
        return 0.;
}
Exemplo n.º 9
0
  inline
  Tp dot_product(const FeatureVector<Tp1, Alloc1>& x, Iterator first, Iterator last, Tp __dot)
  {
    typedef FeatureVector<Tp1, Alloc1> feature_vector1_type;
    
    for (/**/; first != last; ++ first) {
      typename feature_vector1_type::const_iterator iter = x.find(first->first);
      
      if (iter != x.end())
	__dot += iter->second * first->second;
    }
    return __dot;
  }
Exemplo n.º 10
0
  inline
  Tp dot_product(Iterator first, Iterator last, const FeatureVector<Tp2, Alloc2>& y, Tp __dot)
  {
    typedef FeatureVector<Tp2, Alloc2> feature_vector2_type;
    
    for (/**/; first != last; ++ first) {
      typename feature_vector2_type::const_iterator iter = y.find(first->first);
      
      if (iter != y.end())
	__dot += first->second * iter->second;
    }
    
    return __dot;
  }
Exemplo n.º 11
0
void SegmentList::normalizeFeatures() {
   if (isEmpty()) return;
   FeatureVector min = at(0)->features();
   FeatureVector max = at(0)->features();

   foreach (Segment const * const segment, *this) {
      min.setCompwiseMin(segment->features());
      max.setCompwiseMax(segment->features());
   }
   max = max - min;

   foreach (Segment * const segment, *this) {
      segment->features() = (segment->features() - min) / max;
   }
}
Exemplo n.º 12
0
/**************************************
 * Fucntion: is_classifier_correct
 * -------------------------------
 * returns true if weak classifier (wc) correctly identified the 
 * feature vector (fv), false otherwise.
 */
bool AdaBooster::is_classifier_correct(WeakClassifier &wc, FeatureVector &fv){
	
	// check if threshold is greater than (or equal to) feature
	bool guess = ( wc.threshold() >= fv.at(wc.dimension()) );

	// if classifier is flipped, negate guess
	guess = wc.isFlipped() ? !guess : guess;

	// find actual value of point
	bool real = ( fv.val() == POS );

	// return if guess and real agree
	return ( real == guess );

}
Exemplo n.º 13
0
PERIPHERAL_ERROR GetFeatures(const JOYSTICK_INFO* joystick, const char* controller_id,
                             unsigned int* feature_count, JOYSTICK_FEATURE** features)
{
  if (!joystick || !controller_id || !feature_count || !features)
    return PERIPHERAL_ERROR_INVALID_PARAMETERS;

  FeatureVector featureVector;
  if (CStorageManager::Get().GetFeatures(ADDON::Joystick(*joystick), controller_id,  featureVector))
  {
    *feature_count = featureVector.size();
    ADDON::JoystickFeatures::ToStructs(featureVector, features);
    return PERIPHERAL_NO_ERROR;
  }

  return PERIPHERAL_ERROR_FAILED;
}
Exemplo n.º 14
0
int RecBase :: recognizeEuclidean(FeatureVector& charvec, int gHint){
	int	cat,rank,i;
	double	d,d0;
	initResultTable();
	d0 = DBL_MAX;
	for ( cat=0 ; cat<n_cat ; cat++ ){
//		d = charvec.distEuclidean2(dic[cat]);
		d = charvec.distEuclidean2(dic[cat],d0);
		if ( d >= d0 )  continue;
		for ( rank=0 ; rank<n_top ; rank++ ){
			if ( d >= resultTable[rank].dist )  continue;
			for ( i=n_top-2 ; i>=rank ; i-- ){
				resultTable[i+1] = resultTable[i];
			}
			resultTable[rank].dist = d;
			resultTable[rank].id = cat;
			d0 = resultTable[n_top-1].dist;
			break;
		}
	}
	for ( i=0 ; i<n_top ; i++ ){
		resultTable[i].dist = sqrt(resultTable[i].dist);
	}
	return(0);
}
Exemplo n.º 15
0
  inline
  Tp1 dot_product(const FeatureVector<Tp1, Alloc1>& x, const WeightVector<Tp, Alloc>& w, const FeatureVector<Tp2, Alloc2>& y)
  {
    typedef FeatureVector<Tp1, Alloc1> feature_vector1_type;
    typedef WeightVector<Tp, Alloc>    weight_vector_type;
    typedef FeatureVector<Tp2, Alloc2> feature_vector2_type;
    
    if (x.empty() || y.empty()) return Tp1();
    
    if (static_cast<const void*>(&x) == static_cast<const void*>(&y))
      return details::__inner_product(x.begin(), x.end(), w, Tp1());
    
if (x.size() < y.size())
      return dot_product(x.begin(), x.end(), w, y, Tp1());
    else
      return dot_product(x, w, y.begin(), y.end(), Tp1());
  }
Exemplo n.º 16
0
void VectorDataSet::weightedSum(FeatureVector& result, const std::vector<int> &patterns,
								const std::vector<double> &alpha)
{
  int pIndex;
  // ASA FeatureVector is an alias for vector<double>
  vector<double> weight_temp (numFeatures,0);

  for (unsigned int i = 0; i < patterns.size(); i++){
    pIndex = patterns[i];
    for (int j = 0; j != X[pIndex].size(); ++j){
      weight_temp[j] += X[pIndex][j] * alpha[i];
    }
  }
  result.clear();
  result.initialize(weight_temp);
  
}
Exemplo n.º 17
0
 inline
 Tp1 dot_product(const FeatureVector<Tp1, Alloc1>& x, const FeatureVector<Tp2, Alloc2>& y)
 {
   typedef FeatureVector<Tp1, Alloc1> feature_vector1_type;
   typedef FeatureVector<Tp2, Alloc2> feature_vector2_type;
   
   if (x.empty() || y.empty()) return Tp1();
   
   // if the same address, we are identical!
   if (static_cast<const void*>(&x) == static_cast<const void*>(&y))
     return details::__inner_product(x.begin(), x.end(), Tp1());
   
   if (x.size() < y.size())
     return dot_product(x.begin(), x.end(), y, Tp1());
   else
     return dot_product(x, y.begin(), y.end(), Tp1());
 }
FeatureSet FeatureSet::clone() const
{
  FeatureSet other;
  for (FeatureSet::const_iterator i = begin(); i != end(); ++i) {
    FeatureVector cv = i->clone();
    // check that all data is the same:
    if (cv.size() != i->size())
      throw std::runtime_error("ssssssssss");
    for (unsigned int idx=0; idx<cv.size(); ++idx) {
      if (cv[idx] != (*i)[idx]) {
        cout << "(" << cv[idx] << "!=" << (*i)[idx] << ")" << flush;
        throw std::runtime_error("??????????");
      }
    }
    if (cv.getTag<TagTrueClassLabel>().label != i->getTag<TagTrueClassLabel>().label) {
      cout << "(" << cv.getTag<TagTrueClassLabel>().label << "!=" << i->getTag<TagTrueClassLabel>().label << ")" << flush;
      throw std::runtime_error("!!!!!!!!!!!");
    }
    other.push_back(cv);
  }
  return other;
}
Exemplo n.º 19
0
int main(int argc, char** argv)
{
  static const size_t K = 3;
  
  typedef double Scalar;
  typedef Eigen::Matrix<Scalar, 1, K> Feature;
  typedef std::vector<Feature, Eigen::aligned_allocator<Feature> > FeatureVector;
  FeatureVector features(1000*K);
  FeatureVector centers;
  std::vector<unsigned int> membership;

  FILE* data = fopen(argv[1], "r");
  for (size_t i = 0; i < features.size(); ++i) {
    Feature& f = features[i];
    fscanf(data, "%lf %lf %lf", &f[0], &f[1], &f[2]);
  }

  vt::SimpleKmeans<Feature> kmeans(Feature::Zero());
  double sse = kmeans.cluster(features, K, centers, membership);

  for (size_t i = 0; i < centers.size(); ++i) {
    printf("%f %f %f\n", centers[i][0], centers[i][1], centers[i][2]);
  }
  printf("sse = %f\n", sse);

  unsigned int pattern[K];
  pattern[0] = membership[0];
  pattern[1] = membership[1];
  pattern[2] = membership[2];
  for (size_t i = 0; i < membership.size(); i+=K) {
    if (membership[i] != pattern[0] ||
        membership[i+1] != pattern[1] ||
        membership[i+2] != pattern[2]) {
      printf("Misassignment!\n");
      return -1;
    }
  }
}
Exemplo n.º 20
0
void Slic::_InitCenterFeature(int i, int j, FeatureVector &featureVec)
{
    featureVec.clear();
    uchar *buffer = new uchar[5*5*_dataSize*_bandCount];
    _poSrcDS->RasterIO(GF_Read,j-2,i-2,5,5,buffer,5,5,_dataType,_bandCount,0,0,0,0);

    double minEdge = std::numeric_limits<double>::max();
    int minN;
    int minM;
    for (int n=1;n<4;++n)
    {
        for(int m=1;m<4;++m)
        {
            double edge = 0;
            edge += (buffer[(n-1)*5+m]-buffer[(n+1)*5+m])*(buffer[(n-1)*5+m]-buffer[(n+1)*5+m]);
            edge += (buffer[n*5+m-1]-buffer[n*5+m+1])*(buffer[n*5+m-1]-buffer[n*5+m+1]);

            if (edge < minEdge)
            {
                minEdge = edge;
                minN = n;
                minM = m;
            }
        }
    }

    int centerIndex = minN*5+minM;

    uchar* p = buffer;
    for(int k=0;k<_bandCount;++k,p += (5*5*_dataSize))
    {
        featureVec.push_back( SRCVAL(p,_dataType,centerIndex)/_regularizer);
    }
    featureVec.push_back(static_cast<double>(j+minM-2)/_regionSize);       //x
    featureVec.push_back(static_cast<double>(i+minN-2)/_regionSize);       //y
    delete []buffer;
}
Exemplo n.º 21
0
void ClassifySvmSharedCommand::readSharedRAbundVectors(vector<SharedRAbundVector*>& lookup, GroupMap& designMap, LabeledObservationVector& labeledObservationVector, FeatureVector& featureVector) {
    for ( int j = 0; j < lookup.size(); j++ ) {
        //i++;
        vector<individual> data = lookup[j]->getData();
        Observation* observation = new Observation(data.size(), 0.0);
        string sharedGroupName = lookup[j]->getGroup();
        string treatmentName = designMap.getGroup(sharedGroupName);
        //std::cout << "shared group name: " << sharedGroupName << " treatment name: " << treatmentName << std::endl;
        //labeledObservationVector.push_back(std::make_pair(treatmentName, observation));
        labeledObservationVector.push_back(LabeledObservation(j, treatmentName, observation));
        //std::cout << " j=" << j << " label : " << lookup[j]->getLabel() << " group: " << lookup[j]->getGroup();
        for (int k = 0; k < data.size(); k++) {
            //std::cout << " abundance " << data[k].abundance;
            observation->at(k) = double(data[k].abundance);
            if ( j == 0) {
                featureVector.push_back(Feature(k, m->currentSharedBinLabels[k]));
            }
        }
        //std::cout << std::endl;
        // let this happen later?
        //delete lookup[j];
    }
}
Exemplo n.º 22
0
bool CButtonMapXml::Load(void)
{
  TiXmlDocument xmlFile;
  if (!xmlFile.LoadFile(m_strResourcePath))
  {
    esyslog("Error opening %s: %s", m_strResourcePath.c_str(), xmlFile.ErrorDesc());
    return false;
  }

  TiXmlElement* pRootElement = xmlFile.RootElement();
  if (!pRootElement || pRootElement->NoChildren() || pRootElement->ValueStr() != BUTTONMAP_XML_ROOT)
  {
    esyslog("Can't find root <%s> tag", BUTTONMAP_XML_ROOT);
    return false;
  }

  const TiXmlElement* pDevice = pRootElement->FirstChildElement(DEVICES_XML_ELEM_DEVICE);

  if (!pDevice)
  {
    esyslog("Can't find <%s> tag", DEVICES_XML_ELEM_DEVICE);
    return false;
  }

  if (!CDeviceXml::Deserialize(pDevice, m_device))
    return false;

  const TiXmlElement* pController = pDevice->FirstChildElement(BUTTONMAP_XML_ELEM_CONTROLLER);

  if (!pController)
  {
    esyslog("Device \"%s\": can't find <%s> tag", m_device.Name().c_str(), BUTTONMAP_XML_ELEM_CONTROLLER);
    return false;
  }

  // For logging purposes
  unsigned int totalFeatureCount = 0;

  while (pController)
  {
    const char* id = pController->Attribute(BUTTONMAP_XML_ATTR_CONTROLLER_ID);
    if (!id)
    {
      esyslog("Device \"%s\": <%s> tag has no attribute \"%s\"", m_device.Name().c_str(),
              BUTTONMAP_XML_ELEM_CONTROLLER, BUTTONMAP_XML_ATTR_CONTROLLER_ID);
      return false;
    }

    FeatureVector features;
    if (!Deserialize(pController, features))
      return false;

    if (features.empty())
    {
      esyslog("Device \"%s\" has no features for controller %s", m_device.Name().c_str(), id);
    }
    else
    {
      totalFeatureCount += features.size();
      m_buttonMap[id] = std::move(features);
    }

    pController = pController->NextSiblingElement(BUTTONMAP_XML_ELEM_CONTROLLER);
  }

  dsyslog("Loaded device \"%s\" with %u controller profiles and %u total features", m_device.Name().c_str(), m_buttonMap.size(), totalFeatureCount);

  return true;
}
Exemplo n.º 23
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 */
Exemplo n.º 24
0
 inline
 Tp1 dot_product(const WeightVector<Tp1, Alloc1>& x, const FeatureVector<Tp2, Alloc2>& y)
 {
   return dot_product(x, y.begin(), y.end(), Tp1());
 }
Exemplo n.º 25
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 */
Exemplo n.º 26
0
void Pipe::TrainEpoch(int epoch) {
  Instance *instance;
  Parts *parts = CreateParts();
  Features *features = CreateFeatures();
  vector<double> scores;
  vector<double> gold_outputs;
  vector<double> predicted_outputs;
  double total_cost = 0.0;
  double total_loss = 0.0;
  double eta;
  int num_instances = instances_.size();
  double lambda = 1.0/(options_->GetRegularizationConstant() *
                       (static_cast<double>(num_instances)));
  timeval start, end;
  gettimeofday(&start, NULL);
  int time_decoding = 0;
  int time_scores = 0;
  int num_mistakes = 0;

  LOG(INFO) << " Iteration #" << epoch + 1;

  dictionary_->StopGrowth();

  for (int i = 0; i < instances_.size(); i++) {
    int t = num_instances * epoch + i;
    instance = instances_[i];
    MakeParts(instance, parts, &gold_outputs);
    MakeFeatures(instance, parts, features);

    // If using only supported features, must remove the unsupported ones.
    // This is necessary not to mess up the computation of the squared norm
    // of the feature difference vector in MIRA.
    if (options_->only_supported_features()) {
      RemoveUnsupportedFeatures(instance, parts, features);
    }

    timeval start_scores, end_scores;
    gettimeofday(&start_scores, NULL);
    ComputeScores(instance, parts, features, &scores);
    gettimeofday(&end_scores, NULL);
    time_scores += diff_ms(end_scores, start_scores);

    if (options_->GetTrainingAlgorithm() == "perceptron" ||
        options_->GetTrainingAlgorithm() == "mira" ) {
      timeval start_decoding, end_decoding;
      gettimeofday(&start_decoding, NULL);
      decoder_->Decode(instance, parts, scores, &predicted_outputs);
      gettimeofday(&end_decoding, NULL);
      time_decoding += diff_ms(end_decoding, start_decoding);

      if (options_->GetTrainingAlgorithm() == "perceptron") {
        for (int r = 0; r < parts->size(); ++r) {
          if (!NEARLY_EQ_TOL(gold_outputs[r], predicted_outputs[r], 1e-6)) {
            ++num_mistakes;
          }
        }
        eta = 1.0;
      } else {
        CHECK(false) << "Plain mira is not implemented yet.";
      }

      MakeGradientStep(parts, features, eta, t, gold_outputs,
                       predicted_outputs);

    } else if (options_->GetTrainingAlgorithm() == "svm_mira" ||
               options_->GetTrainingAlgorithm() == "crf_mira" ||
               options_->GetTrainingAlgorithm() == "svm_sgd" ||
               options_->GetTrainingAlgorithm() == "crf_sgd") {
      double loss;
      timeval start_decoding, end_decoding;
      gettimeofday(&start_decoding, NULL);
      if (options_->GetTrainingAlgorithm() == "svm_mira" ||
          options_->GetTrainingAlgorithm() == "svm_sgd") {
        // Do cost-augmented inference.
        double cost;
        decoder_->DecodeCostAugmented(instance, parts, scores, gold_outputs,
                                      &predicted_outputs, &cost, &loss);
        total_cost += cost;
      } else {
        // Do marginal inference.
        double entropy;
        decoder_->DecodeMarginals(instance, parts, scores, gold_outputs,
                                  &predicted_outputs, &entropy, &loss);
        CHECK_GE(entropy, 0.0);
      }
      gettimeofday(&end_decoding, NULL);
      time_decoding += diff_ms(end_decoding, start_decoding);

      if (loss < 0.0) {
        if (!NEARLY_EQ_TOL(loss, 0.0, 1e-9)) {
          LOG(INFO) << "Warning: negative loss set to zero: " << loss;
        }
        loss = 0.0;
      }
      total_loss += loss;

      // Compute difference between predicted and gold feature vectors.
      FeatureVector difference;
      MakeFeatureDifference(parts, features, gold_outputs, predicted_outputs,
                            &difference);

      // Get the stepsize.
      if (options_->GetTrainingAlgorithm() == "svm_mira" ||
          options_->GetTrainingAlgorithm() == "crf_mira") {
        double squared_norm = difference.GetSquaredNorm();
        double threshold = 1e-9;
        if (loss < threshold || squared_norm < threshold) {
          eta = 0.0;
        } else {
          eta = loss / squared_norm;
          if (eta > options_->GetRegularizationConstant()) {
            eta = options_->GetRegularizationConstant();
          }
        }
      } else {
        if (options_->GetLearningRateSchedule() == "fixed") {
          eta = options_->GetInitialLearningRate();
        } else if (options_->GetLearningRateSchedule() == "invsqrt") {
          eta = options_->GetInitialLearningRate() /
            sqrt(static_cast<double>(t+1));
        } else if (options_->GetLearningRateSchedule() == "inv") {
          eta = options_->GetInitialLearningRate() /
            static_cast<double>(t+1);
        } else if (options_->GetLearningRateSchedule() == "lecun") {
          eta = options_->GetInitialLearningRate() /
            (1.0 + (static_cast<double>(t) / static_cast<double>(num_instances)));
        } else {
          CHECK(false) << "Unknown learning rate schedule: "
                       << options_->GetLearningRateSchedule();
        }

        // Scale the parameter vector (only for SGD).
        double decay = 1 - eta * lambda;
        CHECK_GT(decay, 0.0);
        parameters_->Scale(decay);
      }

      MakeGradientStep(parts, features, eta, t, gold_outputs,
                       predicted_outputs);
    } else {
      CHECK(false) << "Unknown algorithm: " << options_->GetTrainingAlgorithm();
    }
  }

  // Compute the regularization value (halved squared L2 norm of the weights).
  double regularization_value =
      lambda * static_cast<double>(num_instances) *
      parameters_->GetSquaredNorm() / 2.0;

  delete parts;
  delete features;

  gettimeofday(&end, NULL);
  LOG(INFO) << "Time: " << diff_ms(end,start);
  LOG(INFO) << "Time to score: " << time_scores;
  LOG(INFO) << "Time to decode: " << time_decoding;
  LOG(INFO) << "Number of Features: " << parameters_->Size();
  if (options_->GetTrainingAlgorithm() == "perceptron" ||
      options_->GetTrainingAlgorithm() == "mira") {
    LOG(INFO) << "Number of mistakes: " << num_mistakes;
  }
  LOG(INFO) << "Total Cost: " << total_cost << "\t"
            << "Total Loss: " << total_loss << "\t"
            << "Total Reg: " << regularization_value << "\t"
            << "Total Loss+Reg: " << total_loss + regularization_value << endl;
}
Exemplo n.º 27
0
void 
vtree_user::compute_features( const std::string& cloud_filename,
								  		FeatureVector &feature_vector )
{
	typedef pcl::PointXYZ nx_PointT;
	typedef pcl::Normal nx_Normal;
	typedef pcl::PointCloud<nx_PointT> nx_PointCloud;
	typedef pcl::PointCloud<nx_Normal> nx_PointCloud_normal;
	typedef pcl::PointCloud<int> nx_PointCloud_int;
	
	typedef pcl::UniformSampling<nx_PointT> nx_Sampler;
	typedef pcl::search::KdTree<nx_PointT> nx_SearchMethod;
	typedef pcl::NormalEstimation<nx_PointT, nx_Normal> nx_NormalEst;
	
#if FEATURE == 1
	typedef pcl::FPFHSignature33 nx_FeatureType;
	typedef pcl::FPFHEstimation<nx_PointT, nx_Normal, nx_FeatureType> nx_FeatureEst;		
#elif FEATURE == 2
	typedef pcl::PFHSignature125 nx_FeatureType;
	typedef pcl::PFHEstimation<nx_PointT, nx_Normal, nx_FeatureType> nx_FeatureEst;		
#elif FEATURE == 3
	typedef pcl::VFHSignature308 nx_FeatureType;
	typedef pcl::VFHEstimation<nx_PointT, nx_Normal, nx_FeatureType> nx_FeatureEst;	
#else
	#error A valid feature definition is required!
#endif
	typedef pcl::PointCloud<nx_FeatureType> nx_PointCloud_feature;

	// load the file
	nx_PointCloud::Ptr cld_ptr(new nx_PointCloud);
   pcl::io::loadPCDFile<nx_PointT> ( cloud_filename, *cld_ptr);
   
   ROS_INFO("[vtree_user] Starting keypoint extraction...");
   clock_t tic = clock();
   nx_PointCloud::Ptr keypoints( new nx_PointCloud);
   nx_PointCloud_int::Ptr keypoint_idx(new nx_PointCloud_int);
   nx_Sampler uniform_sampling;
	uniform_sampling.setInputCloud ( cld_ptr );
	uniform_sampling.setRadiusSearch ( keypoint_radius_ );
	uniform_sampling.compute( *keypoint_idx );
	
	pcl::copyPointCloud ( *cld_ptr, keypoint_idx->points, *keypoints);
	
	ROS_INFO("[vtree_user] No of Keypoints found %d", static_cast<int>(keypoint_idx->size()) );
	ROS_INFO("[vtree_user] Keypoint extraction took %f msec.", static_cast<double>((clock()-tic)*1000)/CLOCKS_PER_SEC );
	
	if( keypoints->empty() )
	{
		ROS_WARN("[vtree_user] No keypoints were found...");
		return;
	}
	
	// Compute normals for the input cloud
	ROS_INFO("[vtree_user] Starting normal extraction...");
	tic = clock();
	nx_PointCloud_normal::Ptr normals (new nx_PointCloud_normal);
	nx_SearchMethod::Ptr search_method_xyz (new nx_SearchMethod);
	nx_NormalEst norm_est;
	norm_est.setInputCloud ( cld_ptr );
	norm_est.setSearchMethod ( search_method_xyz );
	norm_est.setRadiusSearch ( normal_radius_ );
	norm_est.compute ( *normals );
	ROS_INFO("[vtree_user] Normal extraction took %f msec.", static_cast<double>((clock()-tic)*1000)/CLOCKS_PER_SEC );
	
	// Get features at the computed keypoints
	ROS_INFO("[vtree_user] Starting feature computation...");
	tic = clock();
	nx_PointCloud_feature::Ptr features(new nx_PointCloud_feature);
	nx_FeatureEst feat_est;
	feat_est.setInputCloud ( keypoints );
	feat_est.setSearchSurface ( cld_ptr );
	feat_est.setInputNormals ( normals );
	
	search_method_xyz.reset(new nx_SearchMethod);
	feat_est.setSearchMethod ( search_method_xyz );
	feat_est.setRadiusSearch ( feature_radius_ );
	feat_est.compute ( *features );
	ROS_INFO("[vtree_user] No of Features found %d", static_cast<int>(features->size()) );
	ROS_INFO("[vtree_user] Feature computation took %f msec.", static_cast<double>((clock()-tic)*1000)/CLOCKS_PER_SEC );
	
	// Rectify the historgram values to ensure they are in [0,100] and create a document
	for( nx_PointCloud_feature::iterator iter = features->begin();
			iter != features->end(); ++iter)
	{
		rectify_histogram( iter->histogram );			
		feature_vector.push_back( FeatureHist( iter->histogram ) );
	}
}
Exemplo n.º 28
0
bool CButtonMapXml::Serialize(const FeatureVector& features, TiXmlElement* pElement)
{
  if (pElement == NULL)
    return false;

  for (FeatureVector::const_iterator it = features.begin(); it != features.end(); ++it)
  {
    const ADDON::JoystickFeature& feature = *it;

    if (!IsValid(feature))
      continue;

    TiXmlElement featureElement(BUTTONMAP_XML_ELEM_FEATURE);
    TiXmlNode* featureNode = pElement->InsertEndChild(featureElement);
    if (featureNode == NULL)
      return false;

    TiXmlElement* featureElem = featureNode->ToElement();
    if (featureElem == NULL)
      return false;

    featureElem->SetAttribute(BUTTONMAP_XML_ATTR_FEATURE_NAME, feature.Name());

    switch (feature.Type())
    {
      case JOYSTICK_FEATURE_TYPE_SCALAR:
      {
        SerializePrimitive(featureElem, feature.Primitive());

        break;
      }
      case JOYSTICK_FEATURE_TYPE_ANALOG_STICK:
      {
        if (!SerializePrimitiveTag(featureElem, feature.Up(), BUTTONMAP_XML_ELEM_UP))
          return false;

        if (!SerializePrimitiveTag(featureElem, feature.Down(), BUTTONMAP_XML_ELEM_DOWN))
          return false;

        if (!SerializePrimitiveTag(featureElem, feature.Right(), BUTTONMAP_XML_ELEM_RIGHT))
          return false;

        if (!SerializePrimitiveTag(featureElem, feature.Left(), BUTTONMAP_XML_ELEM_LEFT))
          return false;

        break;
      }
      case JOYSTICK_FEATURE_TYPE_ACCELEROMETER:
      {
        if (!SerializePrimitiveTag(featureElem, feature.PositiveX(), BUTTONMAP_XML_ELEM_POSITIVE_X))
          return false;

        if (!SerializePrimitiveTag(featureElem, feature.PositiveY(), BUTTONMAP_XML_ELEM_POSITIVE_Y))
          return false;

        if (!SerializePrimitiveTag(featureElem, feature.PositiveZ(), BUTTONMAP_XML_ELEM_POSITIVE_Z))
          return false;

        break;
      }
      default:
        break;
    }
  }

  return true;
}
Exemplo n.º 29
0
bool CButtonMapXml::Deserialize(const TiXmlElement* pElement, FeatureVector& features)
{
  const TiXmlElement* pFeature = pElement->FirstChildElement(BUTTONMAP_XML_ELEM_FEATURE);

  if (!pFeature)
  {
    esyslog("Can't find <%s> tag", BUTTONMAP_XML_ELEM_FEATURE);
    return false;
  }

  while (pFeature)
  {
    const char* name = pFeature->Attribute(BUTTONMAP_XML_ATTR_FEATURE_NAME);
    if (!name)
    {
      esyslog("<%s> tag has no \"%s\" attribute", BUTTONMAP_XML_ELEM_FEATURE, BUTTONMAP_XML_ATTR_FEATURE_NAME);
      return false;
    }
    std::string strName(name);

    const TiXmlElement* pUp = nullptr;
    const TiXmlElement* pDown = nullptr;
    const TiXmlElement* pRight = nullptr;
    const TiXmlElement* pLeft = nullptr;

    const TiXmlElement* pPositiveX = nullptr;
    const TiXmlElement* pPositiveY = nullptr;
    const TiXmlElement* pPositiveZ = nullptr;

    // Determine the feature type
    JOYSTICK_FEATURE_TYPE type;

    ADDON::DriverPrimitive primitive;
    if (DeserializePrimitive(pFeature, primitive, strName))
    {
      type = JOYSTICK_FEATURE_TYPE_SCALAR;
    }
    else
    {
      pUp = pFeature->FirstChildElement(BUTTONMAP_XML_ELEM_UP);
      pDown = pFeature->FirstChildElement(BUTTONMAP_XML_ELEM_DOWN);
      pRight = pFeature->FirstChildElement(BUTTONMAP_XML_ELEM_RIGHT);
      pLeft = pFeature->FirstChildElement(BUTTONMAP_XML_ELEM_LEFT);

      if (pUp || pDown || pRight || pLeft)
      {
        type = JOYSTICK_FEATURE_TYPE_ANALOG_STICK;
      }
      else
      {
        pPositiveX = pFeature->FirstChildElement(BUTTONMAP_XML_ELEM_POSITIVE_X);
        pPositiveY = pFeature->FirstChildElement(BUTTONMAP_XML_ELEM_POSITIVE_Y);
        pPositiveZ = pFeature->FirstChildElement(BUTTONMAP_XML_ELEM_POSITIVE_Z);

        if (pPositiveX || pPositiveY || pPositiveZ)
        {
          type = JOYSTICK_FEATURE_TYPE_ACCELEROMETER;
        }
        else
        {
          esyslog("Feature \"%s\": <%s> tag is not a valid primitive", strName.c_str(), BUTTONMAP_XML_ELEM_FEATURE);
          return false;
        }
      }
    }

    ADDON::JoystickFeature feature(strName, type);

    // Deserialize according to type
    switch (type)
    {
      case JOYSTICK_FEATURE_TYPE_SCALAR:
      {
        feature.SetPrimitive(primitive);
        break;
      }
      case JOYSTICK_FEATURE_TYPE_ANALOG_STICK:
      {
        ADDON::DriverPrimitive up;
        ADDON::DriverPrimitive down;
        ADDON::DriverPrimitive right;
        ADDON::DriverPrimitive left;

        bool bSuccess = true;

        if (pUp && !DeserializePrimitive(pUp, up, strName))
        {
          esyslog("Feature \"%s\": <%s> tag is not a valid primitive", strName.c_str(), BUTTONMAP_XML_ELEM_UP);
          bSuccess = false;
        }

        if (pDown && !DeserializePrimitive(pDown, down, strName))
        {
          esyslog("Feature \"%s\": <%s> tag is not a valid primitive", strName.c_str(), BUTTONMAP_XML_ELEM_DOWN);
          bSuccess = false;
        }

        if (pRight && !DeserializePrimitive(pRight, right, strName))
        {
          esyslog("Feature \"%s\": <%s> tag is not a valid primitive", strName.c_str(), BUTTONMAP_XML_ELEM_RIGHT);
          bSuccess = false;
        }

        if (pLeft && !DeserializePrimitive(pLeft, left, strName))
        {
          esyslog("Feature \"%s\": <%s> tag is not a valid primitive", strName.c_str(), BUTTONMAP_XML_ELEM_LEFT);
          bSuccess = false;
        }

        if (!bSuccess)
          return false;

        feature.SetUp(up);
        feature.SetDown(down);
        feature.SetRight(right);
        feature.SetLeft(left);

        break;
      }
      case JOYSTICK_FEATURE_TYPE_ACCELEROMETER:
      {
        ADDON::DriverPrimitive positiveX;
        ADDON::DriverPrimitive positiveY;
        ADDON::DriverPrimitive positiveZ;

        bool bSuccess = true;

        if (pPositiveX && !DeserializePrimitive(pPositiveX, positiveY, strName))
        {
          esyslog("Feature \"%s\": <%s> tag is not a valid primitive", strName.c_str(), BUTTONMAP_XML_ELEM_POSITIVE_X);
          bSuccess = false;
        }

        if (pPositiveY && !DeserializePrimitive(pPositiveY, positiveY, strName))
        {
          esyslog("Feature \"%s\": <%s> tag is not a valid primitive", strName.c_str(), BUTTONMAP_XML_ELEM_POSITIVE_Y);
          bSuccess = false;
        }

        if (pPositiveZ && !DeserializePrimitive(pPositiveZ, positiveZ, strName))
        {
          esyslog("Feature \"%s\": <%s> tag is not a valid primitive", strName.c_str(), BUTTONMAP_XML_ELEM_POSITIVE_Z);
          bSuccess = false;
        }

        if (!bSuccess)
          return false;

        feature.SetPositiveX(positiveX);
        feature.SetPositiveY(positiveY);
        feature.SetPositiveZ(positiveZ);

        break;
      }
      default:
        break;
    }

    features.push_back(feature);

    pFeature = pFeature->NextSiblingElement(BUTTONMAP_XML_ELEM_FEATURE);
  }

  return true;
}
Exemplo n.º 30
0
void ClassifySvmSharedCommand::processSharedAndDesignData(vector<SharedRAbundVector*> lookup) {
    try {
        OutputFilter outputFilter(verbosity);

        LabeledObservationVector labeledObservationVector;
        FeatureVector featureVector;
        readSharedRAbundVectors(lookup, designMap, labeledObservationVector, featureVector);

        // optionally remove features with low standard deviation
        if ( stdthreshold > 0.0 ) {
            FeatureVector removedFeatureVector = applyStdThreshold(stdthreshold, labeledObservationVector, featureVector);
            if (removedFeatureVector.size() > 0) {
                std::cout << removedFeatureVector.size() << " OTUs were below the stdthreshold of " << stdthreshold << " and were removed" << std::endl;
                if ( outputFilter.debug() ) {
                    std::cout << "the following OTUs were below the standard deviation threshold of " << stdthreshold << std::endl;
                    for (FeatureVector::iterator i = removedFeatureVector.begin(); i != removedFeatureVector.end(); i++) {
                        std::cout << "  " << i->getFeatureLabel() << std::endl;
                    }
                }
            }
        }

        // apply [0,1] standardization
        if ( transformName == "zeroone") {
            std::cout << "transforming data to lie within range [0,1]" << std::endl;
            transformZeroOne(labeledObservationVector);
        }
        else {
            std::cout << "transforming data to have zero mean and unit variance" << std::endl;
            transformZeroMeanUnitVariance(labeledObservationVector);
        }

        SvmDataset svmDataset(labeledObservationVector, featureVector);

        OneVsOneMultiClassSvmTrainer trainer(svmDataset, evaluationFoldCount, trainingFoldCount, *this, outputFilter);

        if ( mode == "rfe" ) {
            SvmRfe svmRfe;
            ParameterRange& linearKernelConstantRange = kernelParameterRangeMap["linear"]["constant"];
            ParameterRange& linearKernelSmoCRange = kernelParameterRangeMap["linear"]["smoc"];
            RankedFeatureList rankedFeatureList = svmRfe.getOrderedFeatureList(svmDataset, trainer, linearKernelConstantRange, linearKernelSmoCRange);

            map<string, string> variables;
            variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
            variables["[distance]"] = lookup[0]->getLabel();
            string filename = getOutputFileName("summary", variables);
            outputNames.push_back(filename);
            outputTypes["summary"].push_back(filename);
            m->mothurOutEndLine();

            std::ofstream outputFile(filename.c_str());

            int n = 0;
            int rfeRoundCount = rankedFeatureList.front().getRank();
            std::cout << "ordered features:" << std::endl;
            std::cout << setw(5)  << "index"
                      << setw(12) << "OTU"
                      << setw(5)  << "rank"
                      << std::endl;
            outputFile << setw(5)  << "index"
                       << setw(12) << "OTU"
                       << setw(5)  << "rank"
                       << std::endl;
            for (RankedFeatureList::iterator i = rankedFeatureList.begin(); i != rankedFeatureList.end(); i++) {
                n++;
                int rank = rfeRoundCount - i->getRank() + 1;
                outputFile << setw(5)  << n
                           << setw(12) << i->getFeature().getFeatureLabel()
                           << setw(5)  << rank
                           << std::endl;
                if ( n <= 20 ) {
                    std::cout << setw(5) << n
                              << setw(12) << i->getFeature().getFeatureLabel()
                              << setw(5) << rank
                              << std::endl;
                }
            }
            outputFile.close();
        }
        else {
            MultiClassSVM* mcsvm = trainer.train(kernelParameterRangeMap);

            map<string, string> variables;
            variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(sharedfile));
            variables["[distance]"] = lookup[0]->getLabel();
            string filename = getOutputFileName("summary", variables);
            outputNames.push_back(filename);
            outputTypes["summary"].push_back(filename);
            m->mothurOutEndLine();

            std::ofstream outputFile(filename.c_str());

            printPerformanceSummary(mcsvm, std::cout);
            printPerformanceSummary(mcsvm, outputFile);

            outputFile << "actual  predicted" << std::endl;
            for ( LabeledObservationVector::const_iterator i = labeledObservationVector.begin(); i != labeledObservationVector.end(); i++ ) {
                Label actualLabel = i->getLabel();
                outputFile << i->getDatasetIndex() << " " << actualLabel << " ";
                try {
                    Label predictedLabel = mcsvm->classify(*(i->getObservation()));
                    outputFile << predictedLabel << std::endl;
                }
                catch ( MultiClassSvmClassificationTie& m ) {
                    outputFile << "tie" << std::endl;
                    std::cout << "classification tie for observation " << i->datasetIndex << " with label " << i->first << std::endl;
                }

            }
            outputFile.close();
            delete mcsvm;
        }

    }
    catch (exception& e) {
        m->errorOut(e, "ClassifySvmSharedCommand", "processSharedAndDesignData");
        exit(1);
    }
}