Beispiel #1
0
        static void TestingOpen1()
        {
            int[] array = { 9, 20, 1 };

            NNModel model = NNModel.Open("nnmodel.dat");
            string  str1  = File.ReadAllText("TD2.txt");

            double[][] vs2 = JsonConvert.DeserializeObject <double[][]>(str1);
            for (int i = 0; i < vs2.Length; i++)
            {
                var inp = new double[9];
                var tmp = vs2[i];
                for (int j = 0; j < 9; j++)
                {
                    inp[j] = tmp[j] / tmp[j + 1];
                }
                model.Compute(inp).Neurons.ForEach(x =>
                {
                    Console.WriteLine(x.Value + "  ");
                });
                Console.WriteLine();
            }
            Console.ReadKey();
            //model.Save();
        }
Beispiel #2
0
    public override void Initialize()
    {
        m_WallJumpSettings = FindObjectOfType <WallJumpSettings>();
        m_Configuration    = Random.Range(0, 5);

        m_AgentRb         = GetComponent <Rigidbody>();
        m_ShortBlockRb    = shortBlock.GetComponent <Rigidbody>();
        m_SpawnAreaBounds = spawnArea.GetComponent <Collider>().bounds;
        m_GroundRenderer  = ground.GetComponent <Renderer>();
        m_GroundMaterial  = m_GroundRenderer.material;

        spawnArea.SetActive(false);

        m_ResetParams = Academy.Instance.EnvironmentParameters;

        // Update model references if we're overriding
        var modelOverrider = GetComponent <ModelOverrider>();

        if (modelOverrider.HasOverrides)
        {
            noWallBrain          = modelOverrider.GetModelForBehaviorName(m_NoWallBehaviorName);
            m_NoWallBehaviorName = ModelOverrider.GetOverrideBehaviorName(m_NoWallBehaviorName);

            smallWallBrain          = modelOverrider.GetModelForBehaviorName(m_SmallWallBehaviorName);
            m_SmallWallBehaviorName = ModelOverrider.GetOverrideBehaviorName(m_SmallWallBehaviorName);

            bigWallBrain          = modelOverrider.GetModelForBehaviorName(m_BigWallBehaviorName);
            m_BigWallBehaviorName = ModelOverrider.GetOverrideBehaviorName(m_BigWallBehaviorName);
        }
    }
Beispiel #3
0
        static void Testing()
        {
            int[] array = { 10, 20, 2 };

            string str = File.ReadAllText("TD1.txt");

            double[][] vs1   = JsonConvert.DeserializeObject <double[][]>(str);
            NNModel    model = new NNModel(array);

            Console.Beep();
            var             watch           = System.Diagnostics.Stopwatch.StartNew();
            BackPropagation backPropagation = new BackPropagation();

            backPropagation.Learn(model, vs1);
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);
            string str1 = File.ReadAllText("TD2.txt");

            double[][] vs2 = JsonConvert.DeserializeObject <double[][]>(str1);
            for (int i = 0; i < vs2.Length; i++)
            {
                model.Compute(NNModel.ReculcArray(vs2[i])).Neurons.ForEach(x =>
                {
                    Console.WriteLine(x.Value + "  ");
                });
                Console.WriteLine();
            }
            Console.WriteLine("_______");
            model.Save();
            Console.Beep();
            Console.ReadKey();
        }
Beispiel #4
0
        private bool LoadRankModel(string strModelFileName, string strFtrFileName)
        {
            if (strModelFileName == null || strModelFileName.Length == 0)
            {
                return(false);
            }
            if (strFtrFileName == null || strFtrFileName.Length == 0)
            {
                return(false);
            }

            modelMSN = NNModel.Create(strModelFileName);
            if (modelMSN == null)
            {
                Console.WriteLine("Failed to load model: " + strModelFileName);
                return(false);
            }

            string[] activeFeatureNames = null;
            //read and process only a subset of activated features as specified in the activeFeatureFile
            activeFeatureNames = File.ReadAllLines(strFtrFileName);
            if (modelMSN.SetFeatureNames(activeFeatureNames) == false)
            {
                Console.WriteLine("Failed to set feature set");
                return(false);
            }

            return(true);
        }
