Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't load model with tf v2.14 if the model was saved with previous tf versions #18830

Closed
ofirgo opened this issue Nov 26, 2023 · 5 comments
Closed
Assignees
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@ofirgo
Copy link

ofirgo commented Nov 26, 2023

Hello,

I have a model that I saved using tf 2.13 version.
When trying to load it using tf 2.14 (or 2.15) I'm getting the following error:

ValueError: Layer 'conv2d_1' expected 2 variables, but received 0 variables during loading. Expected: ['conv2d_1/kernel:0', 'conv2d_1/bias:0']

Some additional information:
This is a simple model that only contains two similar Conv2d layers.
The issue doesn't occur when there aren't at least two similar layers of the same type.
The issue also happens when saving the model with tf version smaller than 2.13 (and loading with 2.14).

Code to reproduce:

`
Run with TF 2.13:
import tensorflow as tf
_in = tf.keras.layers.Input(shape=(8, 8, 3))
x = _in
x = tf.keras.layers.Conv2D(3, 3)(x)
x = tf.keras.layers.Conv2D(3, 3)(x)
_out = x
model = tf.keras.Model(inputs=_in, outputs=_out)

model.save('model_213.keras')

Run with TF 2.14:
loaded_model = tf.keras.models.load_model('model_213.keras')
`

I opened a similar issue in the TF repository, but it seems that this issue is more Keras-related.

Appreciate your support.

@SuryanarayanaY SuryanarayanaY added the type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. label Nov 27, 2023
@SuryanarayanaY
Copy link
Contributor

Hi @ofirgo ,
I have replicated the behaviour with keras2.14 vs 2.15v and keras2.15v vs keras-nightly and attached gist.

The model saved in one version not able to reload in other version but only in same version.Need to check whether this behaviour is intended if not bug.

@ofirgo
Copy link
Author

ofirgo commented Nov 28, 2023

Hi @ofirgo , I have replicated the behaviour with keras2.14 vs 2.15v and keras2.15v vs keras-nightly and attached gist.

The model saved in one version not able to reload in other version but only in same version.Need to check whether this behaviour is intended if not bug.

Great, thanks, waiting to hear the conclusion on whether it is solvable or by-design.

@fchollet
Copy link
Collaborator

This is a known issue that affects certain models across the version boundary. It is already fixed but of course the fix isn't part of old TF 2.13. What I would suggest is to use model.get_weights() to get the list of weights from the old model in TF 2.13, dump it to npz, then reload it in TF 2.15 and re-save it as a .keras file.

Copy link

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

@ofirgo
Copy link
Author

ofirgo commented Nov 30, 2023

This is a known issue that affects certain models across the version boundary. It is already fixed but of course the fix isn't part of old TF 2.13. What I would suggest is to use model.get_weights() to get the list of weights from the old model in TF 2.13, dump it to npz, then reload it in TF 2.15 and re-save it as a .keras file.

Hi @fchollet, thank you for the explanation.

Just want to understand the current status - so when you say that this bug "is already fixed", to what tf version are you referring? Because I'm still unable to save a model in tf 2.15 and load it with the nightly version.
Did you mean that going forward, in future releases, saving and loading with different versions will be possible, or that there will be some support in loading models from older versions (say, 2.13, 2.14, 2.15) with future release versions (say 2.16)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

3 participants