Using EfficientNetB0 and save model will result `Unable to serialize [2.0896919 2.1128857 2.1081853] to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.`

Hello,

I’m trying to use EfficientNetB0 to create a model and save the model to my local disk. However, when saving it, it throws the error below, can anyone helps?

TypeError: Unable to serialize [2.0896919 2.1128857 2.1081853] to JSON. Unrecognized type <class 'tensorflow.python.framework.ops.EagerTensor'>.

Souce code of mine:

import os
import tensorflow as tf
from keras.applications import EfficientNetB0

os.environ["CUDA_VISIBLE_DEVICES"]="0" # single GPU
strategy = tf.distribute.MirroredStrategy(["GPU:0"])

print('\nnumber of devices using for training: {}'.format(strategy.num_replicas_in_sync))
print(tf.__version__)
with strategy.scope():
    print('-- building model from scratch --')
    img_dimension = (150, 280)
    input_shape = (96, img_dimension[0], img_dimension[1], 3)
    base_model = EfficientNetB0(weights='imagenet', input_shape=(input_shape[1:]), include_top=False,
                                drop_connect_rate=0.2)
    base_model.save("D:/Program Files (x86)/CodeRepos/StartupProject/Training_Data/Output/demo")

Full error stacktrace

"D:\Program Files (x86)\CodeRepos\AutoUploader\venv\Scripts\python.exe" "D:/Program Files (x86)/CodeRepos/AutoUploader/main.py" 
2022-10-19 21:31:07.149157: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-10-19 21:31:07.149769: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2022-10-19 21:31:09.584702: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2022-10-19 21:31:09.584908: W tensorflow/stream_executor/cuda/cuda_driver.cc:263] failed call to cuInit: UNKNOWN ERROR (303)
2022-10-19 21:31:09.591677: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-ETGPDN3
2022-10-19 21:31:09.591897: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-ETGPDN3
2022-10-19 21:31:09.592595: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
WARNING:tensorflow:Some requested devices in `tf.distribute.Strategy` are not visible to TensorFlow: /job:localhost/replica:0/task:0/device:GPU:0

number of devices using for training: 1
2.10.0
-- building model from scratch --
WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
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 81). These functions will not be directly callable after loading.
Traceback (most recent call last):
  File "D:\Program Files (x86)\CodeRepos\AutoUploader\main.py", line 16, in <module>
    base_model.save("D:/Program Files (x86)/CodeRepos/StartupProject/Training_Data/Output/demo")
  File "D:\Program Files (x86)\CodeRepos\AutoUploader\venv\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\Whaley\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Users\Whaley\AppData\Local\Programs\Python\Python39\lib\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'>.

Process finished with exit code 1

1 Like

Can you tell us what version of the TF you are using? Thank you

Thanks for reaching out! I’m using TF V2.10

Got the same error message with exactly the same list of numbers [2.0896919 2.1128857 2.1081853] when saving a model using tf2.10 and EfficientnetB2 for a 51 class image classification. The error occurs after the first epoch when the model should be saved.

So I tried to downgrade tensorflow from V2.10 to V2.9.1, this function works fine. In other words, this is a bug in 2.10.0. Hope it helps and please fix this bug for V2.10

2 Likes

Even in Colab, when i tried to import EfficientNetB0 from keras or tensorflow i also faced the same problem using TF 210. Please find the gist for reference. Thank you.

Thanks for the input. From the gist you provided, it seems the working one is using tensorflow @ 2.8.2, can you confirm? Also, if I only installed tensorflow @ 2.10, I can’t find the library tensorflow.keras.applications.efficientnet at all since the tensorflow.keras doesn’t exist.

I just came across this thread - I think I have the same problem and it looks like downgrading to V2.9.1 fixes it.

I’m also using a model based on efficientnet:

model = tf.keras.applications.EfficientNetB0(
  include_top=False,
  input_tensor=inputs,
  weights="imagenet",
)

What I tested:

  • V2.10:

    • model.save(include_optimizer=True) does not work
    • model.save(include_optimizer=False) does not work
    • tf.saved_model.save() does work but then it’s not a keras model when I reload later
  • V2.9.1:

    • model.save() works

Yes, It is not working with TF2.10. Sorry for the confusion.

In TF2.10, please refer to tf.keras.applications.efficientnet.EfficientNetB0

Hi chundruiv@, thanks for the comments. I tried the code you posted in the colab, TF2.10 still not work even with tf.keras.applications.efficientnet.EfficientNetB0.

I think only works in lower versions of TF. Let me know if I need to create issues in github.

I think only works in lower versions of TF. Let me know if I need to create issues in github.

Yes, Please file a bug in Keras Repo. Thank you!

3 Likes

Also ran into this issue - downgrading from 2.10 to 2.9 solved the issue for now.

Thank you for the solution! Had the same problem and downgrading from 2.10 to 2.9 helped. As for now, I would really like to upgrade to 2.11 for its new helpful features but I seem to have the same issue with model saving again. Could you please tell if this issue is fixed in tensorflow 2.11? or maybe new work-arounds except downgrading are invented to fix the issue temporarily? @chunduriv

Same issue here with 2.11.0.
TypeError: Unable to serialize [2.0896919 2.1128857 2.1081853] to JSON. Unrecognized type <class ‘tensorflow.python.framework.ops.EagerTensor’>.

Yes, the problem is still happening (Tf<2.11 (in other words 2.10.1)) but downgrading to 2.9.1 solves it.

January 15th 2023

Is there any workaround for this issue? I just spent 24hrs training my model. I dont want to retrain it again.

1 Like

@Alankrit_Mishra, @Susanna_Gimaeva,

PR #17498 was created to address serialization error with EfficientNet.

Thank you!

hey, i just downgraded tensorflow version to 2.9.1, and it worked !

Hi, 2.9.1 doesn’t work with a callback, it seems that there are inconsistencies present.

I’m using the following code and tried downgrading tf to this version but it didn’t work.

history = model.fit(train_dataset,
epochs=Num_epochs,
validation_data=validation_dataset,
callbacks=[checkpoint, stop_early])

I’m still getting the same error. I’m using EfficientNetV1B7 btw and saving the model as an hdf5 file

1 Like

update on my comment, it works on 2.9.0 (not 2.9.1). Thanks everyone, hoping for a fix for this soon @devs

1 Like