Example #1
0
        static bool ParseArgs(string[] args)
        {
            if (args.Length < 2)
            {
                return(false);
            }
            // get the model file
            _modelPath = args[0];
            // get the image file
            _imagePath = args[1];
            // did they pass a fourth arg?

            if (args.Length > 2)
            {
                string deviceName = args[2];
                if (deviceName == "cpu")
                {
                    _deviceKind = LearningModelDeviceKind.Cpu;
                }
                else if (deviceName == "directx")
                {
                    _deviceKind = LearningModelDeviceKind.DirectX;
                }
            }
            return(true);
        }
Example #2
0
        public static async Task <Model> CreateModelAsync(string filename, bool gpu)
        {
            Log.WriteLine("creating model");
            var file = await AsyncHelper.AsAsync(StorageFile.GetFileFromPathAsync(filename));

            Log.WriteLine("have file");
            var learningModel = await AsyncHelper.AsAsync(LearningModel.LoadFromStorageFileAsync(file));

            Log.WriteLine("loaded model");
            Model model = new Model();

            model._model = learningModel;
            LearningModelDeviceKind kind = LearningModelDeviceKind.Cpu;

            if (gpu)
            {
                Log.WriteLine("using GPU");
                kind = LearningModelDeviceKind.DirectXHighPerformance;
            }
            else
            {
                Log.WriteLine("using CPU");
            }
            model._device  = new LearningModelDevice(kind);
            model._session = new LearningModelSession(model._model, model._device);
            Log.WriteLine("returning model now");
            return(model);
        }
        private async Task LoadModelAsync()
        {
            Debug.Write("LoadModelBegin | ");

            Debug.Write("LoadModel Lock | ");

            _binding?.Clear();
            _session?.Dispose();

            StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_appModel.ModelSource}.onnx"));

            _learningModel = await LearningModel.LoadFromStorageFileAsync(modelFile);

            _inferenceDeviceSelected = UseGpu ? LearningModelDeviceKind.DirectX : LearningModelDeviceKind.Cpu;

            // Lock so can't create a new session or binding while also being disposed
            lock (_processLock)
            {
                _session = new LearningModelSession(_learningModel, new LearningModelDevice(_inferenceDeviceSelected));
                _binding = new LearningModelBinding(_session);
            }

            debugModelIO();
            _inputImageDescription  = _learningModel.InputFeatures.ToList().First().Name;
            _outputImageDescription = _learningModel.OutputFeatures.ToList().First().Name;

            Debug.Write("LoadModel Unlock\n");
        }
Example #4
0
        public MainPage()
        {
            ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

            localSettings.Values["valorIdGroup"] = "1";
            this.InitializeComponent();
            LearningModelDeviceKindSelected = LearningModelDeviceKind.Default;

            var modelosKind = new List <LearningModelDeviceKind>()
            {
                LearningModelDeviceKind.Cpu,
                LearningModelDeviceKind.Default,
                LearningModelDeviceKind.DirectX,
                LearningModelDeviceKind.DirectXHighPerformance,
                LearningModelDeviceKind.DirectXMinPower
            };

            foreach (var item in modelosKind)
            {
                learningModelDeviceKinds.Add(item);
            }
            NetworkInformation.NetworkStatusChanged += cambioConection;
            IniciarModelo().Wait(1000);

            ObtenerVideoDevices();



            if (HayConectividad.Conectividad())
            {
                Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => {
                    btnIniciarStream.IsEnabled = true;
                });
            }
            else
            {
                Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => {
                    btnIniciarStream.IsEnabled = false;
                });
            }


            listaCaras.ItemsSource = listCaras;
            if (localSettings.Values["apiKey"] as string == "")
            {
                this.btnIniciarStream.IsEnabled = false;
            }
            else
            {
                this.btnIniciarStream.IsEnabled = true;
            }
            ShutdownWebCam();
            btnTomarFoto.IsEnabled  = false;
            this.currentState       = ScenarioState.Idle;
            App.Current.Suspending += this.OnSuspending;

            App.Current.LeavingBackground += apagarCamara;
        }
        public void SetEncodingProperties(VideoEncodingProperties encodingProperties, IDirect3DDevice device)
        {
            currentEncodingProperties = encodingProperties;

            canvasDevice = device != null?CanvasDevice.CreateFromDirect3D11Device(device) : CanvasDevice.GetSharedDevice();

            parser        = new TinyYoloParser();
            filteredBoxes = new List <BoundingBox>();

            if (model == null)
            {
                // Use the appropriate option
                if (device == null)
                {
                    // startup WinML using CPU
                    detectedDeviceKind = LearningModelDeviceKind.Cpu;
                }
                else
                {
                    // Startup WinML using DirectX

                    // IF the frame rate is really high, we can probably expect a higher powered device
                    var frames   = encodingProperties.FrameRate.Numerator;
                    var timeSpan = encodingProperties.FrameRate.Denominator;
                    var ratio    = timeSpan / frames;

                    if (ratio > 0.04)
                    {
                        // Greater than 30 frames a second
                        detectedDeviceKind = LearningModelDeviceKind.DirectXHighPerformance;
                    }
                    else if (ratio > 0.01)
                    {
                        // If 30 frames a second or less, set expectations for WinML about what power is available
                        detectedDeviceKind = LearningModelDeviceKind.DirectX;
                    }
                    else
                    {
                        detectedDeviceKind = LearningModelDeviceKind.DirectXMinPower;
                    }
                }

                Debug.WriteLine($"Warning: LearningDeviceKind is set to {detectedDeviceKind}.");

                frameProcessingTimer = ThreadPoolTimer.CreatePeriodicTimer(EvaluateVideoFrame, poolTimerInterval);
            }
        }
        public static async Task <Model> CreateModelAsync(string filename)
        {
            Log.WriteLine("creating model");
            var file = await AsyncHelper.SyncFromAsync(StorageFile.GetFileFromPathAsync(filename), "file");

            Log.WriteLine("have file");
            var learningModel = await AsyncHelper.SyncFromAsync(LearningModel.LoadFromStorageFileAsync(file), "learningModel");

            Log.WriteLine("loaded model");
            Model model = new Model();

            model._model = learningModel;
            //LearningModelDeviceKind kind = LearningModelDeviceKind.DirectXHighPerformance;
            LearningModelDeviceKind kind = LearningModelDeviceKind.Cpu;

            model._device  = new LearningModelDevice(kind);
            model._session = new LearningModelSession(model._model, model._device);
            Log.WriteLine("returning model");
            return(model);
        }
