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");
        }
        // Loads the ML model file and creates LearningModel
        private async Task LoadModelAsync()
        {
            try
            {
                Debug.WriteLine($"*************** LoadModelAsync [START] ***************");
                Debug.WriteLine($" - Locating ONYX file");

                var modelFile = await StorageFile.GetFileFromApplicationUriAsync(ModelUri);

                Debug.WriteLine($" - {modelFile.DisplayName} file located, attempting to create LearningModel");
                Debug.WriteLine($" - Attempting to create LearningModel...");

                model = await LearningModel.LoadFromStorageFileAsync(modelFile);

                Debug.WriteLine($" - {model.Name} LearningModel successfully instantiated.");

                modelCreated = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine($" - Error: Problem loading model file or creating LearningModel: {ex.Message}");
                model        = null;
                modelCreated = false;
            }
            finally
            {
                Debug.WriteLine($"*************** LoadModelAsync [END] ***************");
            }
        }
Esempio n. 3
0
        // Create a model from an ONNX 1.2 file
        public static async Task <CustomVisionModel> CreateONNXModel(StorageFile file)
        {
            LearningModel learningModel = null;

            try
            {
                learningModel = await LearningModel.LoadFromStorageFileAsync(file);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            var inputFeatures = learningModel.InputFeatures;
            ImageFeatureDescriptor inputImageDescription = inputFeatures?.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Image) as ImageFeatureDescriptor;
            uint inputImageWidth = 0, inputImageHeight = 0;

            if (inputImageDescription != null)
            {
                inputImageHeight = inputImageDescription.Height;
                inputImageWidth  = inputImageDescription.Width;
            }

            return(new CustomVisionModel()
            {
                _learningModel = learningModel,
                _session = new LearningModelSession(learningModel),
                InputImageWidth = (int)inputImageWidth,
                InputImageHeight = (int)inputImageHeight
            });
        }
Esempio n. 4
0
        public static async Task <OnnxModel> CreateFromStreamAsync(StorageFile modelFile)
        {
            OnnxModel learningModel = new OnnxModel();

            learningModel.model = await LearningModel.LoadFromStorageFileAsync(modelFile);

            learningModel.session = new LearningModelSession(learningModel.model);
            learningModel.binding = new LearningModelBinding(learningModel.session);

            var inputFeature = learningModel.model.InputFeatures[0];

            learningModel.inName = inputFeature.Name;

            var inputTensorFeature = inputFeature as TensorFeatureDescriptor;

            learningModel.InWidth  = (int)inputTensorFeature.Shape[3];
            learningModel.InHeight = (int)inputTensorFeature.Shape[2];

            var outputFeature = learningModel.model.OutputFeatures[0];

            learningModel.outName = outputFeature.Name;

            var outputTensorFeature = outputFeature as TensorFeatureDescriptor;

            learningModel.OutWidth  = (int)outputTensorFeature.Shape[3];
            learningModel.OutHeight = (int)outputTensorFeature.Shape[2];

            Debug.Log(string.Format("Input Feature Name: {0}", learningModel.inName));
            Debug.Log(string.Format("Output Feature Name: {0}", learningModel.outName));
            Debug.Log(string.Format("Input Width, Height: {0}, {1}", learningModel.InWidth, learningModel.InHeight));
            Debug.Log(string.Format("Output Width, Height: {0}, {1}", learningModel.OutWidth, learningModel.OutHeight));
            return(learningModel);
        }
Esempio n. 5
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);
        }
        /// <summary>
        /// Load the label and model files
        /// </summary>
        /// <returns></returns>
        private async Task LoadModelAsync()
        {
            // just load the model one time.
            if (_model != null)
            {
                return;
            }

            StatusBlock.Text = $"Loading {_kModelFileName} ... patience ";

            try
            {
                // Parse labels from label json file.  We know the file's
                // entries are already sorted in order.
                var fileString = File.ReadAllText($"Assets/{_kLabelsFileName}");
                var fileDict   = JsonConvert.DeserializeObject <Dictionary <string, string> >(fileString);
                foreach (var kvp in fileDict)
                {
                    _labels.Add(kvp.Value);
                }

                // Load and create the model
                var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_kModelFileName}"));

                _model = await LearningModel.LoadFromStorageFileAsync(modelFile);

                // Create the evaluation session with the model and device
                _session = new LearningModelSession(_model, new LearningModelDevice(GetDeviceKind()));
            }
            catch (Exception ex)
            {
                StatusBlock.Text = $"error: {ex.Message}";
                _model           = null;
            }
        }