Beispiel #5
0
 /// <summary>
 /// Constructor for a regualar Pix2Pix inference object
 /// </summary>
 public Pix2Pix()
 {
     // Initialise the model
     _modelAsset  = Resources.Load <NNModel>("Models/blobs_run_1");
     _loadedModel = ModelLoader.Load(_modelAsset);
     _worker      = WorkerFactory.CreateWorker(WorkerFactory.Type.ComputePrecompiled, _loadedModel);
 }
Beispiel #6
0
        static void NewNN(string[] args)
        {
            int[] array = new int[args.Length - 1];
            for (int i = 1; i < args.Length; i++)
            {
                if (Int32.TryParse(args[i], out int res))
                {
                    array[i] = res;
                }
                else
                {
                    return;
                }
            }
            string str = File.ReadAllText(args[0]);

            double[][] vs1 = JsonConvert.DeserializeObject <double[][]>(str);

            NNModel         model           = new NNModel(array);
            BackPropagation backPropagation = new BackPropagation();

            backPropagation.Learn(model, vs1);

            for (int i = 0; i < vs1.Length; i += 2)
            {
                var inp = vs1[i];
                model.Compute(inp).Neurons.ForEach(x =>
                {
                    Console.WriteLine(x.Value + "  ");
                });
                Console.WriteLine();
            }
            Console.ReadKey();
        }
Beispiel #7
0
    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < _agentSpawnerDrawerManager.NumAgents; i++)
        {
            GameObject carObject = Instantiate(_carPrefab, transform.position, Quaternion.identity, transform);


            Material customMaterial = _agentSpawnerDrawerManager.MaterialsList[i];
            if (customMaterial != null && _agentSpawnerDrawerManager.UsingModel)
            {
                carObject.GetComponent <MeshRenderer>().material = customMaterial;
            }

            CarAgent carAgent    = carObject.GetComponent <CarAgent>();
            NNModel  customModel = _agentSpawnerDrawerManager.ModelsList[i];
            if (carAgent != null && customModel != null && _agentSpawnerDrawerManager.UsingModel)
            {
                LearningBrain customBrain = new LearningBrain();
                customBrain.brainParameters = carAgent.brain.brainParameters;
                customBrain.model           = customModel;
                carAgent.brain = customBrain;
                carObject.name = customBrain.model.name;
            }
        }
    }
Beispiel #8
0
        public static void PrintResult(NNModel sender, ConcurrentQueue <RecognitionInfo> result)
        {
            RecognitionInfo tmp;

            result.TryDequeue(out tmp);
            Console.WriteLine(tmp);
        }
Beispiel #9
0
 public void GiveModel(
     string behaviorName,
     NNModel model,
     InferenceDevice inferenceDevice = InferenceDevice.CPU)
 {
     SetModel(behaviorName, model, inferenceDevice);
 }
Beispiel #10
0
        static void Main(string[] args)
        {
            int[] array = { 2, 8, 4 };

            string str = File.ReadAllText("TD1.txt");

            float[][] vs1   = JsonConvert.DeserializeObject <float[][]>(str);
            NNModel   model = new NNModel(array);


            BackPropagation backPropagation = new BackPropagation();

            backPropagation.Learn(model, vs1);

            for (int i = 0; i < vs1.Length; i += 2)
            {
                var inp = vs1[i];
                Array.ForEach(model.Compute(inp).Neurons, x =>
                {
                    Console.WriteLine(x.Value + "  ");
                });
                Console.WriteLine();
            }
            Console.ReadKey();
        }
        public String CreateModel([FromBody] string modelPathclassPath)  //try-catch
        {
            string modelPath = modelPathclassPath.Split("&")[0];
            string classPath = modelPathclassPath.Split("&")[1];

            model = new NNModel(modelPath, classPath);
            return("SUCCESSFULLY CREATED MODEL: OK");
        }
Beispiel #12
0
        /// <summary>
        /// Initializes the Brain with the Model that it will use when selecting actions for
        /// the agents
        /// </summary>
        /// <param name="model"> The Barracuda model to load </param>
        /// <param name="actionSpec"> Description of the actions for the Agent.</param>
        /// <param name="inferenceDevice"> Inference execution device. CPU is the fastest
        /// option for most of ML Agents models. </param>
        /// <param name="seed"> The seed that will be used to initialize the RandomNormal
        /// and Multinomial objects used when running inference.</param>
        /// <exception cref="UnityAgentsException">Throws an error when the model is null
        /// </exception>
        public ModelRunner(
            NNModel model,
            ActionSpec actionSpec,
            InferenceDevice inferenceDevice,
            int seed = 0)
        {
            Model barracudaModel;

            m_Model           = model;
            m_ModelName       = model.name;
            m_InferenceDevice = inferenceDevice;
            m_TensorAllocator = new TensorCachingAllocator();
            if (model != null)
            {
#if BARRACUDA_VERBOSE
                m_Verbose = true;
#endif

                D.logEnabled = m_Verbose;

                barracudaModel = ModelLoader.Load(model);
                WorkerFactory.Type executionDevice;
                switch (inferenceDevice)
                {
                case InferenceDevice.CPU:
                    executionDevice = WorkerFactory.Type.CSharp;
                    break;

                case InferenceDevice.GPU:
                    executionDevice = WorkerFactory.Type.ComputePrecompiled;
                    break;

                case InferenceDevice.Burst:
                    executionDevice = WorkerFactory.Type.CSharpBurst;
                    break;

                case InferenceDevice.Default:     // fallthrough
                default:
                    executionDevice = WorkerFactory.Type.CSharpBurst;
                    break;
                }
                m_Engine = WorkerFactory.CreateWorker(executionDevice, barracudaModel, m_Verbose);
            }
            else
            {
                barracudaModel = null;
                m_Engine       = null;
            }

            m_InferenceInputs = barracudaModel.GetInputTensors();
            m_OutputNames     = barracudaModel.GetOutputNames();
            m_TensorGenerator = new TensorGenerator(
                seed, m_TensorAllocator, m_Memories, barracudaModel);
            m_TensorApplier = new TensorApplier(
                actionSpec, seed, m_TensorAllocator, m_Memories, barracudaModel);
            m_InputsByName     = new Dictionary <string, Tensor>();
            m_InferenceOutputs = new List <TensorProxy>();
        }
Beispiel #13
0
        /// <summary>
        /// Load the NNModel file from the specified path, and give it to the attached agent.
        /// </summary>
        void OverrideModel()
        {
            bool   overrideOk    = false;
            string overrideError = null;

            m_Agent.LazyInitialize();
            var bp           = m_Agent.GetComponent <BehaviorParameters>();
            var behaviorName = bp.BehaviorName;

            NNModel nnModel = null;

            try
            {
                nnModel = GetModelForBehaviorName(behaviorName);
            }
            catch (Exception e)
            {
                overrideError = $"Exception calling GetModelForBehaviorName: {e}";
            }

            if (nnModel == null)
            {
                if (string.IsNullOrEmpty(overrideError))
                {
                    overrideError =
                        $"Didn't find a model for behaviorName {behaviorName}. Make " +
                        "sure the behaviorName is set correctly in the commandline " +
                        "and that the model file exists";
                }
            }
            else
            {
                var modelName = nnModel != null ? nnModel.name : "<null>";
                Debug.Log($"Overriding behavior {behaviorName} for agent with model {modelName}");
                try
                {
                    m_Agent.SetModel(GetOverrideBehaviorName(behaviorName), nnModel);
                    overrideOk = true;
                }
                catch (Exception e)
                {
                    overrideError = $"Exception calling Agent.SetModel: {e}";
                }
            }

            if (!overrideOk && m_QuitOnLoadFailure)
            {
                if (!string.IsNullOrEmpty(overrideError))
                {
                    Debug.LogWarning(overrideError);
                }
                Application.Quit(1);
#if UNITY_EDITOR
                EditorApplication.isPlaying = false;
#endif
            }
        }
        /// <inheritdoc />
        public BarracudaPolicy(
            BrainParameters brainParameters,
            NNModel model,
            InferenceDevice inferenceDevice)
        {
            var modelRunner = Academy.Instance.GetOrCreateModelRunner(model, brainParameters, inferenceDevice);

            m_ModelRunner = modelRunner;
        }
 /// <summary>
 /// Updates the Model for the agent. Any model currently assigned to the
 /// agent will be replaced with the provided one. If the arguments are
 /// identical to the current parameters of the agent, the model will
 /// remain unchanged.
 /// </summary>
 /// <param name="behaviorName"> The identifier of the behavior. This
 /// will categorize the agent when training.
 /// </param>
 /// <param name="model"> The model to use for inference.</param>
 /// <param name = "inferenceDevide"> Define on what device the model
 /// will be run.</param>
 public void GiveModel(
     string behaviorName,
     NNModel model,
     InferenceDevice inferenceDevice = InferenceDevice.CPU)
 {
     m_PolicyFactory.GiveModel(behaviorName, model, inferenceDevice);
     m_Brain?.Dispose();
     m_Brain = m_PolicyFactory.GeneratePolicy(Heuristic);
 }
 /// <summary>
 /// Updates the model and related details for this behavior.
 /// </summary>
 /// <param name="newBehaviorName">New name for the behavior.</param>
 /// <param name="model">New neural network model for this behavior.</param>
 /// <param name="inferenceDevice">New inference device for this behavior.</param>
 public void GiveModel(
     string newBehaviorName,
     NNModel model,
     InferenceDevice inferenceDevice = InferenceDevice.CPU)
 {
     m_Model           = model;
     m_InferenceDevice = inferenceDevice;
     m_BehaviorName    = newBehaviorName;
 }
