📄 nnshaperecognizer.cpp
字号:
returnStatus = deleteFeatureExtractorInstance();
if(returnStatus != SUCCESS)
{
LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error: " << returnStatus << " " <<
" NNShapeRecognizer::~NNShapeRecognizer()" << endl;
throw LTKException(returnStatus);
}
delete m_OSUtilPtr;
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting " <<
"NNShapeRecognizer::~NNShapeRecognizer()" << endl;
}
/**********************************************************************************
* AUTHOR : Saravanan R.
* DATE : 25-Jan-2004
* NAME : train
* DESCRIPTION :
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description
*************************************************************************************/
int NNShapeRecognizer::train(const string& trainingInputFilePath,
const string& mdtHeaderFilePath,
const string &comment,const string &dataset,
const string &trainFileType)
{
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entering " <<
"NNShapeRecognizer::train()" << endl;
int returnStatus = SUCCESS;
if(comment.empty() != true)
{
m_headerInfo[COMMENT] = comment;
}
if(dataset.empty() != true)
{
m_headerInfo[DATASET] = dataset;
}
//Check the prototype Selection method and call accordingly
if(LTKSTRCMP(m_prototypeSelection.c_str(), PROTOTYPE_SELECTION_LVQ) == 0)
{
returnStatus = trainLVQ(trainingInputFilePath, mdtHeaderFilePath, trainFileType);
if(returnStatus != SUCCESS)
{
LTKReturnError(returnStatus);
}
}
if(LTKSTRCMP(m_prototypeSelection.c_str(), PROTOTYPE_SELECTION_CLUSTERING) == 0)
{
returnStatus = trainClustering(trainingInputFilePath,
mdtHeaderFilePath,
trainFileType);
if(returnStatus != SUCCESS)
{
LTKReturnError(returnStatus);
}
}
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting " <<
"NNShapeRecognizer::train()" << endl;
return SUCCESS;
}
/**********************************************************************************
* AUTHOR : Saravanan R.
* DATE : 23-Jan-2007
* NAME : trainClustering
* DESCRIPTION : This function is the train method using Clustering prototype
* selection technique.
* ARGUMENTS :
* RETURNS : SUCCESS : if training done successfully
* errorCode : if traininhas some errors
* NOTES :
* CHANGE HISTROY
* Author Date Description
*************************************************************************************/
int NNShapeRecognizer::trainClustering(const string& trainingInputFilePath,
const string &mdtHeaderFilePath,
const string& inFileType)
{
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entering " <<
"NNShapeRecognizer::trainClustering()" << endl;
//Time at the beginning of Train Clustering
m_OSUtilPtr->recordStartTime();
int returnStatus = SUCCESS;
if(LTKSTRCMP(inFileType.c_str(), INK_FILE) == 0)
{
//If the Input file is UNIPEN Ink file
returnStatus = trainFromListFile(trainingInputFilePath);
if(returnStatus != SUCCESS)
{
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Error: " <<
getErrorMessage(returnStatus) <<
" NNShapeRecognizer::trainClustering()" << endl;
LTKReturnError(returnStatus);
}
}
#ifdef _INTERNAL
else if(LTKSTRCMP(inFileType.c_str(), FEATURE_FILE) == 0)
{
//If the Input file is Feature file
returnStatus = trainFromFeatureFile(trainingInputFilePath);
if(returnStatus != SUCCESS)
{
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Error: " <<
getErrorMessage(returnStatus) <<
" NNShapeRecognizer::trainClustering()" << endl;
LTKReturnError(returnStatus);
}
}
#endif
//Updating the Header Information
updateHeaderWithAlgoInfo();
//Adding header information and checksum generation
LTKCheckSumGenerate cheSumGen;
returnStatus = cheSumGen.addHeaderInfo(mdtHeaderFilePath,
m_nnMDTFilePath,
m_headerInfo);
if(returnStatus != SUCCESS)
{
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Error: " <<
getErrorMessage(returnStatus) <<
" NNShapeRecognizer::trainClustering()" << endl;
LTKReturnError(returnStatus);
}
//Time at the end of Train Clustering
m_OSUtilPtr->recordEndTime();
string timeTaken = "";
m_OSUtilPtr->diffTime(timeTaken);
cout << "Time Taken = " << timeTaken << endl;
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting " <<
"NNShapeRecognizer::trainClustering()" << endl;
return SUCCESS;
}
/**********************************************************************************
* AUTHOR : Saravanan R.
* DATE : 23-Jan-2007
* NAME : appendPrototypesToMDTFile
* DESCRIPTION :
* ARGUMENTS :
* RETURNS : none
* NOTES :
* CHANGE HISTROY
* Author Date Description
*************************************************************************************/
int NNShapeRecognizer::appendPrototypesToMDTFile(const vector<LTKShapeSample>& prototypeVec,
ofstream & mdtFileHandle)
{
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entering " <<
"NNShapeRecognizer::appendPrototypesToMDTFile()" << endl;
//iterators to iterate through the result vector
vector<LTKShapeSample>::const_iterator sampleFeatureIter = prototypeVec.begin();
vector<LTKShapeSample>::const_iterator sampleFeatureIterEnd = prototypeVec.end();
string strFeature = "";
if(!mdtFileHandle)
{
LOG(LTKLogger::LTK_LOGLEVEL_ERR)<<"Error: "<< EINVALID_FILE_HANDLE << " " <<
"Invalid file handle for MDT file"<<
" NNShapeRecognizer::appendPrototypesToMDTFile()" << endl;
LTKReturnError(EINVALID_FILE_HANDLE);
}
for(; sampleFeatureIter != sampleFeatureIterEnd; sampleFeatureIter++)
{
//Write the class Id
int classId = (*sampleFeatureIter).getClassID();
if ( m_MDTFileOpenMode == NN_MDT_OPEN_MODE_ASCII )
{
mdtFileHandle << classId << " ";
}
else
{
mdtFileHandle.write((char*) &classId,sizeof(int));
}
const vector<LTKShapeFeaturePtr>& shapeFeatureVector = (*sampleFeatureIter).getFeatureVector();
if ( m_MDTFileOpenMode == NN_MDT_OPEN_MODE_BINARY )
{
int numberOfFeatures = shapeFeatureVector.size();
int featureDimension = shapeFeatureVector[0]->getFeatureDimension();
mdtFileHandle.write((char *)(&numberOfFeatures), sizeof(int));
mdtFileHandle.write((char *)(&featureDimension), sizeof(int));
floatVector floatFeatureVector;
m_shapeRecUtil.shapeFeatureVectorToFloatVector(shapeFeatureVector,
floatFeatureVector);
int vectorSize = floatFeatureVector.size();
for (int i=0; i< vectorSize; i++)
{
float floatValue = floatFeatureVector[i];
mdtFileHandle.write((char *)(&floatValue), sizeof(float));
}
}
else
{
vector<LTKShapeFeaturePtr>::const_iterator shapeFeatureIter = shapeFeatureVector.begin();
vector<LTKShapeFeaturePtr>::const_iterator shapeFeatureIterEnd = shapeFeatureVector.end();
for(; shapeFeatureIter != shapeFeatureIterEnd; ++shapeFeatureIter)
{
(*shapeFeatureIter)->toString(strFeature);
mdtFileHandle << strFeature << FEATURE_EXTRACTOR_DELIMITER;
}
mdtFileHandle << "\n";
}
}
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting " <<
"NNShapeRecognizer::appendPrototypesToMDTFile()" << endl;
return SUCCESS;
}
/**********************************************************************************
* AUTHOR : Saravanan R.
* DATE : 23-Jan-2007
* NAME : preprocess
* DESCRIPTION : calls the required pre-processing functions from the LTKPreprocessor library
* ARGUMENTS : inTraceGroup - reference to the input trace group
* outPreprocessedTraceGroup - pre-processed inTraceGroup
* RETURNS : SUCCESS on successful pre-processing operation
* NOTES :
* CHANGE HISTROY
* Author Date Description
*************************************************************************************/
int NNShapeRecognizer::preprocess (const LTKTraceGroup& inTraceGroup,
LTKTraceGroup& outPreprocessedTraceGroup)
{
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entering " <<
"NNShapeRecognizer::preprocess()" << endl;
int indx = 0;
string module = "";
string funName = "" ;
LTKTraceGroup local_inTraceGroup;
local_inTraceGroup = inTraceGroup;
if(m_preprocSequence.size() != 0)
{
while(indx < m_preprocSequence.size())
{
module = m_preprocSequence.at(indx).first;
funName = m_preprocSequence.at(indx).second;
FN_PTR_PREPROCESSOR pPreprocFunc = NULL;
pPreprocFunc = m_ptrPreproc->getPreprocptr(funName);
if(pPreprocFunc!= NULL)
{
outPreprocessedTraceGroup.emptyAllTraces();
if((errorCode = (m_ptrPreproc->*(pPreprocFunc))
(local_inTraceGroup,outPreprocessedTraceGroup)) != SUCCESS)
{
LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error: "<< errorCode << " " <<
" NNShapeRecognizer::preprocess()" << endl;
LTKReturnError(errorCode);
}
local_inTraceGroup = outPreprocessedTraceGroup;
}
indx++;
}
}
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)<<"Exiting NNShapeRecognizer::preprocess()"<<endl;
return SUCCESS;
}
/**********************************************************************************
* AUTHOR : Saravanan R.
* DATE : 23-Jan-2007
* NAME : calculateMedian
* DESCRIPTION :
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description
*************************************************************************************/
int NNShapeRecognizer::calculateMedian(const int2DVector& clusteringResult,
const float2DVector& distanceMatrix, vector<int>& outMedianIndexVec)
{
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entering " <<
"NNShapeRecognizer::calculateMedian()" << endl;
int clusteringResultSize = clusteringResult.size();
for (int clusterID = 0; clusterID < clusteringResultSize ; clusterID++)
{
double minDist = FLT_MAX;
int medianIndex = -1;
for (int clusMem = 0; clusMem < clusteringResult[clusterID].size(); clusMem++)// for each element of the cluster
{
double dist = 0;
for(int otherClusMem = 0; otherClusMem < clusteringResult[clusterID].size(); otherClusMem++)
{
if(clusteringResult[clusterID][clusMem] != clusteringResult[clusterID][otherClusMem])
{
if(clusteringResult[clusterID][otherClusMem] > clusteringResult[clusterID][clusMem])
{
int tempi = clusteringResult[clusterID][clusMem];
int tempj = clusteringResult[clusterID][otherClusMem];
dist += distanceMatrix[tempi][tempj-tempi-1];
}
else
{
int tempi = clusteringResult[clusterID][otherClusMem];
int tempj = clusteringResult[clusterID][clusMem];
dist += distanceMatrix[tempi][tempj-tempi-1];
}
}
}
if(dist < minDist)
{
minDist = dist;
medianIndex = clusteringResult[clusterID][clusMem];
}
}
outMedianIndexVec.push_back(medianIndex);
}
LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting " <<
"NNShapeRecognizer::calculateMedian()" << endl;
return SUCCESS;
}
/**********************************************************************************
* AUTHOR : Ramnath. K
* DATE : 19-05-2005
* NAME : computerDTWDistanceClusteringWrapper
* DESCRIPTION : Wrapper function to the function whichcomputes DTW distance between
two characters
* ARGUMENTS : train character, test character
* RETURNS : DTWDistance
* NOTES :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -