private LearningModelSession CreateLearningModelSession(LearningModel model, Nullable <LearningModelDeviceKind> kind = null)
        {
            var device  = new LearningModelDevice(kind ?? SelectedDeviceKind);
            var options = new LearningModelSessionOptions()
            {
                CloseModelOnSessionCreation = true // Close the model to prevent extra memory usage
            };
            var session = new LearningModelSession(model, device, options);

            return(session);
        }
Пример #2
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);
        }
        private void DecryptAndEvauluate()
        {
            // Load the encrypted model.
            // The encrypted model (encrypted.onnx) is embedded as a resource in
            // the native binary: WinMLSamplesGalleryNative.dll.
            var inferenceModel      = WinMLSamplesGalleryNative.EncryptedModels.LoadEncryptedResource(DecryptionKey.Password);
            var postProcessingModel = TensorizationModels.SoftMaxThenTopK(10);

            // Update the status
            var isModelDecrypted = inferenceModel != null;

            UpdateStatus(isModelDecrypted);

            // If loading the decrypted model failed (ie: due to an invalid key/password),
            // then skip performing evaluate.
            if (!isModelDecrypted)
            {
                return;
            }

            // Draw the image to classify in the Image control
            var decoder = ImageHelper.CreateBitmapDecoderFromPath("ms-appx:///InputData/hummingbird.jpg");

            // Create sessions
            var device  = new LearningModelDevice(LearningModelDeviceKind.Cpu);
            var options = new LearningModelSessionOptions()
            {
                CloseModelOnSessionCreation = true // Close the model to prevent extra memory usage
            };
            var inferenceSession      = new LearningModelSession(inferenceModel, device, options);
            var postProcessingSession = new LearningModelSession(postProcessingModel, device, options);

            // Classify the current image
            var softwareBitmap = decoder.GetSoftwareBitmapAsync().GetAwaiter().GetResult();
            var input          = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);

            // Inference
            var inferenceResults = Evaluate(inferenceSession, input);
            var inferenceOutput  = inferenceResults.Outputs.First().Value;

            // PostProcess
            var postProcessedOutputs = Evaluate(postProcessingSession, inferenceOutput);
            var topKValues           = (TensorFloat)postProcessedOutputs.Outputs["TopKValues"];
            var topKIndices          = (TensorInt64Bit)postProcessedOutputs.Outputs["TopKIndices"];

            // Return results
            var probabilities = topKValues.GetAsVectorView();
            var indices       = topKIndices.GetAsVectorView();
            var labels        = indices.Select((index) => ClassificationLabels.ImageNet[index]);

            // Render the classification and probabilities
            RenderInferenceResults(labels, probabilities);
        }
Пример #4
0
        public static async Task <ScoringModel> CreateFromStreamAsync(IRandomAccessStreamReference stream, bool UseGpu = false)
        {
            ScoringModel learningModel = new ScoringModel();

            learningModel.model = await AsAsync(LearningModel.LoadFromStreamAsync(stream));

            var device = new LearningModelDevice(UseGpu ? LearningModelDeviceKind.DirectXHighPerformance : LearningModelDeviceKind.Cpu);

            learningModel.session = new LearningModelSession(learningModel.model, device);
            learningModel.binding = new LearningModelBinding(learningModel.session);
            return(learningModel);
        }
Пример #5
0
        public static async Task <classifierModel> CreateFromStreamAsync(IRandomAccessStreamReference stream)
        {
            classifierModel learningModel = new classifierModel();

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

            // Select GPU or another DirectX device to evaluate the model.
            LearningModelDevice device = new LearningModelDevice(LearningModelDeviceKind.DirectX);

            // Create the evaluation session with the model and device.
            learningModel.session = new LearningModelSession(learningModel.model, device);
            learningModel.binding = new LearningModelBinding(learningModel.session);
            return(learningModel);
        }
