예제 #1
0
        public static NeuroNet LoadNeuronet(string filepath, object NeuroDataSupportClass)
        {
            NeuroNet neuroNet = FileWork.LoadNeuroNet(filepath);

            neuroNet.neuroData.SupportClass = NeuroDataSupportClass;
            return(neuroNet);
        }
예제 #2
0
        public NeuroNet neuroNet;// = NeuroNet.CreateNeuroNet(new int[] {400, 15, 1 }, new NeuroData(1000, 0.3f));

        public NeuroWork(int offset, int number_of_passes, float edukationK)
        {
            neuroNet      = NeuroNet.CreateNeuroNet(new int[] { offset *offset, offset, 1 }, new NeuroData(number_of_passes, edukationK));
            this.offset   = offset;
            PreShow       = new Bitmap(offset, offset);
            start_signals = new int[offset * offset];
        }
예제 #3
0
        public static void SaveNeuroNet(NeuroNet neuroNet, string filePath)
        {
            //Random random = new Random();

            using (StreamWriter writer = new StreamWriter(filePath))
            {
                NeuroData.SaveNeuroData(neuroNet.neuroData, writer);

                writer.WriteLine(neuroNet.neuroLayer.Length);

                for (int i = 0; i < neuroNet.neuroLayer.Length; i++)
                {
                    writer.WriteLine(neuroNet.neuroLayer[i].Neurons.Length);
                }

                for (int i = 0; i < neuroNet.neuroLayer.Length; i++)
                {
                    for (int j = 0; j < neuroNet.neuroLayer[i].Neurons.Length; j++)
                    {
                        for (int k = 0; k < neuroNet.neuroLayer[i].Neurons[j].weights.Length; k++)
                        {
                            writer.WriteLine(neuroNet.neuroLayer[i].Neurons[j].weights[k]);
                        }
                    }

                    if (i < neuroNet.neuroLayer.Length - 1)
                    {
                        for (int k = 0; k < neuroNet.neuroLayer[i + 1].Neurons.Length; k++)
                        {
                            writer.WriteLine(neuroNet.neuroLayer[i].AdditionalNeuron.weights[k]);
                        }
                    }
                }
            }
        }
예제 #4
0
        public void StartWriteText()
        {
            string   NeuroNetFile = "";
            NeuroNet neuroNet;

            OpenFileDialog NeuroNetDialog = new OpenFileDialog
            {
                Title = "Выбор нейросети",

                Filter = "Neuronet files|*.NN|All files|*.*"
            };

            if (NeuroNetDialog.ShowDialog() == DialogResult.OK)
            {
                NeuroNetFile = NeuroNetDialog.FileName;
            }
            else
            {
                return;
            }

            neuroNet = NeuroNet.LoadNeuronet(NeuroNetFile);

            NormalizePhoto();
            FindAllLines();
            DebugLines();
            FindAllLetters();
            PreparationWriteTextPass();

            int k = 0;

            while (WriteTextPass(neuroNet) && k < 200000)
            {
                k++;
            }
            Debug.Log("Расшифровка изображения завершена");
            SaveFileDialog SaveTextDialog = new SaveFileDialog
            {
                Title  = "SaveTextDialog",
                Filter = "Text|*.txt|All files|*.*"
            };

            if (SaveTextDialog.ShowDialog() == DialogResult.OK)
            {
                FileWork.SaveText(final_massage, SaveTextDialog.FileName);
            }
            //FileWork.SaveText(final_massage, @"C:\Users\Admin\Desktop\Letter\ForComplete\text.txt");

            Debug.Log("Текст скопирован в буфер обмена");
            Clipboard.SetText(final_massage);
        }
