コード例 #1
0
 public ConvolutionalChromosome(ConvolutionalNetwork cNet, ConvRandomParams crp, DenseRandomParams drp)
 {
     this.crp = crp;
     this.drp = drp;
     //добавить рандомные параметры
     convPart  = new ConvStructure(cNet.slidingWindows, cNet.convActivationIndexes, cNet.filters, cNet.convDropoutIndexes, cNet.convDropoutRates);
     densePart = new DenseStructure(cNet.denseActivationIndexes, cNet.neurons, cNet.denseDropoutIndexes, cNet.denseDropoutRates);
 } // конструктор для создания хромосомы из полей описательного класса ConvolutionalNetwork для последующего использования в генетических алгоритмах (например, для поиска лучшего варианта чем текущий)
コード例 #2
0
        ///////////////////////////////////////////////////////
        // new Process
        public void UpdateAssessmentParams(ConvolutionalChromosome chromosome, GeneticProgramm gp, int epoch)
        {
            instance.Invoke(new Action(() => {
                if (!instance.errDontClearChB.Checked)
                {
                    instance.ErrorTB.Clear();
                }
                if (!instance.chrDontClearChB.Checked)
                {
                    instance.chrOutTB.Clear();
                }
            }));
            ConvolutionalNetwork tempNet = new ConvolutionalNetwork(chromosome, epoch, gp._nrp.networkName);

            ConvolutionalNetwork.CreateNetworkScript(tempNet, chromosome.crp.convActivations, chromosome.drp.denseActivations);
            ConvolutionalNetwork.CreateConvBatFile("temp_convolution.py", @"C:\keras\Directory\scripts\convolutional\genetic", gp._nrp.datasetPath,
                                                   gp._nrp._modelPath, gp._nrp._labelPath, gp._nrp._plotPath, gp._nrp.networkName, epoch, chromosome.indexNumber, 0);

            CreateConvBatProcess();

            chromosome.accuracy    = instance.tempAccuracy;
            chromosome.paramsCount = instance.tempParameters;
        }
コード例 #3
0
        public static void InsertConvNetworkCode(String path, int line, ConvolutionalNetwork network, List <String> convActivations, List <String> denseActivations)
        {
            string loss_function = "\"categorical_crossentropy\"";

            if (network.outputs == 2)
            {
                loss_function = "\"binary_crossentropy\"";
            }

            string init_lr = network.trainConstSpeed.ToString();

            init_lr = init_lr.Replace(",", ".");
            string callbacks_str = "";


            string code = "";

            for (int i = 0; i < network.filters.Count; i++)
            {
                if (i == 0)
                {
                    code += "model.add(ZeroPadding2D((1, 1), input_shape = (128, 128, 3)))" + Environment.NewLine;
                }
                code += "model.add(Conv2D(" + network.filters[i].ToString() + ", kernel_size = (" +
                        network.slidingWindows[i][0].ToString() + ", " + network.slidingWindows[i][1].ToString() +
                        "), strides = (1, 1), padding='same', activation = '" + convActivations[network.convActivationIndexes[i]] + "'))" + Environment.NewLine;
                code += "model.add(MaxPooling2D(pool_size = (2, 2), strides = (2, 2)))" + Environment.NewLine;
            }
            code += "model.add(Flatten())" + Environment.NewLine;
            int dropoutCounter = 0;

            for (int i = 0; i < network.neurons.Count; i++)
            {
                code += "model.add(Dense(" + network.neurons[i].ToString() + ", activation = '" +
                        denseActivations[network.denseActivationIndexes[i]] + "'))" + Environment.NewLine;
                if (network.denseDropoutIndexes.Count > 0)
                {
                    if (network.denseDropoutIndexes[dropoutCounter] == i)
                    {
                        code += "model.add(Dropout(" + ((float)network.denseDropoutRates[dropoutCounter] / 100).ToString() + "))" + Environment.NewLine;

                        if ((network.denseDropoutIndexes.Count - 1) != dropoutCounter)
                        {
                            dropoutCounter++;
                        }
                    }
                }
            }
            if (network.outputs == 2)
            {
                code += "model.add(Dense(2, activation = \"softmax\"))" + Environment.NewLine;
            }
            else
            {
                code += "model.add(Dense(len(lb.classes_), activation = \"softmax\"))" + Environment.NewLine;
            }
            code += "INIT_LR = " + init_lr + Environment.NewLine +
                    "EPOCHS = " + network.networkLearningEpochs.ToString() + Environment.NewLine +
                    "BS = " + network.batchSize.ToString() + Environment.NewLine + Environment.NewLine;
            code += "print(\"[INFO] training network...\")" + Environment.NewLine;
            code += "opt = SGD(lr=INIT_LR)" + Environment.NewLine;
            code += "model.compile(loss=" + loss_function + ", optimizer=opt, metrics=[\"acc\"])" + Environment.NewLine;
            //code += "" + Environment.NewLine;
            Support.insertLineToFile(path, line, code);

            string forBinaryCode = "trainY = to_categorical(trainY)" + Environment.NewLine +
                                   "testY = to_categorical(testY)" + Environment.NewLine;

            if (network.outputs == 2)
            {
                Support.insertLineToFile(path, 66, forBinaryCode);
            }
        }
コード例 #4
0
        public static void CreateNetworkScript(/*String toCopyPath, String copyPath, int line,*/ ConvolutionalNetwork network, List <String> convActivations, List <String> denseActivations)
        {
            string fileName;  // = "convolutional_v" + Properties.Settings.Default.convScriptNumber + ".py";

            fileName = "temp_convolution.py";
            string path = @"C:\keras\Directory\scripts\convolutional\genetic\";

            //path = @"C:\keras\Directory\scripts\convolutional\genetic\elites\";
            try
            {
                File.Copy(@"C:\keras\Directory\scripts\convolutional\convolutional.py", path + fileName, true);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }

            InsertConvNetworkCode(path + fileName, 75, network, convActivations, denseActivations);
            //Properties.Settings.Default.convScriptNumber++;
            //ConfigEditor.Config.Write("convScriptNumber", Properties.Settings.Default.convScriptNumber);
        }