Пример #6
0
        private async Task <LearningModelSession> CreateLearningModelSession(LearningModel model, int batchSizeOverride = -1)
        {
            var deviceKind = DeviceComboBox.GetDeviceKind();
            var device     = new LearningModelDevice(deviceKind);
            var options    = new LearningModelSessionOptions();

            if (batchSizeOverride > 0)
            {
                options.BatchSizeOverride = (uint)batchSizeOverride;
            }
            var session = new LearningModelSession(model, device, options);

            return(session);
        }
        private LearningModelSession CreateLearningModelSession(LearningModel model)
        {
            var kind =
                (DeviceComboBox.SelectedIndex == 0) ?
                LearningModelDeviceKind.Cpu :
                LearningModelDeviceKind.DirectXHighPerformance;
            var device  = new LearningModelDevice(kind);
            var options = new LearningModelSessionOptions()
            {
                CloseModelOnSessionCreation = true              // Close the model to prevent extra memory usage
            };
            var session = new LearningModelSession(model, device, options);

            return(session);
        }
        public ObjectDetector()
        {
            this.InitializeComponent();

            dmlDevice = new LearningModelDevice(LearningModelDeviceKind.DirectX);
            cpuDevice = new LearningModelDevice(LearningModelDeviceKind.Cpu);

            var modelName = "yolov4.onnx";
            var modelPath = Path.Join(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Models", modelName);
            var model     = LearningModel.LoadFromFilePath(modelPath);

            _session = CreateLearningModelSession(model);

            initialized_ = true;
        }
        private void ChangeAdapter(object sender, RoutedEventArgs e)
        {
            var device_kind_str = adapter_options[AdapterListView.SelectedIndex];

            if (AdapterListView.SelectedIndex < 4)
            {
                device = new LearningModelDevice(
                    GetLearningModelDeviceKind(device_kind_str));
                toggleCodeSnippet(true);
            }
            else
            {
                device = WinMLSamplesGalleryNative.AdapterList.CreateLearningModelDeviceFromAdapter(device_kind_str);
                toggleCodeSnippet(false);
            }
        }
        /// <summary>
        /// If possible, retrieves a WinML LearningModelDevice that corresponds to an ISkillExecutionDevice
        /// </summary>
        /// <param name="executionDevice"></param>
        /// <returns></returns>
        private static LearningModelDevice GetWinMLDevice(ISkillExecutionDevice executionDevice)
        {
            switch (executionDevice.ExecutionDeviceKind)
            {
            case SkillExecutionDeviceKind.Cpu:
                return(new LearningModelDevice(LearningModelDeviceKind.Cpu));

            case SkillExecutionDeviceKind.Gpu:
            {
                var gpuDevice = executionDevice as SkillExecutionDeviceDirectX;
                return(LearningModelDevice.CreateFromDirect3D11Device(gpuDevice.Direct3D11Device));
            }

            default:
                throw new ArgumentException("Passing unsupported SkillExecutionDeviceKind");
            }
        }
        /// <summary>
        /// init a ML model
        /// </summary>
        /// <param name="file"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public async static Task CreateModelAsync(StorageFile file, IMachineLearningModel learningModel, bool _useCPU = false)
        {
            LearningModelDevice device = null;

            if (_useCPU)
            {
                device = new LearningModelDevice(LearningModelDeviceKind.Default);
            }
            else
            {
                device = new LearningModelDevice(LearningModelDeviceKind.DirectXHighPerformance);
            }
            learningModel.LearningModel = await LearningModel.LoadFromStreamAsync(file);

            learningModel.Session = new LearningModelSession(learningModel.LearningModel, device);
            learningModel.Binding = new LearningModelBinding(learningModel.Session);
        }
        public AdapterSelection()
        {
            this.InitializeComponent();
            adapter_options = new List <string> {
                "Cpu",
                "DirectX",
                "DirectXHighPerformance",
                "DirectXMinPower"
            };
            device = new LearningModelDevice(LearningModelDeviceKind.Cpu);
            selectedDeviceKind.Text = "Cpu";

            var adapters_arr = WinMLSamplesGalleryNative.AdapterList.GetAdapters();
            var adapters     = RemoveMicrosoftBasicRenderDriver(adapters_arr);

            adapter_options.AddRange(adapters);
            AdapterListView.ItemsSource = adapter_options;
        }
Пример #13
0
        public static async Task <MLModel> CreateFromStreamAsync(IRandomAccessStreamReference stream)
        {
            var device = new LearningModelDevice(LearningModelDeviceKind.Cpu);

            var model = new MLModel();
            var load  = LearningModel.LoadFromStreamAsync(stream);

            while (load.Status != Windows.Foundation.AsyncStatus.Completed)
            {
                Thread.Sleep(100);
            }
            model._model = load.GetResults();

            model._session = new LearningModelSession(model._model, device);
            model._binding = new LearningModelBinding(model._session);

            return(model);
        }
Пример #14
0
        public ImageEffects()
        {
            this.InitializeComponent();

            dmlDevice = new LearningModelDevice(LearningModelDeviceKind.DirectX);
            cpuDevice = new LearningModelDevice(LearningModelDeviceKind.Cpu);

            BasicGridView.SelectedIndex = 0;

            InitializePicker(ResizeToggleSplitButton, ResizePicker, 2);
            InitializePicker(OrientationToggleSplitButton, OrientationPicker);
            InitializePicker(PixelSwizzleToggleSplitButton, PixelSwizzlePicker);
            InitializePicker(BlurSharpenToggleSplitButton, BlurSharpenPicker);
            InitializePicker(ArtisticEffectsToggleSplitButton, ArtisticEffectsPicker);

            ContrastMaxSlider.Value             = .5;
            ContrastMinSlider.Value             = .5;
            ContrastToggleSplitButton.IsChecked = false;

            initialized_ = true;
            ApplyEffects();
        }
 public void UpdateSession(LearningModelDeviceKind kind)
 {
     _device  = new LearningModelDevice(kind);
     _session = new LearningModelSession(_model, _device);
 }
Пример #16
0
        public static async Task <Model> CreateFromStreamAsync(IRandomAccessStreamReference stream, LearningModelDevice deviceToRunOn)
        {
            Model learningModel = new Model();

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

            learningModel.session = new LearningModelSession(learningModel.model, deviceToRunOn);
            learningModel.binding = new LearningModelBinding(learningModel.session);
            return(learningModel);
        }