Example #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (threadCnn != null && threadCnn.ThreadState == ThreadState.Running)
            {
                return;
            }
            threadCnn = new Thread(() =>
            {
                CnnHelper.SumCount  = 0;
                CnnHelper.TrueCount = 0;
                #region LFW人脸集
                foreach (var file in Directory.GetFiles("img2", "*.jpg"))
                {
                    using (Bitmap img = new Bitmap(file))
                    {
                        byte label = Convert.ToByte(file.Substring(file.LastIndexOf('\\') + 1, 1));
                        Image <Bgr, float> trainingData = new Image <Bgr, float>(img);;
                        double[,] input = new double[img.Width, img.Height];
                        for (int w = 0; w < img.Width; w++)
                        {
                            for (int h = 0; h < img.Height; h++)
                            {
                                input[w, h] = Color.FromArgb(0,
                                                             (int)trainingData.Data[h, w, 2],
                                                             (int)trainingData.Data[h, w, 1],
                                                             (int)trainingData.Data[h, w, 0]
                                                             ).ToArgb() / (double)0xFFFFFF;
                            }
                        }
                        input               = CnnHelper.MatrixExpand(input, 2, 2, 0);
                        double[] labels     = cnn.Predict(input);
                        double[] labelsTrue = new double[10];
                        double maxtype      = labels[0], max = 0;
                        for (int n = 0; n < 10; n++)
                        {
                            if (maxtype < labels[n])
                            {
                                max     = n;
                                maxtype = labels[n];
                            }
                            if (label == n)
                            {
                                labelsTrue[n] = 1;
                            }
                        }
                        //Console.WriteLine(i + ":" + label + "," + max);
                        CnnHelper.ShowChange(labels, labelsTrue, 10000);
                        this.Invoke(new Action(() =>
                        {
                            lblInfo.Text = String.Format("识别次数:{0}/{1} 正确率:{2:00.####%}", CnnHelper.TrueCount, CnnHelper.SumCount, CnnHelper.TrueCount / (double)CnnHelper.SumCount);

                            lblResult.Text = CnnHelper.LabelsNum + " " + CnnHelper.ResultNum;
                            pbImage1.Image = new Bitmap(img);
                        }));
                    }
                }
                #endregion
            });
            threadCnn.Start();
        }
