public void Test() { /* * epoch算法: * 1、把所有的训练集分成N个epoch, * 2、每个epoch执行正向和反向传播后,则需要更新一次权重, * 3、把所有的训练集都走了一遍之后就可以执行精度检测了,需要检测训练集和测试集 * * 一个卷积可对应一个b,例如:有50个卷积核,就会有50个b * 1.每一个图片都必须和当前层的所有卷积核进行计算。 * 2、如果是RGB图片,每个卷积核也是RGB3通道的,最后得到的值相加产生一个特征图 * 3、如果有36个卷积核,输出的特征图是36个, * 4、下一层的每个卷积核的通道数比必须要上一层的特征图的个数保持一致 * //*/ //var layer = new LLayer[] //{ // new LConvolution(50), // new LMaxPool(), // new LRelu(), // new LConvolution(30), // new LMaxPool(), // new LRelu(), //}; string _mi10_50_2 = "[[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0]]"; //string _mi21_50 = "[[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[0.0,1.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0],[1.0,0.0]]"; string strFilePath = @"D:\AI\mi10.jpg"; Bitmap bmp = new Bitmap(strFilePath); LMatrix lable = JsonConvert.DeserializeObject <double[, ]>(_mi10_50_2); #region 标记样本 //setlable from = new setlable(); //from.ShowBitmap(strFilePath, lable: lable.Matrix); //from.ShowDialog(); //LMatrix newLabel = from.GetBitmapLable(bmp.Height / 50, bmp.Width / 50); //string res2 = JsonConvert.SerializeObject(newLabel.Matrix); //return; #endregion Stopwatch sw = new Stopwatch(); sw.Start(); //InitData(); //获取训练的图片集合 Bitmap[] maps = bmp.Split(50, 50); //获取训练图片的大小 int mapsLen = maps.Length; MapInput = new double[mapsLen, 3, maps[0].Height, maps[0].Width]; PercentTitle = "图片压缩中"; for (int index = 0; index < mapsLen; index++) { //保存的是索引index的训练图片的rgb图片 MapInput.SetDimVal(maps[index].ConverBitmap(true), index); Percent = index / mapsLen; } PercentTitle = "初始化权重中"; //获取训练时每层的权重和偏置 InitWeight(3, new int[] { 26, 52, 38 }, new int[] { 3, 26, 52 }); LOptimizer optimizer = new LAdam(0.1); double accuracy = 0; ThreeLayerNet net = null; bool isRead = false; string path = @"d:\wen\good\"; for (int i = 310; i < 50000; i++) { Debug.Write("开始时间" + DateTime.Now.ToString("HH:mm:ss\n")); var inputFeature = neuralnetworkforward(); //LMatrix sss = inputFeature; //var _backdout2 = sss.ResetSize(38, CurRow, CurColumn); //neuralnetworkbackward(_backdout2); //Debug.Write("结束时间" + DateTime.Now.ToString("HH:mm:ss")); //return; if (net == null) { net = new ThreeLayerNet(inputFeature.GetLength(1), 200, 2, isRead: isRead); } net.Input = inputFeature; #region 测试性能 var res = net.Predict(); setlable from = new setlable(); from.ShowBitmap(strFilePath, 50, res: res); from.ShowDialog(); break; #endregion double loss = net.forward(lable); Debug.Write(string.Format("{0}:{1}当前误差:{2}\n", DateTime.Now.ToString("HH:mm:ss"), i, loss)); accuracy = net.Accuracy(lable); Debug.Write(string.Format("{0}:{1}当前识别精度:{2}\n", DateTime.Now.ToString("HH:mm:ss"), i, accuracy)); LMatrix dout = net.backward(lable); List <LMatrix> grads = net.Gradient(); List <LMatrix> param = net.GetParams(); string strWB = JsonConvert.SerializeObject(param.Select(x => x.Matrix)); if (!Directory.Exists(path + i.ToString())) { Directory.CreateDirectory(path + i.ToString()); } File.WriteAllText(path + i.ToString() + @"\wb_" + accuracy.ToString() + "_.txt", strWB); var _backdout = dout.ResetSize(38, CurRow, CurColumn); neuralnetworkbackward(_backdout); #region 卷积梯度更新 int infolen = LayerInfo.Length; for (int idx = 0; idx < infolen; idx++) { LayerInfo[idx].UpdateGradient(param, grads); } #endregion optimizer.Update(param, grads); //isRead = true; //记录每次的卷积值信息 File.WriteAllText(path + i.ToString() + @"\LayerInfo.txt", JsonConvert.SerializeObject(LayerInfo)); File.WriteAllText(path + i.ToString() + @"\ConvInfo.txt", JsonConvert.SerializeObject(ConvLayer)); } //sw.Stop(); //MessageBox.Show(sw.ElapsedMilliseconds.ToString()); }
public void StartTrain(int epoch) { Sources = new CnnSource(epoch); Sources.CollectImgInfo(@"D:\AI\test_img", @"D:\AI\test_img2"); Tuple <double[, , , ], double[, ]> Train = Sources.GetNextEpoch(); MapInput = Train.Item1; LMatrix lable = Train.Item2; #region 初始化隐藏层基本属性和隐藏层权重值 var layers = new LayerAttribute[] { new LayerAttribute { Count = 26, Depth = 3 }, new LayerAttribute { Count = 52, Depth = 26 }, new LayerAttribute { Count = 52, Depth = 38 } }; LayerInfo = new LayerParamsInfo[layers.Length]; for (int index = 0; index < layers.Length; index++) { LayerInfo[index] = new LayerParamsInfo(); LayerInfo[index].CurrentLayer = index; LayerInfo[index].InitWeightBias(layers[index].Count, layers[index].Depth); } #endregion #region 准备训练对象 LOptimizer optimizer = new LAdam(0.1); ThreeLayerNet net = null; bool isRead = false; string path = @"d:\wen\good\"; for (int i = 0; i < 50000; i++) { Debug.Write("开始时间" + DateTime.Now.ToString("HH:mm:ss\n")); var inputFeature = neuralnetworkforward(); if (net == null) { net = new ThreeLayerNet(inputFeature.GetLength(1), 200, 2, isRead: isRead); } net.Input = inputFeature; double loss = net.forward(lable); Debug.Write(string.Format("{0}:{1}当前误差:{2}\n", DateTime.Now.ToString("HH:mm:ss"), i, loss)); double accuracy = net.Accuracy(lable); Debug.Write(string.Format("{0}:{1}当前识别精度:{2}\n", DateTime.Now.ToString("HH:mm:ss"), i, accuracy)); LMatrix dout = net.backward(lable); List <LMatrix> grads = net.Gradient(); List <LMatrix> param = net.GetParams(); string strWB = JsonConvert.SerializeObject(param.Select(x => x.Matrix)); if (!Directory.Exists(path + i.ToString())) { Directory.CreateDirectory(path + i.ToString()); } File.WriteAllText(path + i.ToString() + @"\wb_" + accuracy.ToString() + "_.txt", strWB); var _backdout = dout.ResetSize(38, CurRow, CurColumn); neuralnetworkbackward(_backdout); #region 卷积梯度更新 int infolen = LayerInfo.Length; for (int idx = 0; idx < infolen; idx++) { LayerInfo[idx].UpdateGradient(param, grads); } #endregion optimizer.Update(param, grads); //isRead = true; //记录每次的卷积值信息 File.WriteAllText(path + i.ToString() + @"\LayerInfo.txt", JsonConvert.SerializeObject(LayerInfo)); File.WriteAllText(path + i.ToString() + @"\ConvInfo.txt", JsonConvert.SerializeObject(ConvLayer)); } #endregion }