JavaScript API Reference


WebDNN JavaScript API is consisted of main module (WebDNN) and support modules WebDNN.Math, WebDNN.Image).

You can use this API by follow two usage.

  • Use with import syntax

      // index.js
      import * as WebDNN from "webdnn"
      let runner = WebDNN.load('./model');
  • Use with <script> tag

      <!-- index.html -->
      <script src="./webdnn.js"></script>

Support libraries are also loaded when main module is loaded.

Basic Example

First, load your model by WebDNN.load. This function load your model asynchronously. Therefore you have to use ES2016 async/await syntax or promise syntax.

// ES2016
let runner = await WebDNN.load('./model');
// ES2015
    .then(function(runner) { /* do something */ });

Then you can get DescriptorRunner instance, which executes your model. Runner also provides interface of input and output buffers. You can read and write data through this interface.

For example, load image data by using WebDNN.Image.getImageArray() and set it into input buffer.

// ES2016
let imageArray = await WebDNN.Image.getImageArray(); // Load image RGB data as Float32Array
runner.inputs[0].set(imageArray); // Write data
// ES2015
    .then(function(imageArray) { runner.inputs[0].set(imageArray) });

Also you can access the output buffer.

let result = runner.outputs[0];

Finally, run inference!

// ES2016
// ES2015
    .then(function() { /* do something */ });

Because DescriptorRunner.outputs returns SymbolicTypedArray, which is not actual typed array, you need to convert it into actual typed array by using SymbolicTypedArray.toActual().