Skip to content

[BUG Report]: Loading weights exported from Python doesn't work #1150

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

Closed
OliBomby opened this issue Jul 21, 2023 · 3 comments · Fixed by #1154
Closed

[BUG Report]: Loading weights exported from Python doesn't work #1150

OliBomby opened this issue Jul 21, 2023 · 3 comments · Fixed by #1154
Assignees
Labels
bug Something isn't working

Comments

@OliBomby
Copy link

Description

I'm trying to run a model I trained in Python on TensorFlow.NET. I have the weights in HDF5 format and the model replicated in TensorFlow.NET, but when I use model.load_weights(path.h5) it fails to load the weights because it can't find the weights by name.

I did some debugging and found out that it expects dollar signs in the path for some reason, which are normally not there in the weight names.

https://github.com/SciSharp/TensorFlow.NET/blob/fa2d2dcfc894686846f475a98300b6f8af0bfa94/src/TensorFlowNET.Keras/Saving/hdf5_format.cs#L137C21-L141C61

This code seems to be recently added by #1137. The code looks awful, so please revert that change.

Reproduction Steps

In Python with TensorFlow, export some model weights using model.save_weights('my_checkpoint.h5')

Then in C# with TensorFlow.NET, load the weights using model.load_weights("my_checkpoint.h5");

Known Workarounds

Exporting weights using TensorFlow.NET inserts the dollar signs so loading works in that case.

It might be possible to workaround if you manually replace all weight names in the saved weights file.

Configuration and Other Information

  • TensorFlow.NET 0.110.2
  • TensorFlow.Keras 0.11.2
  • .NET 6
  • Windows 10
@Wanglongzhi2001
Copy link
Collaborator

Wanglongzhi2001 commented Jul 22, 2023

Hello, thank you for your attention to TensorFlow.NET, this PR does looks have some problems, we will fix it. BTW, model.load_weights does not implemented prefect in TensorFlow.NET, you can try use model.save and keras.models.load_model to save and load the whole model, we recommend you to use this api, because it implemented better in TensorFlow.NET

@Beacontownfc
Copy link
Contributor

Thank you for your attention to TensorFlow.NET, I have restored model.load_weights and improve #1137 .

@OliBomby
Copy link
Author

Hello, thank you for your attention to TensorFlow.NET, this PR does looks have some problems, we will fix it. BTW, model.load_weights does not implemented prefect in TensorFlow.NET, you can try use model.save and keras.models.load_model to save and load the whole model, we recommend you to use this api, because it implemented better in TensorFlow.NET

Using keras.models.load_model is what I tried initially but it didnt work because I got a NotImplementedException at


Turns out my model exported from Python has input_spec specified for all layers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants