21 #define GRT_DLL_EXPORTS
31 this->numClusters = numClusters;
33 classType =
"SOMQuantizer";
34 featureExtractionType = classType;
35 debugLog.setProceedingText(
"[DEBUG SOMQuantizer]");
36 errorLog.setProceedingText(
"[ERROR SOMQuantizer]");
37 warningLog.setProceedingText(
"[WARNING SOMQuantizer]");
42 classType =
"SOMQuantizer";
43 featureExtractionType = classType;
44 debugLog.setProceedingText(
"[DEBUG SOMQuantizer]");
45 errorLog.setProceedingText(
"[ERROR SOMQuantizer]");
46 warningLog.setProceedingText(
"[WARNING SOMQuantizer]");
59 this->numClusters = rhs.numClusters;
61 this->quantizationDistances = rhs.quantizationDistances;
71 if( featureExtraction == NULL )
return false;
82 errorLog <<
"clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
102 std::fill(quantizationDistances.begin(),quantizationDistances.end(),0);
114 quantizationDistances.clear();
122 file.open(filename.c_str(), std::ios::out);
136 file.open(filename.c_str(), std::ios::in);
150 if( !file.is_open() ){
151 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
156 file <<
"SOM_QUANTIZER_FILE_V1.0" << std::endl;
160 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
164 file <<
"QuantizerTrained: " << trained << std::endl;
165 file <<
"NumClusters: " << numClusters << std::endl;
170 errorLog <<
"saveModelToFile(fstream &file) - Failed to save SelfOrganizingMap settings to file!" << std::endl;
183 if( !file.is_open() ){
184 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
192 if( word !=
"SOM_QUANTIZER_FILE_V1.0" ){
193 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
199 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
204 if( word !=
"QuantizerTrained:" ){
205 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load QuantizerTrained!" << std::endl;
211 if( word !=
"NumClusters:" ){
212 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load NumClusters!" << std::endl;
219 if( word !=
"SOM:" ){
220 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load SOM!" << std::endl;
225 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load SelfOrganizingMap settings from file!" << std::endl;
230 featureDataReady =
false;
231 quantizationDistances.
resize(numClusters,0);
263 errorLog <<
"train_(MatrixFloat &trainingData) - Failed to train quantizer, the training data is empty!" << std::endl;
268 som.setNetworkSize( numClusters );
269 som.setNetworkTypology( SelfOrganizingMap::RANDOM_NETWORK );
270 som.setAlphaStart( 0.5 );
271 som.setAlphaEnd( 0.1 );
274 if( !som.
train_( trainingData ) ){
275 errorLog <<
"train(MatrixFloat &trainingData) - Failed to train quantizer!" << std::endl;
282 numInputDimensions = trainingData.
getNumCols();
283 numOutputDimensions = 1;
284 featureVector.
resize(numOutputDimensions,0);
285 quantizationDistances.
resize(numClusters,0);
297 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The quantizer model has not been trained!" << std::endl;
301 if( inputVector.
getSize() != numInputDimensions ){
302 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.
getSize() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
307 if( !som.
predict( inputVector ) ){
308 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - Failed to perform map!" << std::endl;
311 quantizationDistances = som.getMappedData();
314 UINT quantizedValue = 0;
316 for(UINT k=0; k<numClusters; k++){
317 if( quantizationDistances[k] > maxValue ){
318 maxValue = quantizationDistances[k];
323 featureVector[0] = quantizedValue;
324 featureDataReady =
true;
326 return quantizedValue;
338 return (trained ? static_cast<UINT>(featureVector[0]) : 0);
342 return quantizationDistances;
351 this->numClusters = numClusters;
virtual bool saveModelToFile(std::string filename) const
UINT quantize(const Float inputValue)
virtual bool predict(VectorFloat inputVector)
virtual bool train_(ClassificationData &trainingData)
bool setNumClusters(const UINT numClusters)
VectorFloat getQuantizationDistances() const
virtual bool computeFeatures(const VectorFloat &inputVector)
MatrixFloat getDataAsMatrixFloat() const
The SOMQuantizer module quantizes the N-dimensional input vector to a 1-dimensional discrete value...
virtual bool resize(const unsigned int size)
virtual bool train_(MatrixFloat &trainingData)
bool getQuantizerTrained() const
SOMQuantizer(const UINT numClusters=10)
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
MatrixFloat getDataAsMatrixFloat() const
unsigned int getNumRows() const
MatrixFloat getDataAsMatrixFloat() const
SelfOrganizingMap getSelfOrganizingMap() const
unsigned int getNumCols() const
virtual bool saveModelToFile(std::fstream &file) const
virtual bool loadModelFromFile(std::fstream &file)
SOMQuantizer & operator=(const SOMQuantizer &rhs)
MatrixFloat getDataAsMatrixFloat() const
bool setMaxNumEpochs(const UINT maxNumEpochs)
virtual bool loadModelFromFile(std::string filename)
UINT getNumClusters() const
UINT getQuantizedValue() const