Skip to content

Instantly share code, notes, and snippets.

@cottrell
Created March 27, 2019 14:35
Show Gist options
  • Save cottrell/580c3c49e13c3317038d6351f82f5156 to your computer and use it in GitHub Desktop.
Save cottrell/580c3c49e13c3317038d6351f82f5156 to your computer and use it in GitHub Desktop.
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
Copy link
Author

cottrell commented Mar 27, 2019

python 3.6.8 in case that is relevant.

@tucan9389
Copy link

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
Copy link

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