Example #1
0
        /// <summary>
        /// Finds the terminal region (=leaf node) for each sample in X.
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public MathNet.Numerics.LinearAlgebra.Generic.Vector <double> apply(
            MathNet.Numerics.LinearAlgebra.Generic.Matrix <double> x)
        {
            //cdef double* threshold = this.threshold

            uint nSamples = (uint)x.RowCount;

            MathNet.Numerics.LinearAlgebra.Generic.Vector <double> @out = DenseVector.Create((int)nSamples, i => 0.0);

            for (int i = 0; i < nSamples; i++)
            {
                uint nodeId = 0;

                // While node_id not a leaf
                while (ChildrenLeft[nodeId] != _TREE_LEAF)
                {
                    // ... and children_right[node_id] != _TREE_LEAF:
                    if (x[i, (int)Feature[nodeId]] <= Threshold[nodeId])
                    {
                        nodeId = ChildrenLeft[nodeId];
                    }
                    else
                    {
                        nodeId = ChildrenRight[nodeId];
                    }
                }

                @out[i] = nodeId;
            }

            return(@out);
        }
Example #2
0
        /// <summary>
        /// Predict target for X.
        /// </summary>
        public MathNet.Numerics.LinearAlgebra.Generic.Matrix <double>[] predict(
            MathNet.Numerics.LinearAlgebra.Generic.Matrix <double> x)
        {
            uint nSamples = (uint)x.RowCount;

            if (nOutputs == 1)
            {
                MathNet.Numerics.LinearAlgebra.Generic.Matrix <double> @out =
                    DenseMatrix.Create((int)nSamples, (int)maxNClasses, (i, j) => 0.0);

                for (int i = 0; i < nSamples; i++)
                {
                    uint nodeId = 0;

                    // While node_id not a leaf
                    while (ChildrenLeft[nodeId] != _TREE_LEAF)
                    {
                        // ... and children_right[node_id] != _TREE_LEAF:
                        if (x[i, (int)Feature[nodeId]] <= Threshold[nodeId])
                        {
                            nodeId = ChildrenLeft[nodeId];
                        }
                        else
                        {
                            nodeId = ChildrenRight[nodeId];
                        }
                    }

                    uint offset = nodeId * valueStride;

                    for (int c = 0; c < NClasses[0]; c++)
                    {
                        @out[i, c] = Value[offset + c];
                    }
                }

                return(new[] { @out });
            }
            else // n_outputs > 1
            {
                // out_multi = np.zeros((n_samples,
                //                      n_outputs,
                //                      max_n_classes), dtype=np.float64)
                // Note: I've changed order
                var outMulti = new MathNet.Numerics.LinearAlgebra.Generic.Matrix <double> [nOutputs];
                for (int i = 0; i < outMulti.Length; i++)
                {
                    outMulti[i] = DenseMatrix.Create((int)nSamples, (int)maxNClasses, (c, r) => 0.0);
                }

                for (int i = 0; i < nSamples; i++)
                {
                    uint nodeId = 0;

                    // While node_id not a leaf
                    while (ChildrenLeft[nodeId] != _TREE_LEAF)
                    {
                        // ... and children_right[node_id] != _TREE_LEAF:
                        if (x[i, (int)Feature[nodeId]] <= Threshold[nodeId])
                        {
                            nodeId = ChildrenLeft[nodeId];
                        }
                        else
                        {
                            nodeId = ChildrenRight[nodeId];
                        }
                    }

                    uint offset = nodeId * valueStride;

                    for (int k = 0; k < nOutputs; k++)
                    {
                        for (int c = 0; c < NClasses[k]; c++)
                        {
                            //out_multi[i, k, c] = value[offset + c];
                            //Note: I've changed order
                            outMulti[k][i, c] = Value[offset + c];
                        }
                        offset += maxNClasses;
                    }
                }

                return(outMulti);
            }
        }
