hub icon indicating copy to clipboard operation
hub copied to clipboard

Examples of Exporting Fine Tuned USE Module

Open meethariprasad opened this issue 7 years ago • 6 comments

Hi All,

We have seen this example of fine tuning but not seen a example where I can use a simple command to export fine tuned module. Can we please extend this example by adding post setting Trainable=True and training estimator, how exactly we can export fine tuned Universal Sentence Embedding Module?

https://colab.research.google.com/github/tensorflow/hub/blob/master/docs/tutorials/text_classification_with_tf_hub.ipynb#scrollTo=25rdoEHih0fm

meethariprasad avatar Nov 26 '19 06:11 meethariprasad

@meethariprasad You can follow the example here to export the model and serving it using Tensorflow serving.

gowthamkpr avatar Nov 26 '19 18:11 gowthamkpr

Thanks a lot @gowthamkpr . But this doesn't solve the Universal Sentence Embeding specific question here. I believe TF Hub need to come up with simplified way of exporting module post fine tuning for all the modules where it allows where Trainable=True. Bare minimum, in Text Classification example, post fine tuning, having steps to exporting the fine tuned module post fine tuning to serve subsequent domain specific embeddings will be most needed and will complete the example.

meethariprasad avatar Nov 28 '19 08:11 meethariprasad

You can find documentation on exporting modules here:

  • For TF1 here: https://www.tensorflow.org/hub/tf1_hub_module#creating_a_new_module
  • TF2 here: https://www.tensorflow.org/hub/tf2_saved_model#creating_savedmodels_for_tf_hub

Here's a minimal code example for re-exporting a fine-tuned module in TF1, though this is even more straightforward in TF2.

import tensorflow as tf
import tensorflow_hub as hub

tf.reset_default_graph()
m = hub.Module("https://tfhub.dev/google/nnlm-en-dim128/1")
with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  sess.run(tf.tables_initializer())
  # <...> Fine-tune embeddings here
  m.export("/tmp/finetuned", sess)

And then to use the finetuned module you can load like this:

hub.Module("/tmp/finetuned")

This is slightly more convoluted within an estimator framework, but still possible.

I hope this unblocks you, but your feedback on improved examples and documentation is noted and we will work on improving documentation around exporting fine-tuned modules.

kempy avatar Dec 05 '19 13:12 kempy

You can find documentation on exporting modules here:

  • For TF1 here: https://www.tensorflow.org/hub/tf1_hub_module#creating_a_new_module
  • TF2 here: https://www.tensorflow.org/hub/tf2_saved_model#creating_savedmodels_for_tf_hub

Here's a minimal code example for re-exporting a fine-tuned module in TF1, though this is even more straightforward in TF2.

import tensorflow as tf
import tensorflow_hub as hub

tf.reset_default_graph()
m = hub.Module("https://tfhub.dev/google/nnlm-en-dim128/1")
with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  sess.run(tf.tables_initializer())
  # <...> Fine-tune embeddings here
  m.export("/tmp/finetuned", sess)

And then to use the finetuned module you can load like this:

hub.Module("/tmp/finetuned")

This is slightly more convoluted within an estimator framework, but still possible.

I hope this unblocks you, but your feedback on improved examples and documentation is noted and we will work on improving documentation around exporting fine-tuned modules.

@kempy : Thanks a lot. Quick query.

How it works in TF2 and TFHub?

Taking from TF2:

Because in https://www.tensorflow.org/hub/tf2_saved_model#creating_savedmodels_for_tf_hub

There is no explanation on how to export the fine tuned module? As I understand Keraslayer object can't get exported or saved as saved TF Saved Model.

meethariprasad avatar Dec 06 '19 05:12 meethariprasad

We've talked this through, leading to the doc update in commit https://github.com/tensorflow/hub/commit/3c933faad3dd52ebe3859256e886113fc7069e8e

arnoegw avatar Dec 20 '19 09:12 arnoegw

We've talked this through, leading to the doc update in commit 3c933fa

Yes @arnoegw . Some queries I raised were before we interacted on this aspect. This is closed properly.

meethariprasad avatar Dec 20 '19 09:12 meethariprasad