Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.NotImplementedException : 'Model load of h5 format has not been supported #1024

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
Cilouche opened this issue Apr 14, 2023 · 16 comments
Labels
enhancement New feature or request model save/load

Comments

@Cilouche
Copy link

Hello,
I am trying to load a .h5 model with the load_model function (Tensorflow.Keras.Models) but I get this error
issue:
System.NotImplementedException : 'Model load of h5 format has not been supported. Please submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues if it's needed.'

any suggestions please,
Thank you

@AsakusaRinne
Copy link
Collaborator

Hello, the loading of "h5" format keras model has not been supported. We has implemented the loading of "SavedModel" format first because it's recommended by tensorflow. The loading of "h5" format is also on our schedule but is not very urgent now. May I ask why you choose "h5" format instead of "SavedModel"?

@Cilouche
Copy link
Author

Thank you for your answer, I chose the .h because it is a network that I have already trained in Python with tensorflow-keras, and since I work in c# I wanted to reuse it

@AsakusaRinne
Copy link
Collaborator

I see. The loading of "h5" format is a big feature and support of the loading complex models (such as bert) with "SavedModel" format has higher priority in tf.net. We will implement the loading of of "h5" format model in the future but it costs time. A possible alternative is loading your model in python and save it again to "SavedModel" format, then load it in C#. If your model contains self-defined keras module, please try after the merge of #1022, which added support for it.

@Cilouche
Copy link
Author

hello,
I followed your suggestions and loaded the .h model then saved it in savedModel (format.pb), so even I retrained the model and saved it in .pb format, but when I tried to test it I got following error:

image

the model attached

saved_model.zip

thanks

@Cilouche
Copy link
Author

in python it loads normal (.h and .pb format) but in c# no

import tensorflow as tf

#import tensorflow.python.framework.convert_variables_to_constants_v2
from tensorflow.python.keras.models import load_model
from tensorflow.keras.models import Model
model = tf.keras.models.load_model('mod/InceptionResNet/')
print(model.summary())

@AsakusaRinne
Copy link
Collaborator

Hi, the file seems to be SavedModel format saved prior to TF 2.5. The loading itself is supported by using tf.saved_model.load. However it seems that your model has other parts such as restoring optimizer so it will still fail. I'll finish the lacked features in the next 2 or 3 days.

@Cilouche
Copy link
Author

Hi, I used another savedModel conversion script, it solved my problem, attached the link so that others can benefit
https://ksingh7.medium.com/part-iii-convert-keras-model-to-tensorflow-frozen-graph-model-a6aa6b1aaeee

Thanks

@AsakusaRinne
Copy link
Collaborator

Hey, is the model converted by this script can be directly loaded by tf.net?

@Cilouche
Copy link
Author

Yes

image

@AsakusaRinne
Copy link
Collaborator

Yes

image

Thanks for your sharing. That's a very good work around. I'll still fix the bug of loading your uploaded model in this issue.

@AsakusaRinne
Copy link
Collaborator

AsakusaRinne commented Apr 21, 2023

hello, I followed your suggestions and loaded the .h model then saved it in savedModel (format.pb), so even I retrained the model and saved it in .pb format, but when I tried to test it I got following error:

image

the model attached

saved_model.zip

thanks

#1032 supports this way by adding an API tf.saved_model.load. However both tensorflow and tf.net will throw an exception that no variable file is found. Did you remove this part in the uploaded file?

@AsakusaRinne AsakusaRinne added enhancement New feature or request model save/load labels Apr 21, 2023
@Cilouche
Copy link
Author

No, I didn't delete anything, as indicated above, I opted for a format.h5 conversion to frozen model.pb with a well-detailed tutorial.

@AsakusaRinne
Copy link
Collaborator

Did you succeed in tensorflow (python)? I tried loading the file with tensorflow python but failed, which tells me it cannot find variable files.

@Cilouche
Copy link
Author

a problem with this model that I put in attachment, I use another model, basically I took my basic model .h5 convert to .pb, but the attached model is wrong

@AsakusaRinne
Copy link
Collaborator

Thanks, I'll close this issue as completed. Please don't hesitate to tell us if any problem in the future.

@jaroslav-hook
Copy link

how about the loading a model? You seem to have implemented model.save , model.save_weights which saves the TF model.
How about loading it ? Cannot find it.
By the way , where why is model.predict() requires a different data format from model.fit()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request model save/load
Projects
None yet
Development

No branches or pull requests

3 participants