private void button5_Click(object sender, EventArgs e) { //Test loading a MNIST image, distorting it, blurring it, converting it back to a DigitImage and then saving it to disk ImageProcessing.DigitImageCollection sourceImages = ImageProcessing.DigitImageCollection.LoadMnistDataSet("TrainingImages.dat", "TrainingLabels.dat", 60000); string imageFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ConvertedImages"); if (Directory.Exists(imageFolder)) { Directory.Delete(imageFolder, true); } Directory.CreateDirectory(imageFolder); DigitImage originalDigitImage = sourceImages.DigitImages[0]; originalDigitImage.SaveToFile(Path.Combine(imageFolder, "Original.jpg")); Bitmap distortedImage = sourceImages.DigitImages[0].ToBitmap().DistortionBlurFilter(25); DigitImage distortedDigitImage = new DigitImage(distortedImage, sourceImages.DigitImages[0].Label); distortedDigitImage.SaveToFile(Path.Combine(imageFolder, "Distorted.jpg")); Bitmap blurredImage = sourceImages.DigitImages[0].ToBitmap().ImageBlurFilter(BlurringBitmapExtensions.BlurType.GaussianBlur5x5); DigitImage blurredDigitImage = new DigitImage(blurredImage, sourceImages.DigitImages[0].Label); blurredDigitImage.SaveToFile(Path.Combine(imageFolder, "Blurred.jpg")); }
public CallbackModelWrapper PopulateImages(CallbackModelWrapper model) { Init(model); var pixels = model.GenerateNewPixelMatrices(); int amount = 0; try { while (true) { for (int i = 0; i < 28; ++i) { for (int j = 0; j < 28; ++j) { byte b = brImages.ReadByte(); pixels[i][j] = b; } } byte lbl = brLabels.ReadByte(); var image = new DigitImage(pixels, lbl, ++amount); model.Images.Add(image); } } catch (Exception e) { //will get exception here //BinaryReader cannot read past end of file error Console.Write(e.ToString()); } brImages.Close(); brLabels.Close(); return(model); }
public void Next() { DigitImage img = Program.RandomTestingData; TrainingItem ti = MnistReader.ConvertSingleImage(img); Image realImg = img.ToBitMap(); input.Image = new Bitmap(realImg, input.Size); byte b = 0; for (int i = 0; i < 4; i++) { b += (byte)(ti.outputs[i] > 0.5 ? Math.Pow(2, i) : 0); } byte o = Program.GetOutput(ti.inputs); output.Text = (timerRunning ? "Test in progress : \n" : "") + "Found : " + o + "\nExpected : " + b; if (timerRunning) { loop++; if (o == b) { worked++; } if (loop >= 100) { timerRunning = false; timer.Stop(); output.Text = "Test finished\nSuccess : " + worked + " %"; } } }
private void SetMainText(DigitImage image) { pcbMain.Image = image.NumberImage; lblExpected.Text = image.Label.ToString(); lblActual.Text = network.GetNumber(image).ToString(); lblActual.ForeColor = (lblActual.Text == lblExpected.Text) ? Color.Green : Color.Red; }
private void button6_Click(object sender, EventArgs e) { //Load the original Mnist training images and create a new blurred data set from them ImageProcessing.DigitImageCollection sourceImages = ImageProcessing.DigitImageCollection.LoadMnistDataSet("TrainingImages.dat", "TrainingLabels.dat", 60000); DigitImageCollection blurredImages = new DigitImageCollection(); blurredImages.DigitImages = new DigitImage[sourceImages.DigitImages.Length]; Parallel.For(0, sourceImages.DigitImages.Length, i => { int imageIndex = i; DigitImage blurredDigitImage = new DigitImage(sourceImages.DigitImages[imageIndex].ToBitmap().ImageBlurFilter(BlurringBitmapExtensions.BlurType.GaussianBlur5x5), sourceImages.DigitImages[imageIndex].Label); blurredImages.DigitImages[imageIndex] = blurredDigitImage; }); string imageFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "BlurredTrainingDataset"); if (Directory.Exists(imageFolder)) { Directory.Delete(imageFolder, true); } Directory.CreateDirectory(imageFolder); blurredImages.SaveToFile(Path.Combine(imageFolder, "BlurredTrainingImages.dat"), Path.Combine(imageFolder, "BlurredTrainingLabels.dat")); MessageBox.Show("Done"); }
public void StartCycle(DigitImage digitImage) { currentLabel = digitImage.label; ForwardPropagation(digitImage); BackwardPropagation(digitImage); }
private void pictureBox1_Paint(object sender, PaintEventArgs e) { var mag = DigitImage.MakeBitmap(DigitImage.FromBitmap(new Bitmap(GetInputImage()), 1), 10); pictureBox2.Image = mag; pictureBox2.Invalidate(); }
private DigitImage addToListView(DigitImage image) { var guid = Guid.NewGuid().ToString(); imageList.Images.Add(guid, image.NumberImage); lvImages.Items.Add(image.Label.ToString(), imageList.Images.IndexOfKey(guid)); lvImages.Items[lvImages.Items.Count - 1].Tag = image; lvImages.Refresh(); return(image); }
public void SetImage(DigitImage image) { MainGrid.Children.Clear(); for (int x = 0; x < 28; x++) { for (int y = 0; y < 28; y++) { SetInputShade(y, x, image.GetValueAt(y, x)); } } }
public int DetectNumberInImage(Bitmap inputImage) { var di = DigitImage.FromBitmap(inputImage, 1); var response = network.Query(di.pixels.SelectMany(su => su.Select(DigitImage.ConvertGrayScaleByteToDouble)).ToArray()); var max = response.Max(x => x); var f = response.ToList().IndexOf(max); return(f); }
public static DigitImage[] LoadImage(string pixelFile, string labelFile) { int numImages = 60000; DigitImage[] digitImages = new DigitImage[numImages]; byte[][] pixels = new byte[28][]; for (int i = 0; i < pixels.Length; i++) { pixels[i] = new byte[28]; } FileStream ifsPixels = new FileStream(pixelFile, FileMode.Open); FileStream ifsLabels = new FileStream(labelFile, FileMode.Open); BinaryReader brImages = new BinaryReader(ifsPixels); BinaryReader brLabels = new BinaryReader(ifsLabels); int magic1 = brImages.ReadInt32(); // обратный порядок байтов magic1 = ReverseBytes(magic1); // преобразуем в формат Intel int imageCount = brImages.ReadInt32(); imageCount = ReverseBytes(imageCount); int numRows = brImages.ReadInt32(); numRows = ReverseBytes(numRows); int numCols = brImages.ReadInt32(); numCols = ReverseBytes(numCols); int magic2 = brLabels.ReadInt32(); magic2 = ReverseBytes(magic2); int numLabels = brLabels.ReadInt32(); numLabels = ReverseBytes(numLabels); for (int i = 0; i < numImages; i++) { for (int j = 0; j < 28; j++) { for (int k = 0; k < 28; k++) { byte b = brImages.ReadByte(); pixels[j][k] = b; } } byte lb = brLabels.ReadByte(); DigitImage digitImage = new DigitImage(28, 28, pixels, lb); digitImages[i] = digitImage; } ifsPixels.Close(); brImages.Close(); ifsLabels.Close(); brLabels.Close(); return(digitImages); }
public void PaintFromDatabase(DigitImage image) { screen.GiveCaller(this); for (int i = 0; i < 28; i++) { for (int j = 0; j < 28; j++) { float temp = image.pixels[i][j]; temp = (255 - temp) / 255f; screenData[i][j].GetComponent <SpriteRenderer>().color = new Color(temp, temp, temp); } } }
private void ReadMNISTDataset() { try { var directories = GetDirectories(); var ifsLabels = new FileStream(directories.LabelsFileDir, FileMode.Open); var ifsImages = new FileStream(directories.ImagesFileDir, FileMode.Open); ifsLabels.Seek(_labelPosition, SeekOrigin.Begin); ifsImages.Seek(_datasetPosition, SeekOrigin.Begin); var brLabels = new BinaryReader(ifsLabels); var brImages = new BinaryReader(ifsImages); int magic1 = brImages.ReadInt32(); int numImages = brImages.ReadInt32(); int numRows = brImages.ReadInt32(); int numCols = brImages.ReadInt32(); int magic2 = brLabels.ReadInt32(); int numLabels = brLabels.ReadInt32(); var pixels = new byte[28][]; for (int i = 0; i < pixels.Length; ++i) { pixels[i] = new byte[28]; } for (int i = 0; i < 28; ++i) { for (int j = 0; j < 28; ++j) { byte b = brImages.ReadByte(); pixels[i][j] = b; } } byte lbl = brLabels.ReadByte(); var dImage = new DigitImage(pixels, lbl); CorrespondingLabel = lbl; PixelArray = dImage.GetImageArray(); var formatEmptyLines = Regex.Replace(dImage.ToString(), @"^\s+$[\r\n]*", string.Empty, RegexOptions.Multiline); DatasetInformation += string.Format("{0}\n\r", formatEmptyLines); ifsImages.Close(); brImages.Close(); ifsLabels.Close(); brLabels.Close(); } catch (Exception ex) { _exceptionLogging.LogException(ex.ToString()); } }
public void ForwardPropagation(DigitImage digitImage) { for (int i = 0; i < firstLayerSize; i++) { for (int j = 0; j < pixelNum; j++) { tempValue += (double)(digitImage.arrayPixels[j] / 255f) * input_FirstLayerWeights[j][i]; } tempValue += firstLayerBias[i]; if (tempValue > gradientClippingMax) { tempValue = gradientClippingMax; } else if (tempValue < gradientClippingMin) { tempValue = gradientClippingMin; } firstLayerZ[i] = tempValue; firstLayerActivations[i] = Sigmoid(tempValue); tempValue = 0; } for (int i = 0; i < secondLayerSize; i++) { for (int j = 0; j < firstLayerSize; j++) { tempValue += firstLayerActivations[j] * firstLayer_SecondLayerWeights[j][i]; } tempValue += secondLayerBias[i]; secondLayerZ[i] = tempValue; secondLayerActivations[i] = Sigmoid(tempValue); tempValue = 0; } for (int i = 0; i < outputLayerSize; i++) { for (int j = 0; j < secondLayerSize; j++) { tempValue += secondLayerActivations[j] * secondLayer_OutputWeights[j][i]; } tempValue += outputLayerBias[i]; outputLayerZ[i] = tempValue; outputLayerActivations[i] = Sigmoid(tempValue); tempValue = 0; } SeeIfCorrect(); }
private async Task <DigitImage> ReadOneImageAsync(CallbackModelWrapper model, int imageSize, int offset_image, int offset_label, int orderInCollection) { int bufferSize = imageSize * imageSize; byte[] image_bytes = new byte[bufferSize]; byte[] label_bytes = new byte[1]; using (FileStream fsi = new FileStream(@"..\..\Files\t10k-images.idx3-ubyte", FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true)) using (FileStream fsl = new FileStream(@"..\..\Files\t10k-labels.idx1-ubyte", FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true)) { //lock (fsi) //{ // fsi.copy //} fsi.Seek(offset_image, SeekOrigin.Begin); fsl.Seek(offset_label, SeekOrigin.Begin); var tmp_label = fsl.Read(label_bytes, 0, 1); var tmp_image = await fsi.ReadAsync(image_bytes, 0, bufferSize); //var tmp_image = fsi.Read(image_bytes, 0, bufferSize); } var pixels = model.GenerateNewPixelMatrices(); for (int i = 0; i < imageSize; ++i) { for (int j = 0; j < imageSize; ++j) { pixels[i][j] = image_bytes[i * imageSize + j]; } } DigitImage image = new DigitImage(pixels, label_bytes[0], orderInCollection); return(image); }
public static Bitmap GetBitmap(DigitImage digitImage, int mag) { int width = digitImage.Width * mag; int height = digitImage.Height * mag; Bitmap bitmap = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(bitmap); for (int i = 0; i < digitImage.Height; i++) { for (int j = 0; j < digitImage.Width; j++) { int pixelColor = 255 - digitImage.Pixels[i][j]; System.Drawing.Color color = System.Drawing.Color.FromArgb(pixelColor, pixelColor, pixelColor); SolidBrush solidBrush = new SolidBrush(color); graphics.FillRectangle(solidBrush, j * mag, i * mag, mag, mag); } } return(bitmap); }
private static System.Drawing.Image GetImage(Layer layer) { byte[][] pixels = new byte[layer.NumOfNeurons][]; for (int i = 0; i < layer.NumOfNeurons; i++) { pixels[i] = new byte[layer.NumOfNeurons]; } for (int i = 0; i < layer.NumOfNeurons; i++) { for (int j = 0; j < layer.NumOfNeurons; j++) { pixels[i][j] = (byte)layer.Neurons.Find(x => x.MapX == i && x.MapY == j).Output; } } DigitImage digitImage = new DigitImage(layer.NumOfNeurons, layer.NumOfNeurons, pixels, 4); Bitmap bitmap = GetBitmap(digitImage, 1); System.Drawing.Image image = bitmap; return(image); }
public void LoadData(Action <int> progressUpdater) { if (File.Exists(jsonFilePath)) { var deserialized = JsonConvert.DeserializeObject <NeuralNetwork>(File.ReadAllText(jsonFilePath)); network = deserialized; progressUpdater.Invoke(100); return; } network = new NeuralNetwork(inputLength, 100, 10, 0.3); var database = MnistDataLoader.LoadData(pixelFile, labelFile); var toDoubles = database.Select(i => i.pixels.SelectMany(s => s) .Select( byt => DigitImage.ConvertGrayScaleByteToDouble(byt))).ToArray(); //Test2(toDoubles, database); var targets = database.Select(i => Convert.ToInt32(i.label)).ToArray(); var percent = 0; for (var index = 0; index < toDoubles.Length; index++) { var doubleArray = toDoubles[index]; var correctAnswer = targets[index]; var target = Enumerable.Range(0, 10).Select(x => 0.01).ToArray(); target[correctAnswer] = 0.99; percent = NotifyDataLoadingProgress(index, toDoubles.Length, percent, progressUpdater); network.Train(doubleArray.ToArray(), target); } var networkJson = JsonConvert.SerializeObject(network); File.WriteAllText(jsonFilePath, networkJson); }
private CallbackModelWrapper PopulateImages(CallbackModelWrapper model, int amount) { var pixels = model.GenerateNewPixelMatrices(); for (int di = 0; di < amount; ++di) { for (int i = 0; i < 28; ++i) { for (int j = 0; j < 28; ++j) { byte b = brImages.ReadByte(); pixels[i][j] = b; } } byte lbl = brLabels.ReadByte(); var image = new DigitImage(pixels, lbl, di + 1); model.Images.Add(image); } brImages.Close(); brLabels.Close(); return(model); }
public static DigitImage[] ReadTraining(string path) { try { FileStream ifsLabels = new FileStream(@path + "train-labels.idx1-ubyte", FileMode.Open); // test labels FileStream ifsImages = new FileStream(@path + "train-images.idx3-ubyte", FileMode.Open); // test images BinaryReader brLabels = new BinaryReader(ifsLabels); BinaryReader brImages = new BinaryReader(ifsImages); int magic1 = brImages.ReadInt32(); // discard int numImages = brImages.ReadInt32(); int numRows = brImages.ReadInt32(); int numCols = brImages.ReadInt32(); DigitImage[] TestImages = new DigitImage[60000]; int magic2 = brLabels.ReadInt32(); int numLabels = brLabels.ReadInt32(); byte[][] pixels = new byte[28][]; for (int i = 0; i < pixels.Length; ++i) { pixels[i] = new byte[28]; } // each test image for (int di = 0; di < 60000; ++di) { for (int i = 0; i < 28; ++i) { for (int j = 0; j < 28; ++j) { byte b = brImages.ReadByte(); pixels[i][j] = b; } } byte lbl = brLabels.ReadByte(); DigitImage dImage = new DigitImage(pixels, lbl); TestImages[di] = dImage; } // each image ifsImages.Close(); brImages.Close(); ifsLabels.Close(); brLabels.Close(); TestImages = TestImages.Take(50000).ToArray(); return(TestImages); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); return(null); } }
private async Task Populate_AddImageAsync(CallbackModelWrapper model, CancellationTokenSource cts) { var index = 0; lock (model) { index = model.index++; } int bufferSize = model.ImageSize * model.ImageSize; var offset_image = model.offset_origin_image + index * bufferSize; var offset_label = model.offset_origin_label + index; var orderInCollection = index + 1; byte[] image_bytes = new byte[bufferSize]; byte[] label_bytes = new byte[1]; // using (FileStream fsi = new FileStream(model.imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true)) using (FileStream fsl = new FileStream(model.labelPath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true)) { var tmp_label = 0; var tmp_image = 0; try { fsi.Seek(offset_image, SeekOrigin.Begin); fsl.Seek(offset_label, SeekOrigin.Begin); tmp_label = fsl.Read(label_bytes, 0, 1); tmp_image = await fsi.ReadAsync(image_bytes, 0, bufferSize, cts.Token); //var tmp_image = fsi.Read(image_bytes, 0, bufferSize); } catch (TaskCanceledException e) { throw e; } //throw exception here, no byte can be read if (tmp_label == 0 || tmp_image == 0) { cts.Cancel(); return; //cancellation token only yields exception for later threads //to prevent the current thread continue, either throw an exception or return //the behavior observed is that the taskcancellation exception can't be caught if //I throw an exception here, so just do a return //throw new Exception("reach the end of stream, use token to cancel all tasks"); } } //var t = System.Threading.Thread.CurrentThread.ManagedThreadId; var pixels = model.GenerateNewPixelMatrices(); for (int i = 0; i < model.ImageSize; ++i) { for (int j = 0; j < model.ImageSize; ++j) { pixels[i][j] = image_bytes[i * model.ImageSize + j]; } } DigitImage image = new DigitImage(pixels, label_bytes[0], orderInCollection); model.Images.Add(image); }
/// <summary> /// Return number of calculated number /// </summary> /// <param name="image">Input image</param> /// <returns>Calculated number</returns> public byte GetNumber(DigitImage image) { var data = Calculate(image.ToDoubleArray()).ToList(); return((byte)data.IndexOf(data.Max())); }
public static List <DigitImage> ReadMNIST(bool Visualize) { var trainingData = new List <DigitImage>(); try { Console.Write("\nLoading MNIST..."); FileStream ifsLabels = new FileStream(@"MNIST/train-labels.idx1-ubyte", FileMode.Open); // test labels FileStream ifsImages = new FileStream(@"MNIST/train-images.idx3-ubyte", FileMode.Open); // test images BinaryReader brLabels = new BinaryReader(ifsLabels); BinaryReader brImages = new BinaryReader(ifsImages); int magic1 = brImages.ReadInt32(); // discard int numImages = brImages.ReadInt32(); int numRows = brImages.ReadInt32(); int numCols = brImages.ReadInt32(); int magic2 = brLabels.ReadInt32(); int numLabels = brLabels.ReadInt32(); byte[][] pixels = new byte[28][]; for (int i = 0; i < pixels.Length; ++i) { pixels[i] = new byte[28]; } // each test image for (int di = 0; di < 60000; ++di) { if (di % 1000 == 0) { Console.Write("."); } for (int i = 0; i < 28; ++i) { for (int j = 0; j < 28; ++j) { byte b = brImages.ReadByte(); pixels[i][j] = b; } } byte lbl = brLabels.ReadByte(); DigitImage dImage = new DigitImage(pixels, lbl); if (Visualize) { Console.WriteLine(dImage.ToString()); } trainingData.Add(dImage); // Console.ReadLine(); } // each image ifsImages.Close(); brImages.Close(); ifsLabels.Close(); brLabels.Close(); Console.WriteLine("\nLoaded " + trainingData.Count + " Elements"); Console.WriteLine("\nCompleted\n"); return(trainingData); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); return(null); } } // Main
public static void Run(int durchlaufe, int ausgebenPro) { double Fehler = 0; SimpleNet Chantall = new SimpleNet(0.1, 784, 30, 20, 10); Chantall.SetAfNames(AF.Sigmoid, "n", "n", "n"); Chantall.SetAfT(1, 1); Chantall.SetAfT(2, 1); Chantall.SetAfT(3, 1); FileStream ifsLabels = new FileStream(@"C:\Users\Gregor\DATENBSNKEBN\train-labels.idx1-ubyte", FileMode.Open); // test labels FileStream ifsImages = new FileStream(@"C:\Users\Gregor\DATENBSNKEBN\train-images.idx3-ubyte", FileMode.Open); // test images BinaryReader brLabels = new BinaryReader(ifsLabels); BinaryReader brImages = new BinaryReader(ifsImages); int magic1 = brImages.ReadInt32(); // discard int numImages = brImages.ReadInt32(); int numRows = brImages.ReadInt32(); int numCols = brImages.ReadInt32(); int magic2 = brLabels.ReadInt32(); int numLabels = brLabels.ReadInt32(); byte[][] pixels = new byte[28][]; for (int i = 0; i < pixels.Length; ++i) { pixels[i] = new byte[28]; } for (int i = 1; i < durchlaufe; ++i) { if (i % 60000 == 0) { ifsImages.Close(); brImages.Close(); ifsLabels.Close(); brLabels.Close(); ifsLabels = new FileStream(@"C:\Users\Gregor\memBrain\train-labels.idx1-ubyte", FileMode.Open); // test labels ifsImages = new FileStream(@"C:\Users\Gregor\memBrain\train-images.idx3-ubyte", FileMode.Open); // test images brLabels = new BinaryReader(ifsLabels); brImages = new BinaryReader(ifsImages); magic1 = brImages.ReadInt32(); // discard numImages = brImages.ReadInt32(); numRows = brImages.ReadInt32(); numCols = brImages.ReadInt32(); magic2 = brLabels.ReadInt32(); numLabels = brLabels.ReadInt32(); pixels = new byte[28][]; for (int j = 0; j < pixels.Length; ++j) { pixels[j] = new byte[28]; } } for (int i1 = 0; i1 < 28; ++i1) { for (int j = 0; j < 28; ++j) { byte b = brImages.ReadByte(); pixels[i1][j] = b; } } byte lbl = brLabels.ReadByte(); double[] erwrteterOutput = new double[10]; erwrteterOutput[lbl] = 1; Chantall.SetInput(ConvertD(pixels)); Chantall.OutputBerechnen(); Chantall.DeltawertBerechen(erwrteterOutput); Chantall.Backpropagation(); Fehler += Chantall.GetFehlerDif(erwrteterOutput); if (i % ausgebenPro == 0) { Fehler /= ausgebenPro; DigitImage dImage = new DigitImage(pixels, lbl); Console.WriteLine(dImage.ToString()); ZweiArrayAusgeben(erwrteterOutput, Chantall.GetOutput()); Console.WriteLine("Treffer Quote: {0}", 100 - (Fehler * 10)); Console.WriteLine(Chantall.ToString()); Chantall.Etha = Fehler; Fehler = 0; } } }
public void BackwardPropagation(DigitImage digitImage) { for (int i = 0; i < outputLayerSize; i++) { if (i == digitImage.label) { expectedValues[i] = 1; } else { expectedValues[i] = 0; } } for (int i = 0; i < outputLayerSize; i++) { outputLayerBiasCosts[i] = SigmoidDerivative(outputLayerZ[i]) * 2 * (outputLayerActivations[i] - expectedValues[i]); for (int j = 0; j < secondLayerSize; j++) { secondLayer_OutputWeightCosts[j][i] = outputLayerBiasCosts[i] * secondLayerActivations[j]; } } for (int j = 0; j < secondLayerSize; j++) { for (int i = 0; i < outputLayerSize; i++) { secondLayerActivationCosts[j] += secondLayer_OutputWeights[j][i] * outputLayerBiasCosts[i]; } } //L for (int i = 0; i < secondLayerSize; i++) { secondLayerBiasCosts[i] = SigmoidDerivative(secondLayerZ[i]) * secondLayerActivationCosts[i]; for (int j = 0; j < firstLayerSize; j++) { firstLayer_SecondLayerWeightCosts[j][i] = secondLayerBiasCosts[i] * firstLayerActivations[j]; } } for (int j = 0; j < firstLayerSize; j++) { for (int i = 0; i < secondLayerSize; i++) { firstLayerActivationCosts[j] += firstLayer_SecondLayerWeights[j][i] * secondLayerBiasCosts[i]; } } //L-1 for (int i = 0; i < firstLayerSize; i++) { firstLayerBiasCosts[i] = SigmoidDerivative(firstLayerZ[i]) * firstLayerActivationCosts[i]; for (int j = 0; j < pixelNum; j++) { input_FirstLayerWeightCosts[j][i] = firstLayerBiasCosts[i] * digitImage.arrayPixels[j]; } } //L-2 AddUpToMiniBatch(); miniBatchCounter++; if (miniBatchCounter >= miniBatchSize) { ApplyCostFunction(); miniBatchCounter = 0; } ResetData();//reset activation costs, as they are added up in iterations }
public static void Run(int durchlaufe, int ausgebenPro) { double Fehler = 0; NeuronalNet Chantall = new NeuronalNet(0, 784, 20, 20, 10); Chantall.StatischeInitialisierung(true); Chantall.Etha = 4; FileStream ifsLabels = new FileStream(@"C:\Users\Gregor\memBrain\train-labels.idx1-ubyte", FileMode.Open); // test labels FileStream ifsImages = new FileStream(@"C:\Users\Gregor\memBrain\train-images.idx3-ubyte", FileMode.Open); // test images BinaryReader brLabels = new BinaryReader(ifsLabels); BinaryReader brImages = new BinaryReader(ifsImages); int magic1 = brImages.ReadInt32(); // discard int numImages = brImages.ReadInt32(); int numRows = brImages.ReadInt32(); int numCols = brImages.ReadInt32(); int magic2 = brLabels.ReadInt32(); int numLabels = brLabels.ReadInt32(); byte[][] pixels = new byte[28][]; for (int i = 0; i < pixels.Length; ++i) { pixels[i] = new byte[28]; } for (int i = 1; i < durchlaufe; ++i) { if (i % 60000 == 0) { ifsImages.Close(); brImages.Close(); ifsLabels.Close(); brLabels.Close(); ifsLabels = new FileStream(@"C:\Users\Gregor\memBrain\train-labels.idx1-ubyte", FileMode.Open); // test labels ifsImages = new FileStream(@"C:\Users\Gregor\memBrain\train-images.idx3-ubyte", FileMode.Open); // test images brLabels = new BinaryReader(ifsLabels); brImages = new BinaryReader(ifsImages); magic1 = brImages.ReadInt32(); // discard numImages = brImages.ReadInt32(); numRows = brImages.ReadInt32(); numCols = brImages.ReadInt32(); magic2 = brLabels.ReadInt32(); numLabels = brLabels.ReadInt32(); pixels = new byte[28][]; for (int j = 0; j < pixels.Length; ++j) { pixels[j] = new byte[28]; } } for (int i1 = 0; i1 < 28; ++i1) { for (int j = 0; j < 28; ++j) { byte b = brImages.ReadByte(); pixels[i1][j] = b; } } byte lbl = brLabels.ReadByte(); double[] erwrteterOutput = new double[10]; erwrteterOutput[lbl] = 1; Chantall.InputGeben(ConvertD(pixels)); Chantall.AllesBerechnen(); Chantall.DeepLearning(erwrteterOutput); Fehler += Chantall.GetFehlerDif(erwrteterOutput); if (i % ausgebenPro == 0) { Fehler /= ausgebenPro; DigitImage dImage = new DigitImage(pixels, lbl); Console.WriteLine(dImage.ToString()); ZweiArrayAusgeben(erwrteterOutput, Chantall.GetOutput()); Console.WriteLine("Treffer Quote: {0}", 100 - (Fehler * 10)); Console.WriteLine(Chantall.ToString()); Chantall.Etha = Fehler * 2; Fehler = 0; } } }