{"id":13720878,"url":"https://github.com/Microsoft/onnxjs","last_synced_at":"2025-05-07T13:30:47.593Z","repository":{"id":41485131,"uuid":"148708393","full_name":"microsoft/onnxjs","owner":"microsoft","description":"ONNX.js: run ONNX models using JavaScript","archived":true,"fork":false,"pushed_at":"2021-12-14T00:51:02.000Z","size":5127,"stargazers_count":1739,"open_issues_count":85,"forks_count":130,"subscribers_count":39,"default_branch":"master","last_synced_at":"2024-05-10T14:36:54.783Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/microsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-13T23:11:53.000Z","updated_at":"2024-05-10T14:36:31.000Z","dependencies_parsed_at":"2022-09-13T03:01:06.310Z","dependency_job_id":null,"html_url":"https://github.com/microsoft/onnxjs","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2Fonnxjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2Fonnxjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2Fonnxjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2Fonnxjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/microsoft","download_url":"https://codeload.github.com/microsoft/onnxjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224604603,"owners_count":17339170,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:01:09.267Z","updated_at":"2024-11-14T10:30:38.430Z","avatar_url":"https://github.com/microsoft.png","language":"TypeScript","readme":"[![npm version](https://badge.fury.io/js/onnxjs.svg)](https://badge.fury.io/js/onnxjs)\n[![GitHub version](https://badge.fury.io/gh/Microsoft%2Fonnxjs.svg)](https://badge.fury.io/gh/Microsoft%2Fonnxjs)\n[![ONNX.js CI - Windows CPU (Electron)](\u003chttps://dev.azure.com/onnxruntime/onnxjs/_apis/build/status/ONNX.js%20CI%20-%20Windows%20CPU%20(Electron)?label=Windows+CPU+(Electron)\u003e)](https://dev.azure.com/onnxruntime/onnxjs/_build/latest?definitionId=24)\n[![ONNX.js CI - Windows CPU (Node.js)](\u003chttps://dev.azure.com/onnxruntime/onnxjs/_apis/build/status/ONNX.js%20CI%20-%20Windows%20CPU%20(Node.js)?label=Windows+CPU+(Node.js)\u003e)](https://dev.azure.com/onnxruntime/onnxjs/_build/latest?definitionId=20)\n[![ONNX.js CI - Windows GPU (Chrome,Edge)](\u003chttps://dev.azure.com/onnxruntime/onnxjs/_apis/build/status/ONNX.js%20CI%20-%20Windows%20GPU%20(Chrome,Edge)?label=Windows+GPU+(Chrome%2CEdge)\u003e)](https://dev.azure.com/onnxruntime/onnxjs/_build/latest?definitionId=22)\n[![ONNX.js CI - Linux CPU (Node.js)](\u003chttps://dev.azure.com/onnxruntime/onnxjs/_apis/build/status/ONNX.js%20CI%20-%20Linux%20CPU%20(Node.js)?label=Linux+CPU+(Node.js)\u003e)](https://dev.azure.com/onnxruntime/onnxjs/_build/latest?definitionId=5)\n[![ONNX.js CI - BrowserStack (Suite0)](\u003chttps://dev.azure.com/onnxruntime/onnxjs/_apis/build/status/ONNX.js%20CI%20-%20BrowserStack%20(Suite0)?label=BrowserStack+(Suite0)\u003e)](https://dev.azure.com/onnxruntime/onnxjs/_build/latest?definitionId=17)\n\n## ONNX.js has been replaced by ONNX Runtime Web which offers enhanced user experience and improved performance. Please visit the following links to get more information:\n\n- [**Migration ONNX.js to ONNX Runtime Web**](./docs/migration-to-ort-web.md)\n- [ONNX Runtime Web (README)](https://github.com/microsoft/onnxruntime/tree/master/js/web)\n- [Get started with ORT for JavaScript - onnxruntime.ai](https://onnxruntime.ai/docs/get-started/with-javascript.html)\n- [Tutorials / Deploy on web - onnxruntime.ai](https://onnxruntime.ai/docs/tutorials/web/)\n- [ONNX Runtime JavaScript API](https://onnxruntime.ai/docs/api/js/index.html)\n\n# ONNX.js\n\nONNX.js is a Javascript library for running ONNX models on browsers and on Node.js.\n\nONNX.js has adopted WebAssembly and WebGL technologies for providing an optimized ONNX model inference runtime for both CPUs and GPUs.\n\n### Why ONNX models\n\nThe [Open Neural Network Exchange](http://onnx.ai/) (ONNX) is an open standard for representing machine learning models. The biggest advantage of ONNX is that it allows interoperability across different open source AI frameworks, which itself offers more flexibility for AI frameworks adoption. See [Getting ONNX Models](#Getting-ONNX-models).\n\n### Why ONNX.js\n\nWith ONNX.js, web developers can score pre-trained ONNX models directly on browsers with various benefits of reducing server-client communication and protecting user privacy, as well as offering install-free and cross-platform in-browser ML experience.\n\nONNX.js can run on both CPU and GPU. For running on CPU, [WebAssembly](https://developer.mozilla.org/en-US/docs/WebAssembly) is adopted to execute the model at near-native speed. Furthermore, ONNX.js utilizes [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) to provide a \"multi-threaded\" environment to parallelize data processing. Empirical evaluation shows very promising performance gains on CPU by taking full advantage of WebAssembly and Web Workers. For running on GPUs, a popular standard for accessing GPU capabilities - WebGL is adopted. ONNX.js has further adopted several novel optimization techniques for reducing data transfer between CPU and GPU, as well as some techniques to reduce GPU processing cycles to further push the performance to the maximum.\n\nSee [Compatibility](#Compatibility) and [Operators Supported](#Operators) for a list of platforms and operators ONNX.js currently supports.\n\n### Benchmarks\n\nBenchmarks have been run against the most prominent open source solutions in the same market. Below are the results collected for Chrome and Edge browsers on one sample machine (computations run on both CPU and GPU):\n\n![alt text](./docs/perf-resnet50.png \"Resnet50 Perf numbers\")\n\n\u003e NOTE:\n\u003e\n\u003e 1. Keras.js doesn't support WebGL usage on Edge\n\u003e 2. Keras.js and TensorFlow.js don't support WebAssembly usage on any browser\n\n\u003e The specs of the machine that was used to perform the benchmarking is listed below:\n\u003e\n\u003e - OS: Microsoft Windows 10 Enterprise Insider Preview\n\u003e - Model: HP Z240 Tower Workstation\n\u003e - Processor: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz, 3401 Mhz, 4 Core(s), 8 Logical Processor(s)\n\u003e - Installed Physical Memory (RAM): 32.0 GB\n\u003e - GPU make / Chip type: AMD FirePro W2100 / AMD FirePro SDI (0x6608)\n\u003e - GPU Memory (approx.): 18.0 GB\n\n### Demo\n\n[ONNX.js demo website](https://microsoft.github.io/onnxjs-demo/) shows the capabilities of ONNX.js. Check the [code](https://github.com/Microsoft/onnxjs-demo).\n\n## Getting Started\n\nThere are multiple ways to use ONNX.js in a project:\n\n### Using `\u003cscript\u003e` tag\n\nThis is the most straightforward way to use ONNX.js. The following HTML example shows how to use it:\n\n```html\n\u003chtml\u003e\n  \u003chead\u003e \u003c/head\u003e\n\n  \u003cbody\u003e\n    \u003c!-- Load ONNX.js --\u003e\n    \u003cscript src=\"https://cdn.jsdelivr.net/npm/onnxjs/dist/onnx.min.js\"\u003e\u003c/script\u003e\n    \u003c!-- Code that consume ONNX.js --\u003e\n    \u003cscript\u003e\n      // create a session\n      const myOnnxSession = new onnx.InferenceSession();\n      // load the ONNX model file\n      myOnnxSession.loadModel(\"./my-model.onnx\").then(() =\u003e {\n        // generate model input\n        const inferenceInputs = getInputs();\n        // execute the model\n        myOnnxSession.run(inferenceInputs).then((output) =\u003e {\n          // consume the output\n          const outputTensor = output.values().next().value;\n          console.log(`model output tensor: ${outputTensor.data}.`);\n        });\n      });\n    \u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\nRefer to [browser/Add](./examples/browser/add) for an example.\n\n### Using NPM and bundling tools\n\nModern browser based applications are usually built by frameworks like [Angular](https://angular.io/), [React](https://reactjs.org/), [Vue.js](https://vuejs.org/) and so on. This solution usually builds the source code into one or more bundle file(s). The following TypeScript example shows how to use ONNX.js in an async context:\n\n1. Import `Tensor` and `InferenceSession`.\n\n```ts\nimport { Tensor, InferenceSession } from \"onnxjs\";\n```\n\n2. Create an instance of `InferenceSession`.\n\n```ts\nconst session = new InferenceSession();\n```\n\n3. Load the ONNX.js model\n\n```ts\n// use the following in an async method\nconst url = \"./data/models/resnet/model.onnx\";\nawait session.loadModel(url);\n```\n\n4. Create your input Tensor(s) similar to the example below. You need to do any pre-processing required by\n   your model at this stage. For that refer to the documentation of the model you have:\n\n```javascript\n// creating an array of input Tensors is the easiest way. For other options see the API documentation\nconst inputs = [\n  new Tensor(new Float32Array([1.0, 2.0, 3.0, 4.0]), \"float32\", [2, 2]),\n];\n```\n\n5. Run the model with the input Tensors. The output Tensor(s) are available once the run operation is complete:\n\n```javascript\n// run this in an async method:\nconst outputMap = await session.run(inputs);\nconst outputTensor = outputMap.values().next().value;\n```\n\nMore verbose examples on how to use ONNX.js are located under the `examples` folder. For further info see [Examples](./examples/README.md)\n\n### Running in Node.js\n\nONNX.js can run in Node.js as well. This is usually for testing purpose. Use the `require()` function to load ONNX.js:\n\n```js\nrequire(\"onnxjs\");\n```\n\nYou can also use NPM package [`onnxjs-node`](https://github.com/fs-eire/onnxjs-node), which offers a Node.js binding of [ONNXRuntime](https://github.com/Microsoft/onnxruntime).\n\n```js\nrequire(\"onnxjs-node\");\n```\n\nSee [usage](https://github.com/fs-eire/onnxjs-node#usage) of onnxjs-node.\n\nRefer to [node/Add](./examples/node/add) for a detailed example.\n\n## Documents\n\n### Developers\n\nFor information on ONNX.js development, please check [Development](./docs/development.md)\n\nFor API reference, please check [API](./docs/api.md).\n\n### Getting ONNX models\n\nYou can get ONNX models easily in multiple ways:\n\n- Choose a pre-trained ONNX model from the [ONNX Model Zoo](https://github.com/onnx/models)\n- Convert models from mainstream frameworks, e.g. PyTorch, TensorFlow and Keras, by following [ONNX tutorials](https://github.com/onnx/tutorials)\n- Use your data to generate a customized ONNX model from [Azure Custom Vision service](https://docs.microsoft.com/en-us/azure/cognitive-services/Custom-Vision-Service/home)\n- [Train a custom model in AzureML](https://github.com/Azure/MachineLearningNotebooks/tree/master/training) and save it in the ONNX format\n\nLearn more about ONNX\n\n- [ONNX website](http://onnx.ai/)\n- [ONNX on GitHub](https://github.com/onnx/onnx)\n\n### Compatibility\n\n#### Desktop Platforms\n\n|    OS/Browser    |       Chrome       |        Edge        |      FireFox       |       Safari       |       Opera        |      Electron      |      Node.js       |\n| :--------------: | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: |\n|    Windows 10    | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |         -          | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |\n|      macOS       | :heavy_check_mark: |         -          | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |\n| Ubuntu LTS 18.04 | :heavy_check_mark: |         -          | :heavy_check_mark: |         -          | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |\n\n#### Mobile Platforms\n\n| OS/Browser |       Chrome       |        Edge        |      FireFox       |       Safari       |       Opera        |\n| :--------: | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: |\n|    iOS     | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |\n|  Android   | :heavy_check_mark: | :heavy_check_mark: |    Coming soon     |         -          | :heavy_check_mark: |\n\n### Operators\n\nONNX.js currently supports most operators in [ai.onnx](https://github.com/onnx/onnx/blob/rel-1.2.3/docs/Operators.md) operator set v7 (opset v7). See [operators.md](./docs/operators.md) for a complete, detailed list of which ONNX operators are supported by the 3 available builtin backends (cpu, wasm, and webgl).\n\nSupport for [ai.onnx.ml](https://github.com/onnx/onnx/blob/master/docs/Operators-ml.md) operators is coming soon. [operators-ml.md](./docs/operators-ml.md) has the most recent status of ai.onnx.ml operators.\n\n## Contribute\n\nWe’d love to embrace your contribution to ONNX.js. Please refer to [CONTRIBUTING.md](./CONTRIBUTING.md).\n\n## Thanks\n\nThanks to [BrowserStack](https://www.browserstack.com/) for providing cross browser testing support.\n\n## License\n\nCopyright (c) Microsoft Corporation. All rights reserved.\n\nLicensed under the [MIT](./LICENSE) License.\n","funding_links":[],"categories":["Inside the browser","Frameworks"],"sub_categories":["Machine Learning"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMicrosoft%2Fonnxjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMicrosoft%2Fonnxjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMicrosoft%2Fonnxjs/lists"}