Example #1
0
        public void Train(int samples)
        {
            if (samples > 0 && TryLoadTrainingSet(samples))
            {
                return;
            }

            List<string> knowledge = new List<string>();
            foreach (string file in knowledgeFiles)
            {
                knowledge.AddRange(File.ReadAllLines(file));
            }

            string[] lines = knowledge.ToArray();
            samples = lines.Length; ;
            int pixels = DimensionY * DimensionX;
            int classes = netKeys.Count;

            Emgu.CV.Matrix<Single> training = new Emgu.CV.Matrix<Single>(samples, pixels);
            Emgu.CV.Matrix<Single> class_training = new Emgu.CV.Matrix<Single>(samples, classes);

            Emgu.CV.Matrix<int> layers = new Emgu.CV.Matrix<int>(3, 1);
            layers[0, 0] = pixels;
            layers[1, 0] = (int)(factor * netKeys.Count);
            layers[2, 0] = classes;

            for (int i = 0; i < samples; i++)
            {
                LetterInfo info = LetterInfo.ReadLetterInfoLine(lines[i]);
                byte[] bytes = Convert.FromBase64String(info.Base64);

                float[] input = AdjustInput(bytes);
                for (int j = 0; j < pixels; j++)
                {
                    training[i, j] = input[j];
                }
                /*
for (int a = -1; a <= 1; a++)
                    for (int b = -1; b <= 1; b++)
                        for (int c = 0; c < DimensionX; c++)
                            for (int d = 0; d < DimensionY; d++)
                            {
                                if (0 > c + a || c + a >= DimensionX) continue;
                                if (0 > d + b || d + b >= DimensionY) continue;
                                training[i, d * DimensionX + c] = bytes[(b + d) * DimensionX + (a + c)];
                            }                 */
                int d = netKeys.IndexOf(info.Char);
                class_training[i, d] = 1;
            }

            nnet = new Emgu.CV.ML.ANN_MLP(layers, Emgu.CV.ML.MlEnum.ANN_MLP_ACTIVATION_FUNCTION.SIGMOID_SYM, 0.6, 1);
            Emgu.CV.ML.Structure.MCvANN_MLP_TrainParams p = new Emgu.CV.ML.Structure.MCvANN_MLP_TrainParams();
            p.term_crit.type = Emgu.CV.CvEnum.TERMCRIT.CV_TERMCRIT_EPS | Emgu.CV.CvEnum.TERMCRIT.CV_TERMCRIT_ITER;
            p.term_crit.max_iter = 1000;
            p.term_crit.epsilon = 0.000001;
            p.train_method = Emgu.CV.ML.MlEnum.ANN_MLP_TRAIN_METHOD.BACKPROP;
            p.bp_dw_scale = 0.1;
            p.bp_moment_scale = 0.1;

            bool success = false;
            try
            {
                if (File.Exists(saveFile))
                {
                    nnet.Load(saveFile);
                    success = true;
                }
            }
            catch
            {
            }

            if (!success)
            {
                int iteration = nnet.Train(training, class_training, null, p, Emgu.CV.ML.MlEnum.ANN_MLP_TRAINING_FLAG.DEFAULT);
                if (saveFile != null)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(saveFile));
                    nnet.Save(saveFile);
                }
            }
        }
Example #2
0
        private bool TryLoadTrainingSet(int samples)
        {
            int pixels = DimensionY * DimensionX;
            int classes = netKeys.Count;

            Emgu.CV.Matrix<Single> training = new Emgu.CV.Matrix<Single>(samples, pixels);
            Emgu.CV.Matrix<Single> class_training = new Emgu.CV.Matrix<Single>(samples, classes);

            Emgu.CV.Matrix<int> layers = new Emgu.CV.Matrix<int>(3, 1);
            layers[0, 0] = pixels;
            layers[1, 0] = (int)(factor * netKeys.Count);
            layers[2, 0] = classes;

            nnet = new Emgu.CV.ML.ANN_MLP(layers, Emgu.CV.ML.MlEnum.ANN_MLP_ACTIVATION_FUNCTION.SIGMOID_SYM, 0.6, 1);
            Emgu.CV.ML.Structure.MCvANN_MLP_TrainParams p = new Emgu.CV.ML.Structure.MCvANN_MLP_TrainParams();
            p.term_crit.type = Emgu.CV.CvEnum.TERMCRIT.CV_TERMCRIT_EPS | Emgu.CV.CvEnum.TERMCRIT.CV_TERMCRIT_ITER;
            p.term_crit.max_iter = 1000;
            p.term_crit.epsilon = 0.000001;
            p.train_method = Emgu.CV.ML.MlEnum.ANN_MLP_TRAIN_METHOD.BACKPROP;
            p.bp_dw_scale = 0.1;
            p.bp_moment_scale = 0.1;

            try
            {
                if (File.Exists(saveFile))
                {
                    nnet.Load(saveFile);
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }