21 #define GRT_DLL_EXPORTS
31 this->numClusters = numClusters;
32 classType =
"KMeansQuantizer";
33 featureExtractionType = classType;
35 debugLog.setProceedingText(
"[DEBUG KMeansQuantizer]");
36 errorLog.setProceedingText(
"[ERROR KMeansQuantizer]");
37 warningLog.setProceedingText(
"[WARNING KMeansQuantizer]");
42 classType =
"KMeansQuantizer";
43 featureExtractionType = classType;
45 debugLog.setProceedingText(
"[DEBUG KMeansQuantizer]");
46 errorLog.setProceedingText(
"[ERROR KMeansQuantizer]");
47 warningLog.setProceedingText(
"[WARNING KMeansQuantizer]");
59 this->numClusters = rhs.numClusters;
60 this->clusters = rhs.clusters;
61 this->quantizationDistances = rhs.quantizationDistances;
71 if( featureExtraction == NULL )
return false;
82 errorLog <<
"clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
100 std::fill(quantizationDistances.begin(),quantizationDistances.end(),0);
111 quantizationDistances.clear();
112 quantizationDistances.clear();
119 if( !file.is_open() ){
120 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
125 file <<
"KMEANS_QUANTIZER_FILE_V1.0" << std::endl;
129 errorLog <<
"saveModelToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
134 file <<
"QuantizerTrained: " << trained << std::endl;
135 file <<
"NumClusters: " << numClusters << std::endl;
138 file <<
"Clusters: \n";
139 for(UINT k=0; k<numClusters; k++){
140 for(UINT j=0; j<numInputDimensions; j++){
141 file << clusters[k][j];
142 if( j != numInputDimensions-1 ) file <<
"\t";
143 else file << std::endl;
156 if( !file.is_open() ){
157 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
165 if( word !=
"KMEANS_QUANTIZER_FILE_V1.0" ){
166 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
172 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
177 if( word !=
"QuantizerTrained:" ){
178 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load QuantizerTrained!" << std::endl;
184 if( word !=
"NumClusters:" ){
185 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load NumClusters!" << std::endl;
191 clusters.
resize(numClusters, numInputDimensions);
193 if( word !=
"Clusters:" ){
194 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load Clusters!" << std::endl;
198 for(UINT k=0; k<numClusters; k++){
199 for(UINT j=0; j<numInputDimensions; j++){
200 file >> clusters[k][j];
205 featureDataReady =
false;
206 quantizationDistances.
resize(numClusters,0);
214 return train( data );
219 return train( data );
224 return train( data );
229 return train( data );
240 kmeans.setComputeTheta(
true );
245 if( !kmeans.
train_(trainingData) ){
246 errorLog <<
"train_(MatrixFloat &trainingData) - Failed to train quantizer!" << std::endl;
252 numInputDimensions = trainingData.
getNumCols();
253 numOutputDimensions = 1;
254 featureVector.
resize(numOutputDimensions,0);
255 clusters = kmeans.getClusters();
256 quantizationDistances.
resize(numClusters,0);
268 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The quantizer has not been trained!" << std::endl;
272 if( inputVector.
getSize() != numInputDimensions ){
273 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.
getSize() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
279 UINT quantizedValue = 0;
281 for(UINT k=0; k<numClusters; k++){
283 quantizationDistances[k] = 0;
284 for(UINT i=0; i<numInputDimensions; i++){
285 quantizationDistances[k] += grt_sqr( inputVector[i]-clusters[k][i] );
287 if( quantizationDistances[k] < minDist ){
288 minDist = quantizationDistances[k];
293 featureVector[0] = quantizedValue;
294 featureDataReady =
true;
296 return quantizedValue;
305 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
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)