Beispiel #17
0
    /// <summary>
    /// Set the agents to use the Connectivity brain
    /// </summary>
    private void SetConnectivityModel()
    {
        NNModel model = Resources.Load <NNModel>("Models/ConfigurablePart_Connectivity");

        foreach (var agent in _agents)
        {
            agent.SetModel("ConfigurablePart", model);
        }
    }
Beispiel #18
0
    void Awake()
    {
        var agents = GetComponent <UnityBrainServer>().m_RemoteAgents;

        try
        {
            DirectoryInfo dir   = new DirectoryInfo(m_BrainFileFolderName);
            FileInfo[]    files = dir.GetFiles("*.nn");
            if (files.Length > 1)
            {
                string warningText = "=====\nFATAL ERROR:\nFound more than one brain file in folder '" + m_BrainFileFolderName + "'\n=====";
                Exit(warningText);
                return;
            }

            else if (files.Length == 0)
            {
                string warningText = "=====\nFATAL ERROR:\nCould not find a brain file in folder '" + m_BrainFileFolderName + "'\n=====";
                Exit(warningText);
                return;
            }

            m_BehaviorNameOverrides.Clear();
            m_BehaviorNameOverrides[m_BehaviourName] = m_BrainFileFolderName + "/" + files[0].Name;

            NNModel nnModel = GetModelForBehaviorName(m_BehaviourName);
            if (nnModel != null)
            {
                Debug.Log("No brain file");
            }

            var name = GetOverrideBehaviorName(m_BehaviourName);
            Debug.Log("name: " + name);

            foreach (var agent in agents)
            {
                agent.LazyInitialize();
                // Need to give the sensors some data before setting up a new model
                // because the process of setting a new model reads the sensors once.
                float[] lowerObservations = new float[m_ObservationsPerSensor];
                float[] upperObservations = new float[m_ObservationsPerSensor];
                agent.SetObservations(lowerObservations, upperObservations);
                agent.SetModel(name, nnModel);
            }

            string successText = "=====\nBrain file '" + files[0].Name + "' found and taken into use\n=====";
            m_WarningText.text = successText;
            Debug.Log("\n\n" + successText + "\n\n");
        }
        catch
        {
            string warningText = "=====\nFATAL ERROR:\nFolder '" + m_BrainFileFolderName + "' not found\n=====";
            Exit(warningText);
            return;
        }
    }
        public void SetUp()
        {
            continuousONNXModel = (NNModel)AssetDatabase.LoadAssetAtPath(k_continuousONNXPath, typeof(NNModel));
            var go = new GameObject("SensorA");

            sensor_21_20_3        = go.AddComponent <Test3DSensorComponent>();
            sensor_21_20_3.Sensor = new Test3DSensor("SensorA", 21, 20, 3);
            sensor_20_22_3        = go.AddComponent <Test3DSensorComponent>();
            sensor_20_22_3.Sensor = new Test3DSensor("SensorB", 20, 22, 3);
        }
Beispiel #20
0
        /// <inheritdoc />
        public BarracudaPolicy(
            ActionSpec actionSpec,
            NNModel model,
            InferenceDevice inferenceDevice)
        {
            var modelRunner = Academy.Instance.GetOrCreateModelRunner(model, actionSpec, inferenceDevice);

            m_ModelRunner = modelRunner;
            m_ActionSpec  = actionSpec;
        }
 public void SetUp()
 {
     continuous2vis8vec2actionModel = (NNModel)AssetDatabase.LoadAssetAtPath(k_continuous2vis8vec2actionPath, typeof(NNModel));
     discrete1vis0vec_2_3action_recurrModel = (NNModel)AssetDatabase.LoadAssetAtPath(k_discrete1vis0vec_2_3action_recurrModelPath, typeof(NNModel));
     var go = new GameObject("SensorA");
     sensor_21_20_3 = go.AddComponent<Test3DSensorComponent>();
     sensor_21_20_3.Sensor = new Test3DSensor("SensorA", 21, 20, 3);
     sensor_20_22_3 = go.AddComponent<Test3DSensorComponent>();
     sensor_20_22_3.Sensor = new Test3DSensor("SensorA", 20, 22, 3);
 }
 private void SelectVSRLModel(string model_name)
 {
     if (VSRL_Models_dic.ContainsKey(model_name))
     {
         VSRL_Current_Model = VSRL_Models_dic[model_name];
     }
     else
     {
         VSRL_Current_Model = VSRL_Models[0];
     }
 }
        /// <inheritdoc />
        public BarracudaPolicy(
            ActionSpec actionSpec,
            NNModel model,
            InferenceDevice inferenceDevice)
        {
            var modelRunner = Academy.Instance.GetOrCreateModelRunner(model, actionSpec, inferenceDevice);

            m_ModelRunner = modelRunner;
            actionSpec.CheckNotHybrid();
            m_SpaceType = actionSpec.NumContinuousActions > 0 ? SpaceType.Continuous : SpaceType.Discrete;
        }
        public BarracudaWorker(NNModel nnModel, WorkerFactory.Type type)
        {
            bool verbose = false;

            model  = ModelLoader.Load(nnModel, verbose);
            worker = WorkerFactory.CreateWorker(type, model, verbose);

            var kernels = ComputeShaderSingleton.Instance.kernels;

            ops = new PrecompiledComputeOps(kernels, kernels[0]);
        }
    public NNHandler(NNModel nnmodel)
    {
        model = ModelLoader.Load(nnmodel);
#if UNITY_WEBGL && !UNITY_EDITOR
        Debug.Log("Worker:CPU");
        worker = WorkerFactory.CreateWorker(WorkerFactory.Type.CSharpBurst, model); // CPU
#else
        Debug.Log("Worker:GPU");
        worker = WorkerFactory.CreateWorker(WorkerFactory.Type.ComputePrecompiled, model); // GPU
#endif
    }
Beispiel #26
0
 /// <summary>
 /// Creates or retrieves an existing ModelRunner that uses the same
 /// NNModel and the InferenceDevice as provided.
 /// </summary>
 /// <param name="model">The NNModel the ModelRunner must use.</param>
 /// <param name="actionSpec"> Description of the actions for the Agent.</param>
 /// <param name="inferenceDevice">
 /// The inference device (CPU or GPU) the ModelRunner will use.
 /// </param>
 /// <returns> The ModelRunner compatible with the input settings.</returns>
 internal ModelRunner GetOrCreateModelRunner(
     NNModel model, ActionSpec actionSpec, InferenceDevice inferenceDevice)
 {
     var modelRunner = m_ModelRunners.Find(x => x.HasModel(model, inferenceDevice));
     if (modelRunner == null)
     {
         modelRunner = new ModelRunner(model, actionSpec, inferenceDevice, m_InferenceSeed);
         m_ModelRunners.Add(modelRunner);
         m_InferenceSeed++;
     }
     return modelRunner;
 }
