Deploying to the Browser using TensorFlow.js

https://js.tensorflow.org/tutorials/import-keras.html


In [1]:
import warnings
warnings.filterwarnings('ignore')

In [2]:
%matplotlib inline
%pylab inline
import matplotlib.pyplot as plt


Populating the interactive namespace from numpy and matplotlib

In [3]:
import pandas as pd
print(pd.__version__)


0.23.4

In [4]:
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
print(tf.__version__)


1.10.0

In [5]:
# let's see what compute devices we have available, hopefully a GPU 
sess = tf.Session()
devices = sess.list_devices()
for d in devices:
    print(d.name)


/job:localhost/replica:0/task:0/device:CPU:0
/job:localhost/replica:0/task:0/device:GPU:0

In [6]:
# a small sane check, does tf seem to work ok?
hello = tf.constant('Hello TF!')
print(sess.run(hello))


b'Hello TF!'

Loading and validating our model


In [7]:
!curl -O https://raw.githubusercontent.com/DJCordhose/ai/master/notebooks/manning/model/insurance.hdf5


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  175k  100  175k    0     0   401k      0 --:--:-- --:--:-- --:--:--  401k

In [14]:
model = tf.keras.models.load_model('insurance.hdf5')

Descison Boundaries for 2 Dimensions


In [15]:
# a little sane check, does it work at all?

# within this code, we expect Olli to be a green customer with a high prabability
# 0: red
# 1: green
# 2: yellow

olli_data = [100, 47, 10]

X = np.array([olli_data])
model.predict(X)


Out[15]:
array([[0.00334801, 0.8710344 , 0.1256177 ]], dtype=float32)

Converting our Keras Model to TensorFlow.js


In [10]:
!pip install tensorflowjs


Requirement already satisfied: tensorflowjs in c:\programdata\anaconda3\lib\site-packages (0.1.0)
Requirement already satisfied: tensorflow>=1.6.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflowjs) (1.10.0)
Requirement already satisfied: h5py>=2.7.1 in c:\programdata\anaconda3\lib\site-packages (from tensorflowjs) (2.8.0)
Requirement already satisfied: six>=1.11.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflowjs) (1.11.0)
Requirement already satisfied: numpy>=1.14.1 in c:\programdata\anaconda3\lib\site-packages (from tensorflowjs) (1.14.5)
Requirement already satisfied: keras>=2.1.4 in c:\programdata\anaconda3\lib\site-packages (from tensorflowjs) (2.1.4)
Requirement already satisfied: absl-py>=0.1.6 in c:\programdata\anaconda3\lib\site-packages (from tensorflow>=1.6.0->tensorflowjs) (0.3.0)
Requirement already satisfied: gast>=0.2.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow>=1.6.0->tensorflowjs) (0.2.0)
Requirement already satisfied: termcolor>=1.1.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow>=1.6.0->tensorflowjs) (1.1.0)
Requirement already satisfied: wheel>=0.26 in c:\programdata\anaconda3\lib\site-packages (from tensorflow>=1.6.0->tensorflowjs) (0.31.1)
Requirement already satisfied: protobuf>=3.6.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow>=1.6.0->tensorflowjs) (3.6.0)
Requirement already satisfied: tensorboard<1.11.0,>=1.10.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow>=1.6.0->tensorflowjs) (1.10.0)
Requirement already satisfied: grpcio>=1.8.6 in c:\programdata\anaconda3\lib\site-packages (from tensorflow>=1.6.0->tensorflowjs) (1.14.1)
Requirement already satisfied: astor>=0.6.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow>=1.6.0->tensorflowjs) (0.7.1)
Requirement already satisfied: setuptools<=39.1.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow>=1.6.0->tensorflowjs) (39.1.0)
Requirement already satisfied: scipy>=0.14 in c:\programdata\anaconda3\lib\site-packages (from keras>=2.1.4->tensorflowjs) (1.1.0)
Requirement already satisfied: pyyaml in c:\programdata\anaconda3\lib\site-packages (from keras>=2.1.4->tensorflowjs) (3.13)
Requirement already satisfied: werkzeug>=0.11.10 in c:\programdata\anaconda3\lib\site-packages (from tensorboard<1.11.0,>=1.10.0->tensorflow>=1.6.0->tensorflowjs) (0.14.1)
Requirement already satisfied: markdown>=2.6.8 in c:\programdata\anaconda3\lib\site-packages (from tensorboard<1.11.0,>=1.10.0->tensorflow>=1.6.0->tensorflowjs) (2.6.11)
twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.
thinc 6.11.2 requires hypothesis<3,>=2, which is not installed.
thinc 6.11.2 requires msgpack-python==0.5.4, which is not installed.
thinc 6.11.2 has requirement msgpack-numpy==0.4.1, but you'll have msgpack-numpy 0.4.3 which is incompatible.
tensorflow-tensorboard 1.5.1 has requirement bleach==1.5.0, but you'll have bleach 2.1.3 which is incompatible.
tensorflow-tensorboard 1.5.1 has requirement html5lib==0.9999999, but you'll have html5lib 1.0.1 which is incompatible.
spacy 2.0.11 has requirement regex==2017.4.5, but you'll have regex 2018.6.21 which is incompatible.
spacy 2.0.11 has requirement thinc<6.11.0,>=6.10.1, but you'll have thinc 6.11.2 which is incompatible.
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

Use the command line tool

Does not always give the best error messages, but seems more robust than the Python API


In [ ]:
!rm -rf tfjs
!mkdir tfjs

In [21]:
!tensorflowjs_converter --input_format keras \
                       ./model/insurance.hdf5 \
                       ./tfjs


Using TensorFlow backend.