tf.LayersModel.save fails in a worker as window object is absent
See original GitHub issueTo get help from the community, we encourage using Stack Overflow and the tensorflow.js
tag.
TensorFlow.js version
1.13
Browser version
Chrome 79
Describe the problem
tf.LayersModel.save("indexeddb://my-model-1")
does not work in a web worker as window
object is not found. It is due https://github.com/tensorflow/tfjs/blob/master/tfjs-core/src/io/indexed_db.ts#L60 line
ReferenceError: window is not defined
at sp (indexed_db.ts:60)
at new t (indexed_db.ts:89)
at cp (indexed_db.ts:242)
at router_registry.ts:96
at Array.forEach (<anonymous>)
at Function.t.getHandlers (router_registry.ts:95)
at Function.t.getSaveHandlers (router_registry.ts:71)
at Object.getSaveHandlers (router_registry.ts:110)
at e.<anonymous> (training.ts:1814)
at common.ts:14
Code to reproduce the bug / link to feature request
Simply try to save a model in a worker. You can try the below example in a worker,
worker.js
const model = tf.sequential(
{layers: [tf.layers.dense({units: 1, inputShape: [3]})]});
console.log('Prediction from original model:');
model.predict(tf.ones([1, 3])).print();
const saveResults = await model.save('indexeddb://my-model-1');
main-thread.js
const w = new Worker("worker.js");
...
const loadedModel = await tf.loadLayersModel('indexeddb://my-model-1');
console.log('Prediction from loaded model:');
loadedModel.predict(tf.ones([1, 3])).print();
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Creates a tf.Tensor with the provided values, shape and dtype.
Pass a `WebGLData` object and specify a shape yourself. // This makes it possible for TF.js applications to avoid GPU / CPU sync....
Read more >Workers, web or service; anyway to access window object?
My idea was to try web workers to download the files, hopefully meaning the issue would go away/the app would be a bit...
Read more >Tensorflow.js tf.LayersModel class .save() Method
An IOHandler is an object which possesses a save method concerning the accurate signature specified. The save method controls the accumulation ...
Read more >How to Train an Object Detection Model with Keras
A Mask R-CNN model can be fit from scratch, although like other computer vision applications, time can be saved and performance can be...
Read more >How to solve "window is not defined" errors in React and Next.js
I initially tried to do if (typeof window !== undefined) and this failed hard because of the reasons mentioned earlier. The other solutions ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I still have this issue in version 2.0.0. I’m using workerize-loader in a project bootstrapped with create-react-app.
As of Version 1.7.0 this issues still existed for me.
If anybody else still has this problem, this worked for me:
I created a custom IOHandler, which is basically a copy of the existing
BrowserIndexedDB
class and I updated thegetIndexedDBFactory
function the handler uses like so:Usage: