Click here to Skip to main content
13,045,566 members (70,269 online)
Click here to Skip to main content


109 bookmarked
Posted 20 Nov 2007

Kohonen's Self Organizing Maps in C++ with Application in Computer Vision Area

, 20 Nov 2007
The article demonstrates the self organizing maps clustering approach for unsupervised AI classification tasks with application examples in computer vision area for faces clustering and recognition

#ifndef Node_h
#define Node_h

class Node
        friend class SOM;        
        Node(const float *weights, int weights_number, 
             const float *coords, int coords_number, int class_ = 0);

        enum DistanceMetric {EUCL, SOSD, TXCB, ANGL, MHLN};

// Operators
        //const Node& operator=(const Node& node);

// Operations
        bool evaluate_class(const int *classes, int classes_number); // const vector<int> &classes);

// Access
// Inquiry
        inline int get_class(void) const;
        inline const float* get_coords() const;
        inline const float* get_weights() const;
        inline float get_precision() const;        

        Node(const Node& node);
        const Node& operator=(const Node& node);

        int m_weights_number;           //number of weights
        float *m_weights;               //weights vector
        float *m_coords;                //x,y,z, ... M_DIM position
        int m_class;                    //class mark 0-undetermined, 1,2,3,....
        float m_precision;              //(max class votes)/(class1 votes + class2 votes + ... + classN votes)
        vector<int> m_votes;            //votes for class1, class2, ... classN  //actual classes numbers stored in REC->clsnum[] array

        inline void train(const float *vec, float learning_rule);  
        inline float get_distance(const float *vec, enum DistanceMetric distance_metric, const float **cov = 0) const;
        inline float mse(const float *vec1, const float *vec2, int size) const;

        inline void add_vote(int class_);
        void clear_votes(int classes_number = 0);
        inline void set_class(int class_);

inline const float* Node::get_coords() const
        return m_coords;

inline const float* Node::get_weights() const
        return m_weights;

inline float Node::get_precision() const
        return m_precision;

inline void Node::train(const float *vec, float learning_rule)
        for (int w = 0; w < m_weights_number; w++)
                m_weights[w] += learning_rule * (vec[w] - m_weights[w]);

inline float Node::get_distance(const float *vec, enum DistanceMetric distance_metric, const float **cov) const
        float distance = 0.0f;
        float n1 = 0.0f, n2 = 0.0f;

        switch (distance_metric) {
        case EUCL:   //euclidian
                if (m_weights_number >= 4) {
                        distance = mse(vec, m_weights, m_weights_number);
                } else {
                        for (int w = 0; w < m_weights_number; w++)
                                distance += (vec[w] - m_weights[w]) * (vec[w] - m_weights[w]);
                return sqrt(distance);

        case SOSD:   //sum of squared distances
                if (m_weights_number >= 4) {
                        distance = mse(vec, m_weights, m_weights_number);
                } else {
                        for (int w = 0; w < m_weights_number; w++)
                                distance += (vec[w] - m_weights[w]) * (vec[w] - m_weights[w]);
                return distance;

        case TXCB:   //taxicab
                for (int w = 0; w < m_weights_number; w++)
                        distance += fabs(vec[w] - m_weights[w]);
                return distance;

        case ANGL:   //angle between vectors
                for (int w = 0; w < m_weights_number; w++) {
                        distance += vec[w] * m_weights[w];
                        n1 += vec[w] * vec[w];
                        n2 += m_weights[w] * m_weights[w];
                return acos(distance / (sqrt(n1)*sqrt(n2)));

        //case MHLN:   //mahalanobis
                //distance = sqrt(m_weights * cov * vec)
                //return distance

inline float Node::mse(const float *vec1, const float *vec2, int size) const
        float z = 0.0f, fres = 0.0f;
        float ftmp[4];
        __m128 mv1, mv2, mres;
        mres = _mm_load_ss(&z);

        for (int i = 0; i < size / 4; i++) {
                mv1 = _mm_loadu_ps(&vec1[4*i]);
                mv2 = _mm_loadu_ps(&vec2[4*i]);
                mv1 = _mm_sub_ps(mv1, mv2);
                mv1 = _mm_mul_ps(mv1, mv1);
                mres = _mm_add_ps(mres, mv1);
        if (size % 4) {                
                for (int i = size - size % 4; i < size; i++)
                        fres += (vec1[i] - vec2[i]) * (vec1[i] - vec2[i]);

        //mres = a,b,c,d
        mv1 = _mm_movelh_ps(mres, mres);   //a,b,a,b
        mv2 = _mm_movehl_ps(mres, mres);   //c,d,c,d
        mres = _mm_add_ps(mv1, mv2);       //res[0],res[1]

        _mm_storeu_ps(ftmp, mres);        

        return fres + ftmp[0] + ftmp[1];

inline void Node::add_vote(int class_)

inline int Node::get_class(void) const
        return m_class;

inline void Node::set_class(int class_)
        m_class = class_;


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Chesnokov Yuriy
Russian Federation Russian Federation
Highly skilled Engineer with 14 years of experience in academia, R&D and commercial product development supporting full software life-cycle from idea to implementation and further support. During my academic career I was able to succeed in MIT Computers in Cardiology 2006 international challenge, as a R&D and SW engineer gain CodeProject MVP, find algorithmic solutions to quickly resolve tough customer problems to pass product requirements in tight deadlines. My key areas of expertise involve Object-Oriented
Analysis and Design OOAD, OOP, machine learning, natural language processing, face recognition, computer vision and image processing, wavelet analysis, digital signal processing in cardiology.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 20 Nov 2007
Article Copyright 2007 by Chesnokov Yuriy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid