Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of serialization error in keras model
seed = 10
import random
random.seed(seed)
import numpy as np
np.random.seed(seed)
import tensorflow as tf
tf.reset_default_graph()
tf.random.set_random_seed(seed)
import tensorflow.keras.backend as K
import tensorflow.keras.layers as kl
import tensorflow.keras.activations as ka
import tensorflow.keras.losses as klo
import tensorflow.keras.models as km
import tensorflow.keras.optimizers as ko
def create_model(X_dim):
X_input = kl.Input(shape=(X_dim,))
X = X_input
X = kl.Dense(10)(X)
X = kl.Dense(1)(X)
X = K.expand_dims(X, axis=2)
X = K.squeeze(X, axis=-1)
y_output = X
model = km.Model(inputs=X_input, outputs=y_output)
optimizer = ko.Adam(learning_rate=0.01)
loss = klo.MeanSquaredError()
model.compile(optimizer, loss)
return model
X_dim = 3
X = np.random.randn(100, X_dim)
y = np.random.randn(100)
model = create_model(X_dim)
yp = model.predict(X)
model.fit(X, y, verbose=10)
model.save('/tmp/here')
@cottrell

This comment has been minimized.

Copy link
Owner Author

@cottrell cottrell commented Mar 27, 2019

Traceback is this:

WARNING: Logging before flag parsing goes to stderr.
W0327 14:37:03.614798 140735813960576 deprecation.py:506] From .../envs/gi/lib/python3.6/site-packages/tensorflow/python/ops/init_ops.py:1257: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
2019-03-27 14:37:03.653723: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
  File "minimal_broken_gist.py", line 38, in <module>
    model.save('/tmp/here')
  File ".../lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1311, in save
    save_model(self, filepath, overwrite, include_optimizer)
  File ".../lib/python3.6/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 103, in save_model
    default=serialization.get_json_type).encode('utf8')
  File ".../lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File ".../lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File ".../lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File ".../lib/python3.6/site-packages/tensorflow/python/util/serialization.py", line 69, in get_json_type
    raise TypeError('Not JSON Serializable:', obj)
TypeError: ('Not JSON Serializable:', b'\n\nExpandDims\x12\nExpandDims\x1a\x0fdense_1/BiasAdd\x1a\x0eExpandDims/dim*\n\n\x04Tdim\x12\x020\x03*\x07\n\x01T\x12\x020\x01')
@cottrell

This comment has been minimized.

Copy link
Owner Author

@cottrell cottrell commented Mar 27, 2019

Possibly related? keras-team/keras#9342

@cottrell

This comment has been minimized.

Copy link
Owner Author

@cottrell cottrell commented Mar 27, 2019

And this is on tf-nightly with

In [7]: tf.__version__
Out[7]: '1.14.1-dev20190327'
@cottrell

This comment has been minimized.

Copy link
Owner Author

@cottrell cottrell commented Mar 27, 2019

python 3.6.8 in case that is relevant.

@tucan9389

This comment has been minimized.

Copy link

@tucan9389 tucan9389 commented Apr 1, 2019

I have same issue

python: 3.5.5
tenorflow: 2.0.0-alpha0
keras version: 2.2.4-tf

W0401 11:10:08.476782 139886707607296 tf_logging.py:161] Model failed to serialize as JSON. Ignoring... ('Not JSON Serializable:', b'\n\tRelu6_105\x12\x05Relu6\x1a(batch_normalization_v2_105/cond/Identity*\x07\n\x01T\x12\x020\x01')

when run this.

save_path = output_path + "outputs/models/" + 'weights.{epoch:02d}-{val_loss:.2f}.hdf5'
check_pointer = tf.keras.callbacks.ModelCheckpoint(save_path, save_best_only=True)

model.fit(data, epochs=5, callbacks=[check_pointer])
@ShaneTian

This comment has been minimized.

Copy link

@ShaneTian ShaneTian commented Apr 15, 2019

So, did you have a good solution?

My code is

parallel_model = keras.utils.multi_gpu_model(model, gpus=2)
parallel_model.compile(tf.optimizers.Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
parallel_model.summary()
save_path = "./results/model.h5"
tb_callback = keras.callbacks.TensorBoard(args.log_dir, histogram_freq=0.1, write_graph=True,
                                          write_grads=True, write_images=True, embeddings_freq=0.5, update_freq='batch')
history = parallel_model.fit(x=x_train, y=y_train, batch_size=args.batch_size, epochs=args.epochs,
                             callbacks=[tb_callback], validation_split=args.fraction_validation, shuffle=True)
model.save(save_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment