-
Notifications
You must be signed in to change notification settings - Fork 158
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
How can I convert this to tf.keras h5 model? #6
Comments
Hi, you can try following steps: |
Hi @JayYip , I used the DocProductPresentationV6-0.2.0.ipynb as a starting point and used this code to do what you mentioned: ` pretrained_path = 'BioBertFolder/biobert_v1.0_pubmed_pmc/' doc = RetreiveQADoc(pretrained_path=pretrained_path, I get this error though:
I manage to create a file with "save_weights" but it's not usable later with the tfliteconverter. I need to save the full model. Any idea how I can overcome this error ? |
So, I found out when the model is subclassed I can use:
Not sure how to proceed from here... Calling the predict method of the doc object does nothing to help. I'm lost at finding out how to set the inputs here. Please help. |
The reason is that in order to save the computation graph, you need to build the graph first. And without the input shape, TF cannot build the graph. Please try calling If you can successfully export the model and deploy using tf.js or tf.lite, please consider making a PR because it seems lots of people encountered the same problem while using our model. |
Interesting. I am assuming that the full computation graph is already built for training and inference. Couldn't you save the computation graph then? Why do you need _set_inputs |
Try the api : https://pocket-derm.firebaseapp.com/ |
Pretty neat but it looks like it costs money. How did you make this? |
@Santosh-Gupta a simple post request w/ json.
|
@pumpkinband Try this? from docproduct.predictor import RetreiveQADoc
pretrained_path = 'BioBertFolder/biobert_v1.0_pubmed_pmc/'
ffn_weight_file = None
bert_ffn_weight_file = 'newFolder/models/bertffn_crossentropy/bertffn'
embedding_file = 'Float16EmbeddingsExpanded5-27-19.pkl'
graph = tf.Graph()
with graph.as_default():
doc = RetreiveQADoc(pretrained_path=pretrained_path,
ffn_weight_file=None,
bert_ffn_weight_file=bert_ffn_weight_file,
embedding_file=embedding_file)
doc.qa_embed.model.save("model.h5") |
Ahh, sorry, some bug in the that code. Try this: from docproduct.predictor import RetreiveQADoc
pretrained_path = 'BioBertFolder/biobert_v1.0_pubmed_pmc/'
ffn_weight_file = None
bert_ffn_weight_file = 'newFolder/models/bertffn_crossentropy/bertffn'
embedding_file = 'Float16EmbeddingsExpanded5-27-19.pkl'
graph = tf.Graph()
with graph.as_default():
doc = RetreiveQADoc(pretrained_path=pretrained_path,
ffn_weight_file=None,
bert_ffn_weight_file=bert_ffn_weight_file,
embedding_file=embedding_file)
doc.qa_embed.model.save("model.h5") PS, this is a very basic python syntax error. You may need to get to know more about python. |
@JayYip I tried the above which was similar to what I've tried before and still get:
I also tried:
|
I am unable test these yet but in the meanwhile try instantiating the model object and then trying to save it
let me know what the result is. If that doesn't work, try to instantiate the Keras model object directly.
|
After trying
i am getting the previous error |
after trying this out
it is asking for inputs
can you please help me with this @Santosh-Gupta |
It says you didn't define What did you set |
here is link to my colab can you please check what inputs i have to put ? https://colab.research.google.com/drive/1PadA5HDsnydt6juZP7f8lVo_sLYCwOGn |
It looks like you didn't define any inputs, so that's what was giving you the |
@JayYip @Santosh-Gupta I am struggling with the same issue, and hasn't been able to solve the issue. I have tried all the suggestion in this post but couldn't resolve it. I really appreciate any help that I can get. `from docproduct.models import MedicalQAModelwithBert medical_qa_model = MedicalQAModelwithBert( medical_qa_model.save("model.h5")` The error is Thanks |
For
The error message mentions "Consider using So I would try that. For
The error message mentions "ValueError: Model <docproduct.models.MedicalQAModelwithBert object at 0x7f765a1e6da0> cannot be saved because the input shapes have not been set. Usually, input shapes are automatically determined from calling .fit() or .predict(). To manually set the shapes, call model._set_inputs(inputs)." So I would try that as well. Let me know if either of those work. |
@Santosh-Gupta Thank you for prompt reply. I did try using "save_weights" but it's not usable later with the tfliteconverter becuase it is not the full model. Regarding the error for Thank you for your time |
The error message also says
So you could also trying calling the predict function first, but if that doesn't work any random text should work. You may also have to supply the rest of the args. Either way, check out the last cell which has does prediction. |
@Santosh-Gupta I tried different variation of what you suggested but no luck, here are the error for each:
ValueError: Model inputs are already set.` if I use if I use |
For the first method you are getting an error on the first line, you're trying to set the input when the input has been already set. Remove that line and try again. medical_qa_model doesn't have it's predictor function directly defined, so I would just stick with RetreiveQADoc https://github.com/re-search/DocProduct/blob/master/docproduct/models.py |
Is the model bertffn_crossentropy/bertffn already been exported to SavedModel ? |
@shravankumar9892 I do not believe it has been exported. I am just skimming through that link but it looks like that is for Estimator? We are using Keras. |
@Santosh-Gupta It seems the issue is that when model subclass was defined, the save() wasn't wasn't implemented |
Since it inherits from |
@behinoo I think you're right. Since model subclassing might contains dynamic graph which is defined in runtime by python. That makes exporting to |
@JayYip I am wondering where could the architecture could possibly be dynamic. I believe Tensorflow is completely static? Perhaps with the variable length input, which may explain why we keep getting the input not defined errors. |
I am getting this error
|
It looks like the old error, where we have to initialize using inputs. I am wondering if anyone has been able to solve this yet. Perhaps we should ask in the keras github or stackover flow on how to resolve this issue. |
I am trying to run the exact code of colab notebook on my local machine(windows 64 bit) but its giving the above error, is this because of tensorflow version? |
What version of TF do you have? Ours is build on tf 2.0 |
yes, that was the issue. I was using stable version of tensorflow. |
Has anyone managed to obtain the .h5 model or a .tflite model? |
Not that I know of, but it may be easier now since TF 2.0 is officially in beta. |
I have little experience with pure Tensorflow and NLP and I'm having a really hard time converting this to a keras or tf.keras h5 model so I can then convert it to Tensorflow lite or Tensorflow js. Someone managed to do that ?
The text was updated successfully, but these errors were encountered: