Ejemplo n.º 1
0
        private UnifiedCameraNeuralNetwork[] _neuralNets; // A network for each pair of camaras (main camera and camera i)

        /// <summary>
        /// Creates a new calibration object.
        /// Calibration will occur for the session that was recorded at sessionTimestamp using the numOfCameras defined.
        /// </summary>
        /// <param name="sessionTimestamp"> Timestamp of the session recorded </param>
        /// <param name="numOfCameras"> Number of cameras in the recording </param>
        public Calibration(DateTime sessionTimestamp, int numOfCameras, SkelDisplay display)
        {
            // Non configurable ANN parameters (each network maps a pair of cameras)
            // The input is one of the cameras, the output is the absolute camera.
            _neuralNets = new UnifiedCameraNeuralNetwork[numOfCameras];
            int inputLayerSize  = SkelJointsData.numOfJoints * 3;
            int outputLayerSize = inputLayerSize;

            // Configurable ANN parameters
            int   hiddenLayerSize = inputLayerSize * inputLayerSize;
            float learningRate    = 0.15f;
            bool  isStochastic    = true;
            float momentum        = 0.1f;

            // Create an ANN for each pair of cameras
            for (int i = 0; i < numOfCameras - 1; i++)
            {
                _neuralNets[i] = new UnifiedCameraNeuralNetwork(inputLayerSize, hiddenLayerSize, outputLayerSize,
                                                                learningRate, isStochastic, momentum);
            }

            _numOfCameras = numOfCameras;
            _display      = display;
            _replay       = new SkelReplay(sessionTimestamp, numOfCameras);
        }
Ejemplo n.º 2
0
        public static void initNeuralNetworkTest()
        {
            initApproxFunc();

            ann = new UnifiedCameraNeuralNetwork(inputLayerSize, hiddenLayerSize, outputLayerSize,
                                                 learningRate, isStochastic, momentum);

            Random random = new Random(123);

            for (int i = 0; i < numOfSamples; i++)
            {
                samples[i] = (float)random.NextDouble() * (maxRange - minRange) + minRange;
            }

            for (int i = 0; i < numOfTests; i++)
            {
                tests[i] = (float)random.NextDouble() * (maxRange - minRange) + minRange;
            }
        }
Ejemplo n.º 3
0
        private static float performEpoch(UnifiedCameraNeuralNetwork ann,
                                          int numOfSamples, float[] samples, int numOfTests, float[] tests,
                                          DrawingGroup drawingGroup)
        {
            int failedTests = 0;

            using (DrawingContext dc = drawingGroup.Open())
            {
                // Draw a transparent background to set the render size
                float RenderWidth  = 640f;
                float RenderHeight = 480f;
                dc.DrawRectangle(Brushes.Black, null, new System.Windows.Rect(0.0, 0.0, RenderWidth, RenderHeight));
                drawingGroup.ClipGeometry = new RectangleGeometry(new System.Windows.Rect(0.0, 0.0, RenderWidth, RenderHeight));

                for (int i = 0; i < numOfSamples; i++)
                {
                    float   x        = samples[i];
                    float[] input    = new float[1];
                    float[] expected = new float[1];
                    input[0]    = (float)x;
                    expected[0] = normalizeFunc(x); // Normalize
                    ann.train(input, expected);

                    if (i % 10000 == 9999)
                    {
                        Console.WriteLine((i + 1) + " samples trained..");
                    }
                }

                if (ann.Stochastic)
                {
                    ann.forceWeightUpdates();
                }

                for (int i = 0; i < numOfTests; i++)
                {
                    float   x     = tests[i];
                    float[] input = new float[1];
                    input[0] = (float)x;
                    float result     = approximationFunc(x);
                    float prediction = ann.feedForward(input)[0, 0];

                    if (float.IsNaN(prediction))
                    {
                        throw new InvalidOperationException("ANN returned a NaN output.");
                    }

                    prediction = denormalizeFunc(prediction); // Denormalize

                    // Draw
                    int   brushSize   = 2;
                    float xPlot       = (x / (maxRange - minRange)) * RenderWidth;
                    float funcPlot    = RenderHeight - normalizeFunc(x) * RenderHeight;
                    float predictPlot = RenderHeight - normalize(prediction) * RenderHeight;
                    dc.DrawEllipse(groundTruthBrush, null, new System.Windows.Point(xPlot, funcPlot), brushSize, brushSize);
                    dc.DrawEllipse(predictionBrush, null, new System.Windows.Point(xPlot, predictPlot), brushSize, brushSize);

                    float diff = Math.Abs(result - prediction);

                    if (diff > 0.0001)
                    {
                        failedTests++;
                    }
                }
            }

            Console.WriteLine("Total failed tests: " + failedTests);
            float successRate = 1.0f - (float)((float)failedTests / (float)numOfTests);

            Console.WriteLine("Success rate: " + successRate);

            return(successRate);
        }