Example #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (threadCnn != null && threadCnn.ThreadState == ThreadState.Running)
            {
                return;
            }
            learningRate = (double)numLearningRate.Value;
            threadCnn    = new Thread(() =>
            {
                try
                {
                    trainCount = 0;
                    //int retry = 0;
                    while (CnnHelper.TruePercent != 1)
                    {
                        trainCount++;
                        CnnHelper.SumCount  = 0;
                        CnnHelper.TrueCount = 0;
                        if (!chkHandwritten.Checked)
                        {
                            #region MNIST库
                            using (FileStream fs = new FileStream("data/MNIST/train-labels.idx1-ubyte", FileMode.Open))
                            {
                                using (FileStream fsImages = new FileStream("data/MNIST/train-images.idx3-ubyte", FileMode.Open))
                                {
                                    byte[] bytes4 = new byte[4];
                                    fsImages.Seek(4, SeekOrigin.Current);
                                    fs.Seek(8, SeekOrigin.Current);
                                    fsImages.Read(bytes4, 0, 4);
                                    int count = ToInt32(bytes4);
                                    fsImages.Read(bytes4, 0, 4);
                                    int height = ToInt32(bytes4);
                                    fsImages.Read(bytes4, 0, 4);
                                    int width = ToInt32(bytes4);
                                    for (int i = 0; i < count; i++)
                                    {
                                        Bitmap img      = GetImage(fsImages, width, height);
                                        byte label      = GetLable(fs);
                                        double[] labels = new double[10];
                                        for (int i2 = 0; i2 < 10; i2++)
                                        {
                                            labels[i2] = 0;
                                        }
                                        labels[label] = 1;
                                        Image <Bgr, float> trainingData = new Image <Bgr, float>(img);;
                                        double[,] input = new double[width, height];
                                        for (int w = 0; w < width; w++)
                                        {
                                            for (int h = 0; h < height; h++)
                                            {
                                                input[w, h] = Color.FromArgb(0,
                                                                             (int)trainingData.Data[h, w, 2],
                                                                             (int)trainingData.Data[h, w, 1],
                                                                             (int)trainingData.Data[h, w, 0]
                                                                             ).ToArgb() / (double)0xFFFFFF;
                                            }
                                        }
                                        int left = CnnHelper.RandomObj.Next(4), top = CnnHelper.RandomObj.Next(4);
                                        input    = CnnHelper.MatrixExpand(input, left, top, 4 - left, 4 - top, 0);
                                        double[] forwardOutputFull = null;
                                        cnn.Train(input, labels, learningRate, ref forwardOutputFull);
                                        CnnHelper.ShowChange(forwardOutputFull, labels, 60000);
                                        this.Invoke(new Action(() =>
                                        {
                                            lblInfo.Text = String.Format("训练周期:{0} 训练次数:{1}/{2} 正确率:{3:00.####%}", trainCount, CnnHelper.TrueCount, CnnHelper.SumCount, CnnHelper.TrueCount / (double)CnnHelper.SumCount);
                                            if (i % 20 == 0)
                                            {
                                                lblResult.Text = CnnHelper.LabelsNum + " " + CnnHelper.ResultNum;
                                                pbImage.Image  = img;
                                            }
                                            //if (CnnHelper.LabelsNum != CnnHelper.ResultNum && retry < 5)
                                            //{
                                            //    i--;
                                            //    retry++;
                                            //}
                                            //else
                                            //{
                                            //    retry = 0;
                                            //}
                                        }));
                                        //img.Save("imgs/" + i + "_" + label + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                                    }
                                    fsImages.Close();
                                    fs.Close();
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            #region 手写字体集
                            foreach (var file in Directory.GetFiles("img", "*.jpg"))
                            {
                                using (Bitmap img = new Bitmap(file))
                                {
                                    byte label      = Convert.ToByte(file.Substring(file.LastIndexOf('\\') + 1, 1));
                                    double[] labels = new double[10];
                                    for (int i2 = 0; i2 < 10; i2++)
                                    {
                                        labels[i2] = 0;
                                    }
                                    labels[label] = 1;
                                    //he
                                    Image <Bgr, float> trainingData = new Image <Bgr, float>(img);
                                    double[,] input = new double[img.Width, img.Height];
                                    for (int w = 0; w < img.Width; w++)
                                    {
                                        for (int h = 0; h < img.Height; h++)
                                        {
                                            input[w, h] = Color.FromArgb(0,
                                                                         (int)trainingData.Data[h, w, 2],
                                                                         (int)trainingData.Data[h, w, 1],
                                                                         (int)trainingData.Data[h, w, 0]
                                                                         ).ToArgb() / (double)0xFFFFFF;
                                        }
                                    }
                                    int left = CnnHelper.RandomObj.Next(4), top = CnnHelper.RandomObj.Next(4);
                                    input    = CnnHelper.MatrixExpand(input, left, top, 4 - left, 4 - top, 0);
                                    double[] forwardOutputFull = null;
                                    cnn.Train(input, labels, learningRate, ref forwardOutputFull);
                                    CnnHelper.ShowChange(forwardOutputFull, labels, 60000);
                                    this.Invoke(new Action(() =>
                                    {
                                        lblInfo.Text = String.Format("训练周期:{0} 训练次数:{1}/{2} 正确率:{3:00.####%}", trainCount, CnnHelper.TrueCount, CnnHelper.SumCount, CnnHelper.TrueCount / (double)CnnHelper.SumCount);

                                        lblResult.Text = CnnHelper.LabelsNum + " " + CnnHelper.ResultNum;
                                        pbImage.Image  = new Bitmap(img);
                                    }));
                                }
                            }
                            #endregion
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            });
            threadCnn.Start();
        }
Example #3
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (threadCnn != null && threadCnn.ThreadState == ThreadState.Running)
            {
                return;
            }
            threadCnn = new Thread(() =>
            {
                CnnHelper.SumCount  = 0;
                CnnHelper.TrueCount = 0;
                if (!chkHandwritten.Checked)
                {
                    #region MNIST库
                    using (FileStream fs = new FileStream("t10k-labels.idx1-ubyte", FileMode.Open))
                    {
                        using (FileStream fsImages = new FileStream("t10k-images.idx3-ubyte", FileMode.Open))
                        {
                            byte[] bytes4 = new byte[4];
                            fsImages.Seek(4, SeekOrigin.Current);
                            fs.Seek(8, SeekOrigin.Current);
                            fsImages.Read(bytes4, 0, 4);
                            int count = ToInt32(bytes4);
                            fsImages.Read(bytes4, 0, 4);
                            int height = ToInt32(bytes4);
                            fsImages.Read(bytes4, 0, 4);
                            int width = ToInt32(bytes4);
                            for (int i = 0; i < count; i++)
                            {
                                Bitmap img = GetImage(fsImages, width, height);
                                byte label = GetLable(fs);
                                Image <Bgr, float> trainingData = new Image <Bgr, float>(img);;
                                double[,] input = new double[width, height];
                                for (int w = 0; w < width; w++)
                                {
                                    for (int h = 0; h < height; h++)
                                    {
                                        input[w, h] = Color.FromArgb(0,
                                                                     (int)trainingData.Data[h, w, 2],
                                                                     (int)trainingData.Data[h, w, 1],
                                                                     (int)trainingData.Data[h, w, 0]
                                                                     ).ToArgb() / (double)0xFFFFFF;
                                    }
                                }
                                input               = CnnHelper.MatrixExpand(input, 2, 2, 0);
                                double[] labels     = cnn.Predict(input);
                                double[] labelsTrue = new double[10];
                                double maxtype      = labels[0], max = 0;
                                for (int n = 0; n < 10; n++)
                                {
                                    if (maxtype < labels[n])
                                    {
                                        max     = n;
                                        maxtype = labels[n];
                                    }
                                    if (label == n)
                                    {
                                        labelsTrue[n] = 1;
                                    }
                                }
                                //Console.WriteLine(i + ":" + label + "," + max);
                                CnnHelper.ShowChange(labels, labelsTrue, 10000);
                                this.Invoke(new Action(() =>
                                {
                                    lblInfo.Text = String.Format("识别次数:{0}/{1} 正确率:{2:00.####%}", CnnHelper.TrueCount, CnnHelper.SumCount, CnnHelper.TrueCount / (double)CnnHelper.SumCount);
                                    if (i % 20 == 0)
                                    {
                                        lblResult.Text = CnnHelper.LabelsNum + " " + CnnHelper.ResultNum;
                                        pbImage.Image  = img;
                                    }
                                }));
                                //img.Save("imgs/" + i + "_" + label + ".jpg");
                            }
                            fsImages.Close();
                            fs.Close();
                        }
                    }
                    #endregion
                }
                else
                {
                    #region 手写字体集
                    foreach (var file in Directory.GetFiles("img2", "*.jpg"))
                    {
                        using (Bitmap img = new Bitmap(file))
                        {
                            byte label = Convert.ToByte(file.Substring(file.LastIndexOf('\\') + 1, 1));
                            Image <Bgr, float> trainingData = new Image <Bgr, float>(img);;
                            double[,] input = new double[img.Width, img.Height];
                            for (int w = 0; w < img.Width; w++)
                            {
                                for (int h = 0; h < img.Height; h++)
                                {
                                    input[w, h] = Color.FromArgb(0,
                                                                 (int)trainingData.Data[h, w, 2],
                                                                 (int)trainingData.Data[h, w, 1],
                                                                 (int)trainingData.Data[h, w, 0]
                                                                 ).ToArgb() / (double)0xFFFFFF;
                                }
                            }
                            input               = CnnHelper.MatrixExpand(input, 2, 2, 0);
                            double[] labels     = cnn.Predict(input);
                            double[] labelsTrue = new double[10];
                            double maxtype      = labels[0], max = 0;
                            for (int n = 0; n < 10; n++)
                            {
                                if (maxtype < labels[n])
                                {
                                    max     = n;
                                    maxtype = labels[n];
                                }
                                if (label == n)
                                {
                                    labelsTrue[n] = 1;
                                }
                            }
                            //Console.WriteLine(i + ":" + label + "," + max);
                            CnnHelper.ShowChange(labels, labelsTrue, 10000);
                            this.Invoke(new Action(() =>
                            {
                                lblInfo.Text = String.Format("识别次数:{0}/{1} 正确率:{2:00.####%}", CnnHelper.TrueCount, CnnHelper.SumCount, CnnHelper.TrueCount / (double)CnnHelper.SumCount);

                                lblResult.Text = CnnHelper.LabelsNum + " " + CnnHelper.ResultNum;
                                pbImage.Image  = new Bitmap(img);
                            }));
                        }
                    }
                    #endregion
                }
            });
            threadCnn.Start();
        }