Example #7
0
        private async void lstBoxModelo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _model   = null;
            _session = null;
            var elementoSelected = ((Windows.UI.Xaml.Controls.Primitives.Selector)sender).SelectedIndex;

            switch (elementoSelected)
            {
            case 5:
                LearningModelDeviceKindSelected = LearningModelDeviceKind.Cpu;
                await IniciarModelo();

                break;

            case 1:
                LearningModelDeviceKindSelected = LearningModelDeviceKind.Default;
                await IniciarModelo();

                break;

            case 2:
                LearningModelDeviceKindSelected = LearningModelDeviceKind.DirectX;
                await IniciarModelo();

                break;

            case 3:
                LearningModelDeviceKindSelected = LearningModelDeviceKind.DirectXHighPerformance;
                await IniciarModelo();

                break;

            case 4:
                LearningModelDeviceKindSelected = LearningModelDeviceKind.DirectXMinPower;
                await IniciarModelo();

                break;
            }
        }
Example #8
0
        /// <summary>
        /// Load the labels and model and initialize WinML
        /// </summary>
        /// <returns></returns>
        private async Task LoadModelAsync(string modelFileName)
        {
            Debug.WriteLine("LoadModelAsync");
            _evaluationLock.Wait();
            {
                m_binding       = null;
                m_model         = null;
                m_session       = null;
                _isReadyForEval = false;

                try
                {
                    // Start stopwatch
                    _perfStopwatch.Restart();

                    // Load Model
                    StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{modelFileName}.onnx"));

                    m_model = await LearningModel.LoadFromStorageFileAsync(modelFile);

                    // Stop stopwatch
                    _perfStopwatch.Stop();

                    // Setting preferred inference device given user's intent
                    m_inferenceDeviceSelected = _useGPU ? LearningModelDeviceKind.DirectXHighPerformance : LearningModelDeviceKind.Cpu;
                    m_session = new LearningModelSession(m_model, new LearningModelDevice(m_inferenceDeviceSelected));

                    // Debugging logic to see the input and output of ther model and retrieve dimensions of input/output variables
                    // ### DEBUG ###
                    foreach (var inputF in m_model.InputFeatures)
                    {
                        Debug.WriteLine($"input | kind:{inputF.Kind}, name:{inputF.Name}, type:{inputF.GetType()}");
                        int i = 0;
                        ImageFeatureDescriptor  imgDesc = inputF as ImageFeatureDescriptor;
                        TensorFeatureDescriptor tfDesc  = inputF as TensorFeatureDescriptor;
                        m_inWidth  = (uint)(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width);
                        m_inHeight = (uint)(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height);
                        m_inName   = inputF.Name;

                        Debug.WriteLine($"N: {(imgDesc == null ? tfDesc.Shape[0] : 1)}, " +
                                        $"Channel: {(imgDesc == null ? tfDesc.Shape[1].ToString() : imgDesc.BitmapPixelFormat.ToString())}, " +
                                        $"Height:{(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height)}, " +
                                        $"Width: {(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width)}");
                    }
                    foreach (var outputF in m_model.OutputFeatures)
                    {
                        Debug.WriteLine($"output | kind:{outputF.Kind}, name:{outputF.Name}, type:{outputF.GetType()}");
                        int i = 0;
                        ImageFeatureDescriptor  imgDesc = outputF as ImageFeatureDescriptor;
                        TensorFeatureDescriptor tfDesc  = outputF as TensorFeatureDescriptor;
                        m_outWidth  = (uint)(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width);
                        m_outHeight = (uint)(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height);
                        m_outName   = outputF.Name;

                        Debug.WriteLine($"N: {(imgDesc == null ? tfDesc.Shape[0] : 1)}, " +
                                        $"Channel: {(imgDesc == null ? tfDesc.Shape[1].ToString() : imgDesc.BitmapPixelFormat.ToString())}, " +
                                        $"Height:{(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height)}, " +
                                        $"Width: {(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width)}");
                    }
                    // ### END OF DEBUG ###

                    // Create output frame
                    _outputFrame?.Dispose();
                    _outputFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)m_outWidth, (int)m_outHeight);

                    Debug.WriteLine($"Elapsed time: {_perfStopwatch.ElapsedMilliseconds} ms");

                    _isReadyForEval = true;
                }
                catch (Exception ex)
                {
                    NotifyUser($"error: {ex.Message}", NotifyType.ErrorMessage);
                    Debug.WriteLine($"error: {ex.Message}");
                }
            }
            _evaluationLock.Release();
        }
 public void UpdateSession(LearningModelDeviceKind kind)
 {
     _device  = new LearningModelDevice(kind);
     _session = new LearningModelSession(_model, _device);
 }