Esempio n. 1
0
        private void Bw1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                //定义BP神经网络类
                BpNet bp = new BpNet(400, 10);

                int      right_count = 0;
                string[] files;
                double[] tmp = new double[20];
                //读取文件
                for (int i = 0; i < 10; i++)
                {
                    right_count = 0;
                    string dir = test_path + @"\" + i + @"\";
                    files = Directory.GetFiles(dir);

                    //共files.Length个样本,每个样本数据有400个字节
                    double[] input  = new double[400];
                    double[] output = new double[10];

                    for (int j = 0; j < files.Length; j++)
                    {
                        Bitmap bmp = new Bitmap(files[j]);

                        for (int k = 0; k < bmp.Height; k++)
                        {
                            for (int l = 0; l < bmp.Width; l++)
                            {
                                input[k * bmp.Width + l] = bmp.GetPixel(l, k).R;
                            }
                        }

                        //交换行,因为位图存储时,先存储最后一行,从图片的底部开始,逐渐向上扫描
                        for (int k = 0; k < bmp.Height / 2; k++)
                        {
                            for (int l = 0; l < bmp.Width; l++)
                            {
                                tmp[l] = input[k * bmp.Width + l];
                                input[k * bmp.Width + l] = input[(bmp.Height - 1 - k) * bmp.Width + l];
                                input[(bmp.Height - 1 - k) * bmp.Width + l] = tmp[l];
                            }
                        }

                        if (i == bp.test(input))
                        {
                            right_count++;
                        }
                    }

                    this.lbTestResult.Items.Add(files.Length + "个" + i + "样本识别成功率:" + (1.0 * right_count / files.Length * 100).ToString("0.00") + "%");
                }
                this.lblMessage.Text = "测试成功!";
            }
            catch (Exception ex)
            {
                MessageBox.Show("出错了" + ex.Message);
            }
        }
Esempio n. 2
0
        private void Bw_DoWork(object sender, DoWorkEventArgs e)
        {
            //定义BP神经网络类
            BpNet bp = new BpNet(400, 10);

            double[] tmp = new double[20];

            try
            {
                //学习率
                double lr    = Double.Parse(this.txtLearnRate.Text.Trim());
                int    count = 0; //计数器
                int    study = 0; //学习(训练)次数

                //数据字典
                Dictionary <string, int> filedictionary = new Dictionary <string, int>();
                for (int i = 0; i < 10; i++)
                {
                    string   dir   = train_path + @"\" + i + @"\";
                    string[] files = Directory.GetFiles(dir);
                    foreach (string item in files)
                    {
                        filedictionary.Add(item, i);
                    }
                }

                //声明数据存储区域
                double[,] input  = new double[filedictionary.Count, 400];
                double[,] output = new double[filedictionary.Count, 10];

                //数据装载
                foreach (KeyValuePair <string, int> item in filedictionary)
                {
                    Bitmap bmp = new Bitmap(item.Key);

                    for (int k = 0; k < bmp.Height; k++)
                    {
                        for (int l = 0; l < bmp.Width; l++)
                        {
                            input[count, k *bmp.Width + l] = bmp.GetPixel(l, k).R;
                        }
                    }

                    //交换行,因为位图存储时,先存储最后一行,从图片的底部开始,逐渐向上扫描
                    for (int k = 0; k < bmp.Height / 2; k++)
                    {
                        for (int l = 0; l < bmp.Width; l++)
                        {
                            tmp[l] = input[count, k *bmp.Width + l];
                            input[count, k *bmp.Width + l] = input[count, (bmp.Height - 1 - k) * bmp.Width + l];
                            input[count, (bmp.Height - 1 - k) * bmp.Width + l] = tmp[l];
                        }
                    }

                    output[count, item.Value] = 1;//第j个图片被分为第i类
                    count++;
                }

                do
                {
                    if (!bw.CancellationPending)//2.检测用户是否取消
                    {
                        //训练
                        bp.train(input, output, lr);
                        study++;
                        this.lblMessage.Text = "第" + study + "次训练的误差: " + bp.e;
                    }
                    else
                    {
                        break;//停止训练
                    }
                } while (bp.e > 0.01 && study < 50000);
            }
            catch (Exception ex)
            {
                MessageBox.Show("出错了" + ex.Message);
            }
            finally//出错或者中途取消也会保存权值矩阵的信息
            {
                bp.saveMatrix(bp.w, "w.txt");
                bp.saveMatrix(bp.v, "v.txt");
                bp.saveMatrix(bp.b1, "b1.txt");
                bp.saveMatrix(bp.b2, "b2.txt");
                this.lblMessage.Text = "训练终止!";
            }
        }