tensorflow / tensorflow

An Open Source Machine Learning Framework for Everyone
https://tensorflow.org
Apache License 2.0
186.22k stars 74.29k forks source link

load_model() cannot load model from previous version #63021

Closed eneskelestemur closed 5 months ago

eneskelestemur commented 8 months ago

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

tf 2.14 and tf 2.15

Custom code

Yes

OS platform and distribution

No response

Mobile device

No response

Python version

No response

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

Hello,

I was trying to load a model I previously saved with tf 2.14, and got the displayed error. I realized that the model was saved with tf 2.14, and the environment I was trying to load the model in was tf 2.15. I'm not sure if this is an expected behavior, but it seemed odd since the version difference wasn't that big.

Standalone code to reproduce the issue

# previously saved model with tf 2.14
# import tensorflow as tf
# from tensorflow import keras
# import numpy as np

# # print version
# print(tf.__version__)

# # random data
# data = np.random.random((100, 10))
# labels = np.random.random((100, ))

# # simple model
# normalizer = keras.layers.Normalization()
# normalizer.adapt(data)
# model = keras.Sequential()
# model.add(normalizer)
# model.add(keras.layers.Dense(10))
# model.add(keras.layers.Dense(1))
# model.compile(loss='mean_squared_error', optimizer='adam')
# model.fit(data, labels, epochs=10, verbose=2)
# model.save('test.keras')

### loading with tf 2.15 ###
import tensorflow as tf
from tensorflow import keras

# print version
print(tf.__version__)

# load model
model = keras.models.load_model('test.keras')

Relevant log output

2.15.0

Traceback (most recent call last):
  File "/work/users/e/n/enesk/phakinpro/test_load.py", line 8, in <module>
    model = keras.models.load_model('test.keras')
  File "/nas/longleaf/home/enesk/miniforge3/envs/tf/lib/python3.9/site-packages/keras/src/saving/saving_api.py", line 254, in load_model
    return saving_lib.load_model(
  File "/nas/longleaf/home/enesk/miniforge3/envs/tf/lib/python3.9/site-packages/keras/src/saving/saving_lib.py", line 281, in load_model
    raise e
  File "/nas/longleaf/home/enesk/miniforge3/envs/tf/lib/python3.9/site-packages/keras/src/saving/saving_lib.py", line 246, in load_model
    model = deserialize_keras_object(
  File "/nas/longleaf/home/enesk/miniforge3/envs/tf/lib/python3.9/site-packages/keras/src/saving/serialization_lib.py", line 728, in deserialize_keras_object
    instance = cls.from_config(inner_config)
  File "/nas/longleaf/home/enesk/miniforge3/envs/tf/lib/python3.9/site-packages/keras/src/engine/sequential.py", line 471, in from_config
    model.add(layer)
  File "/nas/longleaf/home/enesk/miniforge3/envs/tf/lib/python3.9/site-packages/tensorflow/python/trackable/base.py", line 204, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/nas/longleaf/home/enesk/miniforge3/envs/tf/lib/python3.9/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/nas/longleaf/home/enesk/miniforge3/envs/tf/lib/python3.9/site-packages/keras/src/layers/preprocessing/normalization.py", line 188, in build
    raise ValueError(
ValueError: All `axis` values to be kept must have known shape. Got axis: (-1,), input shape: [None, None], with unknown axis at index: 1
Venkat6871 commented 8 months ago

Hi @eneskelestemur , Sorry for late reply, I tried to run your code on Colab using TF v2.14 and 2.15 faced the same issue. Please find the gist here for reference. Thanks you!

Venkat6871 commented 6 months ago

Hi @eneskelestemur ,

I tried to run your code on colab using TF v2.16.1 with nightly now it is working fine. Could you please check with recent version. Here i providing gist for your reference.

Thank you

github-actions[bot] commented 5 months ago

This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 5 months ago

This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.

google-ml-butler[bot] commented 5 months ago

Are you satisfied with the resolution of your issue? Yes No