26 RegisterFeatureExtractionModule< SOMQuantizer > SOMQuantizer::registerModule(
"SOMQuantizer");
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!" << 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!" << endl;
155 file <<
"SOM_QUANTIZER_FILE_V1.0" << endl;
159 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << endl;
163 file <<
"QuantizerTrained: " << trained << endl;
164 file <<
"NumClusters: " << numClusters << endl;
169 errorLog <<
"saveModelToFile(fstream &file) - Failed to save SelfOrganizingMap settings to file!" << endl;
182 if( !file.is_open() ){
183 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << endl;
191 if( word !=
"SOM_QUANTIZER_FILE_V1.0" ){
192 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << endl;
198 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << endl;
203 if( word !=
"QuantizerTrained:" ){
204 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load QuantizerTrained!" << endl;
210 if( word !=
"NumClusters:" ){
211 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load NumClusters!" << endl;
218 if( word !=
"SOM:" ){
219 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load SOM!" << endl;
224 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load SelfOrganizingMap settings from file!" << endl;
229 featureDataReady =
false;
230 quantizationDistances.resize(numClusters,0);
262 errorLog <<
"train_(MatrixDouble &trainingData) - Failed to train quantizer, the training data is empty!" << 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(MatrixDouble &trainingData) - Failed to train quantizer!" << endl;
281 numInputDimensions = trainingData.
getNumCols();
282 numOutputDimensions = 1;
283 featureVector.resize(numOutputDimensions,0);
284 quantizationDistances.resize(numClusters,0);
290 return quantize( VectorDouble(1,inputValue) );
296 errorLog <<
"computeFeatures(const VectorDouble &inputVector) - The quantizer model has not been trained!" << endl;
300 if( inputVector.size() != numInputDimensions ){
301 errorLog <<
"computeFeatures(const VectorDouble &inputVector) - The size of the inputVector (" << inputVector.size() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << endl;
306 if( !som.
predict( inputVector ) ){
307 errorLog <<
"computeFeatures(const VectorDouble &inputVector) - Failed to perform map!" << 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 train_(MatrixDouble &data)
virtual bool loadModelFromFile(fstream &file)
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
bool setMaxNumEpochs(const UINT maxNumEpochs)
virtual bool loadModelFromFile(string filename)
bool train_(ClassificationData &trainingData)
The SOMQuantizer module quantizes the N-dimensional input vector to a 1-dimensional discrete value...
unsigned int getNumCols() const
bool setNumClusters(const UINT numClusters)
SOMQuantizer(const UINT numClusters=10)
bool getQuantizerTrained() const
SelfOrganizingMap getSelfOrganizingMap() const
VectorDouble getQuantizationDistances() const
UINT getNumClusters() const
virtual bool predict(VectorDouble inputVector)
MatrixDouble getDataAsMatrixDouble() const
virtual bool saveModelToFile(fstream &file) const
MatrixDouble getDataAsMatrixDouble() const
virtual bool saveModelToFile(string filename) const
MatrixDouble getDataAsMatrixDouble() const
unsigned int getNumRows() const
SOMQuantizer & operator=(const SOMQuantizer &rhs)
virtual bool computeFeatures(const VectorDouble &inputVector)
UINT getQuantizedValue() const
UINT quantize(const double inputValue)
MatrixDouble getDataAsMatrixDouble() const