예제 #5
0
        void StartLearningThread()
        {
            Debug.Log("начало обучения");
            Bitmap LetterImage;
            char   letter;
            int    count = 100000;

            float[] start_signals;
            float[] ideal_signals;

            //neuroNet = NeuroNet.CreateNeuroNet(new int[] {offset * offset, 42, 255 }, new NeuroData(1, 0.0002f));
            int load_neuroNet = 10800;//34500;

            neuroNet = NeuroNet.LoadNeuronet(@"C:\Users\Admin\Desktop\Letter\forNeuronet\NN" + load_neuroNet + ".NN");

            Debug.SetMaxValueProgressBar(count);
            for (int j = load_neuroNet + 1; j < count; j++)
            {
                neuroNet.ZeroingErrorOfEra();
                for (int i = 0; i < 1030; i++)
                {
                    StreamReader TextReader = new StreamReader(@"C:\Users\Admin\Desktop\ForText\" + i + ".txt");
                    LetterImage   = new Bitmap(@"C:\Users\Admin\Desktop\ForText\" + i + ".jpg");
                    letter        = TextReader.ReadLine()[0];
                    start_signals = GetNormalisedStartSignalsFromImage(LetterImage);
                    ideal_signals = GetNormalisedIdealSignals(letter);

                    neuroNet.Pass(start_signals, ideal_signals);
                }


                if (j % 50 == 0)
                {
                    if (j != 0)
                    {
                        neuroNet.SaveNeuronet(@"C:\Users\Admin\Desktop\Letter\forNeuronet\NN" + j + ".NN");
                        Debug.Log("завершено на " + (j * 100f / count) + "%");
                    }
                    else
                    {
                        neuroNet.SaveNeuronet(@"C:\Users\Admin\Desktop\Letter\forNeuronet\NN" + 0 + ".NN");
                    }
                    Debug.Log("error of era = " + neuroNet.ShowErrorOfEra());
                    //neuroNet.edukationK *= 0.99f;
                }
                //Debug.Log("error of era = " + neuroNet.ShowErrorOfEra());
                Debug.SetCurrentValueProgressBar(j);
            }
            neuroNet.SaveNeuronet(@"C:\Users\Admin\Desktop\Letter\forNeuronet\NN" + count + ".NN");
            Debug.Log("обучение завершено");
        }
예제 #6
0
        public static NeuroNet CreateNeuroNet(int[] layers, NeuroData neuroData)
        {
            NeuroNet neuroNet = new NeuroNet();

            neuroNet.neuroData  = neuroData;
            neuroNet.neuroLayer = new NeuroLayer[layers.Length];
            neuroNet.SetData();

            for (int i = 0; i < layers.Length; i++)
            {
                if (i < layers.Length - 1)
                {
                    neuroNet.neuroLayer[i] = new NeuroLayer(layers[i], layers[i + 1]);
                }
                else
                {
                    neuroNet.neuroLayer[i] = new NeuroLayer(layers[i], 0);
                }
            }
            neuroNet.FullZeroingNeurons();
            return(neuroNet);
        }
예제 #7
0
        public static NeuroNet LoadNeuroNet(string filePath)
        {
            NeuroNet neuroNet;

            using (StreamReader reader = new StreamReader(filePath))
            {
                NeuroData neuroData        = NeuroData.LoadNeuroData(reader);
                int       number_of_layers = Convert.ToInt32(reader.ReadLine());
                int[]     layers           = new int[number_of_layers];

                for (int i = 0; i < number_of_layers; i++)
                {
                    layers[i] = Convert.ToInt32(reader.ReadLine());
                }

                neuroNet = NeuroNet.CreateNeuroNet(layers, neuroData);

                for (int i = 0; i < neuroNet.neuroLayer.Length; i++)
                {
                    for (int j = 0; j < neuroNet.neuroLayer[i].Neurons.Length; j++)
                    {
                        for (int k = 0; k < neuroNet.neuroLayer[i].Neurons[j].weights.Length; k++)
                        {
                            neuroNet.neuroLayer[i].Neurons[j].weights[k] = Convert.ToSingle(reader.ReadLine());
                        }
                    }

                    if (i < neuroNet.neuroLayer.Length - 1)
                    {
                        for (int k = 0; k < neuroNet.neuroLayer[i + 1].Neurons.Length; k++)
                        {
                            neuroNet.neuroLayer[i].AdditionalNeuron.weights[k] = Convert.ToSingle(reader.ReadLine());
                        }
                    }
                }
            }
            return(neuroNet);
        }
예제 #8
0
        bool WriteTextPass(NeuroNet ReadTextNeuroNet)
        {
            if (current_line_WriteTextPass >= LineList.Count)
            {
                //Debug.Log("current_line_WriteTextPass >= LineList.Count");
                return(false);
            }
            if (LineList[current_line_WriteTextPass].LetterList.Count == 0)
            {
                //Debug.Log("LineList[current_line_WriteTextPass].LetterList.Count == 0");
                current_column_WriteTextPass = 0;
                current_line_WriteTextPass++;
                final_massage += "\n ";
                //FileWork.SaveText("\n", @"C:\Users\Admin\Desktop\Letter\ForComplete\text.txt");
                return(true);
            }
            int min_y = LineList[current_line_WriteTextPass].up_y;
            int min_x = LineList[current_line_WriteTextPass].LetterList[current_column_WriteTextPass].left_x;

            ClearStartSignals();

            int[,] start_signals_pre = new int[offset, offset * 5];
            for (int y = 0; y < start_signals_pre.GetLength(1); y++)
            {
                for (int x = 0; x < start_signals_pre.GetLength(0); x++)
                {
                    start_signals_pre[x, y] = 255;
                }
            }

            for (int i = 0; i < start_signals.Length; i++)
            {
                start_signals[i] = 1;
            }


            for (int y = LineList[current_line_WriteTextPass].up_y; y < LineList[current_line_WriteTextPass].down_y; y++)
            {
                for (int x = LineList[current_line_WriteTextPass].LetterList[current_column_WriteTextPass].left_x; x < LineList[current_line_WriteTextPass].LetterList[current_column_WriteTextPass].right_x; x++)
                {
                    if (x - min_x < offset && y - min_y < offset)
                    {
                        Color color = NormalizeImage.GetPixel(x, y);

                        start_signals_pre[(x - min_x), (y - min_y)] = color.R;
                        //NormalizeImage.SetPixel(x, y, Color.White);
                        start_signals[(x - min_x) + (y - min_y) * offset] = color.R;
                    }
                    else
                    {
                        if (x - min_x < start_signals_pre.GetLength(0) && y - min_y < start_signals_pre.GetLength(1))
                        {
                            Color color = NormalizeImage.GetPixel(x, y);
                            start_signals_pre[(x - min_x), (y - min_y)] = color.R;
                        }
                    }
                }
            }

            bool free_line  = true;
            int  start_line = 0;

            for (int y = 0; y < start_signals_pre.GetLength(1); y++)
            {
                for (int x = 0; x < start_signals_pre.GetLength(0); x++)
                {
                    if (start_signals_pre[x, y] == 0)
                    {
                        free_line = false;
                    }
                }
                if (free_line)
                {
                    start_line++;
                }
                else
                {
                    break;
                }
            }

            int current_start_signal = 0;

            for (int y = start_line; y < start_signals_pre.GetLength(1); y++)
            {
                for (int x = 0; x < start_signals_pre.GetLength(0); x++)
                {
                    if (x < offset && y - start_line < offset)
                    {
                        Color color = Color.FromArgb(255, start_signals_pre[x, y], start_signals_pre[x, y], start_signals_pre[x, y]);
                        start_signals[current_start_signal] = start_signals_pre[x, y];
                        current_start_signal++;
                    }
                }
            }

            float[] start_signals_float = GetNormarisedStartSignals(start_signals);
            float[] output_signals      = ReadTextNeuroNet.PassOutput(start_signals_float);

            float max_value = 0;
            int   letter    = 0;

            for (int i = 0; i < output_signals.Length; i++)
            {
                if (output_signals[i] > max_value)
                {
                    max_value = output_signals[i];
                    letter    = i;
                }
            }
            //Debug.Log("letter = " + (char)letter + " (" + letter + ")");
            if (letter > 30)
            {
                final_massage += (char)(letter);
                string mes = "";
                mes += (char)(letter);
                //FileWork.SaveText(mes, @"C:\Users\Admin\Desktop\Letter\ForComplete\text.txt");
            }
            else
            {
                final_massage += "?";
                //FileWork.SaveText("&", @"C:\Users\Admin\Desktop\Letter\ForComplete\text.txt");
            }


            current_column_WriteTextPass++;
            if (LineList[current_line_WriteTextPass].LetterList.Count <= current_column_WriteTextPass)
            {
                current_column_WriteTextPass = 0;
                current_line_WriteTextPass++;
                final_massage += "\n ";
                //FileWork.SaveText("\n", @"C:\Users\Admin\Desktop\Letter\ForComplete\text.txt");
            }

            return(true);
        }