Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Microsoft/onnxjs

ONNX.js: run ONNX models using JavaScript
https://github.com/Microsoft/onnxjs

Last synced: about 2 months ago
JSON representation

ONNX.js: run ONNX models using JavaScript

Awesome Lists containing this project

README

        

[![npm version](https://badge.fury.io/js/onnxjs.svg)](https://badge.fury.io/js/onnxjs)
[![GitHub version](https://badge.fury.io/gh/Microsoft%2Fonnxjs.svg)](https://badge.fury.io/gh/Microsoft%2Fonnxjs)
[![ONNX.js CI - Windows CPU (Electron)]()](https://dev.azure.com/onnxruntime/onnxjs/_build/latest?definitionId=24)
[![ONNX.js CI - Windows CPU (Node.js)]()](https://dev.azure.com/onnxruntime/onnxjs/_build/latest?definitionId=20)
[![ONNX.js CI - Windows GPU (Chrome,Edge)]()](https://dev.azure.com/onnxruntime/onnxjs/_build/latest?definitionId=22)
[![ONNX.js CI - Linux CPU (Node.js)]()](https://dev.azure.com/onnxruntime/onnxjs/_build/latest?definitionId=5)
[![ONNX.js CI - BrowserStack (Suite0)]()](https://dev.azure.com/onnxruntime/onnxjs/_build/latest?definitionId=17)

## 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:

- [**Migration ONNX.js to ONNX Runtime Web**](./docs/migration-to-ort-web.md)
- [ONNX Runtime Web (README)](https://github.com/microsoft/onnxruntime/tree/master/js/web)
- [Get started with ORT for JavaScript - onnxruntime.ai](https://onnxruntime.ai/docs/get-started/with-javascript.html)
- [Tutorials / Deploy on web - onnxruntime.ai](https://onnxruntime.ai/docs/tutorials/web/)
- [ONNX Runtime JavaScript API](https://onnxruntime.ai/docs/api/js/index.html)

# ONNX.js

ONNX.js is a Javascript library for running ONNX models on browsers and on Node.js.

ONNX.js has adopted WebAssembly and WebGL technologies for providing an optimized ONNX model inference runtime for both CPUs and GPUs.

### Why ONNX models

The [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).

### Why ONNX.js

With 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.

ONNX.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.

See [Compatibility](#Compatibility) and [Operators Supported](#Operators) for a list of platforms and operators ONNX.js currently supports.

### Benchmarks

Benchmarks 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):

![alt text](./docs/perf-resnet50.png "Resnet50 Perf numbers")

> NOTE:
>
> 1. Keras.js doesn't support WebGL usage on Edge
> 2. Keras.js and TensorFlow.js don't support WebAssembly usage on any browser

> The specs of the machine that was used to perform the benchmarking is listed below:
>
> - OS: Microsoft Windows 10 Enterprise Insider Preview
> - Model: HP Z240 Tower Workstation
> - Processor: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz, 3401 Mhz, 4 Core(s), 8 Logical Processor(s)
> - Installed Physical Memory (RAM): 32.0 GB
> - GPU make / Chip type: AMD FirePro W2100 / AMD FirePro SDI (0x6608)
> - GPU Memory (approx.): 18.0 GB

### Demo

[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).

## Getting Started

There are multiple ways to use ONNX.js in a project:

### Using `` tag

This is the most straightforward way to use ONNX.js. The following HTML example shows how to use it:

```html
<html>
<head> </head>

<body>
<!-- Load ONNX.js -->
<script src="https://cdn.jsdelivr.net/npm/onnxjs/dist/onnx.min.js">


// create a session
const myOnnxSession = new onnx.InferenceSession();
// load the ONNX model file
myOnnxSession.loadModel("./my-model.onnx").then(() => {
// generate model input
const inferenceInputs = getInputs();
// execute the model
myOnnxSession.run(inferenceInputs).then((output) => {
// consume the output
const outputTensor = output.values().next().value;
console.log(`model output tensor: ${outputTensor.data}.`);
});
});