Add() public method

Add the specified data, must be an ImageNeuralData class.
public Add ( IMLData data ) : void
data IMLData The data The object to add.
return void
示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Title  = "Open Image";
                dlg.Filter = "Image files (*.bmp, *.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.bmp; *.jpg; *.jpeg; *.jpe; *.jfif; *.png";

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    var        bitMap = new Bitmap(dlg.FileName);
                    NeuralForm n      = new NeuralForm();
                    n.BitMap = bitMap;
                    n.Show();

                    //
                    var      network     = new BasicNetwork();
                    FileInfo networkFile = new FileInfo(NETWORK_PATH);
                    if (System.IO.File.Exists(NETWORK_PATH))
                    {
                        network = (BasicNetwork)(Encog.Persist.EncogDirectoryPersistence.LoadObject(networkFile));
                    }


                    int hCounter = 0; int Wcounter = 0;
                    int hMax         = bitMap.Height / SIZE;
                    int wMax         = bitMap.Width / SIZE;
                    var outputBitmap = new Bitmap(bitMap.Width, bitMap.Height);
                    while (hMax > hCounter)
                    {
                        Encog.ML.Data.Image.ImageMLDataSet testingSet = new Encog.ML.Data.Image.ImageMLDataSet(new Encog.Util.DownSample.RGBDownsample(), false, 1, -1);
                        var _25x25bitmap = new Bitmap(SIZE, SIZE);

                        for (int i = 0; i < SIZE; i++)
                        {
                            for (int j = 0; j < SIZE; j++)
                            {
                                var px = bitMap.GetPixel(Wcounter * SIZE + j, hCounter * SIZE + i);
                                _25x25bitmap.SetPixel(i, j, px);
                            }
                        }

                        ImageMLData data = new ImageMLData(_25x25bitmap);
                        testingSet.Add(data, null);
                        testingSet.Downsample(SIZE, SIZE);
                        bool isVege = false;

                        foreach (IMLDataPair pair in testingSet)
                        {
                            IMLData output = network.Compute(pair.Input);

                            if (output[1] > .05)
                            {
                                isVege = true;
                            }
                        }
                        for (int i = 0; i < SIZE; i++)
                        {
                            for (int j = 0; j < SIZE; j++)
                            {
                                var px = _25x25bitmap.GetPixel(i, j);
                                if (!isVege)
                                {
                                    outputBitmap.SetPixel(Wcounter * SIZE + j, hCounter * SIZE + i, px);
                                }
                                else
                                {
                                    outputBitmap.SetPixel(Wcounter * SIZE + j, hCounter * SIZE + i, Color.FromArgb(px.R, 0, px.B));
                                }
                            }
                        }

                        Wcounter++;
                        if (wMax <= Wcounter)
                        {
                            Wcounter = 0;
                            hCounter++;
                        }
                    }


                    NeuralForm n2 = new NeuralForm();
                    n2.BitMap = outputBitmap;
                    n2.Show();
                }
            }
        }
示例#2
0
        public static void Run()
        {
            FileInfo networkFile = new FileInfo(@"D:\Imagery\network\network.eg");

            var network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, true, SIZE * SIZE * 3));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, NERUONCOUNT));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 2));
            network.Structure.FinalizeStructure();
            network.Reset();

            if (System.IO.File.Exists(@"D:\Imagery\network\network.eg"))
            {
                network = (BasicNetwork)(Encog.Persist.EncogDirectoryPersistence.LoadObject(networkFile));
            }



            Encog.ML.Data.Image.ImageMLDataSet trainingSet = new Encog.ML.Data.Image.ImageMLDataSet(new RGBDownsample(), false, 1, -1);

            Random rnd = new Random();
            //take 1000,, take 5000 from nothing and scramble them
            List <string> fileEntries = Directory.GetFiles(@"D:\Imagery\_Vege").OrderBy(x => rnd.Next()).Take(1000).ToList();

            fileEntries.AddRange(Directory.GetFiles(@"D:\Imagery\_Nothing").OrderBy(x => rnd.Next()).Take(5000).ToArray());
            fileEntries = fileEntries.OrderBy(x => rnd.Next()).Take(6000).ToList();
            foreach (var file in fileEntries)
            {
                var         bitmap = new System.Drawing.Bitmap(file);
                ImageMLData data   = new ImageMLData(bitmap);
                if (file.Contains("_Nothing"))
                {
                    BasicMLData ideal = new BasicMLData(Nothing);
                    trainingSet.Add(data, ideal);
                }
                else
                {
                    BasicMLData ideal = new BasicMLData(Vegetation);
                    trainingSet.Add(data, ideal);
                }
            }
            trainingSet.Downsample(SIZE, SIZE);

            IMLTrain train = new Backpropagation(network, trainingSet, .001, 0.02)
            {
            };


            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error: " + train.Error);
                epoch++;
            } while (epoch < 50);
            train.FinishTraining();

            Encog.Persist.EncogDirectoryPersistence.SaveObject(networkFile, (BasicNetwork)network);
            Encog.ML.Data.Image.ImageMLDataSet testingSet = new Encog.ML.Data.Image.ImageMLDataSet(new RGBDownsample(), false, 1, -1);
            fileEntries = Directory.GetFiles(@"D:\Imagery\_VegeTest").ToList();
            foreach (var file in fileEntries)
            {
                ImageMLData data  = new ImageMLData(new System.Drawing.Bitmap(file));
                BasicMLData ideal = new BasicMLData(Vegetation);
                testingSet.Add(data, ideal);
            }
            fileEntries = Directory.GetFiles(@"D:\Imagery\_NothingTest").ToList();
            foreach (var file in fileEntries)
            {
                ImageMLData data  = new ImageMLData(new System.Drawing.Bitmap(file));
                BasicMLData ideal = new BasicMLData(Nothing);
                testingSet.Add(data, ideal);
            }
            testingSet.Downsample(SIZE, SIZE);

            Console.WriteLine(@"Neural Network Results:");
            foreach (IMLDataPair pair in testingSet)
            {
                IMLData output = network.Compute(pair.Input);
                Console.WriteLine(@", actual (" + output[0] + @"," + output[1] + @"),ideal (" + pair.Ideal[0] + @"," + pair.Ideal[1] + ")");
            }

            EncogFramework.Instance.Shutdown();
        }