keras
keras copied to clipboard
Errors in loading TF model
trafficstars
Click to expand!
Issue Type
Bug
Source
binary
Tensorflow Version
tensorflow-macos 2.9.2, colab 2.8.2, linux 2.8.2
Custom Code
No
OS Platform and Distribution
mac m1, linux, colab
Mobile device
No response
Python version
3.8
Bazel version
No response
GCC/Compiler version
No response
CUDA/cuDNN version
No response
GPU model and memory
No response
Current Behaviour?
Error when loading model 1
model of tf.keras.layers.Add with const in the second arg
save_format tf - ok
save_format h5 - error
2
model of tf.keras.layers.Add with const in the first arg
save_format tf - error
save_format h5 - error
3
model of x1 == x2
save_format tf - error
save_format h5 - error
Standalone code to reproduce the issue
Colab Code Link
1
import numpy as np
import tensorflow as tf
1 x1 = tf.keras.layers.Input(shape=(1, 2, 3))
x2 = tf.constant(np.ones([1,1,1,3]))
x = tf.keras.layers.Add()([x1, x2])
model = tf.keras.Model(inputs=[x1], outputs=[x])
model.compile()
print(model.predict(np.random.rand(1, 1, 2, 3)))
model.save("Mymodel", save_format='tf')
loaded_model = tf.keras.models.load_model("Mymodel") #ok
model.save("model.h5")
model = tf.keras.models.load_model("model.h5") #error
2
import numpy as np
import tensorflow as tf
x1 = tf.keras.layers.Input(shape=(1, 2, 3))
x2 = tf.constant(np.ones([1,1,3]))
x = tf.keras.layers.Add()([x2, x1])
model = tf.keras.Model(inputs=[x1], outputs=[x])
model.compile()
print(model.predict(np.random.rand(1, 1, 2, 3)))
model.save("Mymodel",save_format='tf')
loaded_model = tf.keras.models.load_model("Mymodel") #error
model.save("model.h5")
model = tf.keras.models.load_model("model.h5") #error
3
import numpy as np
import tensorflow as tf
x1 = tf.keras.layers.Input(shape=(1, 2, 3))
x2 = tf.keras.layers.Input(shape=(1, 2, 3))
x = x1 == x2
model = tf.keras.Model(inputs=[x1, x2], outputs=[x])
model.compile()
data= np.random.rand(1, 1, 2, 3)
print(model.predict([data, data]))
model.save("Mymodel",save_format='tf')
loaded_model = tf.keras.models.load_model("Mymodel") #error
model.save("model.h5")
model = tf.keras.models.load_model("model.h5") #error
Relevant log output
1
[[[[1.4474285 1.5162606 1.9524314]
[1.6375002 1.3645701 1.248564 ]]]]
INFO:tensorflow:Assets written to: Mymodel/assets
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-df88e1175bcb> in <module>()
8 loaded_model = tf.keras.models.load_model("Mymodel") #ok
9 model.save("model.h5")
---> 10 model = tf.keras.models.load_model("model.h5") #error
1 frames
/usr/local/lib/python3.7/dist-packages/keras/layers/merge.py in <setcomp>(.0)
94 f'Got {len(input_shape)} inputs. '
95 f'Full input_shape received: {input_shape}')
---> 96 batch_sizes = {s[0] for s in input_shape if s} - {None}
97 if len(batch_sizes) > 1:
98 raise ValueError(
TypeError: unhashable type: 'list'
2
WARNING:tensorflow:5 out of the last 5 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7fc230dc1c20> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.
[[[[1.2783022 1.9072516 1.3729118]
[1.1954207 1.6057456 1.5275352]]]]
INFO:tensorflow:Assets written to: Mymodel/assets
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
[<ipython-input-8-9aaa3c26f4cc>](https://localhost:8080/#) in <module>()
6 print(model.predict(np.random.rand(1, 1, 2, 3)))
7 model.save("Mymodel",save_format='tf')
----> 8 loaded_model = tf.keras.models.load_model("Mymodel") #error
9 model.save("model.h5")
10 model = tf.keras.models.load_model("model.h5") #error
1 frames
[/usr/local/lib/python3.7/dist-packages/keras/backend.py](https://localhost:8080/#) in ndim(x)
1499
1500 """
-> 1501 return x.shape.rank
1502
1503
AttributeError: Exception encountered when calling layer "add_2" (type Add).
'list' object has no attribute 'shape'
Call arguments received:
• inputs=[[[['tf.Tensor(shape=(), dtype=float32)', 'tf.Tensor(shape=(), dtype=float32)', 'tf.Tensor(shape=(), dtype=float32)']]], 'tf.Tensor(shape=(None, 1, 2, 3), dtype=float32)']
3
[[[[ True True True]
[ True True True]]]]
INFO:tensorflow:Assets written to: Mymodel/assets
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-6ed972a3dad2> in <module>()
7 print(model.predict([data, data]))
8 model.save("Mymodel",save_format='tf')
----> 9 loaded_model = tf.keras.models.load_model("Mymodel") #error
10 model.save("model.h5")
11 model = tf.keras.models.load_model("model.h5") #error
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py in op_dispatch_handler(*args, **kwargs)
1074 if iterable_params is not None:
1075 args, kwargs = replace_iterable_params(args, kwargs, iterable_params)
-> 1076 result = api_dispatcher.Dispatch(args, kwargs)
1077 if result is not NotImplemented:
1078 return result
TypeError: Missing required positional argument
```</details>