21 #define GRT_DLL_EXPORTS 27 std::string KMeansQuantizer::id =
"KMeansQuantizer";
35 this->numClusters = numClusters;
50 this->numClusters = rhs.numClusters;
51 this->clusters = rhs.clusters;
52 this->quantizationDistances = rhs.quantizationDistances;
62 if( featureExtraction == NULL )
return false;
64 if( this->
getId() == featureExtraction->
getId() ){
73 errorLog <<
"deepCopyFrom(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
91 std::fill(quantizationDistances.begin(),quantizationDistances.end(),0);
102 quantizationDistances.clear();
103 quantizationDistances.clear();
110 if( !file.is_open() ){
111 errorLog <<
"save(fstream &file) - The file is not open!" << std::endl;
116 file <<
"KMEANS_QUANTIZER_FILE_V1.0" << std::endl;
120 errorLog <<
"save(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
125 file <<
"QuantizerTrained: " << trained << std::endl;
126 file <<
"NumClusters: " << numClusters << std::endl;
129 file <<
"Clusters: \n";
130 for(UINT k=0; k<numClusters; k++){
131 for(UINT j=0; j<numInputDimensions; j++){
132 file << clusters[k][j];
133 if( j != numInputDimensions-1 ) file <<
"\t";
134 else file << std::endl;
147 if( !file.is_open() ){
148 errorLog <<
"load(fstream &file) - The file is not open!" << std::endl;
156 if( word !=
"KMEANS_QUANTIZER_FILE_V1.0" ){
157 errorLog <<
"load(fstream &file) - Invalid file format!" << std::endl;
163 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
168 if( word !=
"QuantizerTrained:" ){
169 errorLog <<
"load(fstream &file) - Failed to load QuantizerTrained!" << std::endl;
175 if( word !=
"NumClusters:" ){
176 errorLog <<
"load(fstream &file) - Failed to load NumClusters!" << std::endl;
182 clusters.
resize(numClusters, numInputDimensions);
184 if( word !=
"Clusters:" ){
185 errorLog <<
"load(fstream &file) - Failed to load Clusters!" << std::endl;
189 for(UINT k=0; k<numClusters; k++){
190 for(UINT j=0; j<numInputDimensions; j++){
191 file >> clusters[k][j];
196 featureDataReady =
false;
197 quantizationDistances.
resize(numClusters,0);
205 return train( data );
210 return train( data );
215 return train( data );
220 return train( data );
231 kmeans.setComputeTheta(
true );
236 if( !kmeans.
train_(trainingData) ){
237 errorLog <<
"train_(MatrixFloat &trainingData) - Failed to train quantizer!" << std::endl;
243 numInputDimensions = trainingData.
getNumCols();
244 numOutputDimensions = 1;
245 featureVector.
resize(numOutputDimensions,0);
246 clusters = kmeans.getClusters();
247 quantizationDistances.
resize(numClusters,0);
259 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The quantizer has not been trained!" << std::endl;
263 if( inputVector.
getSize() != numInputDimensions ){
264 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.
getSize() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
270 UINT quantizedValue = 0;
272 for(UINT k=0; k<numClusters; k++){
274 quantizationDistances[k] = 0;
275 for(UINT i=0; i<numInputDimensions; i++){
276 quantizationDistances[k] += grt_sqr( inputVector[i]-clusters[k][i] );
278 if( quantizationDistances[k] < minDist ){
279 minDist = quantizationDistances[k];
284 featureVector[0] = quantizedValue;
285 featureDataReady =
true;
287 return quantizedValue;
296 this->numClusters = numClusters;
virtual bool train_(ClassificationData &trainingData)
virtual bool computeFeatures(const VectorFloat &inputVector)
std::string getId() const
UINT quantize(const Float inputValue)
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)
bool setNumClusters(const UINT numClusters)
MatrixFloat getDataAsMatrixFloat() const
The KMeansQuantizer module quantizes the N-dimensional input vector to a 1-dimensional discrete value...
MatrixFloat getDataAsMatrixFloat() const
unsigned int getNumCols() const
static std::string getId()
bool setMinNumEpochs(const UINT minNumEpochs)
UINT getNumClusters() const
virtual ~KMeansQuantizer()
KMeansQuantizer & operator=(const KMeansQuantizer &rhs)
MatrixFloat getDataAsMatrixFloat() const
virtual bool resize(const unsigned int r, const unsigned int c)
virtual bool save(std::fstream &file) const
bool setMaxNumEpochs(const UINT maxNumEpochs)
virtual bool load(std::fstream &file)
bool setNumClusters(const UINT numClusters)