Search code examples
javascripttensorcodesandboxonnxruntime

onnxruntime-web A tensor's dims must be a number array


I want to replicate this working MNIST inference example which uses the deprecated onnxjs library in favor of using the new onnxruntime, but I'm getting an error while creating a Tensor from the canvas image:

const imgData = this.ctx.getImageData(0, 0, this.CANVAS_SIZE, this.CANVAS_SIZE);
let img_arr= new Float32Array(imgData.data); //560 x 560 = 313600
const dims = [1, 1, 560, 560];
const input = new ort.Tensor(img_arr, "float32", dims);

Error Message:

A tensor's dims must be a number array

The full code to replicate my problem is available in this repository.

I have tried also to replicate my problem on Code sandbox, but it seems that the code sandbox has an issue installing the onnxruntime-web: Unable to open 'ort-web.min.js': File not found (file:///sandbox/node_modules/onnxruntime-web/dist/ort-web.min.js).

So I get another error: invalid wire type 4 at offset 3

Edit 50069724/how-to-add-multiple-material-ui-palette-colors


Solution

  • The convention (parameters order) of Tensor creation has changed in the new API:

    const input = new ort.Tensor("float32", img_arr, dims);