Exemplo n.º 1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "*.cnn|*.cnn";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                CnnHelper.SaveCnn(cnn, sfd.FileName);
                //CnnHelper.SaveCnnGroup(cnn, sfd.FileName);
            }
        }
Exemplo n.º 2
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "*.cnn|*.cnn";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                cnn = CnnHelper.LoadCnn(ofd.FileName);
                //cnn = CnnHelper.LoadCnnGroup(ofd.FileName);
            }
        }
Exemplo n.º 3
0
        private void btnPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "*.jpg|*.jpg";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Bitmap img = new Bitmap(ofd.FileName);
                //img = CnnHelper.ZoomImg(img, 28, 28);
                Image <Bgr, float> trainingData = new Image <Bgr, float>(img);
                double[,] input = new double[28, 28];
                for (int w = 0; w < 28; w++)
                {
                    for (int h = 0; h < 28; 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];
                    }
                    Console.Write(labels[n] + " ");
                }
                Console.WriteLine("");
                //Console.WriteLine(i + ":" + label + "," + max);
                //CnnHelper.ShowChange(labels, labelsTrue, 10000);
                this.Invoke(new Action(() =>
                {
                    lblResult.Text = max.ToString();
                    pbImage.Image  = img;
                }));
            }
        }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
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;
                    string path              = "F:\\迅雷下载\\lfw\\";
                    StreamReader sr          = new StreamReader(path + "pairsDevTrain.txt");
                    int trueCount            = Convert.ToInt32(sr.ReadLine()); //正确数
                    string nowLine           = sr.ReadLine();                  //当前行
                    List <string[]> dataList = new List <string[]>();
                    while (!String.IsNullOrEmpty(nowLine))
                    {
                        string[] values  = nowLine.Split('\t');
                        string[] values2 = new string[4];
                        if (values.Length == 3)
                        {
                            values2[0] = values[0];
                            values2[1] = values[1];
                            values2[2] = values[0];
                            values2[3] = values[2];
                        }
                        else
                        {
                            values2[0] = values[0];
                            values2[1] = values[1];
                            values2[2] = values[2];
                            values2[3] = values[3];
                        }
                        dataList.Add(values2);
                        nowLine = sr.ReadLine();//当前行
                    }
                    while (CnnHelper.TruePercent != 1)
                    {
                        trainCount++;
                        CnnHelper.SumCount  = 0;
                        CnnHelper.TrueCount = 0;
                        #region LFW人脸集
                        List <string[]> randomList = new List <string[]>();
                        foreach (var data in dataList)//随机排序
                        {
                            randomList.Insert(CnnHelper.RandomObj.Next(randomList.Count + 1), data);
                        }
                        foreach (var data in randomList)
                        {
                            string file1 = path + "lfw\\" + data[0] + "\\" + data[0] + "_" + Convert.ToInt32(data[1]).ToString("0000") + ".jpg";
                            string file2 = path + "lfw\\" + data[2] + "\\" + data[2] + "_" + Convert.ToInt32(data[3]).ToString("0000") + ".jpg";
                            using (Bitmap img1 = new Bitmap(file1))
                            {
                                using (Bitmap img2 = new Bitmap(file2))
                                {
                                    double[] labels = new double[1];
                                    if (dataList.IndexOf(data) < trueCount)
                                    {
                                        labels[0] = 1;
                                    }
                                    Image <Bgr, float> trainingData1 = new Image <Bgr, float>(img1);
                                    Image <Bgr, float> trainingData2 = new Image <Bgr, float>(img2);
                                    double[,] input = new double[img1.Width * 2 + 8, img1.Height + 2];
                                    for (int w = 2; w < img1.Width; w++)
                                    {
                                        for (int h = 1; h < img1.Height; h++)
                                        {
                                            input[w, h] = Color.FromArgb(0,
                                                                         (int)trainingData1.Data[h, w, 2],
                                                                         (int)trainingData1.Data[h, w, 1],
                                                                         (int)trainingData1.Data[h, w, 0]
                                                                         ).ToArgb() / (double)0xFFFFFF;
                                            input[img1.Width + 4 + w, h] = Color.FromArgb(0,
                                                                                          (int)trainingData2.Data[h, w, 2],
                                                                                          (int)trainingData2.Data[h, w, 1],
                                                                                          (int)trainingData2.Data[h, w, 0]
                                                                                          ).ToArgb() / (double)0xFFFFFF;
                                        }
                                    }
                                    double[] forwardOutputFull = null;
                                    cnn.Train(input, labels, learningRate, ref forwardOutputFull);
                                    CnnHelper.ShowChange2(forwardOutputFull, labels);
                                    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 = labels[0].ToString();
                                        pbImage1.Image = new Bitmap(img1);
                                        pbImage2.Image = new Bitmap(img2);
                                    }));
                                }
                            }
                        }
                        #endregion
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            });
            threadCnn.Start();
        }