30 this->numClusters = numClusters;
32 classType =
"SOMQuantizer";
33 featureExtractionType = classType;
34 debugLog.setProceedingText(
"[DEBUG SOMQuantizer]");
35 errorLog.setProceedingText(
"[ERROR SOMQuantizer]");
36 warningLog.setProceedingText(
"[WARNING SOMQuantizer]");
41 classType =
"SOMQuantizer";
42 featureExtractionType = classType;
43 debugLog.setProceedingText(
"[DEBUG SOMQuantizer]");
44 errorLog.setProceedingText(
"[ERROR SOMQuantizer]");
45 warningLog.setProceedingText(
"[WARNING SOMQuantizer]");
58 this->numClusters = rhs.numClusters;
60 this->quantizationDistances = rhs.quantizationDistances;
70 if( featureExtraction == NULL )
return false;
81 errorLog <<
"clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
101 std::fill(quantizationDistances.begin(),quantizationDistances.end(),0);
113 quantizationDistances.clear();
121 file.open(filename.c_str(), std::ios::out);
135 file.open(filename.c_str(), std::ios::in);
149 if( !file.is_open() ){
150 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
155 file <<
"SOM_QUANTIZER_FILE_V1.0" << std::endl;
159 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
163 file <<
"QuantizerTrained: " << trained << std::endl;
164 file <<
"NumClusters: " << numClusters << std::endl;
169 errorLog <<
"saveModelToFile(fstream &file) - Failed to save SelfOrganizingMap settings to file!" << std::endl;
182 if( !file.is_open() ){
183 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
191 if( word !=
"SOM_QUANTIZER_FILE_V1.0" ){
192 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
198 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
203 if( word !=
"QuantizerTrained:" ){
204 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load QuantizerTrained!" << std::endl;
210 if( word !=
"NumClusters:" ){
211 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load NumClusters!" << std::endl;
218 if( word !=
"SOM:" ){
219 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load SOM!" << std::endl;
224 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load SelfOrganizingMap settings from file!" << std::endl;
229 featureDataReady =
false;
230 quantizationDistances.
resize(numClusters,0);
262 errorLog <<
"train_(MatrixFloat &trainingData) - Failed to train quantizer, the training data is empty!" << std::endl;
267 som.setNetworkSize( numClusters );
268 som.setNetworkTypology( SelfOrganizingMap::RANDOM_NETWORK );
269 som.setAlphaStart( 0.5 );
270 som.setAlphaEnd( 0.1 );
273 if( !som.
train_( trainingData ) ){
274 errorLog <<
"train(MatrixFloat &trainingData) - Failed to train quantizer!" << std::endl;
281 numInputDimensions = trainingData.
getNumCols();
282 numOutputDimensions = 1;
283 featureVector.
resize(numOutputDimensions,0);
284 quantizationDistances.
resize(numClusters,0);
296 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The quantizer model has not been trained!" << std::endl;
300 if( inputVector.
getSize() != numInputDimensions ){
301 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.
getSize() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
306 if( !som.
predict( inputVector ) ){
307 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - Failed to perform map!" << std::endl;
310 quantizationDistances = som.getMappedData();
313 UINT quantizedValue = 0;
315 for(UINT k=0; k<numClusters; k++){
316 if( quantizationDistances[k] > maxValue ){
317 maxValue = quantizationDistances[k];
322 featureVector[0] = quantizedValue;
323 featureDataReady =
true;
325 return quantizedValue;
337 return (trained ? static_cast<UINT>(featureVector[0]) : 0);
341 return quantizationDistances;
350 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)
unsigned int getSize() const
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