Beispiel #1
0
        /// <summary>
        /// Small menu which appears when training is interrupted
        /// </summary>
        public void InterruptMenu()
        {
            Console.WriteLine();
            for (int i = 0; i < settings.parentsPerGeneration + 3; i++)
            {
                Console.WriteLine();
            }
            Console.WriteLine("## Training Interrupted!");
            Console.WriteLine("1. Evaluate image in current state. \n2. Save best weights. \n3. Reset weights. \n4. Anneal now. \n5. Display all weights. \n6. Set current mutation rate \n0. Continue training.");
            string inp = Console.ReadLine();

            switch (inp)
            {
            case "1":
                Console.WriteLine("Enter image path to evaluate: ");
                string evstr = Console.ReadLine();
                convolution.LoadImage(evstr);
                convolution.Convolve(0, settings);
                System.IO.File.WriteAllBytes("tmpimg.png", convolution.dstImg.ToBytes());
                Console.WriteLine("Saved output!");
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
                break;

            case "2":
                Console.WriteLine("Save as: ");
                string inpath = Console.ReadLine();
                try
                {
                    //System.IO.File.WriteAllBytes("tmpimg.png", convolution.dstImg.ToBytes());
                    //FATAL ERROR, Do not use the following:
                    //var settings = new JsonSerializerSettings { ContractResolver = new OptOutContractResolver() };

                    /*try
                     * {
                     *  System.IO.File.WriteAllText(inpath, JsonConvert.SerializeObject(currentParents, Formatting.Indented));
                     * } catch(Exception e)
                     * {
                     *  Console.WriteLine(e.Message);
                     * }*/
                    List <string> outjson = new List <string>
                    {
                        "{",
                        "\"parentKernels\":",
                        "[",
                        "[",
                        "[",
                        ""
                    };
                    for (int x = 0; x < currentParents.parentKernels.Count; x++)
                    {
                        for (int y = 0; y < settings.nodeLayers; y++)
                        {
                            for (int z = 0; z < convolution.kernels[y].Rows; z++)
                            {
                                for (int w = 0; w < convolution.kernels[y].Cols; w++)
                                {
                                    outjson[outjson.Count - 1] += currentParents.parentKernels[z].kernels[y].At <float>(z, w);   //convolution.kernels[i].At<float>(x, j);// + ((x<convolution.kernels[i].Rows-1)&& (j < convolution.kernels[i].Cols-1)?", ":"");
                                    if (new Size(z + 1, w + 1) != convolution.kernels[y].Size())
                                    {
                                        outjson[outjson.Count - 1] += ", ";
                                    }
                                }
                                outjson.Add("");
                                //Console.WriteLine();
                            }
                            if (y < settings.nodeLayers - 1)
                            {
                                outjson[outjson.Count - 1] += "], ";
                                outjson.Add("[");
                            }
                            else
                            {
                                outjson[outjson.Count - 1] += "]";
                            }
                            outjson.Add("");
                        }
                        if (x < currentParents.parentKernels.Count - 1)
                        {
                            outjson[outjson.Count - 1] += "], ";
                            outjson.Add("[");
                            outjson.Add("[");
                        }
                        else
                        {
                            outjson[outjson.Count - 1] += "]";
                        }
                        outjson.Add("");
                    }
                    outjson[outjson.Count - 1] += "]";
                    outjson.Add("}");
                    System.IO.File.WriteAllLines(inpath, outjson.ToArray());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.WriteLine("Saved!");
                break;

            case "3":
                //InitialiseNetwork();
                shouldPause = false;
                //TrainNetwork();
                break;

            case "4":
                Console.WriteLine("Anneal strength as a decimal: ");
                string inpower   = Console.ReadLine();
                float  newAnneal = 0;
                float.TryParse(inpower, out newAnneal);
                Console.WriteLine("Annealing network for 1 iteration!");
                //FillMutations(currAnneal);
                Mat[] tmpMut = convolution.MutateKernel(currAnneal);
                for (int i = 0; i < convolution.kernels.Length; i++)
                {
                    //convolution.SetKernels();
                    Cv2.Add(convolution.kernels[i], tmpMut[i], convolution.kernels[i]);
                }
                break;

            case "5":
                try
                {
                    for (int i = 0; i < settings.nodeLayers; i++)
                    {
                        System.IO.File.WriteAllBytes("weights" + i + ".png", convolution.kernels[i].ToBytes());

                        for (int x = 0; x < convolution.kernels[i].Rows; x++)
                        {
                            for (int j = 0; j < convolution.kernels[i].Cols; j++)
                            {
                                Console.Write(convolution.kernels[i].At <float>(x, j) + " , ");
                            }
                            Console.WriteLine();
                        }
                    }
                    //System.IO.File.WriteAllText(inpath, JsonConvert.SerializeObject(networkConnections));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.WriteLine("Saved!");
                break;

            case "6":
                Console.WriteLine("New mutation rate as a decimal: ");
                string inrate = Console.ReadLine();
                try
                {
                    settings.mutationRate = float.Parse(inrate);
                } catch
                {
                    Console.WriteLine("Not a float!");
                }
                break;

            case "0":
                shouldPause = false;
                TrainNetwork();
                break;
            }
            InterruptMenu();
        }