mrdbourke / tensorflow-deep-learning

All course materials for the Zero to Mastery Deep Learning with TensorFlow course.
https://dbourke.link/ZTMTFcourse
MIT License
5.05k stars 2.5k forks source link

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

Closed mrdbourke closed 11 months ago

mrdbourke commented 1 year ago

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: https://github.com/mrdbourke/tensorflow-deep-learning/discussions/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: https://github.com/keras-team/tf-keras/issues/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: https://github.com/mrdbourke/tensorflow-deep-learning/issues/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.

arpadikuma commented 1 year ago

There is a way to edit the efficientnet.py file directly to correct this issue, letting us use efficientnet with tf 2.10 and above. It is advised to create a backup of this file before proceeding with the changes.

That being out of the way, here are the steps:

  1. locate efficientnet.py:
  1. edit efficientnet.py:

that's basically it! If you managed to do these steps correctly, you will now have gotten rid of this annoying error!

One extra info for Docker containers - if you work within the Docker container, you will likely have to install an editor first using apt-get install vim or apt-get install nano or whatever else you want to use.

thanks to Mik0ri in Discord for bringing this to my attention!

Source: https://github.com/keras-team/tf-keras/issues/383

Seferovic8 commented 12 months ago

There is a bug in TensorFlow versions 2.10, 2.11 and 2.11. You need to upgrade tensorflow to the 2.13 version or downgrade to 2.9.

Here is the tensorflow issue link with the same problem. https://github.com/tensorflow/tensorflow/issues/61366

mrdbourke commented 11 months ago

Hi all,

After much troubleshooting, I've found the best fix for tf.keras.applications.EfficientNetB0 problems is to simply upgrade to tf.keras.applications.efficientnet_v2.EfficientNetV2B0.

You can see a full write-up of the fix here: https://github.com/mrdbourke/tensorflow-deep-learning/discussions/575