Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the libraries in non-browser NodeJS #33

Closed
ghost opened this issue Nov 10, 2019 · 13 comments
Closed

Using the libraries in non-browser NodeJS #33

ghost opened this issue Nov 10, 2019 · 13 comments
Labels
feature request New feature or request

Comments

@ghost
Copy link

ghost commented Nov 10, 2019

(node:3904) UnhandledPromiseRejectionWarning: ReferenceError: FileReader is not defined
    at D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:160:42
    at new Promise (<anonymous>)
    at BrowserFiles.<anonymous> (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:159:39)      at step (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:48:23)
    at Object.next (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:29:53)
    at D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:23:71
    at new Promise (<anonymous>)
    at __awaiter (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:19:12)
    at BrowserFiles.load (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:153:16)
    at D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:279:50
    at step (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:54:23)
    at Object.next (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:35:53)
    at D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:29:71
    at new Promise (<anonymous>)
    at __awaiter (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:25:12)
    at loadLayersModelFromIOHandler (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:267:12)          at Object.<anonymous> (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:251:35)
    at step (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:54:23)
    at Object.next (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:35:53)
    at D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:29:71
    at new Promise (<anonymous>)
    at __awaiter (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:25:12)
    at Object.loadLayersModelInternal (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\models.js:230:12)        at Object.loadLayersModel (D:\Code\JS\SAHBot\node_modules\@tensorflow\tfjs-layers\dist\exports.js:224:21)
    at Object.<anonymous> (D:\Code\JS\SAHBot\node_modules\@teachablemachine\image\dist\custom-mobilenet.js:378:49)          at step (D:\Code\JS\SAHBot\node_modules\@teachablemachine\image\dist\custom-mobilenet.js:49:23)
    at Object.next (D:\Code\JS\SAHBot\node_modules\@teachablemachine\image\dist\custom-mobilenet.js:30:53)
    at D:\Code\JS\SAHBot\node_modules\@teachablemachine\image\dist\custom-mobilenet.js:24:71
(node:3904) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:3904) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

my code:

const tf = require("@tensorflow/tfjs");
const tfn = require("@tensorflow/tfjs-node");
const tmImage = require('@teachablemachine/image');

tmImage.loadFromFiles('model.json', 'weights.bin', 'metadata.json').then(model => {
 ...
});
@tr7zw
Copy link

tr7zw commented Nov 10, 2019

I tried that too, but it looks like "@teachablemachine/image" isn't made for NodeJs at all. It tries to use Canvases and the dom to work with the images. Hopefully, a Node compatible version gets released soon.

@ghost
Copy link
Author

ghost commented Nov 10, 2019

how does someone make something as useful as this... without thinking "hey someone might want to use this out-of-browser"

@tr7zw
Copy link

tr7zw commented Nov 10, 2019

Well, I'm stuck trying to get it running inside Java. The Savemodel export is broken, and the Keras model is incompatible with deeplearning4j's Keras import. Also a bit confusing that all examples are with a webcam, not showing how to get it running in NodeJS/Java/Browser/whatever to classify normal images via upload/post/filesystem.

@irealva
Copy link
Contributor

irealva commented Nov 11, 2019

Hi @tr7zw and @steamp0rt thanks for filing the issue. We are working on the savedmodel export it should be fixed soon.

And you're right we have not implemented nodeJs support. It's good to know that you would like to use it in this environment and we'll keep it in mind for future planning.

@irealva irealva added bug Something isn't working feature request New feature or request labels Nov 11, 2019
@hapticdata
Copy link
Member

Hi,
Just to add clarification. The model you create with Teachable Machine will work with node.js + Tensorflow.js; however our library does not support node.js.
We absolutely did consider use outside the browser which is why we implemented converters for our models to tensorflow and tflite and have even released a video showing a tflite model working offline on a small coral board.

Having the library work as attempted above would not be possible because it would require including node's 'fs' module in the browser and other aspects such as converting an image to a tensor would be different than in browser.

We will add language to the readme to be more explicit these modules are meant for web users.
thanks

@ghost
Copy link
Author

ghost commented Nov 11, 2019

Thanks for clarifying!

@tr7zw
Copy link

tr7zw commented Nov 12, 2019

After some playing around I was able to get it running in NodeJs with teachablemachine/image by doing some workarounds with jsdom and canvas, still a "native" version would be nice.

@luke-foxed
Copy link

After some playing around I was able to get it running in NodeJs with teachablemachine/image by doing some workarounds with jsdom and canvas, still a "native" version would be nice.

Could you possibly share how you achieved that? I've been trying to do the same with canvas but have had no luck.

@tr7zw
Copy link

tr7zw commented Nov 18, 2019

After some playing around I was able to get it running in NodeJs with teachablemachine/image by doing some workarounds with jsdom and canvas, still a "native" version would be nice.

Could you possibly share how you achieved that? I've been trying to do the same with canvas but have had no luck.

Sure. I quickly threw together a git containing everything needed here.
Just keep in mind that I'm not a Javascript/node/web developer, this setup is enough to be able to expose multiple models as REST API for other projects(languages) to use, but probably has some horrible flaws if you want to have it facing the web^^.

@luke-foxed
Copy link

Thanks! I got something working too after a lot of trial and error, it's along the same idea as yours. Thanks for sharing!

@irealva
Copy link
Contributor

irealva commented Nov 19, 2019

Just a note that the exporting to Tensorflow SavedModel is fixed in latest release.

@irealva irealva removed the bug Something isn't working label Nov 19, 2019
@irealva
Copy link
Contributor

irealva commented Dec 13, 2019

Added your repo to this section, thanks for that!

https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries

@traumverloren
Copy link

traumverloren commented Apr 12, 2020

I found this issue when I was trying to figure out how to use my teachable machine model in Node.js. I thought I would comment in case others end up here wondering how to use their model in Node.js with Tensorflow.js. I created a starting template that might be helpful to others. (FYI it is not using the teachable machine library, only its exported trained model.)

https://github.com/traumverloren/teachable-machine-image-nodejs-starter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants