GithubHelp home page GithubHelp logo

test-mass-forker-org-1 / onnxjs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from microsoft/onnxjs

0.0 0.0 0.0 5.01 MB

ONNX.js: run ONNX models using JavaScript

License: Other

JavaScript 2.34% C++ 4.22% C 0.44% TypeScript 93.00% PureBasic 0.01%

onnxjs's Introduction

npm version GitHub version ONNX.js CI - Windows CPU (Electron) ONNX.js CI - Windows CPU (Node.js) ONNX.js CI - Windows GPU (Chrome,Edge) ONNX.js CI - Linux CPU (Node.js) ONNX.js CI - BrowserStack (Suite0)

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:

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

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 is adopted to execute the model at near-native speed. Furthermore, ONNX.js utilizes 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 and Operators Supported 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

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 shows the capabilities of ONNX.js. Check the code.

Getting Started

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

Using <script> tag

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

<html>
  <head> </head>

  <body>
    <!-- Load ONNX.js -->
    <script src="https://cdn.jsdelivr.net/npm/onnxjs/dist/onnx.min.js"></script>
    <!-- Code that consume ONNX.js -->
    <script>
      // 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}.`);
        });
      });
    </script>
  </body>
</html>

Refer to browser/Add for an example.

Using NPM and bundling tools

Modern browser based applications are usually built by frameworks like Angular, React, Vue.js 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:

  1. Import Tensor and InferenceSession.
import { Tensor, InferenceSession } from "onnxjs";
  1. Create an instance of InferenceSession.
const session = new InferenceSession();
  1. Load the ONNX.js model
// use the following in an async method
const url = "./data/models/resnet/model.onnx";
await session.loadModel(url);
  1. Create your input Tensor(s) similar to the example below. You need to do any pre-processing required by your model at this stage. For that refer to the documentation of the model you have:
// creating an array of input Tensors is the easiest way. For other options see the API documentation
const inputs = [
  new Tensor(new Float32Array([1.0, 2.0, 3.0, 4.0]), "float32", [2, 2]),
];
  1. Run the model with the input Tensors. The output Tensor(s) are available once the run operation is complete:
// run this in an async method:
const outputMap = await session.run(inputs);
const outputTensor = outputMap.values().next().value;

More verbose examples on how to use ONNX.js are located under the examples folder. For further info see Examples

Running in Node.js

ONNX.js can run in Node.js as well. This is usually for testing purpose. Use the require() function to load ONNX.js:

require("onnxjs");

You can also use NPM package onnxjs-node, which offers a Node.js binding of ONNXRuntime.

require("onnxjs-node");

See usage of onnxjs-node.

Refer to node/Add for a detailed example.

Documents

Developers

For information on ONNX.js development, please check Development

For API reference, please check API.

Getting ONNX models

You can get ONNX models easily in multiple ways:

Learn more about ONNX

Compatibility

Desktop Platforms

OS/Browser Chrome Edge FireFox Safari Opera Electron Node.js
Windows 10 ✔️ ✔️ ✔️ - ✔️ ✔️ ✔️
macOS ✔️ - ✔️ ✔️ ✔️ ✔️ ✔️
Ubuntu LTS 18.04 ✔️ - ✔️ - ✔️ ✔️ ✔️

Mobile Platforms

OS/Browser Chrome Edge FireFox Safari Opera
iOS ✔️ ✔️ ✔️ ✔️ ✔️
Android ✔️ ✔️ Coming soon - ✔️

Operators

ONNX.js currently supports most operators in ai.onnx operator set v7 (opset v7). See operators.md for a complete, detailed list of which ONNX operators are supported by the 3 available builtin backends (cpu, wasm, and webgl).

Support for ai.onnx.ml operators is coming soon. operators-ml.md has the most recent status of ai.onnx.ml operators.

Contribute

We’d love to embrace your contribution to ONNX.js. Please refer to CONTRIBUTING.md.

Thanks

Thanks to BrowserStack for providing cross browser testing support.

License

Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the MIT License.

onnxjs's People

Contributors

fs-eire avatar hariharans29 avatar ntt123 avatar xzhu1900 avatar dependabot[bot] avatar liuziyue avatar duli2012 avatar jeffsaremi avatar hodovani avatar elliotwaite avatar 28smiles avatar emmaningms avatar emxuyu avatar maxee998 avatar narsil avatar niels-garve avatar rishav1 avatar hanbitmyths avatar darrelfrancis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.