question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

efficientnetBx model.save() fails due to serialization problem with tf2.10.0

See original GitHub issue

System information.

  • Have I written custom code: derived from Keras image_classification_efficientnet_fine_tuning
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Win10Pro
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): tf2.10.0
  • Python version: 3.10.2
  • Bazel version (if compiling from source):
  • GPU model and memory: NVIDIA RTX TITAN 24Gb
  • Exact command to reproduce: model.save()

Describe the problem clearly here. Be sure to convey here why it’s a bug in Keras or why the requested feature is needed.

Describe the current behavior. model save() fails and reports a serialization problem.

Describe the expected behavior. saving keras model without error.

Contributing.

  • Do you want to contribute a PR? (yes/no): no

Source code / logs.

WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op while saving (showing 5 of 273). These functions will not be directly callable after loading. INFO:tensorflow:Assets written to: ./models/EfficientNetB7_Naiads.h5py\assets INFO:tensorflow:Assets written to: ./models/EfficientNetB7_Naiads.h5py\assets Output exceeds the size limit. Open the full output data in a text editor

TypeError Traceback (most recent call last) Cell In [31], line 1 ----> 1 model.save(‘./models/EfficientNetB7_Naiads.h5py’)

File e:\02- Vision Projects\01- Naiads Projects\notebooks.venv\lib\site-packages\keras\utils\traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs) 67 filtered_tb = _process_traceback_frames(e.traceback) 68 # To get the full stack trace, call: 69 # tf.debugging.disable_traceback_filtering() —> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb

File C:\Python310\lib\json\encoder.py:199, in JSONEncoder.encode(self, o) 195 return encode_basestring(o) 196 # This doesn’t pass the iterator directly to ‘’.join() because the 197 # exceptions aren’t as detailed. The list call should be roughly 198 # equivalent to the PySequence_Fast that ‘’.join() would do. –> 199 chunks = self.iterencode(o, _one_shot=True) 200 if not isinstance(chunks, (list, tuple)): 201 chunks = list(chunks)

File C:\Python310\lib\json\encoder.py:257, in JSONEncoder.iterencode(self, o, _one_shot) 252 else: 253 _iterencode = _make_iterencode( … 255 self.key_separator, self.item_separator, self.sort_keys, 256 self.skipkeys, _one_shot) –> 257 return _iterencode(o, 0)

TypeError: Unable to serialize [2.0897 2.1129 2.1082] to JSON. Unrecognized type <class ‘tensorflow.python.framework.ops.EagerTensor’>.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
hctomkinscommented, Dec 15, 2022

NB if useful:

This issue seems to be triggered by the fact the hard-baked normalisation constants are getting evaluated to an EagerTensor before the scaling layer is built - see here

I fixed this locally by moving the logic into python:

At the top:

IMAGENET_STDDEV_RGB = [0.229, 0.224, 0.225]
IMAGENET_STDDEV_RGB = [1/math.sqrt(i) for i in IMAGENET_STDDEV_RGB]

Then on build just do: x = layers.Rescaling(IMAGENET_STDDEV_RGB)(x)

Don’t have time to a raise a PR rn /w failure test cases (also suspect this isn’t the most elegant solution), but thought at least a guide to hotfix might help for anyone that does!

3reactions
Dobiasdcommented, Nov 28, 2022

Same problem here:

import tensorflow as tf
model = tf.keras.applications.efficientnet.EfficientNetB0()
model.save('model.json')

->

Traceback (most recent call last):
  File "//main.py", line 4, in <module>
    model.save('model.json')
  File "/usr/local/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/usr/local/lib/python3.10/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/lib/python3.10/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
TypeError: Unable to serialize [2.0896919 2.1128857 2.1081853] to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.

Here is a Dockerfile to easily reproduce it:

FROM python:3.10.8

RUN apt-get update
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get install -y build-essential cmake

RUN pip3 install tensorflow==2.10.0

RUN echo "\n\
import tensorflow as tf\n\
model = tf.keras.applications.efficientnet.EfficientNetB0()\n\
model.save('model.json')\n\
" > main.py

RUN python3 main.py
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using EfficientNetB0 and save model will result `Unable to ...
Hello, I'm trying to use EfficientNetB0 to create a model and save the model to my local disk. However, when saving it, it...
Read more >
output exceeds the size limit. open the full output ... - You.com
Getting error in pyspark using vscode "Py4JJavaError : Output exceeds the size limit ... model.save() fails due to serialization problem with tf2.10.0#17199.
Read more >
Unable to save model with tensorflow 2.0.0 beta1
It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable....
Read more >
Please, Help Us Save TF2 - YouTube
If you want to help us save TF2, please spread the message of TF2's botting problem. If you want to help with our...
Read more >
Keras keras Issues - Giters
tf.keras.utils.get_file() produces duplicate file with dot underscore "._" ... efficientnetBx model.save() fails due to serialization problem with tf2.10.0.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found