30 this->numClusters = numClusters;
31 classType =
"KMeansQuantizer";
32 featureExtractionType = classType;
34 debugLog.setProceedingText(
"[DEBUG KMeansQuantizer]");
35 errorLog.setProceedingText(
"[ERROR KMeansQuantizer]");
36 warningLog.setProceedingText(
"[WARNING KMeansQuantizer]");
41 classType =
"KMeansQuantizer";
42 featureExtractionType = classType;
44 debugLog.setProceedingText(
"[DEBUG KMeansQuantizer]");
45 errorLog.setProceedingText(
"[ERROR KMeansQuantizer]");
46 warningLog.setProceedingText(
"[WARNING KMeansQuantizer]");
58 this->numClusters = rhs.numClusters;
59 this->clusters = rhs.clusters;
60 this->quantizationDistances = rhs.quantizationDistances;
70 if( featureExtraction == NULL )
return false;
81 errorLog <<
"clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
99 std::fill(quantizationDistances.begin(),quantizationDistances.end(),0);
110 quantizationDistances.clear();
111 quantizationDistances.clear();
118 if( !file.is_open() ){
119 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
124 file <<
"KMEANS_QUANTIZER_FILE_V1.0" << std::endl;
128 errorLog <<
"saveModelToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
133 file <<
"QuantizerTrained: " << trained << std::endl;
134 file <<
"NumClusters: " << numClusters << std::endl;
137 file <<
"Clusters: \n";
138 for(UINT k=0; k<numClusters; k++){
139 for(UINT j=0; j<numInputDimensions; j++){
140 file << clusters[k][j];
141 if( j != numInputDimensions-1 ) file <<
"\t";
142 else file << std::endl;
155 if( !file.is_open() ){
156 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
164 if( word !=
"KMEANS_QUANTIZER_FILE_V1.0" ){
165 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
171 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
176 if( word !=
"QuantizerTrained:" ){
177 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load QuantizerTrained!" << std::endl;
183 if( word !=
"NumClusters:" ){
184 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load NumClusters!" << std::endl;
190 clusters.
resize(numClusters, numInputDimensions);
192 if( word !=
"Clusters:" ){
193 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load Clusters!" << std::endl;
197 for(UINT k=0; k<numClusters; k++){
198 for(UINT j=0; j<numInputDimensions; j++){
199 file >> clusters[k][j];
204 featureDataReady =
false;
205 quantizationDistances.
resize(numClusters,0);
213 return train( data );
218 return train( data );
223 return train( data );
228 return train( data );
239 kmeans.setComputeTheta(
true );
244 if( !kmeans.
train_(trainingData) ){
245 errorLog <<
"train_(MatrixFloat &trainingData) - Failed to train quantizer!" << std::endl;
251 numInputDimensions = trainingData.
getNumCols();
252 numOutputDimensions = 1;
253 featureVector.
resize(numOutputDimensions,0);
254 clusters = kmeans.getClusters();
255 quantizationDistances.
resize(numClusters,0);
267 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The quantizer has not been trained!" << std::endl;
271 if( inputVector.
getSize() != numInputDimensions ){
272 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.
getSize() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
278 UINT quantizedValue = 0;
280 for(UINT k=0; k<numClusters; k++){
282 quantizationDistances[k] = 0;
283 for(UINT i=0; i<numInputDimensions; i++){
284 quantizationDistances[k] += grt_sqr( inputVector[i]-clusters[k][i] );
286 if( quantizationDistances[k] < minDist ){
287 minDist = quantizationDistances[k];
292 featureVector[0] = quantizedValue;
293 featureDataReady =
true;
295 return quantizedValue;
304 this->numClusters = numClusters;
virtual bool train_(ClassificationData &trainingData)
virtual bool computeFeatures(const VectorFloat &inputVector)
KMeansQuantizer(const UINT numClusters=10)
MatrixFloat getDataAsMatrixFloat() const
virtual bool resize(const unsigned int size)
virtual bool train_(MatrixFloat &data)
virtual bool train(ClassificationData trainingData)
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
bool setMinChange(const Float minChange)
virtual bool saveModelToFile(std::fstream &file) const
unsigned int getSize() const
bool setNumClusters(const UINT numClusters)
MatrixFloat getDataAsMatrixFloat() const
UINT quantize(Float inputValue)
The KMeansQuantizer module quantizes the N-dimensional input vector to a 1-dimensional discrete value...
MatrixFloat getDataAsMatrixFloat() const
unsigned int getNumCols() const
bool setMinNumEpochs(const UINT minNumEpochs)
UINT getNumClusters() const
virtual ~KMeansQuantizer()
KMeansQuantizer & operator=(const KMeansQuantizer &rhs)
MatrixFloat getDataAsMatrixFloat() const
virtual bool loadModelFromFile(std::fstream &file)
virtual bool resize(const unsigned int r, const unsigned int c)
bool setMaxNumEpochs(const UINT maxNumEpochs)
bool setNumClusters(const UINT numClusters)