void Start()
 {
     model_ = ModelLoader.LoadFromStreamingAssets("F:\\2Unity\\RollerBall\\Assets\\RollerBallBrain.nn");
     worker = BarracudaWorkerFactory.CreateWorker(BarracudaWorkerFactory.Type.ComputePrecompiled, model_);
     rBody  = GetComponent <Rigidbody>();
     FxFz   = new float[2];
 }
Exemple #2
0
    // Start is called before the first frame update
    void Start()
    {
        body      = GetComponent <Rigidbody>();
        navigator = GetComponent <PathNavigator>();
        autopilot = GetComponent <Autopilot>();
        vehicle   = GetComponent <Vehicle>();
        workers   = new List <IWorker>();

        int[] inputShape = null;
        for (int i = 0; i < 5; i++)
        {
            var model = ModelLoader.LoadFromStreamingAssets(modelName + i + ".nn");
            workers.Add(WorkerFactory.CreateWorker(WorkerFactory.Type.CSharpBurst, model));
            inputShape = model.inputs[0].shape;
        }

        classificationWorker = WorkerFactory.CreateWorker(WorkerFactory.Type.CSharpBurst, ModelLoader.LoadFromStreamingAssets(modelName + "C" + ".nn"));

        numObservations = (inputShape.Last() - 1) / 3;
        inputs          = new Tensor(new TensorShape(inputShape));
        estimations     = new List <float>();

        curvature   = new float[numObservations];
        camber      = new float[numObservations];
        inclination = new float[numObservations];

        rearLeft  = vehicle.wheels.Where(w => w.localAttachmentPosition.z < 0).Where(w => w.localAttachmentPosition.x < 0).ToArray();
        rearRight = vehicle.wheels.Where(w => w.localAttachmentPosition.z < 0).Where(w => w.localAttachmentPosition.x > 0).ToArray();
    }
Exemple #3
0
        ///<summary>
        ///Loads model into memory
        ///<param>Path to the model</param>
        ///</summary>
        public CoachModel GetModel(string path, int workers = 1, string input = "input", string output = "output")
        {
            var modelPath = Path.Combine(path, "unity.bytes");
            var labelPath = Path.Combine(path, "manifest.json");

            // Load the model
            Model model = ModelLoader.LoadFromStreamingAssets(modelPath);

            var manifest = ReadManifest(labelPath);

            string[] labels       = manifest.labels;
            string   baseModule   = manifest.module;
            float    coachVersion = manifest.coachVersion;

            return(new CoachModel(model, labels, baseModule, coachVersion, workers, input, output));
        }
        // Start is called before the first frame update
        void Start()
        {
            // モデルのロード
            m_Model = ModelLoader.LoadFromStreamingAssets(m_ModelName + ".nn");
            // ワーカーの作成
            m_Worker = BarracudaWorkerFactory.CreateWorker(BarracudaWorkerFactory.Type.ComputePrecompiled, m_Model);

            // テクスチャーペイントの準備
            Texture2D mainTexture = (Texture2D)m_Plane.GetComponent <Renderer>().material.mainTexture;

            Color[] pixels = mainTexture.GetPixels();

            m_Buffer = new Color[pixels.Length];
            pixels.CopyTo(m_Buffer, 0);

            m_DrawTexture            = new Texture2D(mainTexture.width, mainTexture.height, TextureFormat.ARGB32, false);
            m_DrawTexture.filterMode = FilterMode.Point;
        }
