Preparing a TensorFlow model


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


1.10.0

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   511k      0 --:--:-- --:--:-- --:--:--  511k

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

In [9]:
!rm -rf tf

In [10]:
import os

export_path_base = 'tf'
version = 1
export_path = os.path.join(
      tf.compat.as_bytes(export_path_base),
      tf.compat.as_bytes(str(version)))

tf.keras.backend.set_learning_phase(0)
sess = tf.keras.backend.get_session()

classification_inputs = tf.saved_model.utils.build_tensor_info(model.input)
classification_outputs_scores = tf.saved_model.utils.build_tensor_info(model.output)

signature =  tf.saved_model.signature_def_utils.build_signature_def(
    inputs={'inputs': classification_inputs},
    outputs={'scores': classification_outputs_scores},
    method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)

builder = tf.saved_model.builder.SavedModelBuilder(export_path)
builder.add_meta_graph_and_variables(
      sess, [tf.saved_model.tag_constants.SERVING],
      signature_def_map={
           tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
      })
builder.save()


Out[10]:
b'tf\\1\\saved_model.pb'

In [ ]: