Last active
September 27, 2025 12:38
-
-
Save ynkdir/090769fd7690bed2ed5bb525c0816e6e to your computer and use it in GitHub Desktop.
Windows ML example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Load and predict with ONNX Runtime and a very simple model | |
| # https://onnxruntime.ai/docs/api/python/auto_examples/plot_load_and_predict.html | |
| # | |
| # Install | |
| # https://learn.microsoft.com/en-us/windows/ai/new-windows-ml/get-started?tabs=python> | |
| # pip install win32more | |
| # pip install --pre --index-url https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ORT-Nightly/pypi/simple/ --extra-index-url https://pypi.org/simple onnxruntime-winml | |
| import asyncio | |
| import numpy.random | |
| import onnxruntime as rt | |
| from onnxruntime.datasets import get_example | |
| from win32more.Microsoft.Windows.AI.MachineLearning import ExecutionProviderCatalog, ExecutionProviderReadyResultState | |
| async def setup(): | |
| catalog = ExecutionProviderCatalog.GetDefault() | |
| for provider in catalog.FindAllProviders(): | |
| r = await provider.EnsureReadyAsync() | |
| if r.Status != ExecutionProviderReadyResultState.Success: | |
| raise OSError(r.ExtendedError.Value, r.DiagnosticText) | |
| print(f"Provider {provider.Name} {provider.LibraryPath}") | |
| rt.register_execution_provider_library(provider.Name, provider.LibraryPath) | |
| async def main(): | |
| await setup() | |
| example1 = get_example("sigmoid.onnx") | |
| sess = rt.InferenceSession(example1, providers=rt.get_available_providers()) | |
| input_name = sess.get_inputs()[0].name | |
| print("input name", input_name) | |
| input_shape = sess.get_inputs()[0].shape | |
| print("input shape", input_shape) | |
| input_type = sess.get_inputs()[0].type | |
| print("input type", input_type) | |
| output_name = sess.get_outputs()[0].name | |
| print("output name", output_name) | |
| output_shape = sess.get_outputs()[0].shape | |
| print("output shape", output_shape) | |
| output_type = sess.get_outputs()[0].type | |
| print("output type", output_type) | |
| x = numpy.random.random((3, 4, 5)) | |
| x = x.astype(numpy.float32) | |
| res = sess.run([output_name], {input_name: x}) | |
| print(res) | |
| asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment