Deploy tfjs-node project on cloud platform
Stay organized with collections
Save and categorize content based on your preferences.
This doc describes how to run a Node.js process with @tensorflow/tfjs-node package on cloud platforms.
Starting from tfjs-node@1.2.4, running Node.js project on cloud platforms does not require additional configuration. This guide will show how to run the mnist-node example in the @tensorflow/tfjs-examples repository on Heroku and GCloud. Heroku’s Node.js support is documented in this article. Running Node.js on Google Cloud Platform is documented here.
Deploy Node.js project on Heroku
Prerequisites
- Node.js and npm installed
- Heroku account
- Heroku CLI
Create the Node.js app
- Create a folder and copy the
data.js,main.js,model.jsandpackage.jsonfiles from the mnist-node example. - Make sure the @tensorflow/tfjs-node dependency is @1.2.4 or newer version.
Build your app and run it locally
- Run the
npm installcommand in your local directory to install the dependencies that are declared in thepackage.jsonfile. You should be able to see that the tfjs-node package is installed and libtensorflow is downloaded.
$npminstall
>@tensorflow/tfjs-node@1.2.5installmnist-node/node_modules/@tensorflow/tfjs-node
>nodescripts/install.js
CPU-linux-1.2.5.tar.gz
*Downloadinglibtensorflow
[==============================]22675984/bps100%0.0s
*BuildingTensorFlowNode.jsbindings
- Train the model locally by running
npm start.
$npmstart
>tfjs-examples-mnist-node@0.1.0start/mnist-node
>nodemain.js
2019-07-3017:33:34.109195:Itensorflow/core/platform/cpu_feature_guard.cc:142]YourCPUsupportsinstructionsthatthisTensorFlowbinarywasnotcompiledtouse:AVX2FMA
2019-07-3017:33:34.147880:Itensorflow/core/platform/profile_utils/cpu_utils.cc:94]CPUFrequency:3492175000Hz
2019-07-3017:33:34.149030:Itensorflow/compiler/xla/service/service.cc:168]XLAservice0x52f7090executingcomputationsonplatformHost.Devices:
2019-07-3017:33:34.149057:Itensorflow/compiler/xla/service/service.cc:175]StreamExecutordevice(0):<undefined>,<undefined>
Totalparams:594922
Trainableparams:594922
Non-trainableparams:0
_________________________________________________________________
Epoch1/20
Epoch1/20
========================>----------------------------------------------------------------------------------:35.5
- Make sure you ignore build artifacts, such as node_modules, in your .gitignore file.
Create and deploy the Heroku app
- Create a new app on the Heroku website
- Commit your change and push to heroku master
$gitinit
$herokugit:remote-ayour-app-name
$gitadd.
$gitcommit-m"First Commit"
$gitpushherokumaster
- In the build logs, you should be able to see the tfjs-node package downloading the TensorFlow C Library and loading TensorFlow Node.js native addon:
remote:----->Installingdependencies
remote:Installingnodemodules(package.json)
remote:
remote: > @tensorflow/tfjs-node@1.2.5install/tmp/build_de800e169948787d84bcc2b9ccab23f0/node_modules/@tensorflow/tfjs-node
remote: > nodescripts/install.js
remote:
remote:CPU-linux-1.2.5.tar.gz
remote:*Downloadinglibtensorflow
remote:
remote:*BuildingTensorFlowNode.jsbindings
remote:added92packagesfrom91contributorsandaudited171packagesin9.983s
remote:found0vulnerabilities
remote:
In the process logs on Heroku, you should be able to see the model training logs:
Totalparams:594922
Trainableparams:594922
Non-trainableparams:0
_________________________________________________________________
Epoch1/20
Epoch1/20
====>--------------------------------------------------------------------:221.9
You can also start or debug the process in Heroku console.
Using tfjs-node prior to version 1.2.4
If you are using tfjs-node package before version 1.2.4, the package requires g++ to compile the node native addon from source files. You will have to make sure your stack has the Linux build-essential package (newer version stack may not have it on default).
Deploy Node.js project on Google Cloud Platform
Prerequisites
- Have a valid Google Cloud Project with billing account
- Install Google Cloud client tool
- Add app.yaml file to configure the Node.js Runtime
Deploy app to GCloud
Run gcloud app deploy to deploy the local code and configurations to App Engine. In the deploy logs you should be able to see that tfjs-node is installed:
$gcloudappdeploy
Step#1:
Step#1: > @tensorflow/tfjs-node@1.2.5install/app/node_modules/@tensorflow/tfjs-node
Step#1: > nodescripts/install.js
Step#1:
Step#1:CPU-linux-1.2.5.tar.gz
Step#1:*Downloadinglibtensorflow
Step#1:
Step#1:*BuildingTensorFlowNode.jsbindings
Step#1:added88packagesfrom85contributorsandaudited171packagesin13.392s
Step#1:found0vulnerabilities
In the apps logs, you should be able to see the model training process:
Totalparams:594922
Trainableparams:594922
Non-trainableparams:0
Epoch1/20
===============================================================================>
745950ms14626us/step-acc=0.920loss=0.247val_acc=0.987val_loss=0.0445
Loss:0.247(train),0.044(val);Accuracy:0.920(train),0.987(val)(14.62ms/step)
Epoch2/20
===============================================================================>
818140ms16042us/step-acc=0.980loss=0.0655val_acc=0.989val_loss=0.0371
Loss:0.066(train),0.037(val);Accuracy:0.980(train),0.989(val)(16.04ms/step)
Epoch3/20
Epoch3/20