21 #define GRT_DLL_EXPORTS 27 std::string SOMQuantizer::id =
"SOMQuantizer";
35 this->numClusters = numClusters;
51 this->numClusters = rhs.numClusters;
53 this->quantizationDistances = rhs.quantizationDistances;
63 if( featureExtraction == NULL )
return false;
65 if( this->
getId() == featureExtraction->
getId() ){
69 *
this = *
dynamic_cast<const SOMQuantizer*
>(featureExtraction);
74 errorLog <<
"deepCopyFrom(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
94 std::fill(quantizationDistances.begin(),quantizationDistances.end(),0);
106 quantizationDistances.clear();
113 if( !file.is_open() ){
114 errorLog <<
"save(fstream &file) - The file is not open!" << std::endl;
119 file <<
"SOM_QUANTIZER_FILE_V1.0" << std::endl;
123 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
127 file <<
"QuantizerTrained: " << trained << std::endl;
128 file <<
"NumClusters: " << numClusters << std::endl;
132 if( !som.
save( file ) ){
133 errorLog <<
"save(fstream &file) - Failed to save SelfOrganizingMap settings to file!" << std::endl;
146 if( !file.is_open() ){
147 errorLog <<
"load(fstream &file) - The file is not open!" << std::endl;
155 if( word !=
"SOM_QUANTIZER_FILE_V1.0" ){
156 errorLog <<
"load(fstream &file) - Invalid file format!" << std::endl;
162 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
167 if( word !=
"QuantizerTrained:" ){
168 errorLog <<
"load(fstream &file) - Failed to load QuantizerTrained!" << std::endl;
174 if( word !=
"NumClusters:" ){
175 errorLog <<
"load(fstream &file) - Failed to load NumClusters!" << std::endl;
182 if( word !=
"SOM:" ){
183 errorLog <<
"load(fstream &file) - Failed to load SOM!" << std::endl;
187 if( !som.
load( file ) ){
188 errorLog <<
"load(fstream &file) - Failed to load SelfOrganizingMap settings from file!" << std::endl;
193 featureDataReady =
false;
194 quantizationDistances.
resize(numClusters,0);
226 errorLog <<
"train_(MatrixFloat &trainingData) - Failed to train quantizer, the training data is empty!" << std::endl;
231 som.setNetworkSize( numClusters );
232 som.setNetworkTypology( SelfOrganizingMap::RANDOM_NETWORK );
233 som.setAlphaStart( 0.5 );
234 som.setAlphaEnd( 0.1 );
237 if( !som.
train_( trainingData ) ){
238 errorLog <<
"train(MatrixFloat &trainingData) - Failed to train quantizer!" << std::endl;
245 numInputDimensions = trainingData.
getNumCols();
246 numOutputDimensions = 1;
247 featureVector.
resize(numOutputDimensions,0);
248 quantizationDistances.
resize(numClusters,0);
260 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The quantizer model has not been trained!" << std::endl;
264 if( inputVector.
getSize() != numInputDimensions ){
265 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.
getSize() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
270 if( !som.
predict( inputVector ) ){
271 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - Failed to perform map!" << std::endl;
274 quantizationDistances = som.getMappedData();
277 UINT quantizedValue = 0;
279 for(UINT k=0; k<numClusters; k++){
280 if( quantizationDistances[k] > maxValue ){
281 maxValue = quantizationDistances[k];
286 featureVector[0] = quantizedValue;
287 featureDataReady =
true;
289 return quantizedValue;
301 return (trained ? static_cast<UINT>(featureVector[0]) : 0);
305 return quantizationDistances;
314 this->numClusters = numClusters;
std::string getId() 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 load(std::fstream &file)
virtual bool load(std::fstream &file)
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 save(std::fstream &file) const
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
static std::string getId()
unsigned int getNumRows() const
MatrixFloat getDataAsMatrixFloat() const
SelfOrganizingMap getSelfOrganizingMap() const
unsigned int getNumCols() const
SOMQuantizer & operator=(const SOMQuantizer &rhs)
MatrixFloat getDataAsMatrixFloat() const
bool setMaxNumEpochs(const UINT maxNumEpochs)
virtual bool save(std::fstream &file) const
UINT getNumClusters() const
UINT getQuantizedValue() const