tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.37k stars 1.92k forks source link

TypeError:Cannot read property 'backend' of undefined #3131

Closed YIN-jw closed 4 years ago

YIN-jw commented 4 years ago

We are using posenet and knn-classifier (including mobilenet) in tfjs-models for javascript project development, but now the two plugins cannot be used at the same time. When using the knn-classifier plugin, we use knn-classifier@1.2.2, and the required peerDependency is @tensorflow/tfjs-core@^1.2.1; due to network problems, mobilenet uses a static file, MobileNet_v1_1. 0_224. The posenet model uses posenet@2.2.1 and the required peerDependencies are @ tensorflow/tfjs-core@^1.3.0 and @tensorflow/tfjs-converter@^1.3.0. When only using the knn plugin, the installation dependency is @ tensorflow / tfjs @ 0.10.0, and the knn plugin can be used normally.But when the posenet plugin is added,the installation dependencies are @tensorflow/tfjs-core@1.7.2 and @tensorflow/tfjs-converter@1.7.2. ,the knn plugin cannot be used. I think the reason may be that mobilenet uses the static files.We use the Selective-dependency-resolutions,try to add: "resolutions": { "mobilenet.js/@tensorflow/tfjs": "0.10.0" }, in the package.json,but still can't work. Is there any solution based on this problem or how can I not use static files?

console.log error :Uncaught Error:'realDivide' not yet implemented or not found in the registry. Did you forget to import the kernel? Uncaught TypeError:Cannot read property 'backend' of defined.

tafsiri commented 4 years ago

I would use the highest version of tfjs specified by peerDependencies (or even the latest version 1.7.x). PeerDependencies are pretty much advisory, so I would try the latest version and check for error.

tfjs 0.10.0 is very old at this point and I don't think we can help debug that.

the knn plugin cannot be used.

I don't understand this part, why can that knn model not be used?

We use the Selective-dependency-resolutions

I don't think you should need this. I think you should fix how you are loading mobilenet to use a recent version of the package.

If you are able to make a simple browser runnable reproduction of this problem on something like codepen that we can look, we will better be able to see if it is a bug.

YIN-jw commented 4 years ago

Thanks for your reply, we are now trying again to load the latest version and check for errors. According to the prompt of readme in knn-classifier in tfjs-models, I enter at the beginning of index: const tf = require('@tensorflow/tfjs'); const mobilenetModule = require('@tensorflow-models/mobilenet'); const knnClassifier = require('@tensorflow-models/knn-classifier');

The mobilenet version is 2.0.4, and the required version is: "peerDependencies": { "@tensorflow/tfjs-core": "~1.2.1", "@tensorflow/tfjs-converter": "~1.2.1" The knn-classifier version is 1.2.2, and the required version is: "peerDependencies": { "@tensorflow/tfjs-core": "^1.2.1"

Then I type in the index: this.classifier = knnClassifier.create(); this.mobilenet = await mobilenetModule.load(); The dependencies installed by the corresponding code are: "@tensorflow/tfjs": "1.2.1", "@tensorflow/tfjs-converter": "1.2.1", "@tensorflow/tfjs-core": "1.2.1",

In order to implement the function in the code I wrote: isloaded() { return Boolean(this.mobilenet) } let img = document.createElement('img') img.src = this.runtime.ioDevices.video.getFrame({ format: Video.FORMAT_CANVAS, dimensions: Scratch3Knn.DIMENSIONS }).toDataURL("image/png") img.width = 480 img.height = 360 img.onload = () => { const img0 = tf.fromPixels(img); const logits0 = this.mobilenet.infer(img0, 'conv_preds'); this.classifier.addExample(logits0, index); this.trainTypes[index] = args.STRING Then the console shows that the isloaded module is false and reports: Uncaught TypeError: tf.fromPixels is not a function. Is there any solution to this problem,thank you very much!

rthadur commented 4 years ago

please use tf.browser.fromPixels , refer here

YIN-jw commented 4 years ago

Hello, thank you very much for your help.We can now load the latest version of the mobilenet model through the network.Since we used static files before, we want to continue local development. Are there any open source local model files?

GantMan commented 3 years ago

Mine was a tf.tidy cleaning up vars I needed inside a promise. Fixed for me now, but leaving this note for posterity.