Skip to content

Notebook 05: TypeError: Unable to serialize [2.0897 2.1129 2.1082] to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'> (fix inside) #553

@mrdbourke

Description

@mrdbourke

Update for August 2023

If you're using TensorFlow 2.10+, I've found the best fix for tf.keras.applications.efficient.EfficientNetB0 problems is to simply upgrade to tf.keras.applications.efficientnet_v2.EfficientNetV2B0.

You can see a full write-up of the fix here: #575

In short:

New:

base_model = tf.keras.applications.efficientnet_v2.EfficientNetV2B0(include_top=False)

Old:

base_model = tf.keras.applications.efficientnet.EfficientNetB0(include_top=False)

If for some reason, you'd like to keep using tf.keras.applications.efficientnet.X models, keep reading below.


If you're trying to save tf.keras.applications.efficientnet.X models in TensorFlow 2.10+, you may see the following issue:

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

According to the Keras GitHub issues thread (see: keras-team/tf-keras#383) this is an issue with one of the rescaling layers.

There are several solutions in the linked thread above, however, a consistent fix I've found (as of May 2023) is to use TensorFlow 2.9.0:

# Install TensorFlow 2.9.0 ("-U" stands for "update", "-q" stands for "quiet")
!pip install -U -q tensorflow==2.9.0

import tensorflow as tf
print(f"TensorFlow version: {tf.__version__}")

If you're using Google Colab, you can run the code at the top of your notebook and run the rest of Notebook 05: https://github.com/mrdbourke/tensorflow-deep-learning/blob/main/05_transfer_learning_in_tensorflow_part_2_fine_tuning.ipynb without errors.


I tried to run this gist which claims fixes in tf-nightly(2.13.0-dev20230409), however, I found that the fix didn't work in a later version tf-nightly(2.14.0-dev20230520).

Hopefully it gets fixed in future versions of TensorFlow.


See a thread about possible fixes here: #544


Another alternative if you're using a later version of TensorFlow (2.10+) may be to try another model from tf.keras.applications.efficientnet_v2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions