Commit fe920d6d authored by Serrière Guillaume's avatar Serrière Guillaume

Use the new xml interface in classification.

Signed-off-by: default avatarSerrière Guillaume <guillaume.serriere@inria.fr>
parent 36e60e8e
......@@ -18,6 +18,8 @@ IF(PATH_OPENVIBE_TOOLKIT)
MESSAGE(STATUS " Found openvibe-toolkit...")
INCLUDE_DIRECTORIES(${PATH_OPENVIBE_TOOLKIT}/)
INCLUDE("FindOpenViBEModuleXML")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} openvibe-toolkit${OPENVIBE_TOOLKIT_LINKING})
ADD_DEFINITIONS(-DTARGET_HAS_OpenViBEToolkit)
......
......@@ -16,7 +16,7 @@ namespace XML
virtual XML::IXMLNode* parseString(const char* sString, const uint32& uiSize)=0;
//XML extraction
virtual void writeXMLInFile(IXMLNode &rNode, const char* sPath)=0;
virtual XML::boolean writeXMLInFile(IXMLNode &rNode, const char* sPath)=0;
protected:
virtual ~IXMLHandler() { }
......
......@@ -18,7 +18,7 @@ namespace XML
virtual XML::IXMLNode* parseString(const char* sString, const uint32& uiSize);
//XML extraction
virtual void writeXMLInFile(IXMLNode &rNode, const char* sPath);
virtual XML::boolean writeXMLInFile(IXMLNode &rNode, const char* sPath);
//Internal function for parsing
virtual void openChild(const char* sName, const char** sAttributeName, const char** sAttributeValue, uint64 ui64AttributeCount);
......@@ -119,8 +119,16 @@ IXMLNode *IXMLHandlerImpl::parseString(const char *sString, const uint32& uiSize
return m_pRootNode;
}
void IXMLHandlerImpl::writeXMLInFile(IXMLNode &rNode, const char *sPath)
boolean IXMLHandlerImpl::writeXMLInFile(IXMLNode &rNode, const char *sPath)
{
std::ofstream l_oFile(sPath, ios::binary);
if(l_oFile.is_open())
{
l_oFile.write(rNode.getXML().c_str(), rNode.getXML().length());
l_oFile.close();
return true;
}
return false;
}
void IXMLHandlerImpl::openChild(const char *sName, const char **sAttributeName, const char **sAttributeValue, uint64 ui64AttributeCount)
......
......@@ -6,6 +6,9 @@
#include <sstream>
#include <cmath>
#include <xml/IXMLNode.h>
#include <xml/IXMLHandler.h>
using namespace OpenViBE;
using namespace OpenViBE::Kernel;
using namespace OpenViBE::Plugins;
......@@ -25,8 +28,8 @@ boolean CAlgorithmClassifierLDA::train(const IFeatureVectorSet& rFeatureVectorSe
}
if(l_vClassLabels.size() != 2)
{
this->getLogManager() << LogLevel_Error << "A LDA classifier can only be trained with 2 classes, not more, not less - got " << (uint32)l_vClassLabels.size() << "\n";
{
this->getLogManager() << LogLevel_Error << "A LDA classifier can only be trained with 2 classes, not more, not less - got " << (uint32)l_vClassLabels.size() << "\n";
return false;
}
......@@ -100,20 +103,17 @@ boolean CAlgorithmClassifierLDA::train(const IFeatureVectorSet& rFeatureVectorSe
l_sCoefficients << " " << m_oCoefficients[i];
}
m_oConfiguration.setSize(0, true);
XML::IWriter* l_pWriter=XML::createWriter(*this);
l_pWriter->openChild("OpenViBE-Classifier");
l_pWriter->openChild("LDA");
l_pWriter->openChild("Classes");
l_pWriter->setChildData(l_sClasses.str().c_str());
l_pWriter->closeChild();
l_pWriter->openChild("Coefficients");
l_pWriter->setChildData(l_sCoefficients.str().c_str());
l_pWriter->closeChild();
l_pWriter->closeChild();
l_pWriter->closeChild();
l_pWriter->release();
l_pWriter=NULL;
XML::IXMLNode *l_pClassesNode = XML::createNode("Classes");
l_pClassesNode->setPCData(l_sClasses.str().c_str());
XML::IXMLNode *l_pCoefficientsNode = XML::createNode("Coefficients");
l_pCoefficientsNode->setPCData(l_sCoefficients.str().c_str());
XML::IXMLNode *l_pAlgorithmNode = XML::createNode("LDA");
l_pAlgorithmNode->addChild(l_pClassesNode);
l_pAlgorithmNode->addChild(l_pCoefficientsNode);
m_pConfigurationNode = XML::createNode("OpenViBE-Classifier");
m_pConfigurationNode->addChild(l_pAlgorithmNode);
return true;
}
......@@ -149,77 +149,55 @@ boolean CAlgorithmClassifierLDA::classify(const IFeatureVector& rFeatureVector,
uint32 CAlgorithmClassifierLDA::getBestClassification(IMatrix& rFirstClassificationValue, IMatrix& rSecondClassificationValue)
{
if(::fabs(rFirstClassificationValue[0]) < ::fabs(rSecondClassificationValue[0]) )
return -1;
else if(::fabs(rFirstClassificationValue[0]) == ::fabs(rSecondClassificationValue[0]))
return 0;
else
return 1;
if(::fabs(rFirstClassificationValue[0]) < ::fabs(rSecondClassificationValue[0]) )
return -1;
else if(::fabs(rFirstClassificationValue[0]) == ::fabs(rSecondClassificationValue[0]))
return 0;
else
return 1;
}
boolean CAlgorithmClassifierLDA::saveConfiguration(IMemoryBuffer& rMemoryBuffer)
XML::IXMLNode* CAlgorithmClassifierLDA::saveConfiguration(void)
{
rMemoryBuffer.append(m_oConfiguration);
return true;
return m_pConfigurationNode;
}
boolean CAlgorithmClassifierLDA::loadConfiguration(const IMemoryBuffer& rMemoryBuffer)
boolean CAlgorithmClassifierLDA::loadConfiguration(XML::IXMLNode *pConfiguratioNode)
{
m_f64Class1=0;
m_f64Class2=0;
m_pIReader = NULL;
XML::IReader* l_pReader=XML::createReader(*this);
l_pReader->processData(rMemoryBuffer.getDirectPointer(), rMemoryBuffer.getSize());
l_pReader->release();
l_pReader=NULL;
loadClassesFromNode(pConfiguratioNode->getChild(0)->getChildByName("Classes"));
loadCoefficientsFromNode(pConfiguratioNode->getChild(0)->getChildByName("Coefficients"));
return true;
return true;
}
void CAlgorithmClassifierLDA::write(const char* sString)
void CAlgorithmClassifierLDA::loadClassesFromNode(XML::IXMLNode *pNode)
{
m_oConfiguration.append((const uint8*)sString, ::strlen(sString));
}
std::stringstream l_sData(pNode->getPCData());
void CAlgorithmClassifierLDA::openChild(const char* sName, const char** sAttributeName, const char** sAttributeValue, XML::uint64 ui64AttributeCount)
{
m_vNode.push(sName);
l_sData >> m_f64Class1;
l_sData >> m_f64Class2;
}
void CAlgorithmClassifierLDA::processChildData(const char* sData)
void CAlgorithmClassifierLDA::loadCoefficientsFromNode(XML::IXMLNode *pNode)
{
std::stringstream l_sData(sData);
std::stringstream l_sData(pNode->getPCData());
if(m_vNode.top()==CString("Classes"))
{
l_sData >> m_f64Class1;
l_sData >> m_f64Class2;
std::vector < float64 > l_vCoefficients;
while(!l_sData.eof())
{
float64 l_f64Value;
l_sData >> l_f64Value;
l_vCoefficients.push_back(l_f64Value);
}
if(m_vNode.top()==CString("Coefficients"))
{
std::vector < float64 > l_vCoefficients;
while(!l_sData.eof())
{
float64 l_f64Value;
l_sData >> l_f64Value;
l_vCoefficients.push_back(l_f64Value);
}
m_oCoefficients.set_size(l_vCoefficients.size());
for(size_t i=0; i<l_vCoefficients.size(); i++)
{
m_oCoefficients[i]=l_vCoefficients[i];
}
m_oCoefficients.set_size(l_vCoefficients.size());
for(size_t i=0; i<l_vCoefficients.size(); i++)
{
m_oCoefficients[i]=l_vCoefficients[i];
}
}
void CAlgorithmClassifierLDA::closeChild(void)
{
m_vNode.pop();
}
#endif // TARGET_HAS_ThirdPartyITPP
......@@ -8,11 +8,9 @@
#include <openvibe/ov_all.h>
#include <toolkit/ovtk_all.h>
#include <xml/IWriter.h>
#include <xml/IReader.h>
#include <stack>
#include <xml/IXMLNode.h>
#define TARGET_HAS_ThirdPartyITPP
#if defined TARGET_HAS_ThirdPartyITPP
#include <itpp/itbase.h>
......@@ -24,15 +22,15 @@ namespace OpenViBEPlugins
{
namespace Local
{
class CAlgorithmClassifierLDA : public OpenViBEToolkit::CAlgorithmClassifier, public XML::IWriterCallback, public XML::IReaderCallback
class CAlgorithmClassifierLDA : public OpenViBEToolkit::CAlgorithmClassifier
{
public:
virtual OpenViBE::boolean train(const OpenViBEToolkit::IFeatureVectorSet& rFeatureVectorSet);
virtual OpenViBE::boolean classify(const OpenViBEToolkit::IFeatureVector& rFeatureVector, OpenViBE::float64& rf64Class, OpenViBEToolkit::IVector& rClassificationValues);
virtual OpenViBE::boolean saveConfiguration(OpenViBE::IMemoryBuffer& rMemoryBuffer);
virtual OpenViBE::boolean loadConfiguration(const OpenViBE::IMemoryBuffer& rMemoryBuffer);
virtual XML::IXMLNode* saveConfiguration(void);
virtual OpenViBE::boolean loadConfiguration(XML::IXMLNode* pConfiguratioNode);
virtual OpenViBE::uint32 getBestClassification(OpenViBE::IMatrix& rFirstClassificationValue, OpenViBE::IMatrix& rSecondClassificationValue);
......@@ -40,20 +38,16 @@ namespace OpenViBEPlugins
protected:
virtual void write(const char* sString); // XML IWriterCallback
virtual void openChild(const char* sName, const char** sAttributeName, const char** sAttributeValue, XML::uint64 ui64AttributeCount); // XML IReaderCallback
virtual void processChildData(const char* sData); // XML IReaderCallback
virtual void closeChild(void); // XML ReaderCallback
std::stack<OpenViBE::CString> m_vNode;
OpenViBE::float64 m_f64Class1;
OpenViBE::float64 m_f64Class2;
OpenViBE::CMemoryBuffer m_oConfiguration;
itpp::vec m_oCoefficients;
XML::IReader *m_pIReader;
XML::IXMLNode *m_pConfigurationNode;
private:
void loadClassesFromNode(XML::IXMLNode *pNode);
void loadCoefficientsFromNode(XML::IXMLNode *pNode);
};
class CAlgorithmClassifierLDADesc : public OpenViBEToolkit::CAlgorithmClassifierDesc
......
......@@ -56,12 +56,12 @@ uint32 CAlgorithmClassifierNULL::getBestClassification(IMatrix& rFirstClassifica
return 0;
}
boolean CAlgorithmClassifierNULL::saveConfiguration(IMemoryBuffer& rMemoryBuffer)
XML::IXMLNode* CAlgorithmClassifierNULL::saveConfiguration(void)
{
return true;
return NULL;
}
boolean CAlgorithmClassifierNULL::loadConfiguration(const IMemoryBuffer& rMemoryBuffer)
boolean CAlgorithmClassifierNULL::loadConfiguration(XML::IXMLNode *pConfigurationNode)
{
return true;
}
......@@ -5,6 +5,8 @@
#include <openvibe/ov_all.h>
#include <toolkit/ovtk_all.h>
#include <xml/IXMLNode.h>
#define OVP_ClassId_Algorithm_ClassifierNULL OpenViBE::CIdentifier(0x043D09AB, 0xCB5E4859)
#define OVP_ClassId_Algorithm_ClassifierNULLDesc OpenViBE::CIdentifier(0x3B365233, 0x812C47DD)
......@@ -27,8 +29,8 @@ namespace OpenViBEPlugins
virtual OpenViBE::boolean train(const OpenViBEToolkit::IFeatureVectorSet& rFeatureVectorSet);
virtual OpenViBE::boolean classify(const OpenViBEToolkit::IFeatureVector& rFeatureVector, OpenViBE::float64& rf64Class, OpenViBEToolkit::IVector& rClassificationValues);
virtual OpenViBE::boolean saveConfiguration(OpenViBE::IMemoryBuffer& rMemoryBuffer);
virtual OpenViBE::boolean loadConfiguration(const OpenViBE::IMemoryBuffer& rMemoryBuffer);
virtual XML::IXMLNode* saveConfiguration(void);
virtual OpenViBE::boolean loadConfiguration(XML::IXMLNode *pConfigurationNode);
virtual OpenViBE::uint32 getBestClassification(OpenViBE::IMatrix &rFirstClassificationValue, OpenViBE::IMatrix &rSecondClassificationValue);
......
......@@ -179,7 +179,7 @@ void CAlgorithmClassifierOneVsAll::getClassifierConfiguration(IAlgorithmProxy* r
rClassifier->process(OVTK_Algorithm_Classifier_InputTriggerId_SaveConfiguration);
}
boolean CAlgorithmClassifierOneVsAll::saveConfiguration(IMemoryBuffer& rMemoryBuffer)
XML::IXMLNode* CAlgorithmClassifierOneVsAll::saveConfiguration(void)
{
std::stringstream l_sAmountClasses;
l_sAmountClasses << this->m_iAmountClass;
......@@ -187,7 +187,7 @@ boolean CAlgorithmClassifierOneVsAll::saveConfiguration(IMemoryBuffer& rMemoryBu
std::stringstream l_sClassIdentifier;
l_sClassIdentifier << this->m_oSubClassifierAlgorithmIdentifier.toUInteger();
m_oConfiguration = rMemoryBuffer;
//m_oConfiguration = rMemoryBuffer;
XML::IWriter* l_pWriter=XML::createWriter(*this);
l_pWriter->openChild("OpenViBE-Classifier");
l_pWriter->openChild("OneVsAll");
......@@ -203,7 +203,7 @@ boolean CAlgorithmClassifierOneVsAll::saveConfiguration(IMemoryBuffer& rMemoryBu
{
l_pWriter->openChild("SubClassifier");
l_pWriter->setChildData("");
this->getClassifierConfiguration(m_oSubClassifierList[i], rMemoryBuffer);
//this->getClassifierConfiguration(m_oSubClassifierList[i], rMemoryBuffer);
l_pWriter->closeChild();
}
......@@ -212,14 +212,14 @@ boolean CAlgorithmClassifierOneVsAll::saveConfiguration(IMemoryBuffer& rMemoryBu
l_pWriter->release();
l_pWriter=NULL;
return true;
return NULL;
}
boolean CAlgorithmClassifierOneVsAll::loadConfiguration(const IMemoryBuffer& rMemoryBuffer)
boolean CAlgorithmClassifierOneVsAll::loadConfiguration(XML::IXMLNode *pConfigurationNode)
{
this->m_iClassCounter = 0;
XML::IReader* l_pReader=XML::createReader(*this);
l_pReader->processData(rMemoryBuffer.getDirectPointer(), rMemoryBuffer.getSize());
//l_pReader->processData(rMemoryBuffer.getDirectPointer(), rMemoryBuffer.getSize());
l_pReader->release();
l_pReader=NULL;
......@@ -243,7 +243,7 @@ void CAlgorithmClassifierOneVsAll::processChildData(const char* sData)
if(m_vNode.top() == CString("SubClassifier"))
{
//We should be able to load configuration from scratch or to load it in an existing configuration
((CAlgorithmClassifier*)m_oSubClassifierList[this->m_iClassCounter])->loadConfiguration(m_oConfiguration);
//((CAlgorithmClassifier*)m_oSubClassifierList[this->m_iClassCounter])->loadConfiguration(m_oConfiguration);
++(this->m_iClassCounter);
}
else if(m_vNode.top() == CString("SubClassifierIdentifier"))
......
......@@ -19,72 +19,72 @@
namespace OpenViBEPlugins
{
namespace Local
{
class CAlgorithmClassifierOneVsAll : public OpenViBEToolkit::CAlgorithmPairingStrategy, public XML::IWriterCallback, public XML::IReaderCallback
{
public:
namespace Local
{
class CAlgorithmClassifierOneVsAll : public OpenViBEToolkit::CAlgorithmPairingStrategy, public XML::IWriterCallback, public XML::IReaderCallback
{
public:
virtual OpenViBE::boolean uninitialize(void);
virtual OpenViBE::boolean train(const OpenViBEToolkit::IFeatureVectorSet& rFeatureVectorSet);
virtual OpenViBE::boolean classify(const OpenViBEToolkit::IFeatureVector& rFeatureVector, OpenViBE::float64& rf64Class, OpenViBEToolkit::IVector& rClassificationValues);
virtual OpenViBE::boolean designArchitecture(OpenViBE::CIdentifier &rId, OpenViBE::uint64& rClassAmount);
virtual OpenViBE::boolean uninitialize(void);
virtual OpenViBE::boolean train(const OpenViBEToolkit::IFeatureVectorSet& rFeatureVectorSet);
virtual OpenViBE::boolean classify(const OpenViBEToolkit::IFeatureVector& rFeatureVector, OpenViBE::float64& rf64Class, OpenViBEToolkit::IVector& rClassificationValues);
virtual OpenViBE::boolean designArchitecture(OpenViBE::CIdentifier &rId, OpenViBE::uint64& rClassAmount);
virtual OpenViBE::boolean saveConfiguration(OpenViBE::IMemoryBuffer& rMemoryBuffer);
virtual OpenViBE::boolean loadConfiguration(const OpenViBE::IMemoryBuffer& rMemoryBuffer);
virtual OpenViBE::uint32 getBestClassification(OpenViBE::IMatrix& rFirstClassificationValue, OpenViBE::IMatrix& rSecondClassificationValue);
virtual XML::IXMLNode* saveConfiguration(void);
virtual OpenViBE::boolean loadConfiguration(XML::IXMLNode *pConfigurationNode);
virtual OpenViBE::uint32 getBestClassification(OpenViBE::IMatrix& rFirstClassificationValue, OpenViBE::IMatrix& rSecondClassificationValue);
_IsDerivedFromClass_Final_(CAlgorithmPairingStrategy, OVP_ClassId_Algorithm_ClassifierOneVsAll);
_IsDerivedFromClass_Final_(CAlgorithmPairingStrategy, OVP_ClassId_Algorithm_ClassifierOneVsAll);
protected:
protected:
virtual void write(const char* sString); // XML IWriterCallback
virtual void write(const char* sString); // XML IWriterCallback
virtual void getClassifierConfiguration(OpenViBE::Kernel::IAlgorithmProxy* classifier, OpenViBE::IMemoryBuffer& rConfiguration);
virtual void openChild(const char* sName, const char** sAttributeName, const char** sAttributeValue, XML::uint64 ui64AttributeCount); // XML IReaderCallback
virtual void processChildData(const char* sData); // XML IReaderCallback
virtual void closeChild(void); // XML ReaderCallback
virtual void getClassifierConfiguration(OpenViBE::Kernel::IAlgorithmProxy* classifier, OpenViBE::IMemoryBuffer& rConfiguration);
virtual void openChild(const char* sName, const char** sAttributeName, const char** sAttributeValue, XML::uint64 ui64AttributeCount); // XML IReaderCallback
virtual void processChildData(const char* sData); // XML IReaderCallback
virtual void closeChild(void); // XML ReaderCallback
void addNewClassifierAtBack(void);
void removeClassifierAtBack(void);
std::stack<OpenViBE::CString> m_vNode;
std::vector<OpenViBE::Kernel::IAlgorithmProxy*> m_oSubClassifierList;
OpenViBE::uint64 m_iClassCounter; //This variable is use during configuration loading
std::stack<OpenViBE::CString> m_vNode;
OpenViBE::CMemoryBuffer m_oConfiguration;
std::vector<OpenViBE::Kernel::IAlgorithmProxy*> m_oSubClassifierList;
OpenViBE::uint64 m_iClassCounter; //This variable is use during configuration loading
private:
void addNewClassifierAtBack(void);
void removeClassifierAtBack(void);
};
OpenViBE::CMemoryBuffer m_oConfiguration;
};
class CAlgorithmClassifierOneVsAllDesc : public OpenViBEToolkit::CAlgorithmPairingStrategyDesc
{
public:
class CAlgorithmClassifierOneVsAllDesc : public OpenViBEToolkit::CAlgorithmPairingStrategyDesc
{
public:
virtual void release(void) { }
virtual void release(void) { }
virtual OpenViBE::CString getName(void) const { return OpenViBE::CString("OneVsAll pairing classifier"); }
virtual OpenViBE::CString getAuthorName(void) const { return OpenViBE::CString("Guillaume Serriere"); }
virtual OpenViBE::CString getAuthorCompanyName(void) const { return OpenViBE::CString("INRIA/IRISA / INSA/IRISA"); }
virtual OpenViBE::CString getShortDescription(void) const { return OpenViBE::CString(""); }
virtual OpenViBE::CString getDetailedDescription(void) const { return OpenViBE::CString(""); }
virtual OpenViBE::CString getCategory(void) const { return OpenViBE::CString(""); }
virtual OpenViBE::CString getVersion(void) const { return OpenViBE::CString("0.1"); }
virtual OpenViBE::CString getName(void) const { return OpenViBE::CString("OneVsAll pairing classifier"); }
virtual OpenViBE::CString getAuthorName(void) const { return OpenViBE::CString("Guillaume Serriere"); }
virtual OpenViBE::CString getAuthorCompanyName(void) const { return OpenViBE::CString("INRIA/IRISA / INSA/IRISA"); }
virtual OpenViBE::CString getShortDescription(void) const { return OpenViBE::CString(""); }
virtual OpenViBE::CString getDetailedDescription(void) const { return OpenViBE::CString(""); }
virtual OpenViBE::CString getCategory(void) const { return OpenViBE::CString(""); }
virtual OpenViBE::CString getVersion(void) const { return OpenViBE::CString("0.1"); }
virtual OpenViBE::CIdentifier getCreatedClass(void) const { return OVP_ClassId_Algorithm_ClassifierOneVsAll; }
virtual OpenViBE::Plugins::IPluginObject* create(void) { return new OpenViBEPlugins::Local::CAlgorithmClassifierOneVsAll; }
virtual OpenViBE::CIdentifier getCreatedClass(void) const { return OVP_ClassId_Algorithm_ClassifierOneVsAll; }
virtual OpenViBE::Plugins::IPluginObject* create(void) { return new OpenViBEPlugins::Local::CAlgorithmClassifierOneVsAll; }
virtual OpenViBE::boolean getAlgorithmPrototype(
OpenViBE::Kernel::IAlgorithmProto& rAlgorithmPrototype) const
{
CAlgorithmPairingStrategyDesc::getAlgorithmPrototype(rAlgorithmPrototype);
return true;
}
virtual OpenViBE::boolean getAlgorithmPrototype(
OpenViBE::Kernel::IAlgorithmProto& rAlgorithmPrototype) const
{
CAlgorithmPairingStrategyDesc::getAlgorithmPrototype(rAlgorithmPrototype);
return true;
}
_IsDerivedFromClass_Final_(CAlgorithmPairingStrategyDesc, OVP_ClassId_Algorithm_ClassifierOneVsAllDesc);
};
};
_IsDerivedFromClass_Final_(CAlgorithmPairingStrategyDesc, OVP_ClassId_Algorithm_ClassifierOneVsAllDesc);
};
};
};
#endif // __OpenViBEPlugins_Algorithm_OneVsAll_H__
......@@ -5,8 +5,7 @@
#include <openvibe/ov_all.h>
#include <toolkit/ovtk_all.h>
#include <xml/IWriter.h>
#include <xml/IReader.h>
#include <xml/IXMLNode.h>
#include <stack>
#include "../../../contrib/packages/libSVM/svm.h"
......@@ -36,7 +35,7 @@ namespace OpenViBEPlugins
{
namespace Classification
{
class CAlgorithmClassifierSVM : public OpenViBEToolkit::CAlgorithmClassifier, public XML::IWriterCallback, public XML::IReaderCallback
class CAlgorithmClassifierSVM : public OpenViBEToolkit::CAlgorithmClassifier
{
public:
......@@ -47,24 +46,16 @@ namespace OpenViBEPlugins
virtual OpenViBE::boolean train(const OpenViBEToolkit::IFeatureVectorSet& rFeatureVectorSet);
virtual OpenViBE::boolean classify(const OpenViBEToolkit::IFeatureVector& rFeatureVector, OpenViBE::float64& rf64Class, OpenViBEToolkit::IVector& rClassificationValues);
virtual OpenViBE::boolean saveConfiguration(OpenViBE::IMemoryBuffer& rMemoryBuffer);
virtual OpenViBE::boolean loadConfiguration(const OpenViBE::IMemoryBuffer& rMemoryBuffer);
virtual XML::IXMLNode* saveConfiguration(void);
virtual OpenViBE::boolean loadConfiguration(XML::IXMLNode *pConfigurationNode);
virtual OpenViBE::CString paramToString(svm_parameter *pParam);
virtual OpenViBE::CString modelToString();
virtual OpenViBE::CString problemToString(svm_problem *pProb);
virtual OpenViBE::uint32 getBestClassification(OpenViBE::IMatrix &rFirstClassificationValue, OpenViBE::IMatrix &rSecondClassificationValue);
virtual OpenViBE::uint32 getBestClassification(OpenViBE::IMatrix &rFirstClassificationValue, OpenViBE::IMatrix &rSecondClassificationValue);
_IsDerivedFromClass_Final_(CAlgorithmClassifier, OVP_ClassId_Algorithm_ClassifierSVM);
protected:
virtual void write(const char* sString); // XML IWriterCallback
virtual void openChild(const char* sName, const char** sAttributeName, const char** sAttributeValue, XML::uint64 ui64AttributeCount); // XML IReaderCallback
virtual void processChildData(const char* sData); // XML IReaderCallback
virtual void closeChild(void); // XML ReaderCallback
std::stack<OpenViBE::CString> m_vNode;
std::vector <OpenViBE::float64> m_vClass;
struct svm_parameter m_oParam;
......@@ -76,6 +67,13 @@ namespace OpenViBEPlugins
OpenViBE::CMemoryBuffer m_oConfiguration;
//todo a modifier en fonction de svn_save_model
//vector m_oCoefficients;
XML::IXMLNode *m_pConfigurationNode;
private:
void loadParamNodeConfiguration(XML::IXMLNode *pParamNode);
void loadModelNodeConfiguration(XML::IXMLNode *pModelNode);
void loadModelSVsNodeConfiguration(XML::IXMLNode* pSVsNodeParam);
};
class CAlgorithmClassifierSVMDesc : public OpenViBEToolkit::CAlgorithmClassifierDesc
......
......@@ -4,8 +4,10 @@
#include <map>
#include <sstream>
#include <iostream>
#include <system/Memory.h>
#include <xml/IXMLHandler.h>
#include <Eigen/Eigenvalues>
......@@ -193,23 +195,20 @@ boolean CAlgorithmClassifierShrinkageLDA::train(const IFeatureVectorSet& rFeatur
l_sCoefficients << " " << m_oCoefficients(0,i);
}
m_oConfiguration.setSize(0, true);
XML::IWriter* l_pWriter=XML::createWriter(*this);
l_pWriter->openChild("OpenViBE-Classifier");
l_pWriter->openChild("LDA");
l_pWriter->openChild("Creator");
l_pWriter->setChildData("ShrinkageLDA");
l_pWriter->closeChild();
l_pWriter->openChild("Classes");
l_pWriter->setChildData(l_sClasses.str().c_str());
l_pWriter->closeChild();
l_pWriter->openChild("Coefficients");
l_pWriter->setChildData(l_sCoefficients.str().c_str());
l_pWriter->closeChild();
l_pWriter->closeChild();
l_pWriter->closeChild();
l_pWriter->release();
l_pWriter=NULL;
XML::IXMLNode *l_pCreatorNode = XML::createNode("Creator");
l_pCreatorNode->setPCData("ShrinkageLDA");
XML::IXMLNode *l_pClassesNode = XML::createNode("Classes");
l_pClassesNode->setPCData(l_sClasses.str().c_str());
XML::IXMLNode *l_pCoefficientsNode = XML::createNode("Coefficients");
l_pCoefficientsNode->setPCData(l_sCoefficients.str().c_str());
XML::IXMLNode *l_pAlgorithmNode = XML::createNode("LDA");
l_pAlgorithmNode->addChild(l_pCreatorNode);
l_pAlgorithmNode->addChild(l_pClassesNode);
l_pAlgorithmNode->addChild(l_pCoefficientsNode);
m_pConfigurationNode = XML::createNode("OpenViBE-Classifier");
m_pConfigurationNode->addChild(l_pAlgorithmNode);
// Debug output
dumpMatrix(this->getLogManager(), l_oGlobalCov, "Global cov");
......@@ -257,75 +256,55 @@ boolean CAlgorithmClassifierShrinkageLDA::classify(const IFeatureVector& rFeatur
uint32 CAlgorithmClassifierShrinkageLDA::getBestClassification(IMatrix& rFirstClassificationValue, IMatrix& rSecondClassificationValue)
{
if(rFirstClassificationValue[0] < rSecondClassificationValue[0] )
return -1;
else if(rFirstClassificationValue[0] == rSecondClassificationValue[0])
return 0;
else
return 1;
if(rFirstClassificationValue[0] < rSecondClassificationValue[0] )
return -1;
else if(rFirstClassificationValue[0] == rSecondClassificationValue[0])
return 0;
else
return 1;
}
boolean CAlgorithmClassifierShrinkageLDA::saveConfiguration(IMemoryBuffer& rMemoryBuffer)
XML::IXMLNode* CAlgorithmClassifierShrinkageLDA::saveConfiguration(void)
{
rMemoryBuffer.setSize(0, true);
rMemoryBuffer.append(m_oConfiguration);
return true;
return m_pConfigurationNode;
}
boolean CAlgorithmClassifierShrinkageLDA::loadConfiguration(const IMemoryBuffer& rMemoryBuffer)
boolean CAlgorithmClassifierShrinkageLDA::loadConfiguration(XML::IXMLNode *pConfigurationNode)
{
m_f64Class1=0;
m_f64Class2=0;
XML::IReader* l_pReader=XML::createReader(*this);
l_pReader->processData(rMemoryBuffer.getDirectPointer(), rMemoryBuffer.getSize());
l_pReader->release();
l_pReader=NULL;
loadClassesFromNode(pConfigurationNode->getChild(0)->getChildByName("Classes"));
loadCoefficientsFromNode(pConfigurationNode->getChild(0)->getChildByName("Coefficients"));
return true;
}
void CAlgorithmClassifierShrinkageLDA::write(const char* sString)
void CAlgorithmClassifierShrinkageLDA::loadClassesFromNode(XML::IXMLNode *pNode)
{
m_oConfiguration.append((const uint8*)sString, ::strlen(sString));
}
std::stringstream l_sData(pNode->getPCData());
void CAlgorithmClassifierShrinkageLDA::openChild(const char* sName, const char** sAttributeName, const char** sAttributeValue, XML::uint64 ui64AttributeCount)
{
m_vNode.push(sName);
l_sData >> m_f64Class1;
l_sData >> m_f64Class2;
} </