Example #3
0
        /// <summary>
        /// Build a decision tree from the training set (X, y).
        /// </summary>
        public void build(MathNet.Numerics.LinearAlgebra.Generic.Matrix <double> x,
                          MathNet.Numerics.LinearAlgebra.Generic.Matrix <double> y,
                          MathNet.Numerics.LinearAlgebra.Generic.Vector <double> sampleWeight = null)
        {
            // Prepare data before recursive partitioning

            // Initial capacity
            int initCapacity;

            if (this.maxDepth <= 10)
            {
                initCapacity = (int)Math.Pow(2, (this.maxDepth + 1)) - 1;
            }
            else
            {
                initCapacity = 2047;
            }

            this.Resize(initCapacity);

            // Recursive partition (without actual recursion)
            SplitterBase splitter = this.splitter;

            splitter.init(x, y, sampleWeight == null ? null : sampleWeight.ToArray());

            uint stackNValues  = 5;
            uint stackCapacity = 50;

            uint[] stack = new uint[stackCapacity];


            stack[0] = 0;                  // start
            stack[1] = splitter.n_samples; // end
            stack[2] = 0;                  // depth
            stack[3] = _TREE_UNDEFINED;    // parent
            stack[4] = 0;                  // is_left

            uint   pos       = 0;
            uint   feature   = 0;
            double threshold = 0;
            double impurity  = 0;

            while (stackNValues > 0)
            {
                stackNValues -= 5;

                uint start  = stack[stackNValues];
                uint end    = stack[stackNValues + 1];
                uint depth  = stack[stackNValues + 2];
                uint parent = stack[stackNValues + 3];
                bool isLeft = stack[stackNValues + 4] != 0;

                uint nNodeSamples = end - start;
                bool isLeaf       = ((depth >= this.maxDepth) ||
                                     (nNodeSamples < this.minSamplesSplit) ||
                                     (nNodeSamples < 2 * this.minSamplesLeaf));

                splitter.node_reset(start, end, ref impurity);
                isLeaf = isLeaf || (impurity < 1e-7);

                if (!isLeaf)
                {
                    splitter.node_split(ref pos, ref feature, ref threshold);
                    isLeaf = isLeaf || (pos >= end);
                }

                uint nodeId = this.AddNode(parent, isLeft, isLeaf, feature,
                                           threshold, impurity, nNodeSamples);

                if (isLeaf)
                {
                    // Don't store value for internal nodes
                    splitter.node_value(this.Value, nodeId * this.valueStride);
                }
                else
                {
                    if (stackNValues + 10 > stackCapacity)
                    {
                        stackCapacity *= 2;
                        var newStack = new uint[stackCapacity];
                        Array.Copy(stack, newStack, stack.Length);
                        stack = newStack;
                    }

                    // Stack right child
                    stack[stackNValues]     = pos;
                    stack[stackNValues + 1] = end;
                    stack[stackNValues + 2] = depth + 1;
                    stack[stackNValues + 3] = nodeId;
                    stack[stackNValues + 4] = 0;
                    stackNValues           += 5;

                    // Stack left child
                    stack[stackNValues]     = start;
                    stack[stackNValues + 1] = pos;
                    stack[stackNValues + 2] = depth + 1;
                    stack[stackNValues + 3] = nodeId;
                    stack[stackNValues + 4] = 1;
                    stackNValues           += 5;
                }
            }

            this.Resize((int)this.NodeCount);
            this.splitter = null; // Release memory
        }
