Fix "'webgpu' has not yet been initialized" error #34
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
await tf.ready()
to handposeawait tf.ready()
to pose-detectiontf.setBackend("webgl")
to neuralNetworkHi @shiffman, I think the problem here is caused by tf.js using webgpu, instead of webgl, as the default backend on some browsers and machines. (I tried running the neural network example in both Chrome and Firefox and the error only occurred in Chrome. Firefox doesn't support webgpu yet.)
I am not exactly sure how the dependencies from pose-detection trigger this error, but I found a solution. We have to call
await tf.ready();
, more info here, before calling any other tensorflow.js functions because unlike webgl, setting webgpu backend is an asynchronous task.I have added

await tf.ready();
to all other models; however, using the webgpu backend for neuralNetwork caused another problem, which seems like a much deeper issue to resolve:I think the best solution, for now, is to force neuralNetwork to use the original WebGL backend by calling
tf.setBackend("webgl");
Just some other random thoughts:
I was switching between the webgl an webgpu backends and found that the frame rate varied depending on the model and the computer being used. I think it might be helpful to add an option argument to tf.js-based models, allowing more advanced users to manually set the backend and fine-tune their projects.