onnxruntime/docs/get-started/with-javascript.md
Cassie a0f3e30de6
Docs update: updated nav, get started sections, home page, apis (#9060)
* initial setup and rename "how to" to "setup"

* move API to main nav

* move api to main nav

* add get starated, rework nav order

* rename to install move mds out of install section

* update api nav and home page

* add install docs and python qs updates

* python get started work

* remove c and obj c for now

* move java, python, and obj-c docs under api folder

* move java api html to iframe (ugh)

* remove api docs w/o details, move api text getstar

* remove api docs wo detail updates get started

* remvoe iframes

* move eco system to main nav

* fix api buttons

* added more examples moved intro to ORT

* fix links

* fix get started titles

* fix get started titles

* fix more links

* fix more links

* more link fixes

* fix nav remove inferencing and training subnav

* fix top nav remove inference and training nav

* fix title

* fix tutorials nav hierarchy

* fix python api button

* add tenorflow keras example

* fix quickstart toc

* add imports fix spacing

* fix links

* update nav and python get started page

* move ort training example, add coming soon for iot

* update C# get started

* fix spacing on quantization

* Add some js get started content

* fix formatting

* fix typo

* removed onnx-pytorch and onnx-tf

* updated pip install torch and added links iot page

* added pytorch tutorial heirarchy

* updated web to docs soon added release blog link

* add web link
2021-09-15 16:23:42 -05:00

3.7 KiB

title parent toc nav_order
JavaScript Get Started true 3

Get started with ORT for JavaScript

{: .no_toc }

ONNX Runtime JavaScript API is the unified interface used by ONNX Runtime Node.js binding, ONNX Runtime Web and ONNX Runtime for React Native.

Contents

{: .no_toc }

  • TOC placeholder {:toc}

JavaScript Examples (Install and import)

Web ORT (client)

npm install onnxruntime-web
const ort = require('onnxruntime-web');

Node ORT (server)

npm install onnxruntime-node
const ort = require('onnxruntime-node');

React Native ORT

npm install onnxruntime-react-native
const ort = require('onnxruntime-react-native');

JavaScript Usage Example

// use an async context to call onnxruntime functions.
async function main() {
    try {
        // create a new session and load the specific model.
        //
        // the model in this example contains a single MatMul node
        // it has 2 inputs: 'a'(float32, 3x4) and 'b'(float32, 4x3)
        // it has 1 output: 'c'(float32, 3x3)
        const session = await ort.InferenceSession.create('./model.onnx');

        // prepare inputs. a tensor need its corresponding TypedArray as data
        const dataA = Float32Array.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
        const dataB = Float32Array.from([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]);
        const tensorA = new ort.Tensor('float32', dataA, [3, 4]);
        const tensorB = new ort.Tensor('float32', dataB, [4, 3]);

        // prepare feeds. use model input names as keys.
        const feeds = { a: tensorA, b: tensorB };

        // feed inputs and run
        const results = await session.run(feeds);

        // read from results
        const dataC = results.c.data;
        document.write(`data of result tensor 'c': ${dataC}`);

    } catch (e) {
        document.write(`failed to inference ONNX model: ${e}.`);
    }
}

Supported Versions

  • ONNX Runtime Node.js binding: Node.js v12.x+ or Electron v5.x+
  • ONNX Runtime Web: mainstream modern browsers on Windows, macOS, Android and iOS.
  • ONNX Runtime for React Native: same as ORT Mobile (Android/iOS)

Builds

Builds are published to npm and can be installed using npm install

Package Artifact Description Supported Platforms
Node.js binding onnxruntime-node CPU (Release) Windows x64 CPU NAPI_v3, Linux x64 CPU NAPI_v3, MacOS x64 CPU NAPI_v3
Web onnxruntime-web CPU and GPU Browsers (wasm, webgl), Node.js (wasm)
React Native onnxruntime-react-native CPU Android, iOS

For Node.js binding, to use on platforms without pre-built binaries, you can build Node.js binding from source and consume using npm install <onnxruntime_repo_root>/js/node/.

API Reference

See Typescript declarations for Inference Session, Tensor and Environment Flags for reference.

See also ONNX Runtime JavaScript examples.