AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

A question of SOM and color sample

The forum is to discuss topics from different artificial intelligence areas, like neural networks, genetic algorithms, machine learning, etc.

A question of SOM and color sample

Postby whitebird » Wed Dec 15, 2010 4:11 pm

Hi all! I’m a beginner of network.
I have a few questions about SOM Network and the sample of Color.
I have rewrite the sample of Color in order to load a special image but a random one.
The code of load a special image and init network like this:

Code: Select all
OpenFileDialog ofdLoadImage = new OpenFileDialog();
            if (ofdLoadImage.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                mapBitmap = new Bitmap(ofdLoadImage.FileName);
                network = new DistanceNetwork(3, mapBitmap.Width * mapBitmap.Height);
                InitNetWorkFromLoadImage(); // init netWork according to the image         
            }

InitNetWorkFromLoadImage method is used to init netWork, following is the code of InitNetWorkFromLoadImage method.private void
Code: Select all
InitNetWorkFromLoadImage()
        {
            // lock
            Monitor.Enter(this);
            // lock bitmap
            BitmapData mapData = mapBitmap.LockBits(new Rectangle(0, 0, mapBitmap.Width, mapBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int stride = mapData.Stride;
            int offset = stride - mapBitmap.Width * 3;
            Layer layer = network[0];
            unsafe
            {
                byte* ptr = (byte*)mapData.Scan0;
                // for all rows
                for (int y = 0; y < mapBitmap.Height; y++)
                {
                    // for all pixels
                    for (int x = 0; x < mapBitmap.Width; x++)
                    {
                        Neuron neuron = layer[i];
                        // red
                        neuron[0] = ptr[2];
                        // green
                        neuron[1] = ptr[1];
                        // blue
                        neuron[2] = ptr[0];
                        ptr += 3;
                    }
                    ptr += offset;
                }
            }
            // unlock image
            mapBitmap.UnlockBits(mapData);
            // unlock
            Monitor.Exit(this);
            // update map
            UpdateMap();
        }

The key code of UpdateMap method has been change like this:
Code: Select all
private void UpdateMap()
        {
            …………
                for (int y = 0; y < mapBitmap.Height; y++)
                {
                    // for all pixels
                    for (int x = 0; x < mapBitmap.Width; x++)
                    {
                        Neuron neuron = layer[i];
                        // red
                        ptr[2] = (byte)Math.Max(0, Math.Min(255, neuron[0]));
                        // green
                        ptr[1] = (byte)Math.Max(0, Math.Min(255, neuron[1]));
                        // blue
                        ptr[0] = (byte)Math.Max(0, Math.Min(255, neuron[2]));
                        ptr += 3;
                    }
                    ptr += offset;
                }
            }
            …………
            mapPanel.Invalidate();
        }

And then I chaged the network training code :
Code: Select all
// loop
         while ( !needToStop )
         {
            trainer.LearningRate = driftingLearningRate * ( iterations - i ) / iterations + fixedLearningRate;
            trainer.LearningRadius = (double) radius * ( iterations - i ) / iterations;
               int index = rand.Next(mapBitmap.Width * mapBitmap.Height);
               input[0] = network[0][index][0];
               input[1] = network[0][index][1];
               input[2] = network[0][index][2];
            trainer.Run( input );
            // update map once per 50 iterations
            if ( ( i % 10 ) == 9 )
            {
               UpdateMap( );
            }
            // increase current iteration
            i++;
            // set current iteration's info
                SetText( currentIterationBox, i.ToString( ) );
            // stop ?
            if ( i >= iterations )
                    //mapBitmap.UnlockBits(mapData);
               break;
         }

Then , I test a random create image and two other images. the result image don’t like a RGB palette, but a mixture of all colors.
I know there must be something wrong. But I don’t know why. The characteristic of SOM Network is clustering. I guess when the training finished; the same color will be get together. But why I got a opposite result?
How a SOM network achieve its aim of clustering?
Thanks very much!
Last edited by whitebird on Thu Dec 16, 2010 11:25 am, edited 1 time in total.
whitebird
 
Posts: 7
Joined: Wed Dec 15, 2010 3:56 pm

Re: A question of SOM and color sample

Postby andrew.kirillov » Thu Dec 16, 2010 9:58 am

Hello,

whitebird wrote:the result image don’t like a RGB palette, but a mixture of all colors.


whitebird wrote:But why I got a opposite result?

Make a screen shot of that image and attach it here. Otherwise it is not clear what are you talking about.

whitebird wrote:I guess when the training finished; the same color will be get together.

Similar colors will be together.

whitebird wrote:How a SOM network achieve its aim of clustering?

There are quite a lot of documents on the Internet about Kohonen SOM. Just google a bit.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3451
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: A question of SOM and color sample

Postby whitebird » Thu Dec 16, 2010 11:35 am

Hi Andrew. Thanks for your reply.
the test image are listed in the following.
1: original image: a random create image
Iteraions: 5000
result
:
001-1.jpg
001-1.jpg (45.78 KiB) Viewed 10812 times


2: original image:
t03.jpg
t03.jpg (25.47 KiB) Viewed 10812 times

Iteraions: 5000
result
:
r03.jpg
r03.jpg (31.4 KiB) Viewed 10811 times
whitebird
 
Posts: 7
Joined: Wed Dec 15, 2010 3:56 pm

Re: A question of SOM and color sample

Postby whitebird » Thu Dec 16, 2010 11:44 am

3:original image:
t06.jpg
t06.jpg (25.41 KiB) Viewed 10804 times

Iteraions: 5000
result:
r06.jpg
r06.jpg (31.06 KiB) Viewed 10803 times


4:original image:
t07.jpg
t07.jpg (26.21 KiB) Viewed 10801 times

Iteraions: 5000
result:
whitebird
 
Posts: 7
Joined: Wed Dec 15, 2010 3:56 pm

Re: A question of SOM and color sample

Postby andrew.kirillov » Thu Dec 16, 2010 11:50 am

It seems the network works fine. You give it 2 colors (black and white, or blue and green), so it creates a map which looks like a gradient between these 2 colors. What would you expect?

The only problem I see is that SOM images don't contain pure white/black or red/white colors. But this could be caused by the way you set learning rate and radius.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3451
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: A question of SOM and color sample

Postby whitebird » Thu Dec 16, 2010 11:59 am

4result:
r07.jpg
r07.jpg (32.82 KiB) Viewed 10794 times


the code of color sample which has been rewrite
Color Sample.rar
(117.56 KiB) Downloaded 623 times


I have read some documents of SOM network and know the base principle of it.
Maybe the color sample is just a demo of net training process, it is not the usage of SOM network.
usage is after the training process.
after a network been trained, we input a color (or other object)to the net, an then ,the network will not adjust its weights, but calculate a winner neuron and show it in some way.
because we have know the type of all neuron, so ,wo get the type of the object which we just input to a trained network.
it is clustering, is that right??
whitebird
 
Posts: 7
Joined: Wed Dec 15, 2010 3:56 pm

Re: A question of SOM and color sample

Postby andrew.kirillov » Thu Dec 16, 2010 12:12 pm

Right.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3451
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: A question of SOM and color sample

Postby whitebird » Thu Dec 16, 2010 12:18 pm

andrew.kirillov wrote:It seems the network works fine. You give it 2 colors (black and white, or blue and green), so it creates a map which looks like a gradient between these 2 colors. What would you expect?


the first thing , when i create a image randomly in the sample amd run the training process. i got a result image looks like there is a gray mask, just like the first result image, when i change Iteraions to 15000, the result looks like just a gray image but a RGB palette? is that the right result? why the original sample give a result image looks like a RGB palette?
whitebird
 
Posts: 7
Joined: Wed Dec 15, 2010 3:56 pm




Return to Artificial Intelligence