DEV Community

Cover image for Machine Learning In Node.js With TensorFlow.js

Machine Learning In Node.js With TensorFlow.js

James Thomas on August 09, 2018

TensorFlow.js is a new version of the popular open-source library which brings deep learning to JavaScript. Developers can now define, train, and...
Collapse
 
_diddlysquat profile image
Jonas Heitzer • Edited

Hi James,
thanks or that really cool article and the example. Sadly i can't get it to work :(.

I am on a MacBook and it throws the following Error:
2018-08-10 15:17:12.564735: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
(node:5745) Warning: N-API is an experimental feature and could change at any time.
(node:5745) UnhandledPromiseRejectionWarning: Error: SOI not found

Do you have any experience of running it on a Mac? Might Docker be a solution?

Collapse
 
jthomas profile image
James Thomas

Hello Jonas. I've been running it on a Mac! Looking around, it looks like this is an issue with the image you are trying to classify.
stackoverflow.com/questions/493916...

Are you using the panda test image provided in the blog post or something else?

Collapse
 
_diddlysquat profile image
Jonas Heitzer

Hi James! Thank for getting back to me. I used the panda you provided, but it seems the image was corrupted. I tried it with several other pandas and it works fine now.

Thanks again!

Collapse
 
aralroca profile image
Aral Roca • Edited

Good article! 😊 Nevertheless, in the tfjs documentation I see that there is the method tf.fromPixels that creates a tf.Tensor from an image. Maybe is possible to replace it? Instead of jpeg-js library?

I'm not familiar with tensorflow.js, but I think that for matrix operations like this is performing better (with GPU) than using two nested fors. (Not sure...)

Thank you!! BTW; Very good article!

Collapse
 
jthomas profile image
James Thomas

Hello Aral, tf.fromPixels can only be used in the browser I'm afraid. It extract pixels from DOM elements like video, image or canvas tags, rather than native buffers.

Collapse
 
aralroca profile image
Aral Roca

Thank you for that clarification! I would like to try tfjs in node.js!

Collapse
 
raghul22 profile image
Raghul • Edited

Hi James,

i am a newbie to machine learning and its really cool article. This article really helped me in understanding some of the concepts. I have tried your script, it didn't work for the mentioned panda image and got to know that image is corrupted. When i tried with another "cat" image, I am getting this error: Error: browserHTTPRequest is not supported outside the web browser without a fetch polyfill.

I am not sure, why this error is coming, as we are referring the model locally. Please help.

>node script.js mobilenet/model.json panda.jpg 
cpu backend was already registered. Reusing existing backend
2018-10-22 16:47:24.306471: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
(node:94614) Warning: N-API is an experimental feature and could change at any time.
(node:94614) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: **SOI not found**
(node:94614) [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.

>node script.js mobilenet/model.json cat.jpg 
cpu backend was already registered. Reusing existing backend
2018-10-22 16:47:17.300905: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
(node:94615) Warning: N-API is an experimental feature and could change at any time.
(node:94615) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: **browserHTTPRequest is not supported outside the web browser without a fetch polyfill.**
(node:94615) [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.

Thanks!

Collapse
 
itachiuchiha profile image
Itachi Uchiha

Omg. This is amazing.

Collapse
 
tomdong profile image
Jiangfeng.Dong

Thanks for the great article, I'm following the exact steps as you mentioned, but face some issues when loading the mobilenet model, I guess something might be changed. could you please share the version of the components used in your project?

BTW,
I'm using
"@tensorflow-models/mobilenet": "1.0.0",
"@tensorflow/tfjs": "1.0.1",
"@tensorflow/tfjs-node": "1.0.1",

Thanks a lot!

Collapse
 
rajesh36923908 profile image
rajesh

Hi
Very useful and interesting Article. Node.js is truly powerful and useful for developers. You have shared lots of information that makes the tasks easy.

thanks
elena

Collapse
 
elenajessi1 profile image
Elena jessi

Hi
Interesting information and knowledge. Especially the attachment that you have shared, will be much helpful for me to understand the main theme of focus dimensions and use it. there are lot of C++ libraries you could wrap up under JavaScript quite easily. Node.js can be a nice gateway to such a library.

thanks
elena

Collapse
 
veselinastaneva profile image
Vesi Staneva

Great way to show that Machine Learning and Node.js go well together!πŸ‘πŸ‘πŸ‘

My team just completed an open-sourced Content Moderation Service built Node.js, TensorFlowJS, and ReactJS that we have been working over the past weeks. We have now released the first part of a series of three tutorials - How to create an NSFW Image Classification REST API and we would love to hear your feedback. Any comments & suggestions are more than welcome. Thanks in advance!
(Fork it on GitHub or click🌟star to support us and stay connectedπŸ™Œ)

Collapse
 
japitase profile image
Japita Systems Engineering

node app.js model_data/model.json panda.jpg
cpu backend was already registered. Reusing existing backend
2019-11-08 13:31:40.445755: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
(node:15585) UnhandledPromiseRejectionWarning: Error: browserHTTPRequest is not supported outside the web browser without a fetch polyfill.
at new BrowserHTTPRequest (/home/giuseppe/Software/tfjs/mobilenet/node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:46:19)
at Object.browserHTTPRequest (/home/giuseppe/Software/tfjs/mobilenet/node_modules/@tens

Collapse
 
jochemstoel profile image
Jochem Stoel

Excellent work, James. I'm gonna follow you around.

Collapse
 
sreenatha profile image
Sreenatha Reddy K R

Thank you for sharing... I will give it a try...

Collapse
 
toukoxu profile image
ToukoXu

as