Oortone
Oortone

Reputation: 185

Local tensorflow.js won't work as a module in webworker

I'm using a local copy of tensorflow.js tfjs.js in an experimental setup. All is browser side, no node.js.

I want to move the tensorflow functionality to it's own thread in a webworker since it's heavy on the browser. However the way I got the tensorflow module to work in a worker does not make sense:

This was the initial idea:

//main.js
let predictor = new Worker ('prediction_ww.js', {type: 'module'})

//prediction_ww.js
import * as tf from "tfjs.js" 

But this produces the following error when I start execution (note: 'loadGraphModel' is a method in tensorflow.js):

prediction_ww.js:44 Uncaught (in promise) TypeError: tf.loadGraphModel is not a function...
etc...

After testing without a real idea of what I'm doing I get this code, without the type: 'module' to work:

//main.js
let predictor = new Worker ('prediction_ww.js')

//prediction_ww.js
var tf = import ("./tfjs.js")

I would like to understand why this works but not the case using module which my reading suggests should be the correct way of using an external module in a webworker on the browserside.

Also I noticed it only works using the ./ before the filename which I also don't understand why.

I use Chrome 96 on macOS 10.14.

Upvotes: 3

Views: 370

Answers (1)

Vladimir Mandic
Vladimir Mandic

Reputation: 883

First, tfjs works just fine in a web worker unless you try to use specific functions that rely on DOM (e.g. can't use tf.browser.* methods) - and that's not the case here.

When you say local tfjs.js - which variation of tfjs is that? it ships as many variations and only ESM modules can be loaded using {type: "module"}

Default tf.js is NOT ESM, but tf.es2017.js is

Upvotes: 2

Related Questions