├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── config └── index.js ├── package.json ├── protos └── mnist_inference.proto └── test ├── fixtures └── image.js └── integration └── classify-test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Clarence Leung 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-tensorflow-serving-demo 2 | 3 | Example app combining Express and TensorFlow Serving. Work in progress. 4 | 5 | ### Getting Started 6 | 7 | More details soon, but for now: 8 | 9 | 1. Set up a TensorFlow Serving instance. 10 | 2. Run the [basic tutorial with the MNIST inference server](https://tensorflow.github.io/serving/serving_basic). 11 | 3. Point this Node app to your TensorFlow Serving host in `config/index.js` in `TENSORFLOW_SERVING_HOST`. 12 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const grpc = require('grpc'); 2 | const express = require('express'); 3 | const bodyParser = require('body-parser'); 4 | 5 | // TensorFlow Serving configuration settings 6 | const config = require('./config'); 7 | 8 | // Load Protocol Buffers 9 | const proto = grpc.load('./protos/mnist_inference.proto').tensorflow.serving; 10 | const MnistService = proto.MnistService; 11 | 12 | // Create TensorFlow Serving MNIST client 13 | const client = new MnistService(config.TENSORFLOW_SERVING_HOST, grpc.credentials.createInsecure()); 14 | 15 | // Express application initialization 16 | const app = module.exports = express(); 17 | const port = process.env.PORT || 3000; 18 | 19 | // Express middleware 20 | app.use(bodyParser.json()); 21 | 22 | // Express routes 23 | app.post('/classify', (req, res, next) => { 24 | // Snake-case because this gRPC protocol requires it (see `protos/mnist_inference.proto`) 25 | const image_data = req.body.image; 26 | client.classify({ image_data }, (err, mnistResponse) => { 27 | if (err) { 28 | // TODO: Implement actual error handler 29 | next(err); 30 | } else { 31 | const results = mnistResponse ? mnistResponse.value : []; 32 | // Get the digit-classification percentages rounded to nearest hundreths place 33 | const percentages = results.map((result) => Math.ceil(result * 10000) / 100); 34 | res.json({ percentages }); 35 | } 36 | }); 37 | }); 38 | 39 | console.log(`App now listening on port ${port}...`); 40 | app.listen(port); -------------------------------------------------------------------------------- /config/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | TENSORFLOW_SERVING_HOST: '' 3 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-tensorflow-serving-demo", 3 | "version": "1.0.0", 4 | "description": "Example app combining Express and TensorFlow Serving", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "mocha test/integration" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/clarle/node-tensorflow-serving-demo.git" 12 | }, 13 | "keywords": [ 14 | "tensorflow" 15 | ], 16 | "author": "Clarence Leung ", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/clarle/node-tensorflow-serving-demo/issues" 20 | }, 21 | "homepage": "https://github.com/clarle/node-tensorflow-serving-demo#readme", 22 | "dependencies": { 23 | "body-parser": "^1.15.1", 24 | "express": "^4.13.4", 25 | "grpc": "^0.14.1" 26 | }, 27 | "devDependencies": { 28 | "chai": "^3.5.0", 29 | "mocha": "^2.5.1", 30 | "supertest": "^1.2.0" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /protos/mnist_inference.proto: -------------------------------------------------------------------------------- 1 | // Copyright 2015-2016, Google Inc. 2 | // All rights reserved. 3 | // 4 | // Redistribution and use in source and binary forms, with or without 5 | // modification, are permitted provided that the following conditions are 6 | // met: 7 | // 8 | // * Redistributions of source code must retain the above copyright 9 | // notice, this list of conditions and the following disclaimer. 10 | // * Redistributions in binary form must reproduce the above 11 | // copyright notice, this list of conditions and the following disclaimer 12 | // in the documentation and/or other materials provided with the 13 | // distribution. 14 | // * Neither the name of Google Inc. nor the names of its 15 | // contributors may be used to endorse or promote products derived from 16 | // this software without specific prior written permission. 17 | // 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | 30 | // Protobuf definition of MNIST model inference server. 31 | // The server classifies 28x28 greyscale image into digit 0-9. 32 | 33 | syntax = "proto3"; 34 | 35 | package tensorflow.serving; 36 | 37 | message MnistRequest { 38 | // Row-major encoding of a 28x28 image, each byte being the grayscale value 39 | // of a pixel rescaled from [0, 255] down to [-0.5, 0.5]. 40 | repeated float image_data = 1 [packed = true]; 41 | }; 42 | 43 | message MnistResponse { 44 | // Matching probability of digit 0-9 in range [0.0, 1.0]. 45 | repeated float value = 1 [packed = true]; 46 | }; 47 | 48 | service MnistService { 49 | // Classifies image into digits. 50 | rpc Classify(MnistRequest) returns (MnistResponse); 51 | } 52 | -------------------------------------------------------------------------------- /test/fixtures/image.js: -------------------------------------------------------------------------------- 1 | module.exports = [ 2 | 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.02352941408753395, 0.5098039507865906, 0.9411765336990356, 0.9960784912109375, 0.9019608497619629, 0.6666666865348816, 0.13333334028720856, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.03921568766236305, 0.4745098352432251, 0.9960784912109375, 0.8352941870689392, 0.5568627715110779, 0.7529412508010864, 0.960784375667572, 0.9647059440612793, 0.2196078598499298, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.4705882668495178, 1.0, 0.6784313917160034, 0.03529411926865578, 0.0, 0.0, 0.2666666805744171, 0.9490196704864502, 0.960784375667572, 0.16078431904315948, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.062745101749897, 0.9137255549430847, 0.8666667342185974, 0.0313725508749485, 0.0, 0.0, 0.0, 0.0, 0.2705882489681244, 0.9490196704864502, 0.847058892250061, 0.46666669845581055, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.250980406999588, 0.9960784912109375, 0.43137258291244507, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.615686297416687, 0.9960784912109375, 0.9960784912109375, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.46666669845581055, 0.9803922176361084, 0.09803922474384308, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.501960813999176, 0.9960784912109375, 0.7843137979507446, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.7960785031318665, 0.6823529601097107, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.501960813999176, 0.9960784912109375, 0.6823529601097107, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.7176470756530762, 0.7411764860153198, 0.05882353335618973, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.501960813999176, 0.9960784912109375, 0.43137258291244507, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.22745099663734436, 0.9803922176361084, 0.7960785031318665, 0.3176470696926117, 0.0, 0.0, 0.0, 0.0, 0.0, 0.501960813999176, 0.9960784912109375, 0.37254902720451355, 0.03529411926865578, 0.4941176772117615, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.45098042488098145, 0.9960784912109375, 0.9294118285179138, 0.3137255012989044, 0.0, 0.0, 0.0, 0.0, 0.501960813999176, 0.9960784912109375, 0.5647059082984924, 0.8431373238563538, 0.5764706134796143, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.43137258291244507, 0.8862745761871338, 0.9960784912109375, 0.5490196347236633, 0.08235294371843338, 0.05882353335618973, 0.007843137718737125, 0.8235294818878174, 0.9960784912109375, 0.9960784912109375, 0.8196079134941101, 0.0784313753247261, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1098039299249649, 0.46274513006210327, 0.9411765336990356, 0.9960784912109375, 0.9843137860298157, 0.7803922295570374, 0.9960784912109375, 0.9960784912109375, 0.7803922295570374, 0.0784313753247261, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10588236153125763, 0.4784314036369324, 0.501960813999176, 0.49803924560546875, 0.9019608497619629, 0.9960784912109375, 0.062745101749897, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8117647767066956, 0.9333333969116211, 0.0470588281750679, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8117647767066956, 0.7450980544090271, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.24313727021217346, 0.9960784912109375, 0.41960787773132324, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.6705882549285889, 0.9960784912109375, 0.125490203499794, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0313725508749485, 0.8705883026123047, 0.9019608497619629, 0.062745101749897, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2392157018184662, 0.9960784912109375, 0.458823561668396, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.27843138575553894, 0.9568628072738647, 0.12941177189350128, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]; 3 | -------------------------------------------------------------------------------- /test/integration/classify-test.js: -------------------------------------------------------------------------------- 1 | const expect = require('chai').expect; 2 | const request = require('supertest'); 3 | 4 | const config = require('../../config'); 5 | const app = require('../../app'); 6 | const image = require('../fixtures/image'); 7 | 8 | describe('Integration test pre-check', () => { 9 | it('should have a configuration property with the TF Serving Host', () => { 10 | expect(config.TENSORFLOW_SERVING_HOST).to.not.be.empty; 11 | }); 12 | }); 13 | 14 | describe('POST /classify', () => { 15 | it('should return the probabilities of each digit', (done) => { 16 | request(app) 17 | .post('/classify') 18 | .send({ image }) 19 | .set('Accept', 'application/json') 20 | .expect(200) 21 | .expect((res) => { 22 | const percentages = res.body.percentages; 23 | if (!percentages) 24 | throw new Error('Missing `percentages` key in JSON response'); 25 | if (percentages.length !== 10) 26 | throw new Error('Incorrect number of digit classes in JSON response'); 27 | }) 28 | .end(done); 29 | }); 30 | }); 31 | --------------------------------------------------------------------------------