Example #4
0
 public static void writeMatrixToMatLabFile(MathNet.Numerics.LinearAlgebra.Generic.Matrix <double> matrix, string file, string name)
 {
     MathNet.Numerics.LinearAlgebra.IO.MatlabMatrixWriter wr = new MathNet.Numerics.LinearAlgebra.IO.MatlabMatrixWriter(file);
     wr.WriteMatrix(matrix, name);
     wr.Close();
 }
        static void Main(string[] args)
        {
            if (args.Length < 3 || !(File.Exists(Path.GetFullPath(args[0]))) || !(File.Exists(Path.GetFullPath(args[1]))))
            {
                Help();
                Environment.Exit(0);
            }

            Console.WriteLine("Running Pre Step.");

            Bitmap      srcBitmap = null;
            DenseMatrix srcMat    = new DenseMatrix(256, 256);

            try
            {
                srcBitmap = new Bitmap(Path.GetFullPath(args[0]));
            }
            catch (FileNotFoundException e)
            {
                Help();
                Environment.Exit(0);
            }

            Console.WriteLine("Running Step1.");

            for (int j = 0; j < 256; j++)
            {
                for (int i = 0; i < 256; i++)
                {
                    srcMat.At(j, i, srcBitmap.GetPixel(i, j).R);
                }
            }

            Console.WriteLine(" Load target image.");

            FDT data = FDT.DeSerializeObject(Path.GetFullPath(args[1]));

            Console.WriteLine(" Load data file.");

            DenseMatrix output = new DenseMatrix(256, 256);

            Console.WriteLine("Running Step2.");

            foreach (XYSO xyso in data.DataList)
            {
                MathNet.Numerics.LinearAlgebra.Generic.Matrix <double> highMat = srcMat.SubMatrix(xyso.highy * 4, 16, xyso.highx * 4, 16);
                DenseMatrix high8Mat = new DenseMatrix(8, 8);

                for (int j = 0; j < 8; j++)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        double z = xyso.s * 0.25 * (highMat[2 * j, 2 * i] + highMat[2 * j, 2 * i + 1] + highMat[2 * j + 1, 2 * i] + highMat[2 * j, 2 * i + 1]) + xyso.o;

                        high8Mat.At(j, i, z);
                    }
                }

                output.SetSubMatrix(xyso.lowy * 8, 8, xyso.lowx * 8, 8, high8Mat);
            }

            Bitmap outBitmap = new Bitmap(256, 256);

            for (int j = 0; j < 256; j++)
            {
                for (int i = 0; i < 256; i++)
                {
                    if (output[j, i] > 255)
                    {
                        outBitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));
                    }
                    else if (output[j, i] < 0)
                    {
                        outBitmap.SetPixel(i, j, Color.FromArgb(0, 0, 0));
                    }
                    else
                    {
                        outBitmap.SetPixel(i, j, Color.FromArgb(Convert.ToInt32(output[j, i]), Convert.ToInt32(output[j, i]), Convert.ToInt32(output[j, i])));
                    }
                }
            }

            outBitmap.Save(Path.GetFullPath(args[2]));

            Console.WriteLine("Done.");
        }
        static void Main(string[] args)
        {
            if (args.Length < 2 || !(File.Exists(Path.GetFullPath(args[0]))))
            {
                //System.Diagnostics.Trace.WriteLine("Args[0] target file => " + Path.GetFullPath(args[0]));
                Help();
                Environment.Exit(0);
            }

            Bitmap srcBitmap = null;

            try
            {
                srcBitmap = new Bitmap(Path.GetFullPath(args[0]));
            }
            catch (FileNotFoundException e)
            {
                Help();
                Environment.Exit(0);
            }

            //for (int x = 0; x < srcBitmap.Width; x++)
            //{
            //    for (int y = 0; y < srcBitmap.Height; y++)
            //    {
            //    }
            //}

            // Pre Step.
            Console.WriteLine("Running Pre Step.");
            DenseMatrix imgMat = new DenseMatrix(srcBitmap.Height, srcBitmap.Width);

            for (int j = 0; j < srcBitmap.Height; j++)
            {
                for (int i = 0; i < srcBitmap.Width; i++)
                {
                    imgMat.At(j, i, srcBitmap.GetPixel(i, j).R);
                }
            }

            // Step 1.
            Console.WriteLine("Running Step 1.");

            int lowBlkWidthCnt  = srcBitmap.Width / 8;
            int lowBlkHeightCnt = srcBitmap.Height / 8;

            List <DenseMatrix> lowBlkList  = new List <DenseMatrix>(lowBlkWidthCnt * lowBlkHeightCnt);
            List <double>      lowIntwList = new List <double>(lowBlkWidthCnt * lowBlkHeightCnt);

            for (int j = 0; j < lowBlkHeightCnt; j++)
            {
                for (int i = 0; i < lowBlkWidthCnt; i++)
                {
                    DenseMatrix mat  = null;
                    double      intw = 0;

                    try
                    {
                        MathNet.Numerics.LinearAlgebra.Generic.Matrix <double> tmpMatrix = imgMat.SubMatrix(j * 8, 8, i * 8, 8);

                        mat = new DenseMatrix(tmpMatrix.RowCount, tmpMatrix.ColumnCount);
                        mat.SetSubMatrix(0, 8, 0, 8, tmpMatrix);

                        IEnumerable <Tuple <int, int, double> > e = mat.IndexedEnumerator();

                        intw = e.Sum(element => element.Item3);
                    }
                    catch (ArgumentOutOfRangeException e)
                    {
                    }
                    catch (ArgumentException e)
                    {
                    }

                    lowBlkList.Add(mat);
                    lowIntwList.Add(intw);
                    Console.Write(".");
                }
            }
            Console.WriteLine();

            // Step 2.
            Console.WriteLine("Running Step 2.");

            int highBlkWidthCnt  = (srcBitmap.Width - 16) / 4;
            int highBlkHeightCnt = (srcBitmap.Height - 16) / 4;

            List <DenseMatrix> highBlkList    = new List <DenseMatrix>(highBlkWidthCnt * highBlkHeightCnt);
            List <double>      highggAList    = new List <double>(highBlkWidthCnt * highBlkHeightCnt);
            List <double>      highgAList     = new List <double>(highBlkWidthCnt * highBlkHeightCnt);
            List <DenseMatrix> highInvMatList = new List <DenseMatrix>(highBlkWidthCnt * highBlkHeightCnt);

            for (int j = 0; j < highBlkHeightCnt; j++)
            {
                for (int i = 0; i < highBlkWidthCnt; i++)
                {
                    DenseMatrix mat = new DenseMatrix(8, 8);

                    for (int l = 0; l < 8; l++)
                    {
                        for (int k = 0; k < 8; k++)
                        {
                            mat.At(l, k, 0.25 * (double)(srcBitmap.GetPixel((i * 4) + (k * 2), (j * 4) + (l * 2)).R + srcBitmap.GetPixel((i * 4) + (k * 2), (j * 4) + (l * 2 + 1)).R + srcBitmap.GetPixel((i * 4) + (k * 2 + 1), (j * 4) + (l * 2)).R + srcBitmap.GetPixel((i * 4) + (k * 2), (j * 4) + (l * 2 + 1)).R));
                        }
                    }

                    double intggA = mat.IndexedEnumerator().Sum(element => element.Item3 * element.Item3);
                    double intgA  = mat.IndexedEnumerator().Sum(element => element.Item3);

                    highBlkList.Add(mat);
                    highggAList.Add(intggA);
                    highgAList.Add(intgA);

                    DenseMatrix multMat = new DenseMatrix(2, 2);
                    multMat.At(0, 0, intggA);
                    multMat.At(0, 1, intgA);
                    multMat.At(1, 0, intgA);
                    multMat.At(1, 1, 64);

                    DenseMatrix invMat = new DenseMatrix(2, 2);
                    invMat.SetSubMatrix(0, 2, 0, 2, multMat.Inverse());

                    highInvMatList.Add(invMat);

                    Console.Write(".");
                }
            }
            Console.WriteLine();

            // Step 3.
            Console.WriteLine("Running Step 3.");

            FDT result = new FDT();

            for (int j = 0; j < lowBlkHeightCnt; j++)
            {
                for (int i = 0; i < lowBlkWidthCnt; i++)
                {
                    XYSO minxyso;
                    minxyso.lowx  = i;
                    minxyso.lowy  = j;
                    minxyso.highx = 0;
                    minxyso.highy = 0;
                    minxyso.s     = 0;
                    minxyso.o     = 0;
                    minxyso.e     = double.MaxValue;

                    for (int l = 0; l < highBlkHeightCnt; l++)
                    {
                        for (int k = 0; k < highBlkWidthCnt; k++)
                        {
                            DenseMatrix targetHighMat = highBlkList[l * highBlkWidthCnt + k];
                            DenseMatrix targetLowMat  = lowBlkList[j * lowBlkWidthCnt + i];

                            double[] targetHighMatData = targetHighMat.ToRowWiseArray();
                            double[] targetLowMatData  = targetLowMat.ToRowWiseArray();

                            //IEnumerable<Tuple<int, int, double>> targetHighMatEnum = targetHighMat.IndexedEnumerator();
                            //IEnumerable<Tuple<int, int, double>> targetLowMatEnum = targetLowMat.IndexedEnumerator();

                            double targetHnLElementSum = 0;

                            for (int m = 0; m < 64; m++)
                            {
                                targetHnLElementSum += targetHighMatData[m] * targetLowMatData[m];
                            }

                            MathNet.Numerics.LinearAlgebra.Generic.Matrix <double> so = highInvMatList[l * highBlkWidthCnt + k].Multiply(new DenseMatrix(2, 1, new double[] { targetHnLElementSum, lowIntwList[j * lowBlkWidthCnt + i] }));
                            double e = (lowBlkList[j * lowBlkWidthCnt + i] - (so[0, 0] * highBlkList[l * highBlkWidthCnt + k]) - new DenseMatrix(8, 8, so[1, 0])).IndexedEnumerator().Sum(element => element.Item3 * element.Item3);

                            if (e < minxyso.e)
                            {
                                minxyso.highx = k;
                                minxyso.highy = l;
                                minxyso.s     = so[0, 0];
                                minxyso.o     = so[1, 0];
                                minxyso.e     = e;
                            }
                        }
                    }

                    //Console.WriteLine("Low Block (x,y)=(" + minxyso.lowx + "," + minxyso.lowy + "), High Block (x,y)=(" + minxyso.highx + "," + minxyso.highy + "), so= (" + minxyso.s + "," + minxyso.o + "), e=" + minxyso.e);

                    result.DataList.Add(minxyso);
                    Console.Write(".");
                }
            }


            Console.WriteLine();

            Console.WriteLine("Running Post Step.");
            FDT.SerializeObject(args[1] + ".fdt", result);

            Console.WriteLine("Done.");
        }
        public MainWindow()
        {
            //this must ALWAYS be first
            InitializeComponent();

            //// one sensor is currently supported
            this.kinectsensor = KinectSensor.GetDefault();
            // get the depth (display) extents
            FrameDescription frameDescription = this.kinectsensor.DepthFrameSource.FrameDescription;

            // open the reader for the body frames
            this.bodyFrameReader = this.kinectsensor.BodyFrameSource.OpenReader();
            this.bodyFrameReader.FrameArrived += this.Reader_FrameArrived;
            // open the sensor
            kinectsensor.Open();
            recordNumber = 0;
            //initialization Buttons for visibility

            initButtons();

            this.SetThreshold(HARD_THRESHOLD);

            currentUser = -1; //bugs out?!
            currentState = ScreenStates.HOME;

            //initialize global skeleton buffer
            bStorage = new Body[6]; //"hard"-coded limit of 6 (more like 2)

            //setup timer
            timer = new System.Windows.Threading.DispatcherTimer();
            timer.Tick += new EventHandler(timerTick);
            timer.Interval = new TimeSpan(0, 0, 0, 0, 100); //trigger every 50 ms (faster updates in progress bar)
            timerCount = 0;

            //initialize dataStore -- 4 user limit
            dataStore = new MathNet.Numerics.LinearAlgebra.Generic.Matrix<float>[4][][];
            for (int i = 0; i < dataStore.Length; i++)
            {
                //for each user give space for "1" gesture
                dataStore[i] = new MathNet.Numerics.LinearAlgebra.Generic.Matrix<float>[1][];
                for (int j = 0; j < dataStore[i].Length; j++)
                {
                    //each gesture give space for "1" sample of the gesture
                    dataStore[i][j] = new MathNet.Numerics.LinearAlgebra.Generic.Matrix<float>[numRecords];
                }
            }
            userNames = new string[]{"User 1", "User 2", "User 3", "User 4"};
            userHasData = new bool[4] { false, false, false, false };
            dataEnrolled = false;

            //set up recordingBuffer
            rb = new RecordingBuffer(5 * 30, 25); //full joints 5 seconds;
            rb.clearBuffer();

            //set state to welcome
            setProgramState(ScreenStates.HOME);

            this.kinectManager = KinectManager.Default;

            //setup the cursor with images
            Image[] cursorImages = new Image[3];
            Image cursorImage0 = new Image();
            cursorImage0.Source = ImageExtensions.ToBitmapSource(KinectLogin.Properties.Resources.hand1);
            cursorImages[0] = cursorImage0;
            Image cursorImage1 = new Image();
            cursorImage1.Source = ImageExtensions.ToBitmapSource(KinectLogin.Properties.Resources.hand2);
            cursorImages[1] = cursorImage1;
            Image cursorImage2 = new Image();
            cursorImage2.Source = ImageExtensions.ToBitmapSource(KinectLogin.Properties.Resources.hand3);
            cursorImages[2] = cursorImage2;

            this.cursorView.SetCursorImages(cursorImages);

            this.kinectManager.HandManager.Cursor = this.cursorView;
            this.kinectManager.AddStateListener(this);

            //MessageBox.Show("Success!");
            //Loaded += OnLoaded;
        }