Esempio n. 7
0
        private async Task LoadAndEvaluateModelAsync(VideoFrame _inputFrame, string _modelFileName)
        {
            LearningModelBinding _binding     = null;
            VideoFrame           _outputFrame = null;
            LearningModelSession _session;

            try
            {
                //Load and create the model
                if (_model == null)
                {
                    var modelFile =
                        await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///{_modelFileName}"));

                    _model = await LearningModel.LoadFromStorageFileAsync(modelFile);
                }

                // Create the evaluation session with the model
                _session = new LearningModelSession(_model);

                // Get input and output features of the model
                var inputFeatures  = _model.InputFeatures.ToList();
                var outputFeatures = _model.OutputFeatures.ToList();

                // Create binding and then bind input/ output features
                _binding = new LearningModelBinding(_session);

                _inputImageDescriptor =
                    inputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor) as TensorFeatureDescriptor;

                _outputTensorDescriptor =
                    outputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor) as TensorFeatureDescriptor;

                TensorFloat       outputTensor = TensorFloat.Create(_outputTensorDescriptor.Shape);
                ImageFeatureValue imageTensor  = ImageFeatureValue.CreateFromVideoFrame(_inputFrame);

                // Bind inputs +outputs
                _binding.Bind(_inputImageDescriptor.Name, imageTensor);
                _binding.Bind(_outputTensorDescriptor.Name, outputTensor);


                // Evaluate and get the results
                var results = await _session.EvaluateAsync(_binding, "test");

                Debug.WriteLine("ResultsEvaluated: " + results.ToString());

                var outputTensorList = outputTensor.GetAsVectorView();
                var resultsList      = new List <float>(outputTensorList.Count);
                for (int i = 0; i < outputTensorList.Count; i++)
                {
                    resultsList.Add(outputTensorList[i]);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"error: {ex.Message}");
                _model = null;
            }
        }
Esempio n. 8
0
        public async Task Initialize()
        {
            // Load and create the model and session
            var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets//Network.onnx"));

            _learning_model = await LearningModel.LoadFromStorageFileAsync(modelFile);

            _session = new LearningModelSession(_learning_model);
        }
Esempio n. 9
0
        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="file">The ONNX file</param>
        public async Task Init(StorageFile file)
        {
            this.model = await LearningModel.LoadFromStorageFileAsync(file);

            this.session = new LearningModelSession(this.model);

            Debug.Assert(this.model.InputFeatures.Count == 1, "The number of input must be 1");
            Debug.Assert(this.model.OutputFeatures.Count == 1, "The number of output must be 1");
        }
Esempio n. 10
0
        public async Task <bool> IniciarModelo()
        {
            var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_kModelFileName}"));

            _model = await LearningModel.LoadFromStorageFileAsync(modelFile);

            _session = new LearningModelSession(_model, new LearningModelDevice(LearningModelDeviceKindSelected));

            return(true);
        }
Esempio n. 11
0
        public static async Task <MyCustomVisionModel> CreateFromStreamAsync(StorageFile stream)
        {
            MyCustomVisionModel learningModel = new MyCustomVisionModel();

            learningModel.model = await LearningModel.LoadFromStorageFileAsync(stream);

            learningModel.session = new LearningModelSession(learningModel.model);
            learningModel.binding = new LearningModelBinding(learningModel.session);
            return(learningModel);
        }
        public static async Task <CustomVisionModel> CreateOnnxModel(StorageFile file)
        {
            CustomVisionModel learningModel = new CustomVisionModel();

            learningModel.model = await LearningModel.LoadFromStorageFileAsync(file);

            learningModel.session = new LearningModelSession(learningModel.model);
            learningModel.binding = new LearningModelBinding(learningModel.session);
            return(learningModel);
        }
