Ejemplo n.º 1
0
 public vectorvfEnumerator(vectorvf collection)
 {
     collectionRef = collection;
     currentIndex  = -1;
     currentObject = null;
     currentSize   = collectionRef.Count;
 }
Ejemplo n.º 2
0
 public vectorvf(vectorvf other) : this(csogmaneoPINVOKE.new_vectorvf__SWIG_1(vectorvf.getCPtr(other)), true)
 {
     if (csogmaneoPINVOKE.SWIGPendingException.Pending)
     {
         throw csogmaneoPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 3
0
 public void SetRange(int index, vectorvf values)
 {
     csogmaneoPINVOKE.vectorvf_SetRange(swigCPtr, index, vectorvf.getCPtr(values));
     if (csogmaneoPINVOKE.SWIGPendingException.Pending)
     {
         throw csogmaneoPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 4
0
 public void learn(vectorvf inputsPredict)
 {
     csogmaneoPINVOKE.Hierarchy_learn__SWIG_1(swigCPtr, vectorvf.getCPtr(inputsPredict));
     if (csogmaneoPINVOKE.SWIGPendingException.Pending)
     {
         throw csogmaneoPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 5
0
 public void activate(vectorvf inputsFeed)
 {
     csogmaneoPINVOKE.Hierarchy_activate(swigCPtr, vectorvf.getCPtr(inputsFeed));
     if (csogmaneoPINVOKE.SWIGPendingException.Pending)
     {
         throw csogmaneoPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Ejemplo n.º 6
0
        public vectorvf getPredictions()
        {
            vectorvf ret = new vectorvf(csogmaneoPINVOKE.Hierarchy_getPredictions(swigCPtr), false);

            if (csogmaneoPINVOKE.SWIGPendingException.Pending)
            {
                throw csogmaneoPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Ejemplo n.º 7
0
        public static vectorvf Repeat(ValueField2D value, int count)
        {
            global::System.IntPtr cPtr = csogmaneoPINVOKE.vectorvf_Repeat(ValueField2D.getCPtr(value), count);
            vectorvf ret = (cPtr == global::System.IntPtr.Zero) ? null : new vectorvf(cPtr, true);

            if (csogmaneoPINVOKE.SWIGPendingException.Pending)
            {
                throw csogmaneoPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Ejemplo n.º 8
0
        public vectorvf GetRange(int index, int count)
        {
            global::System.IntPtr cPtr = csogmaneoPINVOKE.vectorvf_GetRange(swigCPtr, index, count);
            vectorvf ret = (cPtr == global::System.IntPtr.Zero) ? null : new vectorvf(cPtr, true);

            if (csogmaneoPINVOKE.SWIGPendingException.Pending)
            {
                throw csogmaneoPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Ejemplo n.º 9
0
    void Update()
    {
        if (applicationExiting)
        {
            return;
        }

        if (cameraTexture == null || predictionTexture == null || carController == null)
        {
            return;
        }

        ogmaneo.Vec2i pixelPos = new Vec2i();

        // Remember currently active render texture
        RenderTexture currentActiveRT = RenderTexture.active;

        // Transfer the camera capture into the prediction texture (temporarily)
        RenderTexture.active = cameraTexture;
        predictionTexture.ReadPixels(new Rect(0, 0, _inputWidth, _inputHeight), 0, 0);
        predictionTexture.Apply();

        // Restore active render texture
        RenderTexture.active = currentActiveRT;

        // Transfer the RGB camera texture into ValueField2D fields
        Color actualPixel = new Color();
        Color yuvPixel    = new Color(0.0f, 0.0f, 0.0f);

        for (int x = 0; x < _inputWidth; x++)
        {
            for (int y = 0; y < _inputHeight; y++)
            {
                actualPixel = predictionTexture.GetPixel(x, y);

                // SDTV (BT.601) Y'UV conversion
                yuvPixel.r = actualPixel.r * 0.299f + actualPixel.g * 0.587f + actualPixel.b * 0.114f;   // Y' luma component

                // Chrominance
                // U = r * -0.14713 + g * -0.28886 + b * 0.436
                //yuvPixel.g = 0.0f;
                // V = r * 0.615 + g * -0.51499 + b * -0.10001
                //yuvPixel.b = 0.0f;

                predictionTexture.SetPixel(x, y, yuvPixel);
            }
        }

        // Edge Detection Convolution methods:
        //   Laplacian of the Gaussian (LoG) - https://en.wikipedia.org/wiki/Blob_detection#The_Laplacian_of_Gaussian
        // - Sobel-Feldman and Sharr operators - https://en.wikipedia.org/wiki/Sobel_operator
        // - Prewitt operator - https://en.wikipedia.org/wiki/Prewitt_operator
        //   Kirch operator - https://en.wikipedia.org/wiki/Kirsch_operator
        Texture2D horzTexture = ConvolutionFilter.Apply(predictionTexture, ConvolutionFilter.Sobel3x3Horizontal); // ConvolutionFilter.Prewitt3x3Horizontal);
        Texture2D vertTexture = ConvolutionFilter.Apply(predictionTexture, ConvolutionFilter.Sobel3x3Vertical);   // ConvolutionFilter.Prewitt3x3Vertical);

        Texture2D convolvedTexture = new Texture2D(_inputWidth, _inputHeight, predictionTexture.format, false);
        Color     tempPixel        = new Color(0.0f, 0.0f, 0.0f);

        for (int x = 0; x < _inputWidth; x++)
        {
            for (int y = 0; y < _inputHeight; y++)
            {
                Color horzPixel = horzTexture.GetPixel(x, y);
                Color vertPixel = vertTexture.GetPixel(x, y);

                tempPixel.r = Mathf.Sqrt((horzPixel.r * horzPixel.r) + (vertPixel.r * vertPixel.r));
                tempPixel.g = tempPixel.r; // Mathf.Sqrt((horzPixel.g * horzPixel.g) + (vertPixel.g * vertPixel.g));
                tempPixel.b = tempPixel.r; // Mathf.Sqrt((horzPixel.b * horzPixel.b) + (vertPixel.b * vertPixel.b));

                convolvedTexture.SetPixel(x, y, tempPixel);
            }
        }

        predictionTexture.SetPixels(convolvedTexture.GetPixels());
        predictionTexture.Apply();

        // Transfer the RGB camera texture into ValueField2D fields
        for (int x = 0; x < _inputWidth; x++)
        {
            for (int y = 0; y < _inputHeight; y++)
            {
                actualPixel = predictionTexture.GetPixel(x, y);

                pixelPos.x = x;
                pixelPos.y = y;

                _inputField.setValue(pixelPos, actualPixel.r);

                previousImage[x, y] = sourceImage[x, y];
                sourceImage[x, y]   = actualPixel.r;// * 0.299f + actualPixel.g * 0.587f + actualPixel.b * 0.114f;
            }
        }

        // Encode scalar values from the car controller
        Steer = carController.CurrentSteerAngle / carController.m_MaximumSteerAngle;
        Accel = carController.AccelInput;
        Brake = carController.BrakeInput;

        pixelPos.x = 0;
        pixelPos.y = 0;
        _inputValues.setValue(pixelPos, Steer);

        // Setup the hierarchy input vector
        vectorvf inputVector = new vectorvf();

        inputVector.Add(_inputField);
        inputVector.Add(_inputValues);

        // Step the hierarchy
        _hierarchy.activate(inputVector);

        if (Training)
        {
            _hierarchy.learn(inputVector);
        }

        // Grab the predictions vector
        vectorvf prediction = _hierarchy.getPredictions();

        // Transfer the ValueField2D fields into the RGB prediction texture
        Color predictedPixel = new Color();

        for (int x = 0; x < _inputWidth; x++)
        {
            for (int y = 0; y < _inputHeight; y++)
            {
                pixelPos.x = x;
                pixelPos.y = y;

                predictedPixel.r = prediction[0].getValue(pixelPos);
                predictedPixel.g = predictedPixel.r; // prediction[1].getValue(pixelPos);
                predictedPixel.b = predictedPixel.r; // prediction[2].getValue(pixelPos);

                predictionTexture.SetPixel(x, y, predictedPixel);

                predictedImage[x, y] = predictedPixel.r;// * 0.299f + predictedPixel.g * 0.587f + predictedPixel.b * 0.114f;
            }
        }
        predictionTexture.Apply();

        // Wait for physics to settle
        if (_time < 1.0f)
        {
            _time += Time.deltaTime;

            // Apply hand brake
            carSteer  = 0.0f;
            carAccel  = 0.0f;
            carBrake  = -1.0f;
            HandBrake = 1.0f;
        }
        else
        {
            // Release hand brake
            HandBrake = 0.0f;

            Accel = -1.0f;
            Brake = Accel;

            pixelPos.x = 0;
            pixelPos.y = 0;

            // Update the car controller
            PredictedSteer = prediction[1].getValue(pixelPos);
            PredictedAccel = Accel;
            PredictedBrake = Brake;

            carSteer = PredictedSteer;// * carController.m_MaximumSteerAngle;
            carAccel = PredictedAccel;
            carBrake = PredictedBrake;

            // Search along the spline for the closest point to the current car position
            float   bestT = 0.0f, minDistance = 100000.0f;
            Vector3 carPosition = carController.gameObject.transform.localPosition;

            // When not training use the track spline
            BezierSpline spline = trackSpline;

            if (Training)
            {
                spline = splineList[SplineIndex];
            }

            float totalDistance = 0.0f;

            for (float t = 0.0f; t <= 1.0f; t += 0.001f)
            {
                Vector3 position     = spline.GetPoint(t);
                Vector3 positionPrev = spline.GetPoint(t - 0.001f);

                float distance = Vector3.Distance(position, carPosition);

                totalDistance += Vector3.Distance(position, positionPrev);

                if (distance <= minDistance)
                {
                    minDistance = distance;
                    bestT       = t;
                }
            }

            // Reset car position and direction?
            if (Input.GetKeyUp(KeyCode.R) || carController.Collided)
            {
                if (ForcePredictionMode == false)
                {
                    Training = true;
                }

                carController.ResetCollided();

                // Spline 0 is usually set as the spline used to create the track
                SplineIndex = 0;

                Vector3 position = spline.GetPoint(bestT);
                carController.gameObject.transform.localPosition = position;

                Vector3 splineDirection = spline.GetDirection(bestT).normalized;
                carController.gameObject.transform.forward = -splineDirection;
            }

            // Determine the difference between the input image (t) and predicted image (t+1)
            CalculateNormalizedCrossCorrelation();

            // Toggle training on iff too divergent?
            if (Training == false && ForcePredictionMode == false && NCC < 0.25f)
            {
                Training = true;
            }

            // Toggle training off iff quite confident?
            if (Training == true && NCC > 0.85f && LapCount >= initialTrainingLaps)
            {
                Training = false;
            }

            if (carController.CurrentSpeed < 2.0f)
            {
                Training = true;
            }

            if (Training)
            {
                _trainingCount++;
            }
            else
            {
                _predictingCount++;
            }

            if (Training && spline != null)
            {
                Vector3 carDirection = -carController.gameObject.transform.forward.normalized;

                Vector3 targetPosition = spline.GetPoint(bestT + SteerAhead / totalDistance);

                //Vector3 splineDirection = spline.GetDirection(bestT).normalized;

                Vector3 targetDirection = (targetPosition - carPosition).normalized;

                float angle = (1.0f - Vector3.Dot(carDirection, targetDirection));// * Mathf.Rad2Deg;

                Vector3 right  = Vector3.Cross(carDirection, Vector3.up);
                float   angle2 = Vector3.Dot(right, targetDirection);

                float newCarSteer = Mathf.Exp(256.0f * angle) - 1.0f;

                if (Mathf.Abs(minDistance) > 0.01f)//newCarSteer > Mathf.PI / 64.0f)
                {
                    newCarSteer += angle2 * Mathf.Abs(minDistance);
                }

                if (angle2 > 0.0f)
                {
                    newCarSteer = -newCarSteer;
                }

                if (newCarSteer > 1.0f)
                {
                    newCarSteer = 1.0f;
                }
                else
                if (newCarSteer < -1.0f)
                {
                    newCarSteer = -1.0f;
                }

                float steerBlend = 0.75f;
                carSteer = (steerBlend * newCarSteer) + ((1.0f - steerBlend) * carSteer);

                if (enableDebugLines)
                {
                    debugLinePositions[0] = carController.gameObject.transform.localPosition;
                    debugLinePositions[1] = debugLinePositions[0] + carDirection * 10.0f;
                    debugLinePositions[2] = carController.gameObject.transform.localPosition;
                    debugLinePositions[3] = debugLinePositions[2] + targetDirection * 10.0f;
                    debugLine.SetPositions(debugLinePositions);
                }
            }

            float totalCount = _trainingCount + _predictingCount;

            if (totalCount == 0.0f)
            {
                TrainingPercent   = 1.0f;
                PredictionPercent = 0.0f;
            }
            else
            {
                TrainingPercent   = (float)_trainingCount / totalCount;
                PredictionPercent = (float)_predictingCount / totalCount;
            }

            if (bestT < prevBestT)
            {
                LapCount++;

                _trainingCount   = 0;
                _predictingCount = 0;

                if ((LapCount % lapsPerSpline) == 0)
                {
                    SplineIndex++;

                    if (SplineIndex >= splineList.Length)
                    {
                        SplineIndex = 0;
                    }
                }
            }

            prevBestT = bestT;
        }

        if (userControl)
        {
            // Control overides
            // pass the input to the car!
            float h = CrossPlatformInputManager.GetAxis("Horizontal");
            float v = CrossPlatformInputManager.GetAxis("Vertical");
#if !MOBILE_INPUT
            float handbrake = CrossPlatformInputManager.GetAxis("Jump");
#endif
            carSteer  = h;
            carAccel  = v;
            carBrake  = v;
            HandBrake = handbrake;
        }

        // Toggle training?
        if (Input.GetKeyUp(KeyCode.T))
        {
            Training            = !Training;
            ForcePredictionMode = false;
        }
        else
        // Force prediction mode?
        if (Input.GetKeyUp(KeyCode.F))
        {
            Training            = false;
            ForcePredictionMode = true;
        }

        // Save out the current state of the hierarchy?
        if (Input.GetKeyUp(KeyCode.O) && hierarchyFileName.Length > 0)
        {
            _hierarchy.save(_res.getComputeSystem(), hierarchyFileName);
            print("Saved OgmaNeo hierarchy to " + hierarchyFileName);
        }
    }
Ejemplo n.º 10
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(vectorvf obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            int numSimSteps = 500;

            ComputeSystem.DeviceType deviceType = ComputeSystem.DeviceType._gpu;

            Resources _res = new Resources();

            _res.create(deviceType);

            Architect arch = new Architect();

            arch.initialize(1234, _res);

            // Input size (width and height)
            int w = 4;
            int h = 4;

            ParameterModifier inputParams = arch.addInputLayer(new Vec2i(w, h));

            inputParams.setValue("in_p_alpha", 0.02f);
            inputParams.setValue("in_p_radius", 16);

            for (int i = 0; i < 2; i++)
            {
                ParameterModifier layerParams = arch.addHigherLayer(new Vec2i(36, 36), SparseFeaturesType._chunk);
                layerParams.setValue("sfc_chunkSize", new Vec2i(6, 6));
                layerParams.setValue("sfc_ff_radius", 12);
                layerParams.setValue("hl_poolSteps", 2);
                layerParams.setValue("sfc_weightAlpha", 0.02f);
                layerParams.setValue("sfc_biasAlpha", 0.001f);
                layerParams.setValue("p_alpha", 0.08f);
                layerParams.setValue("p_beta", 0.16f);
                layerParams.setValue("p_radius", 16);
            }

            Hierarchy hierarchy = arch.generateHierarchy();

            ValueField2D inputField = new ValueField2D(new Vec2i(w, h));

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    inputField.setValue(new Vec2i(x, y), (y * w) + x);
                }
            }

            vectorvf inputVector = new vectorvf();

            inputVector.Add(inputField);

            System.Console.WriteLine("Stepping the hierarchy...");
            for (int i = 0; i < numSimSteps; i++)
            {
                hierarchy.simStep(inputVector, true);
            }

            vectorvf prediction = hierarchy.getPredictions();

            System.Console.Write("Input      :");
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    System.Console.Write(" " + inputField.getValue(new Vec2i(x, y)).ToString("n2"));
                }
            }
            System.Console.WriteLine();

            System.Console.Write("Prediction :");
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    System.Console.Write(" " + prediction[0].getValue(new Vec2i(x, y)).ToString("n2"));
                }
            }
            System.Console.WriteLine();
        }