This tutorial demonstrates how to load a TensorFlow model with deeplearn-tensorflow directly into deeplearn.js.
We will modify the existing MNIST demo to simplify the needed steps.
To build the MNIST demo, we need to clone the source code with:
git clone https://github.com/PAIR-code/deeplearnjs.git
cd deeplearnjsYarn is a requirement of deeplearn.js and could be installed with the following command:
npm install --global yarnPlease make sure you have Python and TensorFlow installed to train the MNIST model by running:
python demos/mnist/fully_connected_feed.pyThe following command will copy the trained model to the mnist demo dirctory:
cp -R /tmp/tensorflow/mnist/logs/fully_connected_feed demos/mnist/(Note: The trained model could be at a different location depending on the used operation system.)
To load and resolve all dependencies run the following npm command:
npm run prepTo be able to use the deeplearn-tensorflow integration add deeplearn-tensorflow as dependencies for the demos with:
cd demos
yarn add deeplearn-tensorflow
cd ..First run the unchanged MNIST demo to make sure everything is setup correctly:
./scripts/watch-demo demos/mnistTo view the MNIST demo visit http://127.0.0.1:8080/demos/mnist/ in your
browser.
To load the trained TensorFlow model with deeplearn-tensorflow, the following
modifications needs to be done for demos/mnist/mnist.ts.
// tslint:disable-next-line:max-line-length
- import {Array1D, Array2D, CheckpointLoader, ENV, NDArray, Scalar} from 'deeplearn';
+ import {Array1D, Array2D, ENV, NDArray, Scalar} from 'deeplearn';import {Array1D, Array2D, ENV, NDArray, Scalar} from 'deeplearn';
+ import {TensorflowLoader} from 'deeplearn-tensorflow';- // manifest.json lives in the same directory as the mnist demo.
- const reader = new CheckpointLoader('.');
- reader.getAllVariables().then(vars => {
+ // Tensorflow reader demo.
+ const tensorflowReader = new TensorflowLoader(NDArray);
+ tensorflowReader.loadRemoteFiles('fully_connected_feed/model.ckpt-999').then((vars) => {Visit http://127.0.0.1:8080/demos/mnist/ to see the new loader in action.