public void GetNegativeNumbersUnderDiagnol_1() { var matrix = new List <List <int> >(); var l1 = new List <int> { 1, 2, 3, 4 }; var l2 = new List <int> { 3, 5, 5, 6 }; var l3 = new List <int> { -1, -2, 3, 7 }; var l4 = new List <int> { -1, -2, -10, 7 }; matrix.Add(l1); matrix.Add(l2); matrix.Add(l3); matrix.Add(l4); var matrixProcessor = new MatrixProcessor(matrix); var negativeNums = matrixProcessor.GetNegativeNumbersUnderDiagonal(); Assert.Equal(new List <int> { -10, -2 }, negativeNums); }
public void FlipMatrixTest() { var initial = new double[2, 3, 3] { { { 11, 12, 13 }, { 21, 22, 23 }, { 31, 32, 33 } }, { { 11, 12, 13 }, { 21, 22, 23 }, { 31, 32, 33 } }, }; var expected = new double[2, 3, 3] { { { 33, 32, 31 }, { 23, 22, 21 }, { 13, 12, 11 } }, { { 33, 32, 31 }, { 23, 22, 21 }, { 13, 12, 11 } }, }; var actual = MatrixProcessor.Flip(initial); Helper.CompareArrays(expected, actual); }
public override Value PassForward(Value value) { var res = MatrixProcessor.MaxPool(value.Multi, _kernelSize); _maxValues.ForEach((k, i, j) => _maxValues[k, i, j] = res.Item2[k, i, j]); return(new MultiValue(res.Item1)); }
public void MaxPoolingTest() { var input = MatrixProcessor.MaxPool(new double[5, 5] { { 5, 2, 1, 9, 8 }, { 3, 1, 2, 3, 5 }, { 0, 6, 2, 8, 1 }, { 3, 4, 9, 2, 1 }, { 6, 3, 1, 2, 2 } }, 2); var actual = MatrixProcessor.ReverseMaxPool(new double[3, 3] { { 3, 3, 3 }, { 3, 3, 3 }, { 3, 3, 3 } }, 2, 5, input.MaxCoordinates); var expected = new double[5, 5] { { 3, 0, 0, 3, 3 }, { 0, 0, 0, 0, 0 }, { 0, 3, 0, 0, 3 }, { 0, 0, 3, 0, 0 }, { 3, 0, 0, 3, 3 } }; for (int i = 0; i < actual.GetLength(0); i++) { for (int j = 0; j < actual.GetLength(1); j++) { Assert.AreEqual(actual[i, j], expected[i, j]); } } }
public static bool Validate(List <string> expression, bool debugMode) { foreach (var t in expression.Where(t => t.Length != 1)) { if (debugMode) { Console.Write(" | "); } if (debugMode) { Console.Write(t); } var invalidMatrixFound = false; var processedMatrix = MatrixProcessor.ProcessMatrix(t); for (var j = 1; j < processedMatrix.Count && !invalidMatrixFound; j++) { if (processedMatrix[j].Count != processedMatrix[j - 1].Count) { invalidMatrixFound = true; } } if (!invalidMatrixFound) { TextProc.WriteColor($" | (matrix definition) OK\n", ConsoleColor.Green); continue; } TextProc.WriteColor($" <-- not allowed matrix definition\n", ConsoleColor.Red); return(false); } return(true); }
/// <summary> /// Transforms a point by multiplying it with this matrix. /// </summary> /// <param name="x">X component of the point.</param> /// <param name="y">Y component of the point.</param> /// <returns>A Point2D describing the transformed input.</returns> public Point2D TransformVector(Double x, Double y) { DoubleComponent[] transferPoints = new DoubleComponent[] { x, y, 1 }; MatrixProcessor.Multiply(transferPoints, this); return(new Point2D((Double)transferPoints[0], (Double)transferPoints[1])); }
public void ConvolutionTest() { var input = new double[2][, ] { new double[5, 5] { { 1, 1, 1, 0, 0 }, { 0, 1, 1, 1, 0 }, { 0, 0, 1, 1, 1 }, { 0, 0, 1, 1, 0 }, { 0, 1, 1, 0, 0 } }, new double[5, 5] { { 1, 1, 1, 0, 0 }, { 0, 1, 1, 1, 0 }, { 0, 0, 1, 1, 1 }, { 0, 0, 1, 1, 0 }, { 0, 1, 1, 0, 0 } }, }; var kernels = new double[2][, ] { new double[3, 3] { { 1, 0, 1 }, { 0, 1, 0 }, { 1, 0, 1 } }, new double[3, 3] { { 1, 0, 1 }, { 0, 1, 0 }, { 1, 0, 1 } }, }; var expected = new double[][, ] { new double[3, 3] { { 4, 3, 4 }, { 2, 4, 3 }, { 2, 3, 4 } }, new double[3, 3] { { 4, 3, 4 }, { 2, 4, 3 }, { 2, 3, 4 } }, }; var actual = MatrixProcessor.Convolute(input, kernels); Helper.CompareArrays(expected, actual); }
public void NewReverseMaxPoolingTest() { var input = new double[, , ] { { { 70, 96, 21 }, { 68, 70, 99 }, { 93, 90, 37 } }, { { 69, 83, 63 }, { 96, 64, 96 }, { 89, 91, 31 }, } }; var expected = new double[, , ] { { { 0, 0, 0, 0, 0 }, { 70, 0, 0, 96, 21 }, { 0, 0, 0, 70, 0 }, { 0, 68, 0, 0, 99 }, { 0, 93, 0, 90, 37 } }, { { 69, 0, 0, 83, 63 }, { 0, 0, 0, 0, 0 }, { 96, 0, 0, 0, 0 }, { 0, 0, 64, 0, 96 }, { 89, 0, 0, 91, 31 }, } }; var input2 = new bool[, , ] { { { false, false, false, false, false }, { true, false, false, true, true }, { false, false, false, true, false }, { false, true, false, false, true }, { false, true, false, true, true } }, { { true, false, false, true, true }, { false, false, false, false, false }, { true, false, false, false, false }, { false, false, true, false, true }, { true, false, false, true, true }, } }; var actual = MatrixProcessor.ReverseMaxPool(input, input2, 2); Helper.CompareArrays(expected, actual); }
public void NewMaxPoolingTest() { var input = new double[, , ] { { { 53, 29, 3, 55, 2 }, { 70, 14, 35, 96, 21 }, { 54, 64, 66, 70, 16 }, { 16, 68, 1, 6, 99 }, { 83, 93, 61, 90, 37 } }, { { 69, 11, 11, 83, 63 }, { 24, 54, 58, 61, 12 }, { 96, 7, 1, 36, 2 }, { 75, 21, 64, 49, 96 }, { 89, 51, 46, 91, 31 }, } }; var expected = new double[, , ] { { { 70, 96, 21 }, { 68, 70, 99 }, { 93, 90, 37 } }, { { 69, 83, 63 }, { 96, 64, 96 }, { 89, 91, 31 }, } }; var expected2 = new bool[, , ] { { { false, false, false, false, false }, { true, false, false, true, true }, { false, false, false, true, false }, { false, true, false, false, true }, { false, true, false, true, true } }, { { true, false, false, true, true }, { false, false, false, false, false }, { true, false, false, false, false }, { false, false, true, false, true }, { true, false, false, true, true }, } }; var actual = MatrixProcessor.MaxPool(input, 2); Helper.CompareArrays(expected, actual.Item1); Helper.CompareArrays(expected2, actual.Item2); }
public override Value PassForward(Value value) { for (int i = 0; i < _kernels.Count; i++) { MatrixProcessor .Convolute(value.Multi, _kernels[i].Weights) .ForEach((q, j, k) => _featureMaps[i, j, k] = q); } return(new MultiValue(_featureMaps)); }
private static string ProcessMatrixExpression(string expression, bool debugMode) { if (debugMode) { TextProc.WriteProcess("preprocessing"); } expression = ConstantsValidator.ValidateMatrixExpression(expression); if (expression.Length == 0) { return("error (length)"); } if (!CharValidator.Validate(CharValidator.ExpressionType.Matrix, expression, debugMode)) { return("error (chars)"); } if (!BracketsValidator.Validate(BracketsValidator.BracketsTypes.Round, expression, debugMode)) { return("error (brackets)"); } if (!BracketsValidator.Validate(BracketsValidator.BracketsTypes.Square, expression, debugMode)) { return("error (brackets)"); } if (!PointersValidator.Validate('.', expression, debugMode)) { return("error (pointers)"); } if (!SequenceValidator.ValidateMatrixExpression(expression, debugMode)) { return("error (sequence)"); } if (debugMode) { TextProc.WriteProcess("processing"); } var separatedExpression = MatrixProcessor.GetExpressionSeparated(expression, debugMode); if (!MatrixValidator.Validate(separatedExpression, debugMode)) { return("error (matrix)"); } if (debugMode) { TextProc.WriteProcess("calculating"); } return(MatrixProcessor.ResolveExpression(separatedExpression)); }
public void UnpadMatrixTest() { var initial = new double[2, 7, 7] { { { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 11, 12, 13, 0, 0 }, { 0, 0, 21, 22, 23, 0, 0 }, { 0, 0, 31, 32, 33, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 }, }, { { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 11, 12, 13, 0, 0 }, { 0, 0, 21, 22, 23, 0, 0 }, { 0, 0, 31, 32, 33, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0 }, }, }; var expected = new double[2, 3, 3] { { { 11, 12, 13 }, { 21, 22, 23 }, { 31, 32, 33 } }, { { 11, 12, 13 }, { 21, 22, 23 }, { 31, 32, 33 } }, }; var actual = MatrixProcessor.Unpad(initial, 2); Helper.CompareArrays(expected, actual); }
public void GetNegativeNumbersUnderDiagnol_2() { var matrix = new List <List <int> >(); var l1 = new List <int> { 1, 2 }; var l2 = new List <int> { 3, 5 }; matrix.Add(l1); matrix.Add(l2); var matrixProcessor = new MatrixProcessor(matrix); var negativeNums = matrixProcessor.GetNegativeNumbersUnderDiagonal(); Assert.Equal(new List <int> { }, negativeNums); }
public override Value PassBackward(Value value) { var output = new double[_inputeFm.Channels, _inputeFm.Size, _inputeFm.Size]; for (int i = 0; i < _numberOfKernels; i++) { var kernel = value.Multi.GetSlice(i); kernel = MatrixProcessor.Pad(kernel, _kernelSize - 1); for (int j = 0; j < _inputeFm.Channels; j++) { var weight = _kernels[i].Weights.GetSlice(j); weight = MatrixProcessor.Flip(weight); var conv = MatrixProcessor.Convolute(kernel, weight); conv.ForEach((q, ii, jj) => output[j, ii, jj] += q); } } return(new MultiValue(output)); }
public void GetNegativeNumbersUnderDiagnol_6() { var matrix = new List <List <int> >(); var l1 = new List <int> { 1, 2, 3, 4, 5, 9 }; var l2 = new List <int> { 3, 5, 5, 6, -3, -5 }; var l3 = new List <int> { -1, -2, 3, 7, 6, 10 }; var l4 = new List <int> { 6, -2, 10, 7, -3, -5 }; var l5 = new List <int> { 2, 2, -30, 7, 12, 83 }; var l6 = new List <int> { -1, -3, -40, 7, -100, -40 }; matrix.Add(l1); matrix.Add(l2); matrix.Add(l3); matrix.Add(l4); matrix.Add(l5); matrix.Add(l6); var matrixProcessor = new MatrixProcessor(matrix); var negativeNums = matrixProcessor.GetNegativeNumbersUnderDiagonal(); Assert.Equal(new List <int> { -100, -40, -40, -30, -5, -5, -3, -3 }, negativeNums); }
public void NewConvolutionTest() { var input = new double[, , ] { { { -95, -38, 47, -65, 75 }, { -81, 34, -10, -32, -80 }, { -68, -14, -92, 11, -64 }, { -59, -74, -36, 67, 17 }, { 95, 86, -24, 44, 94 }, }, { { -62, 78, -82, -17, 1 }, { 14, -78, 8, 56, 31 }, { -93, -76, 81, -98, -28 }, { 16, 60, 13, -94, 82 }, { 41, 30, 71, 2, 6 }, }, }; var k1 = new double[, , ] { { { 0, 1, 1 }, { -1, 0, 0 }, { -1, 1, -1 }, }, { { 0, 0, -1 }, { 1, 1, 1 }, { 0, -1, -1 }, }, }; var k2 = new double[, , ] { { { 0, -1, 1 }, { -1, -1, -1 }, { -1, 1, 0 }, }, { { 1, 1, 1 }, { -1, -1, 0 }, { -1, 1, 1 }, }, }; var k3 = new double[, , ] { { { -1, 0, 0 }, { 1, -1, -1 }, { 1, -1, -1 }, }, { { 0, 0, -1 }, { 1, 0, -1 }, { -1, -1, -1 }, }, }; var e1 = new double[, ] { { 257, -121, 407 }, { -56, -125, 2 }, { -125, -157, -22 } }; var e2 = new double[, ] { { 292, -74, -4 }, { 285, -49, 287 }, { -22, -87, -82 }, }; var e3 = new double[, ] { { 204, 157, 37 }, { -101, -85, -72 }, { -68, 124, -310 }, }; MatrixProcessor.Convolute(input, k1) .ForEach((q, i, j) => Assert.AreEqual(q, e1[i, j])); MatrixProcessor.Convolute(input, k2) .ForEach((q, i, j) => Assert.AreEqual(q, e2[i, j])); MatrixProcessor.Convolute(input, k3) .ForEach((q, i, j) => Assert.AreEqual(q, e3[i, j])); }
public override Value PassBackward(Value value) { var output = MatrixProcessor.ReverseMaxPool(value.Multi, _maxValues, _kernelSize); return(new MultiValue(output)); }
public void updateResult(string text, bool draw) { if (this.infoTxtBlk.Dispatcher.Thread != Thread.CurrentThread) { this.infoTxtBlk.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new UpdateResultCallback(this.updateResult), text, draw); } else { try { if (kinectList == null) { return; } this.infoTxtBlk.Text = "[Fused Kinect Result]" + Environment.NewLine + fusedKinectParameters.printKinectParameters(); //show synchy info this.infoTxtBlk.Text = this.infoTxtBlk.Text + "[Synchy Info] num = " + GUIComponents.synchyList.Count + Environment.NewLine; for (int i = 0; i < GUIComponents.synchyList.Count; ++i) { this.infoTxtBlk.Text = this.infoTxtBlk.Text + i + " : " + GUIComponents.synchyList[i].printSynchy() + Environment.NewLine; } MatrixProcessor mp = new MatrixProcessor(); server.fusedKinectParameter = fusedKinectParameters; if (!draw) { return; } //3D plot functions show_viewport3D.Children.Clear(); ClearViewport(); PerspectiveCamera cam = (PerspectiveCamera)show_viewport3D.Camera; show_viewport3D.Children.Add(new ModelVisual3D() { Content = new DirectionalLight(Colors.White, new Vector3D(cam.LookDirection.X, cam.LookDirection.Y, cam.LookDirection.Z)) }); show_viewport3D.Children.Add(new ModelVisual3D() { Content = new DirectionalLight(Colors.White, new Vector3D(-cam.LookDirection.X, -cam.LookDirection.Y, -cam.LookDirection.Z)) }); drawSynchy(); PerspectiveCamera c = (PerspectiveCamera)show_viewport3D.Camera.GetCurrentValueAsFrozen(); for (int i = 0; i < transfMatrix.Count; ++i) { drawFrameAxis(i); } if (fusedKinectParameters != null) { if (fusedKinectParameters.skeletonArray != null) { for (int j = 0; j < fusedKinectParameters.skeletonArray.Length; ++j) { if (fusedKinectParameters.skeletonArray[j] != null) { drawSkeleton(fusedKinectParameters.skeletonArray[j]); } } } } show_viewport3D.UpdateLayout(); } catch (Exception ex) { string ss = ex.ToString(); } } }
//get transformation matrix from kinect 0 void getMatrixFromAll(object sender, RoutedEventArgs e) { //check for points avaliable if (pointsToCalibrate == null) { MessageBox.Show("Avaliable points not enough. Please Get more points!!"); return; } if (pointsToCalibrate[0] == null) { MessageBox.Show("Avaliable points not enough. Please Get more points!!"); return; } //remove failed points for (int i = 0; i < pointsToCalibrate[0].Count; ++i) { bool removeThisPoint = false; for (int j = 0; j < pointsToCalibrate.Length; ++j) { if (pointsToCalibrate[j][i][2, 0] < 0.5 || pointsToCalibrate[j][i][2, 0] > 2.5) { removeThisPoint = true; } } if (removeThisPoint) { for (int j = 0; j < pointsToCalibrate.Length; ++j) { pointsToCalibrate[j].RemoveAt(i); } i--; } } //return if there are not enough avaliable points if (pointsToCalibrate[0].Count < 4) { int n = 4 - pointsToCalibrate[0].Count; MessageBox.Show("Avaliable points not enough. Please Get " + n.ToString() + " more points!!"); return; } List <DenseMatrix> transfMatrixList = new List <DenseMatrix>(); MatrixProcessor mp = new MatrixProcessor(); transfMatrixList.Add(DenseMatrix.CreateIdentity(4)); //transformation matrix for kinect 0 for (int i = 1; i < pointsToCalibrate.Length; ++i) { transfMatrixList.Add(mp.getTransformationMatrixFromPoint(pointsToCalibrate[0], pointsToCalibrate[i])); } //Compute error sum, when is the sum of errors between the corresponding points List <double> errorList = new List <double>(); for (int j = 0; j < pointsToCalibrate.Length; ++j) { double errorSum = 0; for (int i = 0; i < pointsToCalibrate[0].Count; ++i) { DenseMatrix dm0 = (DenseMatrix)pointsToCalibrate[0][i].Clone(); DenseMatrix dm1 = (DenseMatrix)pointsToCalibrate[j][i].Clone(); dm1 = transfMatrixList[j] * dm1; errorSum += Math.Sqrt((dm0[0, 0] - dm1[0, 0]) * (dm0[0, 0] - dm1[0, 0]) + (dm0[1, 0] - dm1[1, 0]) * (dm0[1, 0] - dm1[1, 0]) + (dm0[2, 0] - dm1[2, 0]) * (dm0[2, 0] - dm1[2, 0])); } errorList.Add(errorSum); } //print result string textToPrint = ""; for (int j = 0; j < pointsToCalibrate.Length; ++j) { textToPrint += "[KINECT " + j.ToString() + "] error sum = " + errorList[j] + Environment.NewLine + mp.printMatrix(transfMatrixList[j]); } MessageBox.Show("calibration result = " + Environment.NewLine + textToPrint); prevMatrices = System.IO.File.ReadAllText("matrices.txt"); TextWriter tw1 = new StreamWriter("matrices.txt"); for (int j = 0; j < pointsToCalibrate.Length; ++j) { for (int row = 0; row < 4; ++row) { for (int col = 0; col < 4; ++col) { tw1.Write(transfMatrixList[j][row, col].ToString() + " "); } } } tw1.Close(); GUIComponents.fc.fp.getTransformationMatrix(); }
//get transformation matrix from vector more then current matrice void addNewMatrix(object sender, RoutedEventArgs e) { if (pointsToCalibrate == null) { MessageBox.Show("Avaliable points not enough. Please Get more points!!"); return; } if (pointsToCalibrate[0] == null) { MessageBox.Show("Avaliable points not enough. Please Get more points!!"); return; } //get current matrices //Get transformation matrix from matrices.txt string text = System.IO.File.ReadAllText("matrices.txt"); string[] matricesData = text.Split(' '); for (int i = 0; i < matricesData.Length; i++) { matricesData[i].TrimEnd('\n'); } int step = 16; // a matrix int oldKinectNum = matricesData.Length / step; List <DenseMatrix> transfMatrix = new List <DenseMatrix>(); MatrixProcessor mp = new MatrixProcessor(); for (int i = 0; i < oldKinectNum; ++i) { DenseMatrix m = new DenseMatrix(4); for (int row = 0; row < 4; ++row) { for (int col = 0; col < 4; ++col) { m[row, col] = Convert.ToDouble(matricesData[step * i + row * 4 + col]); } } transfMatrix.Add(m); } MessageBox.Show("Existing matrix num = " + transfMatrix.Count); //check whether the point is enough for each kinect without matrix for (int kinectIndex = oldKinectNum; kinectIndex < pointsToCalibrate.Length; ++kinectIndex) { int pointNum = 0; for (int pointIndex = 0; pointIndex < pointsToCalibrate[kinectIndex].Count; ++pointIndex) { if (pointsToCalibrate[kinectIndex][pointIndex][2, 0] > 0.5) { bool hasKnownPoint = false; //one of the previous kinect has value for (int i = 0; i < oldKinectNum; ++i) { if (pointsToCalibrate[i][pointIndex][2, 0] > 0.5) { hasKnownPoint = true; break; } } if (hasKnownPoint) { pointNum++; } } } if (pointNum < 4) { MessageBox.Show("Get " + (4 - pointNum) + " more points for kinect " + kinectIndex); return; } } //using existing information to calibrate kinects without matrix yet string textToPrint = ""; for (int kinectIndex = oldKinectNum; kinectIndex < pointsToCalibrate.Length; ++kinectIndex) { //int pointNum = 0; List <DenseMatrix> pointsToCalibrate0 = new List <DenseMatrix>(); List <DenseMatrix> pointsToCalibrate1 = new List <DenseMatrix>(); for (int pointIndex = 0; pointIndex < pointsToCalibrate[kinectIndex].Count; ++pointIndex) { if (pointsToCalibrate[kinectIndex][pointIndex][2, 0] > 0.5) { // bool hasKnownPoint = false; //one of the existing kinects has value for (int i = 0; i < oldKinectNum; ++i) { if (pointsToCalibrate[i][pointIndex][2, 0] > 0.5) { pointsToCalibrate0.Add(transfMatrix[i] * pointsToCalibrate[i][pointIndex]); pointsToCalibrate1.Add(pointsToCalibrate[kinectIndex][pointIndex]); break; } } } } transfMatrix.Add(mp.getTransformationMatrixFromPoint(pointsToCalibrate0, pointsToCalibrate1)); double errorSum = 0; for (int i = 0; i < pointsToCalibrate0.Count; ++i) { DenseMatrix dm0 = (DenseMatrix)pointsToCalibrate0[i].Clone(); DenseMatrix dm1 = (DenseMatrix)pointsToCalibrate1[i].Clone(); dm1 = transfMatrix[kinectIndex] * dm1; errorSum += Math.Sqrt((dm0[0, 0] - dm1[0, 0]) * (dm0[0, 0] - dm1[0, 0]) + (dm0[1, 0] - dm1[1, 0]) * (dm0[1, 0] - dm1[1, 0]) + (dm0[2, 0] - dm1[2, 0]) * (dm0[2, 0] - dm1[2, 0])); } textToPrint += "[KINECT " + kinectIndex.ToString() + "] error sum = " + errorSum + Environment.NewLine + mp.printMatrix(transfMatrix[kinectIndex]); } MessageBox.Show("calibration result = " + Environment.NewLine + textToPrint); //store previous result prevMatrices = text; TextWriter tw1 = new StreamWriter("matrices.txt"); for (int j = 0; j < pointsToCalibrate.Length; ++j) { for (int row = 0; row < 4; ++row) { for (int col = 0; col < 4; ++col) { tw1.Write(transfMatrix[j][row, col].ToString() + " "); } } } tw1.Close(); GUIComponents.fc.fp.getTransformationMatrix(); }