mrdbourke / tensorflow-deep-learning

All course materials for the Zero to Mastery Deep Learning with TensorFlow course.

Home Page:https://dbourke.link/ZTMTFcourse

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

mrdbourke opened this issue · comments

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.

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:
  • if you're using a conda environment, it is likely located in your env/Lib/site-packages/keras/applications/ folder
  • if you're using a Docker container, it is likely located in /usr/local/lib/pythonX.X/dist-packages/keras/applications/
  • if you're using something else, you can try pip show keras function to show you the location of keras, then open its "applications" subdirectory. Or use your OS' search function to find efficientnet.py directly. If you have multiple environments, make sure to find the correct one, or just edit them all.
  1. edit efficientnet.py:
  • locate the following line: x = layers.Rescaling(1.0 / tf.math.sqrt(IMAGENET_STDDEV_RGB))(x) (it will be around line 367, depending on version, of course)
  • comment the line out, just in case you need to restore it
  • insert this code directly below: x = layers.Rescaling( [1.0 / math.sqrt(stddev) for stddev in IMAGENET_STDDEV_RGB] )(x)
  • save the file and close it.

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:
keras-team/tf-keras#383

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.
tensorflow/tensorflow#61366

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: #575