Exemple #5
0
    // Start is called before the first frame update
    void Start()
    {
        var model = ModelLoader.LoadFromStreamingAssets(modelName + ".bytes");

        var worker = BarracudaWorkerFactory.CreateWorker(BarracudaWorkerFactory.Type.Compute, model);

        foreach (var layer in model.layers)
        {
            Debug.Log("Layer " + layer.name + " does: " + layer.inputs);
        }

        var inputs = new Dictionary <string, Tensor>();

        Texture2D img = Resources.Load("tennis_in_crowd") as Texture2D;
        //    Texture2D img = testImage.mainTexture as Texture2D;

        var tensor = new Tensor(img, 3);

        inputs.Add("image", tensor);

        worker.ExecuteAndWaitForCompletion(inputs);
        var Heatmap = worker.Fetch("heatmap");
        var Offset  = worker.Fetch("offset_2");
        var Dis_fwd = worker.Fetch("displacement_fwd_2");
        var Dis_bwd = worker.Fetch("displacement_bwd_2");

        poses = posenet.DecodeMultiplePosesOG(Heatmap, Offset, Dis_fwd, Dis_bwd,
                                              outputStride: 16, maxPoseDetections: 2, scoreThreshold: 0.02f, nmsRadius: 20);

        gl = GameObject.Find("GLRenderer").GetComponent <GLRenderer>();

        Debug.Log(poses.Length);

//        Debug.Log(Heatmap.height);
        Heatmap.Dispose();
        Offset.Dispose();
        Dis_fwd.Dispose();
        Dis_bwd.Dispose();
        worker.Dispose();
    }
        // Start is called before the first frame update
        void Start()
        {
            // 動作確認のサンプルなので, すべて Start() 上で実行しています.

            // モデルのロード
            var model = ModelLoader.LoadFromStreamingAssets(m_ModelName + ".nn");

            // ワーカー (推論エンジン) の作成
            var worker = BarracudaWorkerFactory.CreateWorker(BarracudaWorkerFactory.Type.ComputePrecompiled, model);

            // 入力の作成. 第2引数はチャンネル数.
            var tensor = new Tensor(m_InputTexture, 1);

            // 推論の実行
            worker.Execute(tensor);

            // 推論結果の取得
            var O = worker.Peek();

            // 結果の表示
            int   pred   = 0;
            float maxVal = float.MinValue;

            for (int i = 0; i < 10; ++i)
            {
                if (maxVal < O.readonlyArray[i])
                {
                    pred   = i;
                    maxVal = O.readonlyArray[i];
                }
            }
            Debug.Log("Pred: " + pred.ToString());

            // 後片付け (メモリの解放など)
            O.Dispose();
            worker.Dispose();
        }
Exemple #7
0
    private void Start()
    {
        // デバッグ用ファイルを開く
        //Encoding enc = Encoding.GetEncoding("Shift_JIS");
        //var csvPath = System.IO.Path.Combine(Application.streamingAssetsPath, "data.csv");
        //writer = new StreamWriter(csvPath, false, enc);

        if (DebugMode)
        {
            if (User3Input)
            {
                UpdateVNectModel = new UpdateVNectModelDelegate(UpdateVNectAsync);
            }
            else
            {
                UpdateVNectModel = new UpdateVNectModelDelegate(UpdateVNect);
            }

            /*
             * var streamingPath = System.IO.Path.Combine(Application.streamingAssetsPath, HighQualityModelName);
             * var writer = new BinaryWriter(new FileStream(streamingPath, FileMode.Create));
             * writer.Write(NNModel.modelData.Value);
             * writer.Close();
             */
            _model = ModelLoader.Load(NNModel, Verbose);
        }
        else
        {
            var streamingPath = System.IO.Path.Combine(Application.streamingAssetsPath, HighQualityModelName);
            if (!File.Exists(streamingPath))
            {
                ModelQuality = 0;
            }

            if (ModelQuality == 0)
            {
                InputImageSize   = 224;
                HeatMapCol       = 14;
                User3Input       = false;
                UpdateVNectModel = new UpdateVNectModelDelegate(UpdateVNect);
                _model           = ModelLoader.Load(NNModel, Verbose);
            }
            else
            {
                InputImageSize   = 448;
                HeatMapCol       = 28;
                User3Input       = true;
                UpdateVNectModel = new UpdateVNectModelDelegate(UpdateVNectAsync);
                _model           = ModelLoader.LoadFromStreamingAssets(streamingPath);
            }
        }

        // Init VideoCapture
        videoCapture.Init(InputImageSize, InputImageSize);
        videoCapture.VideoReady += videoCapture_VideoReady;

        HeatMapCol_Half     = HeatMapCol / 2;
        HeatMapCol_Squared  = HeatMapCol * HeatMapCol;
        HeatMapCol_Cube     = HeatMapCol * HeatMapCol * HeatMapCol;
        HeatMapCol_JointNum = HeatMapCol * JointNum;
        heatMap2D           = new float[JointNum * HeatMapCol_Squared];
        offset2D            = new float[JointNum * HeatMapCol_Squared * 2];
        heatMap3D           = new float[JointNum * HeatMapCol_Cube];
        offset3D            = new float[JointNum * HeatMapCol_Cube * 3];
        InputImageSizeF     = InputImageSize;
        InputImageSizeHalf  = InputImageSizeF / 2f;
        unit = 1f / (float)HeatMapCol;

        cubeOffsetLinear  = HeatMapCol * JointNum_Cube;
        cubeOffsetSquared = HeatMapCol_Squared * JointNum_Cube;

        // Disabel sleep
        Screen.sleepTimeout = SleepTimeout.NeverSleep;

        _worker = WorkerFactory.CreateWorker(WorkerType, _model, Verbose);
        StartCoroutine("WaitLoad");

        var texture = new RenderTexture(InputImageSize, InputImageSize, 0, RenderTextureFormat.RGB565, RenderTextureReadWrite.sRGB)
        {
            useMipMap        = false,
            autoGenerateMips = false,
            wrapMode         = TextureWrapMode.Clamp,
            filterMode       = FilterMode.Point,
        };

        if (User3Input)
        {
            inputs[inputName_1] = new Tensor(texture, 3);
            inputs[inputName_2] = new Tensor(texture, 3);
            inputs[inputName_3] = new Tensor(texture, 3);
            _worker.Execute(inputs);
            inputs[inputName_1].Dispose();
            inputs[inputName_2].Dispose();
            inputs[inputName_3].Dispose();
        }
        else
        {
            input = new Tensor(texture, 3);
            _worker.Execute(input);
            input.Dispose();
        }
    }
    private void Start()
    {
        if (DebugMode)
        {
            if (User3Input)
            {
                UpdateVNectModel = new UpdateVNectModelDelegate(UpdateVNectAsync);
            }
            else
            {
                UpdateVNectModel = new UpdateVNectModelDelegate(UpdateVNect);
            }

            /*
             * var streamingPath = System.IO.Path.Combine(Application.streamingAssetsPath, HighQualityModelName);
             * var writer = new BinaryWriter(new FileStream(streamingPath, FileMode.Create));
             * writer.Write(NNModel.Value);
             * writer.Close();
             */
            _model = ModelLoader.Load(NNModel, Verbose);
        }
        else
        {
            var streamingPath = System.IO.Path.Combine(Application.streamingAssetsPath, HighQualityModelName);
            if (!File.Exists(streamingPath))
            {
                ModelQuality = 0;
            }

            if (ModelQuality == 0)
            {
                InputImageSize   = 224;
                HeatMapCol       = 14;
                UpdateVNectModel = new UpdateVNectModelDelegate(UpdateVNect);
                _model           = ModelLoader.Load(NNModel, Verbose);
            }
            else
            {
                InputImageSize   = 448;
                HeatMapCol       = 28;
                UpdateVNectModel = new UpdateVNectModelDelegate(UpdateVNectAsync);
                _model           = ModelLoader.LoadFromStreamingAssets(streamingPath);
            }
        }

        HeatMapCol_Half     = HeatMapCol / 2;
        HeatMapCol_Squared  = HeatMapCol * HeatMapCol;
        HeatMapCol_Cube     = HeatMapCol * HeatMapCol * HeatMapCol;
        HeatMapCol_JointNum = HeatMapCol * JointNum;
        heatMap2D           = new float[JointNum * HeatMapCol_Squared];
        offset2D            = new float[JointNum * HeatMapCol_Squared * 2];
        heatMap3D           = new float[JointNum * HeatMapCol_Cube];
        offset3D            = new float[JointNum * HeatMapCol_Cube * 3];
        InputImageSizeF     = InputImageSize;
        InputImageSizeHalf  = InputImageSizeF / 2f;
        unit = 1f / (float)HeatMapCol;

        cubeOffsetLinear  = HeatMapCol * JointNum_Cube;
        cubeOffsetSquared = HeatMapCol_Squared * JointNum_Cube;

        // Disabel sleep
        Screen.sleepTimeout = SleepTimeout.NeverSleep;

        _worker = WorkerFactory.CreateWorker(WorkerType, _model, Verbose);
        StartCoroutine("WaitLoad");

        // Init VideoCapture
        videoCapture.Init(InputImageSize, InputImageSize);
        videoCapture.VideoReady += videoCapture_VideoReady;
    }
 // Start is called before the first frame update
 void Start()
 {
     var model  = ModelLoader.LoadFromStreamingAssets(RollerBallBrain + ".nn");
     var worker = BarracudaWorkerFactory.CreateWorker(BarracudaWorkerFactory.Type.ComputePrecompiled, model);
 }
    IEnumerator PoseUpdateNoTex(NativeArray <Color32> buffer, int width, int height, float secondsToWait)
    {
        //    isPosing = true;

        var _model = ModelLoader.LoadFromStreamingAssets(modelName + ".bytes");

        //var _model = model;

        var _worker = BarracudaWorkerFactory.CreateWorker(BarracudaWorkerFactory.Type.Compute, _model);
        //var _worker = worker;

        var frame = new Texture2D(width, height, TextureFormat.RGB24, false);

        frame.SetPixels32(buffer.ToArray());
        frame.Apply();

        //yield return new WaitForSeconds(secondsToWait);

        yield return(new WaitForEndOfFrame());

        //frame.ResizePro(Width, Height, false, false);
        posenet.scale(frame, Width, Height, FilterMode.Bilinear);

        // Save frame image jpg to disk for debugging
        /// var randomInt = UnityEngine.Random.Range(0, 100000000000000000);
        /// File.WriteAllBytes(Application.persistentDataPath + "/pose-" + randomInt + ".jpg", frame.EncodeToJPG());
        /// Debug.Log("Saved size converted image path: " + Application.persistentDataPath + "/pose-" + randomInt + ".jpg");

        var inputs = new Dictionary <string, Tensor>();

        var tensor = new Tensor(frame, 3);

        inputs.Add("image", tensor);

        _worker.ExecuteAndWaitForCompletion(inputs);

        //yield return new WaitForSeconds(secondsToWait);
        yield return(new WaitForEndOfFrame());

        var Heatmap = _worker.Fetch("heatmap");

        //yield return new WaitForSeconds(secondsToWait);
        yield return(new WaitForEndOfFrame());

        var Offset = _worker.Fetch("offset_2");

        //yield return new WaitForSeconds(secondsToWait);
        yield return(new WaitForEndOfFrame());

        var Dis_fwd = _worker.Fetch("displacement_fwd_2");

        //yield return new WaitForSeconds(secondsToWait);
        yield return(new WaitForEndOfFrame());

        var Dis_bwd = _worker.Fetch("displacement_bwd_2");

        //yield return new WaitForSeconds(secondsToWait);
        yield return(new WaitForEndOfFrame());

        poses = posenet.DecodeMultiplePosesOG(Heatmap, Offset, Dis_fwd, Dis_bwd,
                                              outputStride: 16, maxPoseDetections: 1, scoreThreshold: 0.8f, nmsRadius: 30);


        Offset.Dispose();
        Dis_fwd.Dispose();
        Dis_bwd.Dispose();
        Heatmap.Dispose();
        _worker.Dispose();


        isPosing = false;

        frame  = null;
        inputs = null;

        //    Resources.UnloadUnusedAssets();


        yield return(null);
    }