void TouchTracker::Calibrator::getAverageTemplateDistance() { MLSignal temp(mWidth, mHeight); MLSignal tempSample(kTemplateSize, kTemplateSize); float sum = 0.f; int samples = 0; for(int j=0; j<mHeight; ++j) { for(int i=0; i<mWidth; ++i) { int idx = j*mWidth + i; // put mean of input samples into temp signal at i, j temp.clear(); tempSample.copy(mDataSum[idx]); tempSample.scale(1.f / (float)mSampleCount[idx]); temp.add2D(tempSample, i - kTemplateRadius, j - kTemplateRadius); float diff = differenceFromTemplateTouch(temp, Vec2(i, j)); sum += diff; samples++; // debug() << "template diff [" << i << ", " << j << "] : " << diff << "\n"; } } mAvgDistance = sum / (float)samples; }
void SampleSet::swapSamples(const int samIndexToMoveToBack, const int samIndexToMoveFromBack) { //trainSet[samIndexToMoveToBack].swap(trainSet[samIndexToMoveFromBack]); if (samIndexToMoveToBack != samIndexToMoveFromBack) { Sample tempSample(samples[samIndexToMoveToBack]); samples[samIndexToMoveToBack] = samples[samIndexToMoveFromBack]; samples[samIndexToMoveFromBack] = tempSample; } }
bool ClassificationData::loadDatasetFromFile(const std::string &filename){ std::fstream file; file.open(filename.c_str(), std::ios::in); UINT numClasses = 0; clear(); if( !file.is_open() ){ errorLog << "loadDatasetFromFile(const std::string &filename) - could not open file!" << std::endl; return false; } std::string word; //Check to make sure this is a file with the Training File Format file >> word; if(word != "GRT_LABELLED_CLASSIFICATION_DATA_FILE_V1.0"){ errorLog << "loadDatasetFromFile(const std::string &filename) - could not find file header!" << std::endl; file.close(); return false; } //Get the name of the dataset file >> word; if(word != "DatasetName:"){ errorLog << "loadDatasetFromFile(const std::string &filename) - failed to find DatasetName header!" << std::endl; errorLog << word << std::endl; file.close(); return false; } file >> datasetName; file >> word; if(word != "InfoText:"){ errorLog << "loadDatasetFromFile(const std::string &filename) - failed to find InfoText header!" << std::endl; file.close(); return false; } //Load the info text file >> word; infoText = ""; while( word != "NumDimensions:" ){ infoText += word + " "; file >> word; } //Get the number of dimensions in the training data if( word != "NumDimensions:" ){ errorLog << "loadDatasetFromFile(const std::string &filename) - failed to find NumDimensions header!" << std::endl; file.close(); return false; } file >> numDimensions; //Get the total number of training examples in the training data file >> word; if( word != "TotalNumTrainingExamples:" && word != "TotalNumExamples:" ){ errorLog << "loadDatasetFromFile(const std::string &filename) - failed to find TotalNumTrainingExamples header!" << std::endl; file.close(); return false; } file >> totalNumSamples; //Get the total number of classes in the training data file >> word; if(word != "NumberOfClasses:"){ errorLog << "loadDatasetFromFile(string filename) - failed to find NumberOfClasses header!" << std::endl; file.close(); return false; } file >> numClasses; //Resize the class counter buffer and load the counters classTracker.resize(numClasses); //Get the total number of classes in the training data file >> word; if(word != "ClassIDsAndCounters:"){ errorLog << "loadDatasetFromFile(const std::string &filename) - failed to find ClassIDsAndCounters header!" << std::endl; file.close(); return false; } for(UINT i=0; i<classTracker.getSize(); i++){ file >> classTracker[i].classLabel; file >> classTracker[i].counter; file >> classTracker[i].className; } //Check if the dataset should be scaled using external ranges file >> word; if(word != "UseExternalRanges:"){ errorLog << "loadDatasetFromFile(const std::string &filename) - failed to find UseExternalRanges header!" << std::endl; file.close(); return false; } file >> useExternalRanges; //If we are using external ranges then load them if( useExternalRanges ){ externalRanges.resize(numDimensions); for(UINT i=0; i<externalRanges.getSize(); i++){ file >> externalRanges[i].minValue; file >> externalRanges[i].maxValue; } } //Get the main training data file >> word; if( word != "LabelledTrainingData:" && word != "Data:"){ errorLog << "loadDatasetFromFile(const std::string &filename) - failed to find LabelledTrainingData header!" << std::endl; file.close(); return false; } ClassificationSample tempSample( numDimensions ); data.resize( totalNumSamples, tempSample ); for(UINT i=0; i<totalNumSamples; i++){ UINT classLabel = 0; VectorFloat sample(numDimensions,0); file >> classLabel; for(UINT j=0; j<numDimensions; j++){ file >> sample[j]; } data[i].set(classLabel, sample); } file.close(); //Sort the class labels sortClassLabels(); return true; }
void swapSamples(Sample& s1, Sample& s2) { Sample tempSample(s1); s1 = s2; s2 = tempSample; }