Beispiel #27
0
        static void Main(string[] args)
        {
            //Console.WriteLine("If you want to stop recognition press ESС");
            string img    = Console.ReadLine();
            string curDir = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.Parent.Parent.FullName;

            NNModel Mnist = new NNModel(Path.Combine(curDir, "mnist-8.onnx"), Path.Combine(curDir, "classlabel.txt"));

            Mnist.MessageToUser += PrintMessageToUser;
            Mnist.OutputResult  += PrintResult;
            var t = Task.Run(() => { return(Mnist.MakePrediction()); }).Result;
        }
Beispiel #28
0
        /// <inheritdoc />
        public BarracudaPolicy(
            BrainParameters brainParameters,
            NNModel model,
            InferenceDevice inferenceDevice)
        {
            var aca = Object.FindObjectOfType <Academy>();

            aca.LazyInitialization();
            var modelRunner = aca.GetOrCreateModelRunner(model, brainParameters, inferenceDevice);

            m_ModelRunner = modelRunner;
        }
Beispiel #29
0
        /// <summary>
        /// Generate an InferenceEvent for the model.
        /// </summary>
        /// <param name="nnModel"></param>
        /// <param name="behaviorName"></param>
        /// <param name="inferenceDevice"></param>
        /// <param name="sensors"></param>
        /// <param name="actionSpec"></param>
        /// <param name="actuators"></param>
        /// <returns></returns>
        internal static InferenceEvent GetEventForModel(
            NNModel nnModel,
            string behaviorName,
            InferenceDevice inferenceDevice,
            IList <ISensor> sensors,
            ActionSpec actionSpec,
            IList <IActuator> actuators
            )
        {
            var barracudaModel = ModelLoader.Load(nnModel);
            var inferenceEvent = new InferenceEvent();

            // Hash the behavior name so that there's no concern about PII or "secret" data being leaked.
            inferenceEvent.BehaviorName = AnalyticsUtils.Hash(behaviorName);

            inferenceEvent.BarracudaModelSource   = barracudaModel.IrSource;
            inferenceEvent.BarracudaModelVersion  = barracudaModel.IrVersion;
            inferenceEvent.BarracudaModelProducer = barracudaModel.ProducerName;
            inferenceEvent.MemorySize             = (int)barracudaModel.GetTensorByName(TensorNames.MemorySize)[0];
            inferenceEvent.InferenceDevice        = (int)inferenceDevice;

            if (barracudaModel.ProducerName == "Script")
            {
                // .nn files don't have these fields set correctly. Assign some placeholder values.
                inferenceEvent.BarracudaModelSource   = "NN";
                inferenceEvent.BarracudaModelProducer = "tensorflow_to_barracuda.py";
            }

#if UNITY_EDITOR
            var barracudaPackageInfo = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(Tensor).Assembly);
            inferenceEvent.BarracudaPackageVersion = barracudaPackageInfo.version;
#else
            inferenceEvent.BarracudaPackageVersion = null;
#endif

            inferenceEvent.ActionSpec       = EventActionSpec.FromActionSpec(actionSpec);
            inferenceEvent.ObservationSpecs = new List <EventObservationSpec>(sensors.Count);
            foreach (var sensor in sensors)
            {
                inferenceEvent.ObservationSpecs.Add(EventObservationSpec.FromSensor(sensor));
            }

            inferenceEvent.ActuatorInfos = new List <EventActuatorInfo>(actuators.Count);
            foreach (var actuator in actuators)
            {
                inferenceEvent.ActuatorInfos.Add(EventActuatorInfo.FromActuator(actuator));
            }

            inferenceEvent.TotalWeightSizeBytes = GetModelWeightSize(barracudaModel);
            inferenceEvent.ModelHash            = GetModelHash(barracudaModel);
            return(inferenceEvent);
        }
Beispiel #30
0
    public override void Initialize()
    {
        var modelOverrider = GetComponent <ModelOverrider>();

        if (modelOverrider.HasOverrides)
        {
            CaptureArtifact        = modelOverrider.GetModelForBehaviorName(fighterCaptureBehavior);
            fighterCaptureBehavior = ModelOverrider.GetOverrideBehaviorName(fighterCaptureBehavior);

            ReturnArtifact        = modelOverrider.GetModelForBehaviorName(fighterReturnBehavior);
            fighterReturnBehavior = ModelOverrider.GetOverrideBehaviorName(fighterReturnBehavior);
        }
    }