mrdbourke / tensorflow-deep-learning

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

Error saving the model in Section 8 #545

Open ivanthecrazy opened 1 year ago

ivanthecrazy commented 1 year ago

Section 8, video 189. I'm getting this error when saving the model:

model.save("drive/MyDrive/101_food_classes_10_percent_saved_big_dog_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.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[<ipython-input-17-7b06e6431aba>](https://localhost:8080/#) in <cell line: 1>()
----> 1 model.save("drive/MyDrive/101_food_classes_10_percent_saved_big_dog_model")

2 frames
[/usr/lib/python3.10/json/encoder.py](https://localhost:8080/#) in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    258 
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

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

I found out that tf downgrade to 2.9 should do the job, but I'm unable to install 2.9 on colab.

ostadnavid commented 1 year ago

I think this is because your model includes custom layers or functions that are not tracked by Keras. try saving with h5 format:

model.save("saved_model", save_format='tf')

or you can save weight and biases and then import them

model.save_weights("model_weights.h5")

model.load_weights("model_weights.h5")
ivanthecrazy commented 1 year ago

@ostadnavid I already opened another issue with saving weights and biases :)

mrdbourke commented 1 year ago

Hi Ivan, can you link to the notebook you're working with here?

I'm currently working through trying to find a fix.

Seems to be older versions of TensorFlow working (e.g. 2.9.0) but later versions are causing errors with tf.keras.applications.efficientnet models.

I see you posted here as well: https://github.com/mrdbourke/tensorflow-deep-learning/issues/544 (more info here)

Thank you for that, will try to sort both of these out.

m-gopichand commented 1 year ago

Hi @ivanthecrazy , this may help , The problem is with the models rescaling layer. 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)

Onturk89 commented 1 year ago

!pip install tensorflow==2.9 pip install --force-reinstall -v protobuf==3.20.3

from tensorflow.keras import mixed_precision mixed_precision.set_global_policy(policy="float32") # set global policy to mixed precision

ivanthecrazy commented 1 year ago

Thank you guys, with correct downgrade it seems to work