tensorflow / tensorflow/probability

keras_saved_model fails becase model is not json serializable

Open
#325 12 comments 5 reactions 1 assignee View on GitHub

@jburnim is already working on this.

Since Mar 11, 2019.

keras layers
Dominant language
Jupyter Notebook
Stars
4.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

tf 1.13
tfp 0.6
ubuntu 18.04

Trying to save a Bayesian neural net in a way that can be run in production using either the java or c++ api. I've tried

save_keras_model(model, saved_model_path='model_path', serving_only=True, custom_objects={'DenseFlipout': tfp.layers.DenseFlipout, 'Conv2DFlipout': tfp.layers.Convolution2DFlipout})

and just:
save_keras_model(model, saved_model_path='model_path', serving_only=True)

Both fail because model.to_json() fails

This minimal example produces the below error:

`import tensorflow as tf
import tensorflow_probability as tfp
from tensorflow.contrib.saved_model import save_keras_model

tf.enable_eager_execution()

neural_net = tf.keras.Sequential([
tfp.layers.Convolution2DFlipout(6,
input_shape=(3, 255, 255),
kernel_size=5,
padding="SAME",
activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D(pool_size=[2, 2],
strides=[2, 2],
padding="SAME"),
tfp.layers.Convolution2DFlipout(16,
kernel_size=5,
padding="SAME",
activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D(pool_size=[2, 2],
strides=[2, 2],
padding="SAME"),
tfp.layers.Convolution2DFlipout(120,
kernel_size=5,
padding="SAME",
activation=tf.nn.relu),
tf.keras.layers.Flatten(),
tfp.layers.DenseFlipout(84, activation=tf.nn.relu),
tfp.layers.DenseFlipout(10)
])

save_keras_model(neural_net, saved_model_path='model_path', serving_only=True)
`

File "/home/tony/.conda/envs/tf/lib/python3.6/site-packages/tensorflow/contrib/saved_model/python/saved_model/keras_saved_model.py", line 141, in save_keras_model
_export_model_json(model, export_dir)
File "/home/tony/.conda/envs/tf/lib/python3.6/site-packages/tensorflow/contrib/saved_model/python/saved_model/keras_saved_model.py", line 151, in _export_model_json
model_json = model.to_json()
File "/home/tony/.conda/envs/tf/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1563, in to_json
return json.dumps(model_config, default=get_json_type, **kwargs)
File "/home/tony/.conda/envs/tf/lib/python3.6/json/init.py", line 238, in dumps
**kw).encode(obj)
File "/home/tony/.conda/envs/tf/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/tony/.conda/envs/tf/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/tony/.conda/envs/tf/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1560, in get_json_type
raise TypeError('Not JSON Serializable:', obj)
TypeError: ('Not JSON Serializable:', <function default_loc_scale_fn.._fn at 0x7f8a640f2378>)

Are there any tested ways of taking tensorflow_probability to production other than running pure python?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.