Esempio n. 13
0
        internal async Task InitModelAsync()
        {
            var model_file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets//Yolo.onnx"));

            _model = await LearningModel.LoadFromStorageFileAsync(model_file);

            var device = new LearningModelDevice(LearningModelDeviceKind.Cpu);

            _session = new LearningModelSession(_model, device);
            _binding = new LearningModelBinding(_session);
        }
Esempio n. 14
0
        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="file">The ONNX file</param>
        ///
        public async Task Init()
        {
            StorageFile ModelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/" + "model.onnx"));

            this.model = await LearningModel.LoadFromStorageFileAsync(ModelFile);

            this.session = new LearningModelSession(this.model);
            this.binding = new LearningModelBinding(this.session);

            Debug.Assert(this.model.InputFeatures.Count == 1, "The number of input must be 1");
            Debug.Assert(this.model.OutputFeatures.Count == 1, "The number of output must be 1");
        }
Esempio n. 15
0
File: Yolov2.cs Progetto: JRRN/Pabi
        public static async Task <LearningModelYolo> CreateFromStreamAsync()
        {
            var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///OnnxModel/yolov2.onnx"));

            var learningModel = new LearningModelYolo
            {
                Model = await LearningModel.LoadFromStorageFileAsync(modelFile)
            };

            learningModel.Session = new LearningModelSession(learningModel.Model);
            learningModel.Binding = new LearningModelBinding(learningModel.Session);
            return(learningModel);
        }
Esempio n. 16
0
        private async Task EvaluateImageAsync(string imagePath, string modelPath)
        {
            var selectedStorageFile = await StorageFile.GetFileFromPathAsync(imagePath);

            SoftwareBitmap softwareBitmap;

            using (var stream = await selectedStorageFile.OpenAsync(FileAccessMode.Read)) {
                // Create the decoder from the stream
                var decoder = await BitmapDecoder.CreateAsync(stream);

                // Get the SoftwareBitmap representation of the file in BGRA8 format
                softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8,
                                                        BitmapAlphaMode.Premultiplied);
            }

            // Encapsulate the image within a VideoFrame to be bound and evaluated
            var inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);

            if (_model == null)
            {
                var modelFile = await StorageFile.GetFileFromPathAsync(modelPath);

                _model = new SqueezeNetModel {
                    LearningModel = await LearningModel.LoadFromStorageFileAsync(modelFile)
                };
                _model.Session = new LearningModelSession(_model.LearningModel,
                                                          new LearningModelDevice(LearningModelDeviceKind.Default));
                _model.Binding = new LearningModelBinding(_model.Session);
            }

            if (_model == null)
            {
                return;
            }

            var input = new SqueezeNetInput {
                Image = ImageFeatureValue.CreateFromVideoFrame(inputImage)
            };

            try {
                var output = (SqueezeNetOutput)await _model.EvaluateAsync(input);

                var(label, probability) = output.classLabelProbs.FirstOrDefault();
                DispatchEvent(Result, probability + ", " + label);
            }
            catch (Exception ex) {
                Trace(ex.Message, ex.StackTrace);
            }
        }
Esempio n. 17
0
        public static async Task <SmartInkModel> CreateModelAsync(IStorageFile file, IList <string> tags)
        {
            try
            {
                SmartInkModel learningModel = new SmartInkModel();
                learningModel._model = await LearningModel.LoadFromStorageFileAsync(file);

                learningModel._session = new LearningModelSession(learningModel._model);
                learningModel._binding = new LearningModelBinding(learningModel._session);
                return(learningModel);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private async Task <bool> LoadModelAsync()
        {
            var modelStorageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(Constants.MODEL_PATH));

            try
            {
                _model = await LearningModel.LoadFromStorageFileAsync(modelStorageFile);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            // since we do not specify the device, we are using the default CPU option
            _session = new LearningModelSession(_model);

            List <ILearningModelFeatureDescriptor> inputFeatures;
            List <ILearningModelFeatureDescriptor> outputFeatures;

            if (_model.InputFeatures == null)
            {
                return(false);
            }
            else
            {
                inputFeatures = _model.InputFeatures.ToList();
            }

            if (_model.OutputFeatures == null)
            {
                return(false);
            }
            else
            {
                outputFeatures = _model.OutputFeatures.ToList();
            }

            _inputImageDescriptor =
                inputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor) as TensorFeatureDescriptor;

            _outputTensorDescriptor =
                outputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor) as TensorFeatureDescriptor;

            return(true);
        }
        // Create a model from an ONNX 1.2 file
        public static async Task <CustomVisionModel> CreateONNXModel(StorageFile file)
        {
            LearningModel learningModel = null;

            try
            {
                learningModel = await LearningModel.LoadFromStorageFileAsync(file);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(new CustomVisionModel()
            {
                _learningModel = learningModel,
                _session = new LearningModelSession(learningModel)
            });
        }
Esempio n. 20
0
        // Create a model from an ONNX 1.2 file
        public static async Task <ONNXModel> CreateONNXModel(StorageFile file)
        {
            LearningModel learningModel = null;

            try
            {
                learningModel = await LearningModel.LoadFromStorageFileAsync(file);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                throw e;
            }
            return(new ONNXModel()
            {
                _learningModel = learningModel,
                _session = new LearningModelSession(learningModel)
            });
        }
        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);
        }
Esempio n. 22
0
            public static async Task <OnnxModel> CreateOnnxModel(StorageFile file)
            {
                LearningModel learningModel = null;

                try
                {
                    learningModel = await LearningModel.LoadFromStorageFileAsync(file);
                }
                catch (Exception e)
                {
                    var exceptionStr = e.ToString();
                    System.Console.WriteLine(exceptionStr);
                    throw e;
                }
                return(new OnnxModel()
                {
                    _learningModel = learningModel,
                    _session = new LearningModelSession(learningModel)
                });
            }
Esempio n. 23
0
        public static async Task <CustomVisionModel> CreateFromStorageFile(StorageFile file)
        {
            CustomVisionModel learningModel = new CustomVisionModel();

            learningModel.model = await LearningModel.LoadFromStorageFileAsync(file);

            IReadOnlyList <ILearningModelFeatureDescriptor> input = learningModel.model.InputFeatures.ToList();
            MapFeatureDescriptor imgDesc = input[0] as MapFeatureDescriptor;

            TensorFeatureDescriptor tfDesc = input[0] as TensorFeatureDescriptor;

            learningModel.inputParameterName = input[0].Name;
            learningModel.inputWidth         = (int)tfDesc.Shape[2];
            learningModel.inputHeight        = (int)tfDesc.Shape[3];
            IReadOnlyList <ILearningModelFeatureDescriptor> output = learningModel.model.OutputFeatures.ToList();
            MapFeatureDescriptor    imgDesc1 = output[0] as MapFeatureDescriptor;
            TensorFeatureDescriptor tfDesc1  = output[0] as TensorFeatureDescriptor;

            learningModel.outputParameterName = output[0].Name;
            learningModel.session             = new LearningModelSession(learningModel.model);
            learningModel.binding             = new LearningModelBinding(learningModel.session);
            return(learningModel);
        }
Esempio n. 24
0
        public static async Task <TinyYoloV2Model> CreateModel(StorageFile file)
        {
            LearningModel model = await LearningModel.LoadFromStorageFileAsync(file);


            // todo investigate the input image description and the outputDescription.

            //Get input and output features of the model
            List <ILearningModelFeatureDescriptor> inputFeatures  = model.InputFeatures.ToList();
            List <ILearningModelFeatureDescriptor> outputFeatures = model.OutputFeatures.ToList();

            // Retrieve the first input feature which is an image
            var _inputImageDescription = inputFeatures.FirstOrDefault(
                feature => feature.Kind == LearningModelFeatureKind.Image) as ImageFeatureDescriptor;

            // Retrieve the first output feature which is a tensor
            var _outputImageDescription = outputFeatures.FirstOrDefault(
                feature => feature.Kind == LearningModelFeatureKind.Tensor) as TensorFeatureDescriptor;

            return(new TinyYoloV2Model()
            {
                learningModel = model
            });
        }
Esempio n. 25
0
        public static async Task <RoadSignDetectionMLModel> Createmlmodel(StorageFile file)
        {
            LearningModel learningModel = null;

            try
            {
                learningModel = await LearningModel.LoadFromStorageFileAsync(file);
            }
            catch (Exception e)
            {
                var exceptionStr = e.ToString();
                System.Console.WriteLine(exceptionStr);
                throw e;
            }
            var model = new RoadSignDetectionMLModel()
            {
                model   = learningModel,
                session = new LearningModelSession(learningModel),
            };

            model.binding = new LearningModelBinding(model.session);

            return(model);
        }
Esempio n. 26
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();
        }
Esempio n. 27
0
        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="file">The ONNX file</param>
        public async Task Init(StorageFile file)
        {
            this.model = await LearningModel.LoadFromStorageFileAsync(file);

            this.session = new LearningModelSession(this.model);
        }
Esempio n. 28
0
    public async Task LoadModelAsync(bool shouldUseGpu, bool resourceLoad)
    {
        try
        {
            // Parse labels from label file
            var labelsTextAsset = Resources.Load(LabelsFileName) as TextAsset;
            using (var streamReader = new StringReader(labelsTextAsset.text))
            {
                string line       = "";
                char[] charToTrim = { '\"', ' ' };
                while (streamReader.Peek() >= 0)
                {
                    line = streamReader.ReadLine();
                    line.Trim(charToTrim);
                    var indexAndLabel = line.Split(':');
                    if (indexAndLabel.Count() == 2)
                    {
                        _labels.Add(indexAndLabel[1]);
                    }
                }
            }

#if ENABLE_WINMD_SUPPORT
            if (resourceLoad)
            {
                // Load from Unity Resources via awkward UWP streams and initialize model
                using (var modelStream = new InMemoryRandomAccessStream())
                {
                    var dataWriter    = new DataWriter(modelStream);
                    var modelResource = Resources.Load(ModelFileName) as TextAsset;
                    dataWriter.WriteBytes(modelResource.bytes);
                    await dataWriter.StoreAsync();

                    var randomAccessStream = RandomAccessStreamReference.CreateFromStream(modelStream);

                    _model = await LearningModel.LoadFromStreamAsync(randomAccessStream);

                    var deviceKind = shouldUseGpu ? LearningModelDeviceKind.DirectXHighPerformance : LearningModelDeviceKind.Cpu;
                    _session = new LearningModelSession(_model, new LearningModelDevice(deviceKind));
                }
            }
            else
            {
                try
                {
                    var modelFile = await StorageFile.GetFileFromApplicationUriAsync(
                        new Uri($"ms-appx:///Data/StreamingAssets/SqueezeNet.onnx"));

                    _model = await LearningModel.LoadFromStorageFileAsync(modelFile);

                    var deviceKind = shouldUseGpu ? LearningModelDeviceKind.DirectXHighPerformance : LearningModelDeviceKind.Cpu;
                    _session = new LearningModelSession(_model, new LearningModelDevice(deviceKind));
                }
                catch (Exception e)
                {
                    var exceptionStr = e.ToString();
                    //StatusBlock.text = exceptionStr;
                }
            }

            // Get model input and output descriptions
            var inputImageDescription =
                _model.InputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Image)
                as ImageFeatureDescriptor;
            // Check if input is passed as image if not try to interpret it as generic tensor
            if (inputImageDescription != null)
            {
                InputWidth  = inputImageDescription.Width;
                InputHeight = inputImageDescription.Height;
            }
            else
            {
                var inputTensorDescription =
                    _model.InputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor)
                    as TensorFeatureDescriptor;
                InputWidth  = (uint)inputTensorDescription.Shape[3];
                InputHeight = (uint)inputTensorDescription.Shape[2];
            }

            _outputDescription =
                _model.OutputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor)
                as TensorFeatureDescriptor;
#endif
        }
        catch
        {
#if ENABLE_WINMD_SUPPORT
            _model = null;
#endif
            throw;
        }
    }