Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TF2.0] Not JSON Serializable error wasn thrown when using tf.keras.activations operators in keras model. #28010

Closed
KichangKim opened this issue Apr 21, 2019 · 15 comments
Assignees
Labels
comp:keras Keras related issues TF 2.0 Issues relating to TensorFlow 2.0 type:bug Bug

Comments

@KichangKim
Copy link

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10 x64 1809
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary): pip
  • TensorFlow version (use command below): tensorflow-gpu 2.0.0a0
  • Python version: 3.6.7
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version: 10
  • GPU model and memory: Geforce 1070

Describe the current behavior
When I used tf.keras.activations operators in my keras model, serialization of model was failed due to Not JSON Serializable error.

Describe the expected behavior
It should be serialized without any error.

Code to reproduce the issue

from tensorflow import keras
from tensorflow.keras import layers

inputs = keras.Input(shape=(784,), name='digits')
x = layers.Activation('relu')(inputs)
# x = keras.activations.relu(inputs)
outputs = layers.Dense(10, activation='softmax', name='predictions')(x)


model = keras.Model(inputs=inputs, outputs=outputs, name='3_layer_mlp')
model.summary()

model.save('path_to_my_model.h5')

If you changed from Activation() to relu, it failed to serialize.

@KichangKim KichangKim changed the title Not JSON Serializable error wasn thrown when using tf.keras.activations operators in keras model. [TF2.0] Not JSON Serializable error wasn thrown when using tf.keras.activations operators in keras model. Apr 22, 2019
@achandraa achandraa self-assigned this Apr 22, 2019
@achandraa achandraa added comp:keras Keras related issues type:bug Bug labels Apr 22, 2019
@achandraa achandraa assigned ymodak and unassigned achandraa Apr 22, 2019
@ymodak
Copy link
Contributor

ymodak commented Apr 29, 2019

I have slightly edited your code to be consistent with tf.keras and was able to reproduce the error using TF-gpu 2.0 alpha.

import tensorflow as tf
#from tensorflow import keras
#from tensorflow.keras import layers

inputs = tf.keras.Input(shape=(784,), name='digits')
#x = tf.keras.layers.Activation('relu')(inputs)
x = tf.keras.activations.relu(inputs)
outputs = tf.keras.layers.Dense(10, activation='softmax', name='predictions')(x)

model = tf.keras.Model(inputs=inputs, outputs=outputs, name='3_layer_mlp')
model.summary()

model.save('path_to_my_model.h5')

Output:

---> 13 model.save('path_to_my_model.h5')
TypeError: ('Not JSON Serializable:', b'\n\x06Relu_8\x12\x04Relu\x1a\tdigits_25*\x07\n\x01T\x12\x020\x01')

@ymodak ymodak assigned fchollet and unassigned ymodak Apr 29, 2019
@ymodak ymodak added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Apr 29, 2019
@arthurflor23
Copy link

arthurflor23 commented May 1, 2019

I got the same error, but with:

  • tf.keras.backend.expand_dims
  • tf.keras.backend.squeeze
  • tf.expand_dims
  • tf.squeeze

returns:

W0501 00:06:16.129765 139790005454464 tf_logging.py:161] Model failed to serialize as JSON. Ignoring... ('Not JSON Serializable:', b'\n\nExpandDims\x12\nExpandDims\x1a\x05input\x1a\x0eExpandDims/dim*\x07\n\x01T\x12\x020\x01*\n\n\x04Tdim\x12\x020\x03')

@ymodak ymodak added the TF 2.0 Issues relating to TensorFlow 2.0 label May 2, 2019
@KichangKim
Copy link
Author

Additionally, using "+" operator instead of tf.keras.layers.Add makes this issue same.

@luaGeeko
Copy link

I am facing the same issue, any suggestions or solutions for work around for now ??

@mketcha
Copy link

mketcha commented Jul 3, 2019

I am having the same issue when using tf.concat in an otherwise keras-only model (can't use keras Concatenations due to issue #30355 ).

Only work around I have found so far is to use model.save_weights(). When loading, you need to redefine the model and then use model.load_weights()

@ismael-elatifi
Copy link

ismael-elatifi commented Jul 17, 2019

Having same issue with TF 1.14 and Python 3.6 with tf.transpose and tensorflow.python.keras.layers.concatenate :

W0717 09:24:35.629342 14908 summary_ops_v2.py:1110] Model failed to serialize as JSON. Ignoring... ('Not JSON Serializable:', b'\n\ttranspose\x12\tTranspose\x1a\ndense/Relu\x1a\x0etranspose/perm*\x0b\n\x05Tperm\x12\x020\x03*\x07\n\x01T\x12\x020\x01')

W0717 13:56:19.515430 16984 summary_ops_v2.py:1110] Model failed to serialize as JSON. Ignoring... ('Not JSON Serializable:', b'\n\x05Shape\x12\x05Shape\x1a\x15concatenate_28/concat*\x07\n\x01T\x12\x020\x01*\x0e\n\x08out_type\x12\x020\x03')

Thus callbacks.ModelCheckpoint with save_weights_only=False causes the training to stop because it fails to backup the model (I get a useless model file of 6 KB).
As @mketcha mentionned, a workaround is to use save_weights_only=True. But it should only be temporary waiting a fix because saving only the weights forces to keep the code of the corresponding model somewhere and this is really annoying if the model code changes often.

@Daniel451
Copy link

Daniel451 commented Jul 20, 2019

Same problem here. Is this being worked on?

 Model failed to serialize as JSON. Ignoring...
('Not JSON Serializable:',
b'\n\tLeakyRelu\x12\tLeakyRelu\x1a\x1cbatch_normalization/Identity*\x07\n\x01T\x12\x020\x01*\x0e\n\x05alpha\x12\x05%\xcd\xccL>')

TensorFlow 2.0.0-b1

@omalleyt12
Copy link
Contributor

Was able to repro and have a fix out for this, should be in in a day or two

@tensorflowbutler tensorflowbutler removed the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Jul 23, 2019
@loiccordone
Copy link

Hello,
Any news? Thanks

@ppham27
Copy link
Contributor

ppham27 commented Jul 29, 2019

Hello,
Any news? Thanks

Should be fixed: 7cc180f

@ymodak
Copy link
Contributor

ymodak commented Aug 2, 2019

Closing this issue since its fixed in latest tf nightly build '2.0.0-dev20190802'
Thanks!

@ymodak ymodak closed this as completed Aug 2, 2019
@tensorflow-bot
Copy link

tensorflow-bot bot commented Aug 2, 2019

Are you satisfied with the resolution of your issue?
Yes
No

@yassinetb
Copy link

yassinetb commented Mar 15, 2020

Had the problem callbacks.ModelCheckpoint and ModelCheckpoint, setting save_weights_only=True solved the problem for me.

@2020zyc
Copy link

2020zyc commented Sep 2, 2020

Had the problem callbacks.ModelCheckpoint and ModelCheckpoint, setting save_weights_only=True solved the problem for me.

@yassinetb Did you have any solution for the error "TypeError: ('Not JSON Serializable:', tf.float32)" from setting save_weights_only=False

@ravinderkhatri
Copy link

I am facing the same issue using tf.keras.callbacks.ModelCheckpoint . Any solution ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues TF 2.0 Issues relating to TensorFlow 2.0 type:bug Bug
Projects
None yet
Development

No branches or pull requests