public void CheckLayerDisposed(bool invokeDispose) { bool disposeCheck = false; void onDispose(object obj, EventArgs e) { disposeCheck = true; } void createLayerAndAttach() { new MatrixLayer <byte>(50, 50).Disposed += onDispose; } if (invokeDispose) { MatrixLayer <byte> layer = new MatrixLayer <byte>(50, 50); layer.Disposed += onDispose;; layer.Dispose(); } else { createLayerAndAttach(); GC.Collect(); GC.WaitForPendingFinalizers(); } Assert.True(disposeCheck); }
// vidi konstruktore za MLayer // public MatrixBackPropLayer(int inputSize, int outputSize, TransferFunction transferFunction) { // inputs = new double[inputSize]; // outputs = new double[outputSize]; // weights = new double[outputSize][inputSize]; // // this.transferFunction = transferFunction; // } public MatrixMlpLayer(Layer sourceLayer, MatrixLayer previousLayer, TransferFunction transferFunction) { this.sourceLayer = sourceLayer; this.previousLayer = previousLayer; if (!(previousLayer is MatrixInputLayer)) { ((MatrixMlpLayer)previousLayer).NextLayer = this; } this.transferFunction = transferFunction; this.neuronsCount = sourceLayer.NeuronsCount; // if (sourceLayer.getNeuronAt(neuronsCount-1) instanceof BiasNeuron) this.neuronsCount = this.neuronsCount -1; this.inputsCount = previousLayer.Outputs.Length; outputs = new double[neuronsCount]; // biases = new double[neuronsCount]; // deltaBiases = new double[neuronsCount]; inputs = new double[inputsCount]; netInput = new double[neuronsCount]; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: weights = new double[neuronsCount][inputsCount]; weights = RectangularArrays.ReturnRectangularDoubleArray(neuronsCount, inputsCount); //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: deltaWeights = new double[neuronsCount][inputsCount]; deltaWeights = RectangularArrays.ReturnRectangularDoubleArray(neuronsCount, inputsCount); errors = new double[neuronsCount]; copyNeuronsToMatrices(); }
public static void FillGrayBitmap(this Bitmap bitmap, IMatrixLayer layer) { BitmapData bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); IntPtr intPtr = bitmapData.Scan0; int stride = bitmapData.Stride; int bytes = Math.Abs(stride) * bitmap.Height; byte[] vs;// = new byte[bytes]; // Bitmap storage MatrixLayer <byte> byteLayer = layer.ToByteLayer(false); vs = byteLayer.GetStorage(false); if (bitmap.Width == stride) { System.Runtime.InteropServices.Marshal.Copy(vs, 0, intPtr, vs.Length); } else { int offsetArr = 0; int offsetPtr = 0; for (int i = 0; i < bitmap.Height; i++) { System.Runtime.InteropServices.Marshal.Copy(vs, offsetArr, (intPtr + offsetPtr), layer.Width); offsetArr += layer.Width; offsetPtr += stride; } } bitmap.UnlockBits(bitmapData); }
void EditReadOnly() { MatrixLayer <byte> layer = new MatrixLayer <byte>(50, 50); layer.MakeReadOnly(false); Assert.Throws <ImageLib.Image.MatrixReadOnlyException>(() => layer.SetPoint(1, 0, 0)); }
public static Mat <TElement> GetCVMat <TElement>(this MatrixLayer <TElement> layer) where TElement : unmanaged, IComparable <TElement> { Mat <TElement> mat = new Mat <TElement>(layer.Height, layer.Width); mat.SetArray(layer.GetStorage(false)); return(mat); }
public override IMatrixImage Invoke(IMatrixImage input) { MatrixLayer <byte> layer = new MatrixLayer <byte>(255, 255); layer.ForEachPixelsSet((x, y) => (byte)(x * 16 - x % 16)); return(new MatrixImage <byte>(new MatrixLayer <byte>[] { layer }, false)); }
void MatrixActivationPrime() { MatrixLayer ml = SimpleMatrixLayer(); var result = ml.ActivationPrime(BasicInput()); Assert.That(result[0], Is.InRange(0.24, 0.25)); Assert.That(result[1], Is.InRange(0.24, 0.25)); }
void MatrixActivation() { MatrixLayer ml = SimpleMatrixLayer(); var result = ml.Activation(BasicInput()); Assert.That(result[0], Is.InRange(0.54, 0.55)); Assert.That(result[1], Is.InRange(0.59, 0.60)); }
public MainForm() { InitializeComponent(); MatrixImage <byte> m1 = new MatrixImage <byte>(256, 256, 1); m1.Split(false)[0].ForEachPixelsSet((x, y) => (byte)y); imageHistoryView1.AddHistoryElement(new ImageHistory(m1, "m1")); MatrixImage <byte> m2 = new MatrixImage <byte>(256, 256, 1); m2.Split(false)[0].ForEachPixelsSet((x, y) => (byte)x); imageHistoryView1.AddHistoryElement(new ImageHistory(m2, "m2")); MatrixImage <byte> vs = new MatrixImage <byte>(256, 256, 4); vs.Split(false)[0].ForEachPixelsSet((x, y) => (byte)x); vs.Split(false)[1].ForEachPixelsSet((x, y) => (byte)y); //vs.Split(false)[2].ForEachPixelsSet((x, y) => (byte)(x + y); vs.Split(false)[2].ForEachPixelsSet((x, y) => (byte)((x + y) > 255 ? 255 - (x + y % 256) : x + y)); vs.Split(false)[3].ForEachPixelsSet((x, y) => (byte)(255 - (y / 2))); MatrixLayer <byte> layer = new MatrixLayer <byte>(32, 32); vs.Split(false)[3].Insert(layer, 128 - 16, 128 - 16); //vs.Split(false)[3].ForEachPixelsSet((x, y) => y == 127 || y == 128 ? (byte)1 : y > 128 ? (byte)(y * 2) : (byte)((255 - y) * 2)); imageHistoryView1.AddHistoryElement(new ImageHistory(vs, "vs")); //vs.Split(false)[3].ForEachPixels((x, y) => (byte)255); //vs.Split(false)[2].ForEachPixels((x, y) => (byte)y); BitmapHandler bitmapHandler = vs.CreateBitmap(); this.imageView1.BitmapHandler = bitmapHandler; imageHistoryView1.HistorySelected += (_, img) => { imageView1.BitmapHandler = img.CreateImage().CreateBitmap(); }; //Random r = new Random(); //new Thread(() => //{ // try // { // Thread.Sleep(2000); // while (true) // { // this.Invoke(new Action(() => // vs.Split(false)[0].SetPoint((byte)r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)))); // Thread.Sleep(5); // } // } // catch // { // } //}).Start(); }
void MatrixForward() { MatrixLayer ml = SimpleMatrixLayer(); var result = ml.Forward(BasicInput()); Assert.That(result[0], Is.InRange(0.67, 0.68)); Assert.That(result[1], Is.InRange(0.77, 0.78)); Assert.That(result[2], Is.InRange(0.59, 0.60)); }
void MatrixAggregation() { MatrixLayer ml = SimpleMatrixLayer(); var result = ml.Aggregation(BasicInput()); Assert.That(result[0], Is.InRange(0.71, 0.72)); Assert.That(result[1], Is.InRange(1.22, 1.23)); Assert.That(result[2], Is.InRange(0.36, 0.37)); }
private void createMatrixLayers() { matrixLayers = new MatrixLayer[sourceNetwork.LayersCount]; matrixLayers[0] = new MatrixInputLayer(sourceNetwork.getLayerAt(0).NeuronsCount); MatrixLayer prevLayer = matrixLayers[0]; for (int i = 1; i < sourceNetwork.LayersCount; i++) { Layer layer = sourceNetwork.getLayerAt(i); MatrixMlpLayer newBpLayer = new MatrixMlpLayer(layer, prevLayer, new Tanh()); matrixLayers[i] = newBpLayer; prevLayer = newBpLayer; } }
public static void FillGrayBitmap(this Bitmap bitmap, IMatrixLayer layer) { BitmapData bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); IntPtr intPtr = bitmapData.Scan0; int stride = bitmapData.Stride; int bytes = Math.Abs(stride) * bitmap.Height; byte[] vs;// = new byte[bytes]; // Bitmap storage MatrixLayer <byte> byteLayer = layer.ToByteLayer(false); vs = byteLayer.GetStorage(false); System.Runtime.InteropServices.Marshal.Copy(vs, 0, intPtr, bytes); bitmap.UnlockBits(bitmapData); }
void MatrixLayerCreation() { MatrixLayer ml = SimpleMatrixLayer(); Assert.That(ml.Weights, Is.Not.Null); Assert.That(ml.Biases, Is.Not.Null); for (var i = 0; i < ml.Weights.RowCount; i++) { for (var j = 0; j < ml.Weights.ColumnCount; j++) { Assert.That(ml.Weights[i, j], Is.InRange(0, 1)); } } for (var j = 0; j < ml.Biases.Count; j++) { Assert.That(ml.Biases[j], Is.InRange(0, 1)); } }
public static LineSeries CreateLineSeries(IMatrixLayer layer, OxyColor?color = null, string title = null) { LineSeries ls = new LineSeries(); if (color.HasValue) { ls.Color = color.Value; } if (title != null) { ls.Title = title; } MatrixLayer <byte> byteLayer = layer.ToByteLayer(false); int[] values = new int[256]; byteLayer.ForEachPixels(a => values[a]++); for (int i = 0; i < 256; i++) { ls.Points.Add(new DataPoint(i, values[i])); } return(ls); }
public static MatrixImage <byte> ToMatrixImage(this Bitmap bitmap) { BitmapData bitmapData; IntPtr intPtr; int stride; int bytes; byte[] vs; byte[][] storages; MatrixLayer <byte>[] layers; if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed) { bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); intPtr = bitmapData.Scan0; stride = Math.Abs(bitmapData.Stride); bytes = bitmap.Width * bitmap.Height; vs = new byte[bytes]; if (stride == bitmap.Width) { System.Runtime.InteropServices.Marshal.Copy(intPtr, vs, 0, bytes); } else { int offset = 0; for (int i = 0; i < bitmap.Height; i++) { System.Runtime.InteropServices.Marshal.Copy(intPtr, vs, offset, bitmap.Width); offset += bitmap.Width; intPtr += stride; } } bitmap.UnlockBits(bitmapData); layers = new MatrixLayer <byte>[] { new MatrixLayer <byte>(bitmap.Width, bitmap.Height, vs, false) }; return(new MatrixImage <byte>(layers)); } else if (bitmap.PixelFormat == PixelFormat.Format24bppRgb) { bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); intPtr = bitmapData.Scan0; stride = bitmapData.Stride; bytes = Math.Abs(stride) * bitmap.Height; vs = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(intPtr, vs, 0, bytes); bitmap.UnlockBits(bitmapData); storages = new byte[3][]; for (int i = 0; i < 3; i++) { storages[i] = new byte[bitmap.Width * bitmap.Height]; } int len = Math.Min(bitmap.Width * bitmap.Height * 3, vs.Length); for (int i = 0; i < bitmap.Height; i++) { int storageOffset = i * bitmap.Width; int vsOffset = i * stride; for (int j = 0; j < bitmap.Width; j++) { storages[0][storageOffset] = vs[vsOffset++]; storages[1][storageOffset] = vs[vsOffset++]; storages[2][storageOffset++] = vs[vsOffset++]; } } //for (int i = 0; i < len; i++) //{ // storages[i % 3][i / 3] = vs[i]; //} layers = new MatrixLayer <byte> [3]; for (int i = 0; i < 3; i++) { layers[i] = new MatrixLayer <byte>(bitmap.Width, bitmap.Height, storages[i], bitmap.Width * bitmap.Height != storages[i].Length); } return(new MatrixImage <byte>(layers)); } else if (bitmap.PixelFormat == PixelFormat.Format32bppPArgb) { bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb); intPtr = bitmapData.Scan0; stride = bitmapData.Stride; bytes = Math.Abs(stride) * bitmap.Height; vs = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(intPtr, vs, 0, bytes); bitmap.UnlockBits(bitmapData); storages = new byte[4][]; for (int i = 0; i < 4; i++) { storages[i] = new byte[bitmap.Width * bitmap.Height]; } int len = Math.Min(bitmap.Width * bitmap.Height * 4, vs.Length); for (int i = 0; i < len; i++) { storages[i % 4][i / 4] = vs[i]; } layers = new MatrixLayer <byte> [4]; for (int i = 0; i < 4; i++) { layers[i] = new MatrixLayer <byte>(bitmap.Width, bitmap.Height, storages[i], false); } return(new MatrixImage <byte>(layers)); } else if (bitmap.PixelFormat == PixelFormat.Format32bppArgb) { bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); intPtr = bitmapData.Scan0; stride = bitmapData.Stride; bytes = Math.Abs(stride) * bitmap.Height; vs = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(intPtr, vs, 0, bytes); bitmap.UnlockBits(bitmapData); storages = new byte[4][]; for (int i = 0; i < 4; i++) { storages[i] = new byte[vs.Length / 4]; } for (int i = 0; i < vs.Length; i++) { storages[i % 4][i / 4] = vs[i]; } layers = new MatrixLayer <byte> [4]; for (int i = 0; i < 4; i++) { layers[i] = new MatrixLayer <byte>(bitmap.Width, bitmap.Height, storages[i], false); } return(new MatrixImage <byte>(layers)); } else if (bitmap.PixelFormat == PixelFormat.Format32bppRgb) { bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb); intPtr = bitmapData.Scan0; stride = bitmapData.Stride; bytes = Math.Abs(stride) * bitmap.Height; vs = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(intPtr, vs, 0, bytes); bitmap.UnlockBits(bitmapData); storages = new byte[3][]; for (int i = 0; i < 3; i++) { storages[i] = new byte[bitmap.Width * bitmap.Height]; } for (int i = 0; i < vs.Length; i++) { if (i % 4 == 3) { continue; } storages[i % 4][i / 4] = vs[i]; } layers = new MatrixLayer <byte> [3]; for (int i = 0; i < 3; i++) { layers[i] = new MatrixLayer <byte>(bitmap.Width, bitmap.Height, storages[i], false); } return(new MatrixImage <byte>(layers)); } throw new NotSupportedException(); }
public static MatrixImage <byte> BitmapToMatrixImage(this Bitmap bitmap) { BitmapData bitmapData; IntPtr intPtr; int stride; int bytes; byte[] vs; byte[][] storages; MatrixLayer <byte>[] layers; if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed) { bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); intPtr = bitmapData.Scan0; stride = bitmapData.Stride; bytes = Math.Abs(stride) * bitmap.Height; vs = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(intPtr, vs, 0, bytes); bitmap.UnlockBits(bitmapData); layers = new MatrixLayer <byte>[] { new MatrixLayer <byte>(bitmap.Width, bitmap.Height, vs, false) }; return(new MatrixImage <byte>(layers)); } else if (bitmap.PixelFormat == PixelFormat.Format24bppRgb) { bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); intPtr = bitmapData.Scan0; stride = bitmapData.Stride; bytes = Math.Abs(stride) * bitmap.Height; vs = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(intPtr, vs, 0, bytes); bitmap.UnlockBits(bitmapData); storages = new byte[3][]; for (int i = 0; i < vs.Length; i++) { storages[i % 3][i / 3] = vs[i]; } layers = new MatrixLayer <byte> [3]; for (int i = 0; i < 3; i++) { layers[i] = new MatrixLayer <byte>(bitmap.Width, bitmap.Height, storages[i], false); } return(new MatrixImage <byte>(layers)); } else if (bitmap.PixelFormat == PixelFormat.Format32bppPArgb) { bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb); intPtr = bitmapData.Scan0; stride = bitmapData.Stride; bytes = Math.Abs(stride) * bitmap.Height; vs = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(intPtr, vs, 0, bytes); bitmap.UnlockBits(bitmapData); storages = new byte[4][]; for (int i = 0; i < vs.Length; i++) { storages[i % 4][i / 4] = vs[i]; } layers = new MatrixLayer <byte> [4]; for (int i = 0; i < 4; i++) { layers[i] = new MatrixLayer <byte>(bitmap.Width, bitmap.Height, storages[i], false); } return(new MatrixImage <byte>(layers)); } throw new NotSupportedException(); }
public static void SetCVMat <TElement>(this MatrixLayer <TElement> layer, Mat mat) where TElement : unmanaged, IComparable <TElement> { mat.GetArray <TElement>(out TElement[] data); layer.SetStorage(data, false); }